lotus/chain/deals/state_store.go

78 lines
1.5 KiB
Go
Raw Normal View History

2019-08-06 22:04:21 +00:00
package deals
import (
"bytes"
2019-10-31 21:01:44 +00:00
"github.com/filecoin-project/lotus/lib/statestore"
"github.com/filecoin-project/lotus/lib/cborrpc"
2019-08-06 22:04:21 +00:00
"github.com/ipfs/go-cid"
)
2019-09-13 19:19:13 +00:00
type MinerStateStore struct {
2019-10-31 21:01:44 +00:00
*statestore.StateStore
2019-09-13 19:19:13 +00:00
}
func (st *MinerStateStore) MutateMiner(i cid.Cid, mutator func(*MinerDeal) error) error {
2019-10-31 21:01:44 +00:00
return st.Mutate(i, minerMutator(mutator))
2019-08-06 22:04:21 +00:00
}
func minerMutator(m func(*MinerDeal) error) func([]byte) ([]byte, error) {
2019-08-06 22:04:21 +00:00
return func(in []byte) ([]byte, error) {
deal := new(MinerDeal)
err := cborrpc.ReadCborRPC(bytes.NewReader(in), deal)
2019-08-06 22:04:21 +00:00
if err != nil {
return nil, err
}
if err := m(deal); err != nil {
2019-08-06 22:04:21 +00:00
return nil, err
}
return cborrpc.Dump(deal)
2019-08-06 22:04:21 +00:00
}
}
2019-09-13 19:19:13 +00:00
type ClientStateStore struct {
2019-10-31 21:01:44 +00:00
*statestore.StateStore
2019-09-13 19:19:13 +00:00
}
func (st *ClientStateStore) MutateClient(i cid.Cid, mutator func(*ClientDeal) error) error {
2019-10-31 21:01:44 +00:00
return st.Mutate(i, clientMutator(mutator))
2019-08-06 22:04:21 +00:00
}
func clientMutator(m func(*ClientDeal) error) func([]byte) ([]byte, error) {
2019-08-06 22:04:21 +00:00
return func(in []byte) ([]byte, error) {
deal := new(ClientDeal)
err := cborrpc.ReadCborRPC(bytes.NewReader(in), deal)
2019-08-06 22:04:21 +00:00
if err != nil {
return nil, err
}
if err := m(deal); err != nil {
2019-08-06 22:04:21 +00:00
return nil, err
}
return cborrpc.Dump(deal)
2019-08-06 22:04:21 +00:00
}
}
2019-09-13 19:19:13 +00:00
func (st *ClientStateStore) ListClient() ([]ClientDeal, error) {
2019-09-10 14:13:24 +00:00
var out []ClientDeal
2019-10-31 21:01:44 +00:00
l, err := st.List()
2019-09-10 14:13:24 +00:00
if err != nil {
return nil, err
}
2019-10-31 21:01:44 +00:00
for _, res := range l {
2019-09-10 14:13:24 +00:00
var deal ClientDeal
err := cborrpc.ReadCborRPC(bytes.NewReader(res.Value), &deal)
2019-09-10 14:13:24 +00:00
if err != nil {
return nil, err
}
out = append(out, deal)
}
return out, nil
}