2020-11-05 04:41:04 +00:00
|
|
|
package storageadapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
2020-11-10 03:23:58 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-11-05 04:41:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2021-01-21 11:21:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2020-11-05 04:41:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type getCurrentDealInfoAPI interface {
|
2020-11-10 03:23:58 +00:00
|
|
|
StateLookupID(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
2020-11-05 04:41:04 +00:00
|
|
|
StateMarketStorageDeal(context.Context, abi.DealID, types.TipSetKey) (*api.MarketDeal, error)
|
|
|
|
StateSearchMsg(context.Context, cid.Cid) (*api.MsgLookup, error)
|
2021-01-21 10:54:32 +00:00
|
|
|
|
2021-01-21 11:21:19 +00:00
|
|
|
diffPreCommits(ctx context.Context, actor address.Address, pre, cur types.TipSetKey) (*miner.PreCommitChanges, error)
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentDealInfo gets current information on a deal, and corrects the deal ID as needed
|
2021-01-21 10:54:32 +00:00
|
|
|
func GetCurrentDealInfo(ctx context.Context, ts *types.TipSet, api getCurrentDealInfoAPI, dealID abi.DealID, proposal market.DealProposal, publishCid *cid.Cid) (abi.DealID, *api.MarketDeal, types.TipSetKey, error) {
|
2020-11-05 04:41:04 +00:00
|
|
|
marketDeal, dealErr := api.StateMarketStorageDeal(ctx, dealID, ts.Key())
|
|
|
|
if dealErr == nil {
|
2020-11-10 03:23:58 +00:00
|
|
|
equal, err := checkDealEquality(ctx, ts, api, proposal, marketDeal.Proposal)
|
|
|
|
if err != nil {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, err
|
2020-11-10 03:23:58 +00:00
|
|
|
}
|
|
|
|
if equal {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, marketDeal, types.EmptyTSK, nil
|
2020-11-10 00:30:47 +00:00
|
|
|
}
|
|
|
|
dealErr = xerrors.Errorf("Deal proposals did not match")
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
if publishCid == nil {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, dealErr
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
// attempt deal id correction
|
|
|
|
lookup, err := api.StateSearchMsg(ctx, *publishCid)
|
|
|
|
if err != nil {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, err
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lookup.Receipt.ExitCode != exitcode.Ok {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, xerrors.Errorf("looking for publish deal message %s: non-ok exit code: %s", *publishCid, lookup.Receipt.ExitCode)
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var retval market.PublishStorageDealsReturn
|
|
|
|
if err := retval.UnmarshalCBOR(bytes.NewReader(lookup.Receipt.Return)); err != nil {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, xerrors.Errorf("looking for publish deal message: unmarshaling message return: %w", err)
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(retval.IDs) != 1 {
|
|
|
|
// market currently only ever sends messages with 1 deal
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, xerrors.Errorf("can't recover dealIDs from publish deal message with more than 1 deal")
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if retval.IDs[0] == dealID {
|
|
|
|
// DealID did not change, so we are stuck with the original lookup error
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, lookup.TipSet, dealErr
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dealID = retval.IDs[0]
|
|
|
|
marketDeal, err = api.StateMarketStorageDeal(ctx, dealID, ts.Key())
|
|
|
|
|
2020-11-10 03:23:58 +00:00
|
|
|
if err == nil {
|
|
|
|
equal, err := checkDealEquality(ctx, ts, api, proposal, marketDeal.Proposal)
|
|
|
|
if err != nil {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, err
|
2020-11-10 03:23:58 +00:00
|
|
|
}
|
|
|
|
if !equal {
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, nil, types.EmptyTSK, xerrors.Errorf("Deal proposals did not match")
|
2020-11-10 03:23:58 +00:00
|
|
|
}
|
2020-11-10 00:30:47 +00:00
|
|
|
}
|
2021-01-21 10:54:32 +00:00
|
|
|
return dealID, marketDeal, lookup.TipSet, err
|
2020-11-05 04:41:04 +00:00
|
|
|
}
|
2020-11-10 03:23:58 +00:00
|
|
|
|
|
|
|
func checkDealEquality(ctx context.Context, ts *types.TipSet, api getCurrentDealInfoAPI, p1, p2 market.DealProposal) (bool, error) {
|
|
|
|
p1ClientID, err := api.StateLookupID(ctx, p1.Client, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
p2ClientID, err := api.StateLookupID(ctx, p2.Client, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return p1.PieceCID.Equals(p2.PieceCID) &&
|
|
|
|
p1.PieceSize == p2.PieceSize &&
|
|
|
|
p1.VerifiedDeal == p2.VerifiedDeal &&
|
|
|
|
p1.Label == p2.Label &&
|
|
|
|
p1.StartEpoch == p2.StartEpoch &&
|
|
|
|
p1.EndEpoch == p2.EndEpoch &&
|
|
|
|
p1.StoragePricePerEpoch.Equals(p2.StoragePricePerEpoch) &&
|
|
|
|
p1.ProviderCollateral.Equals(p2.ProviderCollateral) &&
|
|
|
|
p1.ClientCollateral.Equals(p2.ClientCollateral) &&
|
|
|
|
p1.Provider == p2.Provider &&
|
|
|
|
p1ClientID == p2ClientID, nil
|
|
|
|
}
|