74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package market
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
cborrpc "github.com/filecoin-project/go-cbor-util"
|
|
"github.com/ipfs/go-datastore"
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
dsq "github.com/ipfs/go-datastore/query"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
)
|
|
|
|
const dsKeyAddr = "Addr"
|
|
|
|
type Store struct {
|
|
ds datastore.Batching
|
|
}
|
|
|
|
func newStore(ds dtypes.MetadataDS) *Store {
|
|
ds = namespace.Wrap(ds, datastore.NewKey("/fundmgr/"))
|
|
return &Store{
|
|
ds: ds,
|
|
}
|
|
}
|
|
|
|
// save the state to the datastore
|
|
func (ps *Store) save(state *FundedAddressState) error {
|
|
k := dskeyForAddr(state.Addr)
|
|
|
|
b, err := cborrpc.Dump(state)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return ps.ds.Put(k, b)
|
|
}
|
|
|
|
// forEach calls iter with each address in the datastore
|
|
func (ps *Store) forEach(iter func(*FundedAddressState)) error {
|
|
res, err := ps.ds.Query(dsq.Query{Prefix: dsKeyAddr})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Close() //nolint:errcheck
|
|
|
|
for {
|
|
res, ok := res.NextSync()
|
|
if !ok {
|
|
break
|
|
}
|
|
|
|
if res.Error != nil {
|
|
return err
|
|
}
|
|
|
|
var stored FundedAddressState
|
|
if err := stored.UnmarshalCBOR(bytes.NewReader(res.Value)); err != nil {
|
|
return err
|
|
}
|
|
|
|
iter(&stored)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// The datastore key used to identify the address state
|
|
func dskeyForAddr(addr address.Address) datastore.Key {
|
|
return datastore.KeyWithNamespaces([]string{dsKeyAddr, addr.String()})
|
|
}
|