Skip to content

Commit

Permalink
storageadapter: Fix tests, more testable diff api
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jan 21, 2021
1 parent 42b481f commit de0a452
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 49 deletions.
44 changes: 37 additions & 7 deletions markets/storageadapter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-fil-markets/shared"
Expand All @@ -22,8 +23,11 @@ import (
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/apibstore"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/adt"
marketactor "github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/events"
"github.com/filecoin-project/lotus/chain/events/state"
"github.com/filecoin-project/lotus/chain/market"
Expand All @@ -34,9 +38,7 @@ import (
)

type ClientNodeAdapter struct {
full.StateAPI
full.ChainAPI
full.MpoolAPI
*clientApi

fundmgr *market.FundManager
ev *events.Events
Expand All @@ -46,14 +48,42 @@ type ClientNodeAdapter struct {
type clientApi struct {
full.ChainAPI
full.StateAPI
full.MpoolAPI
}

func (ca *clientApi) diffPreCommits(ctx context.Context, actor address.Address, pre, cur types.TipSetKey) (*miner.PreCommitChanges, error) {
store := adt.WrapStore(ctx, cbor.NewCborStore(apibstore.NewAPIBlockstore(ca)))

preAct, err := ca.StateGetActor(ctx, actor, pre)
if err != nil {
return nil, xerrors.Errorf("getting pre actor: %w", err)
}
curAct, err := ca.StateGetActor(ctx, actor, cur)
if err != nil {
return nil, xerrors.Errorf("getting cur actor: %w", err)
}

preSt, err := miner.Load(store, preAct)
if err != nil {
return nil, xerrors.Errorf("loading miner actor: %w", err)
}
curSt, err := miner.Load(store, curAct)
if err != nil {
return nil, xerrors.Errorf("loading miner actor: %w", err)
}

diff, err := miner.DiffPreCommits(preSt, curSt)
if err != nil {
return nil, xerrors.Errorf("diff precommits: %w", err)
}

return diff, err
}

func NewClientNodeAdapter(stateapi full.StateAPI, chain full.ChainAPI, mpool full.MpoolAPI, fundmgr *market.FundManager) storagemarket.StorageClientNode {
capi := &clientApi{chain, stateapi}
capi := &clientApi{chain, stateapi, mpool}
return &ClientNodeAdapter{
StateAPI: stateapi,
ChainAPI: chain,
MpoolAPI: mpool,
clientApi: capi,

fundmgr: fundmgr,
ev: events.NewEvents(context.TODO(), capi),
Expand Down
5 changes: 2 additions & 3 deletions markets/storageadapter/getcurrentdealinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
Expand All @@ -19,9 +20,7 @@ type getCurrentDealInfoAPI interface {
StateMarketStorageDeal(context.Context, abi.DealID, types.TipSetKey) (*api.MarketDeal, error)
StateSearchMsg(context.Context, cid.Cid) (*api.MsgLookup, error)

StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error)
ChainReadObj(context.Context, cid.Cid) ([]byte, error)
ChainHasObj(context.Context, cid.Cid) (bool, error)
diffPreCommits(ctx context.Context, actor address.Address, pre, cur types.TipSetKey) (*miner.PreCommitChanges, error)
}

// GetCurrentDealInfo gets current information on a deal, and corrects the deal ID as needed
Expand Down
13 changes: 3 additions & 10 deletions markets/storageadapter/getcurrentdealinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
test "github.com/filecoin-project/lotus/chain/events/state/mock"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -236,16 +237,8 @@ type mockGetCurrentDealInfoAPI struct {
MarketDeals map[marketDealKey]*api.MarketDeal
}

func (mapi *mockGetCurrentDealInfoAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) {
panic("implement me")
}

func (mapi *mockGetCurrentDealInfoAPI) ChainReadObj(ctx context.Context, c cid.Cid) ([]byte, error) {
panic("implement me")
}

func (mapi *mockGetCurrentDealInfoAPI) ChainHasObj(ctx context.Context, c cid.Cid) (bool, error) {
panic("implement me")
func (mapi *mockGetCurrentDealInfoAPI) diffPreCommits(ctx context.Context, actor address.Address, pre, cur types.TipSetKey) (*miner.PreCommitChanges, error) {
return &miner.PreCommitChanges{}, nil
}

func (mapi *mockGetCurrentDealInfoAPI) StateMarketStorageDeal(ctx context.Context, dealID abi.DealID, ts types.TipSetKey) (*api.MarketDeal, error) {
Expand Down
29 changes: 2 additions & 27 deletions markets/storageadapter/ondealsectorcommitted.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import (
"sync"

"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"

"github.com/filecoin-project/lotus/api/apibstore"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/events"
Expand Down Expand Up @@ -61,31 +58,9 @@ func OnDealSectorPreCommitted(ctx context.Context, api getCurrentDealInfoAPI, ev
}
}

store := adt.WrapStore(ctx, cbor.NewCborStore(apibstore.NewAPIBlockstore(api)))

publishAct, err := api.StateGetActor(ctx, provider, publishTs)
if err != nil {
return false, false, xerrors.Errorf("getting provider actor: %w", err)
}

curAct, err := api.StateGetActor(ctx, provider, ts.Key())
if err != nil {
return false, false, xerrors.Errorf("getting provider actor: %w", err)
}

curSt, err := miner.Load(store, curAct)
diff, err := api.diffPreCommits(ctx, provider, publishTs, ts.Key())
if err != nil {
return false, false, xerrors.Errorf("leading miner actor: %w", err)
}

pubSt, err := miner.Load(store, publishAct)
if err != nil {
return false, false, xerrors.Errorf("leading miner actor: %w", err)
}

diff, err := miner.DiffPreCommits(pubSt, curSt)
if err != nil {
return false, false, xerrors.Errorf("diff precommits: %w", err)
return false, false, err
}

for _, info := range diff.Added {
Expand Down
3 changes: 1 addition & 2 deletions markets/storageadapter/ondealsectorcommitted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ func TestOnDealSectorPreCommitted(t *testing.T) {
deals: map[abi.DealID]*api.MarketDeal{},
},
},
expectedCBCallCount: 1,
expectedCBError: errors.New("handling applied event: something went wrong"),
expectedCBCallCount: 0,
expectedError: errors.New("failed to set up called handler: something went wrong"),
},
"proposed deal epoch timeout": {
Expand Down
33 changes: 33 additions & 0 deletions markets/storageadapter/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"

Expand All @@ -21,9 +22,12 @@ import (
"github.com/filecoin-project/go-state-types/exitcode"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/apibstore"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/events"
"github.com/filecoin-project/lotus/chain/events/state"
"github.com/filecoin-project/lotus/chain/types"
Expand Down Expand Up @@ -70,6 +74,35 @@ func NewProviderNodeAdapter(fc *config.MinerFeeConfig) func(dag dtypes.StagingDA
}
}

func (n *ProviderNodeAdapter) diffPreCommits(ctx context.Context, actor address.Address, pre, cur types.TipSetKey) (*miner.PreCommitChanges, error) {
store := adt.WrapStore(ctx, cbor.NewCborStore(apibstore.NewAPIBlockstore(n)))

preAct, err := n.StateGetActor(ctx, actor, pre)
if err != nil {
return nil, xerrors.Errorf("getting pre actor: %w", err)
}
curAct, err := n.StateGetActor(ctx, actor, cur)
if err != nil {
return nil, xerrors.Errorf("getting cur actor: %w", err)
}

preSt, err := miner.Load(store, preAct)
if err != nil {
return nil, xerrors.Errorf("loading miner actor: %w", err)
}
curSt, err := miner.Load(store, curAct)
if err != nil {
return nil, xerrors.Errorf("loading miner actor: %w", err)
}

diff, err := miner.DiffPreCommits(preSt, curSt)
if err != nil {
return nil, xerrors.Errorf("diff precommits: %w", err)
}

return diff, err
}

func (n *ProviderNodeAdapter) PublishDeals(ctx context.Context, deal storagemarket.MinerDeal) (cid.Cid, error) {
log.Info("publishing deal")

Expand Down

0 comments on commit de0a452

Please sign in to comment.