2020-09-12 03:07:52 +00:00
|
|
|
package market
|
|
|
|
|
|
|
|
import (
|
2020-09-17 02:13:12 +00:00
|
|
|
"bytes"
|
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2021-09-29 20:16:19 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-09-24 00:40:29 +00:00
|
|
|
|
2022-09-07 23:30:55 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-10-03 05:51:30 +00:00
|
|
|
verifregtypes "github.com/filecoin-project/go-state-types/builtin/v9/verifreg"
|
2022-09-06 15:49:29 +00:00
|
|
|
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
|
|
|
|
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
|
2022-09-07 23:30:55 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-12 03:07:52 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 06:19:32 +00:00
|
|
|
var _ State = (*state0)(nil)
|
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
func load0(store adt.Store, root cid.Cid) (State, error) {
|
|
|
|
out := state0{store: store}
|
|
|
|
err := store.Get(store.Context(), root, &out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
func make0(store adt.Store) (State, error) {
|
|
|
|
out := state0{store: store}
|
|
|
|
|
2022-04-07 18:46:23 +00:00
|
|
|
ea, err := adt0.MakeEmptyArray(store).Root()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
em, err := adt0.MakeEmptyMap(store).Root()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out.State = *market0.ConstructState(ea, em, em)
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
type state0 struct {
|
2020-09-24 00:40:29 +00:00
|
|
|
market0.State
|
2020-09-12 03:07:52 +00:00
|
|
|
store adt.Store
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) TotalLocked() (abi.TokenAmount, error) {
|
2020-09-12 03:07:52 +00:00
|
|
|
fml := types.BigAdd(s.TotalClientLockedCollateral, s.TotalProviderLockedCollateral)
|
|
|
|
fml = types.BigAdd(fml, s.TotalClientStorageFee)
|
|
|
|
return fml, nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 05:19:43 +00:00
|
|
|
func (s *state0) BalancesChanged(otherState State) (bool, error) {
|
2020-09-18 21:59:27 +00:00
|
|
|
otherState0, ok := otherState.(*state0)
|
2020-09-17 02:13:12 +00:00
|
|
|
if !ok {
|
2020-09-20 21:53:46 +00:00
|
|
|
// there's no way to compare different versions of the state, so let's
|
2020-09-17 02:13:12 +00:00
|
|
|
// just say that means the state of balances has changed
|
2020-09-23 05:19:43 +00:00
|
|
|
return true, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2020-09-23 05:19:43 +00:00
|
|
|
return !s.State.EscrowTable.Equals(otherState0.State.EscrowTable) || !s.State.LockedTable.Equals(otherState0.State.LockedTable), nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 05:19:43 +00:00
|
|
|
func (s *state0) StatesChanged(otherState State) (bool, error) {
|
2020-09-18 21:59:27 +00:00
|
|
|
otherState0, ok := otherState.(*state0)
|
2020-09-17 02:13:12 +00:00
|
|
|
if !ok {
|
2020-09-20 21:53:46 +00:00
|
|
|
// there's no way to compare different versions of the state, so let's
|
2020-09-17 02:13:12 +00:00
|
|
|
// just say that means the state of balances has changed
|
2020-09-23 05:19:43 +00:00
|
|
|
return true, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2020-09-23 05:19:43 +00:00
|
|
|
return !s.State.States.Equals(otherState0.State.States), nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) States() (DealStates, error) {
|
|
|
|
stateArray, err := adt0.AsArray(s.store, s.State.States)
|
2020-09-17 02:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
return &dealStates0{stateArray}, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 05:19:43 +00:00
|
|
|
func (s *state0) ProposalsChanged(otherState State) (bool, error) {
|
2020-09-18 21:59:27 +00:00
|
|
|
otherState0, ok := otherState.(*state0)
|
2020-09-17 02:13:12 +00:00
|
|
|
if !ok {
|
2020-09-20 21:53:46 +00:00
|
|
|
// there's no way to compare different versions of the state, so let's
|
2020-09-17 02:13:12 +00:00
|
|
|
// just say that means the state of balances has changed
|
2020-09-23 05:19:43 +00:00
|
|
|
return true, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2020-09-23 05:19:43 +00:00
|
|
|
return !s.State.Proposals.Equals(otherState0.State.Proposals), nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) Proposals() (DealProposals, error) {
|
|
|
|
proposalArray, err := adt0.AsArray(s.store, s.State.Proposals)
|
2020-09-17 02:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
return &dealProposals0{proposalArray}, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) EscrowTable() (BalanceTable, error) {
|
|
|
|
bt, err := adt0.AsBalanceTable(s.store, s.State.EscrowTable)
|
2020-09-17 07:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
return &balanceTable0{bt}, nil
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) LockedTable() (BalanceTable, error) {
|
|
|
|
bt, err := adt0.AsBalanceTable(s.store, s.State.LockedTable)
|
2020-09-17 07:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
return &balanceTable0{bt}, nil
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
2020-09-15 23:47:58 +00:00
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *state0) VerifyDealsForActivation(
|
2020-09-15 23:47:58 +00:00
|
|
|
minerAddr address.Address, deals []abi.DealID, currEpoch, sectorExpiry abi.ChainEpoch,
|
|
|
|
) (weight, verifiedWeight abi.DealWeight, err error) {
|
2021-04-27 18:48:32 +00:00
|
|
|
w, vw, err := market0.ValidateDealsForActivation(&s.State, s.store, deals, minerAddr, sectorExpiry, currEpoch)
|
|
|
|
return w, vw, err
|
2020-09-15 23:47:58 +00:00
|
|
|
}
|
2020-09-17 02:13:12 +00:00
|
|
|
|
2021-03-21 07:11:42 +00:00
|
|
|
func (s *state0) NextID() (abi.DealID, error) {
|
|
|
|
return s.State.NextID, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
type balanceTable0 struct {
|
|
|
|
*adt0.BalanceTable
|
2020-09-17 07:32:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (bt *balanceTable0) ForEach(cb func(address.Address, abi.TokenAmount) error) error {
|
|
|
|
asMap := (*adt0.Map)(bt.BalanceTable)
|
2020-09-17 07:32:10 +00:00
|
|
|
var ta abi.TokenAmount
|
|
|
|
return asMap.ForEach(&ta, func(key string) error {
|
|
|
|
a, err := address.NewFromBytes([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cb(a, ta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
type dealStates0 struct {
|
2020-09-17 02:13:12 +00:00
|
|
|
adt.Array
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealStates0) Get(dealID abi.DealID) (*DealState, bool, error) {
|
2020-09-24 00:40:29 +00:00
|
|
|
var deal0 market0.DealState
|
2020-09-18 21:59:27 +00:00
|
|
|
found, err := s.Array.Get(uint64(dealID), &deal0)
|
2020-09-17 02:13:12 +00:00
|
|
|
if err != nil {
|
2020-09-17 07:32:10 +00:00
|
|
|
return nil, false, err
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
if !found {
|
2020-09-17 07:32:10 +00:00
|
|
|
return nil, false, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
deal := fromV0DealState(deal0)
|
2020-09-17 07:32:10 +00:00
|
|
|
return &deal, true, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:09:33 +00:00
|
|
|
func (s *dealStates0) ForEach(cb func(dealID abi.DealID, ds DealState) error) error {
|
2020-09-24 00:40:29 +00:00
|
|
|
var ds0 market0.DealState
|
2020-09-22 21:09:33 +00:00
|
|
|
return s.Array.ForEach(&ds0, func(idx int64) error {
|
|
|
|
return cb(abi.DealID(idx), fromV0DealState(ds0))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealStates0) decode(val *cbg.Deferred) (*DealState, error) {
|
2020-09-24 00:40:29 +00:00
|
|
|
var ds0 market0.DealState
|
2020-09-18 21:59:27 +00:00
|
|
|
if err := ds0.UnmarshalCBOR(bytes.NewReader(val.Raw)); err != nil {
|
2020-09-17 23:08:54 +00:00
|
|
|
return nil, err
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
ds := fromV0DealState(ds0)
|
2020-09-17 23:08:54 +00:00
|
|
|
return &ds, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealStates0) array() adt.Array {
|
2020-09-17 23:08:54 +00:00
|
|
|
return s.Array
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 23:36:27 +00:00
|
|
|
func fromV0DealState(v0 market0.DealState) DealState {
|
2022-09-26 16:47:48 +00:00
|
|
|
|
|
|
|
return DealState{
|
|
|
|
SectorStartEpoch: v0.SectorStartEpoch,
|
|
|
|
LastUpdatedEpoch: v0.LastUpdatedEpoch,
|
|
|
|
SlashEpoch: v0.SlashEpoch,
|
|
|
|
VerifiedClaim: 0,
|
|
|
|
}
|
|
|
|
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
type dealProposals0 struct {
|
2020-09-17 02:13:12 +00:00
|
|
|
adt.Array
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealProposals0) Get(dealID abi.DealID) (*DealProposal, bool, error) {
|
2020-09-24 00:40:29 +00:00
|
|
|
var proposal0 market0.DealProposal
|
2020-09-18 21:59:27 +00:00
|
|
|
found, err := s.Array.Get(uint64(dealID), &proposal0)
|
2020-09-17 07:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2022-03-12 18:07:35 +00:00
|
|
|
|
|
|
|
proposal, err := fromV0DealProposal(proposal0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, xerrors.Errorf("decoding proposal: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-17 07:32:10 +00:00
|
|
|
return &proposal, true, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealProposals0) ForEach(cb func(dealID abi.DealID, dp DealProposal) error) error {
|
2020-09-24 00:40:29 +00:00
|
|
|
var dp0 market0.DealProposal
|
2020-09-18 21:59:27 +00:00
|
|
|
return s.Array.ForEach(&dp0, func(idx int64) error {
|
2022-03-12 18:07:35 +00:00
|
|
|
dp, err := fromV0DealProposal(dp0)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decoding proposal: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(abi.DealID(idx), dp)
|
2020-09-17 07:32:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealProposals0) decode(val *cbg.Deferred) (*DealProposal, error) {
|
2020-09-24 00:40:29 +00:00
|
|
|
var dp0 market0.DealProposal
|
2020-09-18 21:59:27 +00:00
|
|
|
if err := dp0.UnmarshalCBOR(bytes.NewReader(val.Raw)); err != nil {
|
2020-09-17 23:08:54 +00:00
|
|
|
return nil, err
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2022-03-12 18:07:35 +00:00
|
|
|
|
|
|
|
dp, err := fromV0DealProposal(dp0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-17 23:08:54 +00:00
|
|
|
return &dp, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
func (s *dealProposals0) array() adt.Array {
|
2020-09-17 23:08:54 +00:00
|
|
|
return s.Array
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 23:36:27 +00:00
|
|
|
func fromV0DealProposal(v0 market0.DealProposal) (DealProposal, error) {
|
2022-04-07 21:36:17 +00:00
|
|
|
|
2022-04-07 23:36:27 +00:00
|
|
|
label, err := labelFromGoString(v0.Label)
|
2022-09-06 15:49:29 +00:00
|
|
|
|
2022-04-07 23:36:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return DealProposal{}, xerrors.Errorf("error setting deal label: %w", err)
|
|
|
|
}
|
2022-04-07 21:36:17 +00:00
|
|
|
|
2022-04-07 23:36:27 +00:00
|
|
|
return DealProposal{
|
|
|
|
PieceCID: v0.PieceCID,
|
|
|
|
PieceSize: v0.PieceSize,
|
|
|
|
VerifiedDeal: v0.VerifiedDeal,
|
|
|
|
Client: v0.Client,
|
|
|
|
Provider: v0.Provider,
|
|
|
|
|
|
|
|
Label: label,
|
|
|
|
|
|
|
|
StartEpoch: v0.StartEpoch,
|
|
|
|
EndEpoch: v0.EndEpoch,
|
|
|
|
StoragePricePerEpoch: v0.StoragePricePerEpoch,
|
|
|
|
|
|
|
|
ProviderCollateral: v0.ProviderCollateral,
|
|
|
|
ClientCollateral: v0.ClientCollateral,
|
|
|
|
}, nil
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
func (s *state0) GetState() interface{} {
|
|
|
|
return &s.State
|
|
|
|
}
|
2021-09-29 20:16:19 +00:00
|
|
|
|
|
|
|
var _ PublishStorageDealsReturn = (*publishStorageDealsReturn0)(nil)
|
|
|
|
|
|
|
|
func decodePublishStorageDealsReturn0(b []byte) (PublishStorageDealsReturn, error) {
|
|
|
|
var retval market0.PublishStorageDealsReturn
|
|
|
|
if err := retval.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to unmarshal PublishStorageDealsReturn: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &publishStorageDealsReturn0{retval}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type publishStorageDealsReturn0 struct {
|
|
|
|
market0.PublishStorageDealsReturn
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:34:45 +00:00
|
|
|
func (r *publishStorageDealsReturn0) IsDealValid(index uint64) (bool, int, error) {
|
2022-04-07 18:46:23 +00:00
|
|
|
|
|
|
|
// PublishStorageDeals only succeeded if all deals were valid in this version of actors
|
2022-04-12 19:34:45 +00:00
|
|
|
return true, int(index), nil
|
2022-04-07 18:46:23 +00:00
|
|
|
|
2021-09-29 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *publishStorageDealsReturn0) DealIDs() ([]abi.DealID, error) {
|
2022-04-07 18:46:23 +00:00
|
|
|
return r.IDs, nil
|
2021-09-29 20:16:19 +00:00
|
|
|
}
|
2022-10-03 05:51:30 +00:00
|
|
|
|
2022-10-05 18:43:16 +00:00
|
|
|
func (s *state0) GetAllocationIdForPendingDeal(dealId abi.DealID) (verifregtypes.AllocationId, error) {
|
2022-10-03 05:51:30 +00:00
|
|
|
|
|
|
|
return 0, xerrors.Errorf("unsupported before actors v9")
|
|
|
|
|
|
|
|
}
|