Skip to content

Commit 26edd21

Browse files
committed
loopdb: add new bucket to save liquidity params
This commit adds a new bucket to save liquidity parameters. We've skipped the serialization and deserialization implementations here and leave them to be handled by the liquidity package.
1 parent 8217ee3 commit 26edd21

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

loopdb/interface.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ type SwapStore interface {
3333
UpdateLoopIn(hash lntypes.Hash, time time.Time,
3434
state SwapStateData) error
3535

36+
// PutLiquidityParams writes the serialized `manager.Parameters` bytes
37+
// into the bucket.
38+
//
39+
// NOTE: it's the caller's responsibility to encode the param. Atm,
40+
// it's encoding using the proto package's `Marshal` method.
41+
PutLiquidityParams(params []byte) error
42+
43+
// FetchLiquidityParams reads the serialized `manager.Parameters` bytes
44+
// from the bucket.
45+
//
46+
// NOTE: it's the caller's responsibility to decode the param. Atm,
47+
// it's decoding using the proto package's `Unmarshal` method.
48+
FetchLiquidityParams() ([]byte, error)
49+
3650
// Close closes the underlying database.
3751
Close() error
3852
}

loopdb/store.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ var (
9494
// value: uint32 confirmation value
9595
confirmationsKey = []byte("confirmations")
9696

97+
// liquidtyBucket is a root bucket used to save liquidity manager
98+
// related info.
99+
liquidityBucket = []byte("liquidity")
100+
101+
// liquidtyParamsKey specifies the key used to store the liquidity
102+
// parameters.
103+
liquidtyParamsKey = []byte("params")
104+
97105
byteOrder = binary.BigEndian
98106

99107
keyLength = 33
@@ -190,6 +198,12 @@ func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) (
190198
return err
191199
}
192200

201+
// Create liquidity manager's bucket.
202+
_, err = tx.CreateBucketIfNotExists(liquidityBucket)
203+
if err != nil {
204+
return err
205+
}
206+
193207
return nil
194208
})
195209
if err != nil {
@@ -712,3 +726,41 @@ func (s *boltSwapStore) UpdateLoopIn(hash lntypes.Hash, time time.Time,
712726
func (s *boltSwapStore) Close() error {
713727
return s.db.Close()
714728
}
729+
730+
// PutLiquidityParams writes the serialized `manager.Parameters` bytes into the
731+
// bucket.
732+
//
733+
// NOTE: it's the caller's responsibility to encode the param. Atm, it's
734+
// encoding using the proto package's `Marshal` method.
735+
func (s *boltSwapStore) PutLiquidityParams(params []byte) error {
736+
return s.db.Update(func(tx *bbolt.Tx) error {
737+
// Read the root bucket.
738+
rootBucket := tx.Bucket(liquidityBucket)
739+
if rootBucket == nil {
740+
return errors.New("liquidity bucket does not exist")
741+
}
742+
return rootBucket.Put(liquidtyParamsKey, params)
743+
})
744+
}
745+
746+
// FetchLiquidityParams reads the serialized `manager.Parameters` bytes from
747+
// the bucket.
748+
//
749+
// NOTE: it's the caller's responsibility to decode the param. Atm, it's
750+
// decoding using the proto package's `Unmarshal` method.
751+
func (s *boltSwapStore) FetchLiquidityParams() ([]byte, error) {
752+
var params []byte
753+
754+
err := s.db.View(func(tx *bbolt.Tx) error {
755+
// Read the root bucket.
756+
rootBucket := tx.Bucket(liquidityBucket)
757+
if rootBucket == nil {
758+
return errors.New("liquidity bucket does not exist")
759+
}
760+
761+
params = rootBucket.Get(liquidtyParamsKey)
762+
return nil
763+
})
764+
765+
return params, err
766+
}

loopdb/store_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,31 @@ func TestLegacyOutgoingChannel(t *testing.T) {
471471
t.Fatal("invalid outgoing channel")
472472
}
473473
}
474+
475+
// TestLiquidityParams checks that reading and writing to liquidty bucket are
476+
// as expected.
477+
func TestLiquidityParams(t *testing.T) {
478+
tempDirName, err := ioutil.TempDir("", "clientstore")
479+
require.NoError(t, err, "failed to db")
480+
defer os.RemoveAll(tempDirName)
481+
482+
store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams)
483+
require.NoError(t, err, "failed to create store")
484+
485+
// Test when there's no params saved before, an empty bytes is
486+
// returned.
487+
params, err := store.FetchLiquidityParams()
488+
require.NoError(t, err, "failed to fetch params")
489+
require.Empty(t, params, "expect empty bytes")
490+
491+
params = []byte("test")
492+
493+
// Test we can save the params.
494+
err = store.PutLiquidityParams(params)
495+
require.NoError(t, err, "failed to put params")
496+
497+
// Now fetch the db again should return the above saved bytes.
498+
paramsRead, err := store.FetchLiquidityParams()
499+
require.NoError(t, err, "failed to fetch params")
500+
require.Equal(t, params, paramsRead, "unexpected return value")
501+
}

store_mock_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ func (s *storeMock) UpdateLoopIn(hash lntypes.Hash, time time.Time,
171171
return nil
172172
}
173173

174+
// PutLiquidityParams writes the serialized `manager.Parameters` bytes into the
175+
// bucket.
176+
//
177+
// NOTE: Part of the loopdb.SwapStore interface.
178+
func (s *storeMock) PutLiquidityParams(params []byte) error {
179+
return nil
180+
}
181+
182+
// FetchLiquidityParams reads the serialized `manager.Parameters` bytes from
183+
// the bucket.
184+
//
185+
// NOTE: Part of the loopdb.SwapStore interface.
186+
func (s *storeMock) FetchLiquidityParams() ([]byte, error) {
187+
return nil, nil
188+
}
189+
174190
func (s *storeMock) Close() error {
175191
return nil
176192
}

0 commit comments

Comments
 (0)