2020-09-12 03:07:52 +00:00
|
|
|
package market
|
|
|
|
|
|
|
|
import (
|
2022-03-12 18:07:35 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-09-29 20:16:19 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-09-14 11:14:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2021-04-30 15:51:24 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/cbor"
|
2020-09-17 07:32:10 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-09-17 23:08:54 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2020-09-14 11:14:06 +00:00
|
|
|
|
2020-09-19 03:46:03 +00:00
|
|
|
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
|
2022-03-12 18:07:35 +00:00
|
|
|
market8 "github.com/filecoin-project/specs-actors/v8/actors/builtin/market"
|
2021-04-26 14:48:20 +00:00
|
|
|
|
|
|
|
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
|
2020-09-28 20:13:18 +00:00
|
|
|
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-01-16 04:58:18 +00:00
|
|
|
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-04-23 12:29:46 +00:00
|
|
|
builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
|
2020-09-19 03:46:03 +00:00
|
|
|
|
2021-05-06 05:44:11 +00:00
|
|
|
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
|
|
|
|
|
2021-09-15 15:22:25 +00:00
|
|
|
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
builtin7 "github.com/filecoin-project/specs-actors/v7/actors/builtin"
|
|
|
|
|
2022-03-01 03:57:40 +00:00
|
|
|
builtin8 "github.com/filecoin-project/specs-actors/v8/actors/builtin"
|
|
|
|
|
2022-04-04 11:25:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2022-04-04 11:30:42 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2020-09-24 00:40:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-09-14 11:14:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-12 03:07:52 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
func init() {
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
builtin.RegisterActorState(builtin0.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load0(store, root)
|
|
|
|
})
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2020-09-28 20:13:18 +00:00
|
|
|
builtin.RegisterActorState(builtin2.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load2(store, root)
|
2020-09-24 00:40:29 +00:00
|
|
|
})
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-01-18 23:01:44 +00:00
|
|
|
builtin.RegisterActorState(builtin3.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load3(store, root)
|
|
|
|
})
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-04-23 12:29:46 +00:00
|
|
|
builtin.RegisterActorState(builtin4.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load4(store, root)
|
|
|
|
})
|
2021-05-06 05:44:11 +00:00
|
|
|
|
|
|
|
builtin.RegisterActorState(builtin5.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load5(store, root)
|
|
|
|
})
|
2021-09-15 15:22:25 +00:00
|
|
|
|
|
|
|
builtin.RegisterActorState(builtin6.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load6(store, root)
|
|
|
|
})
|
2021-11-04 15:59:29 +00:00
|
|
|
|
|
|
|
builtin.RegisterActorState(builtin7.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load7(store, root)
|
|
|
|
})
|
2022-03-01 03:57:40 +00:00
|
|
|
|
|
|
|
builtin.RegisterActorState(builtin8.StorageMarketActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
|
|
|
|
return load8(store, root)
|
|
|
|
})
|
2020-09-24 00:40:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
var (
|
2022-03-01 03:57:40 +00:00
|
|
|
Address = builtin8.StorageMarketActorAddr
|
|
|
|
Methods = builtin8.MethodsMarket
|
2020-10-08 20:32:54 +00:00
|
|
|
)
|
2020-09-15 23:47:58 +00:00
|
|
|
|
2021-04-26 14:48:20 +00:00
|
|
|
func Load(store adt.Store, act *types.Actor) (State, error) {
|
2022-04-04 11:25:03 +00:00
|
|
|
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
|
2022-04-04 11:30:42 +00:00
|
|
|
if name != "storagemarket" {
|
|
|
|
return nil, xerrors.Errorf("actor code is not storagemarket: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch av {
|
|
|
|
|
|
|
|
case actors.Version0:
|
|
|
|
return load0(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version2:
|
|
|
|
return load2(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version3:
|
|
|
|
return load3(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version4:
|
|
|
|
return load4(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version5:
|
|
|
|
return load5(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version6:
|
|
|
|
return load6(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version7:
|
|
|
|
return load7(store, act.Head)
|
|
|
|
|
|
|
|
case actors.Version8:
|
|
|
|
return load8(store, act.Head)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, xerrors.Errorf("unknown actor version: %d", av)
|
|
|
|
}
|
2022-04-04 11:25:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
switch act.Code {
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
case builtin0.StorageMarketActorCodeID:
|
2020-09-24 00:40:29 +00:00
|
|
|
return load0(store, act.Head)
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2020-09-28 20:13:18 +00:00
|
|
|
case builtin2.StorageMarketActorCodeID:
|
|
|
|
return load2(store, act.Head)
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-01-16 04:58:18 +00:00
|
|
|
case builtin3.StorageMarketActorCodeID:
|
|
|
|
return load3(store, act.Head)
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-04-23 12:29:46 +00:00
|
|
|
case builtin4.StorageMarketActorCodeID:
|
|
|
|
return load4(store, act.Head)
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-05-06 05:44:11 +00:00
|
|
|
case builtin5.StorageMarketActorCodeID:
|
|
|
|
return load5(store, act.Head)
|
|
|
|
|
2021-09-15 15:22:25 +00:00
|
|
|
case builtin6.StorageMarketActorCodeID:
|
|
|
|
return load6(store, act.Head)
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
case builtin7.StorageMarketActorCodeID:
|
|
|
|
return load7(store, act.Head)
|
|
|
|
|
2022-03-01 03:57:40 +00:00
|
|
|
case builtin8.StorageMarketActorCodeID:
|
|
|
|
return load8(store, act.Head)
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
|
|
|
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
func MakeState(store adt.Store, av actors.Version) (State, error) {
|
|
|
|
switch av {
|
|
|
|
|
|
|
|
case actors.Version0:
|
|
|
|
return make0(store)
|
|
|
|
|
|
|
|
case actors.Version2:
|
|
|
|
return make2(store)
|
|
|
|
|
|
|
|
case actors.Version3:
|
|
|
|
return make3(store)
|
|
|
|
|
|
|
|
case actors.Version4:
|
|
|
|
return make4(store)
|
|
|
|
|
2021-05-27 10:27:47 +00:00
|
|
|
case actors.Version5:
|
|
|
|
return make5(store)
|
|
|
|
|
2021-09-15 15:22:25 +00:00
|
|
|
case actors.Version6:
|
|
|
|
return make6(store)
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
case actors.Version7:
|
|
|
|
return make7(store)
|
|
|
|
|
2022-03-01 03:57:40 +00:00
|
|
|
case actors.Version8:
|
|
|
|
return make8(store)
|
|
|
|
|
2022-04-04 11:30:42 +00:00
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
return nil, xerrors.Errorf("unknown actor version %d", av)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetActorCodeID(av actors.Version) (cid.Cid, error) {
|
2022-04-04 11:30:42 +00:00
|
|
|
if c, ok := actors.GetActorCodeID(av, "storagemarket"); ok {
|
|
|
|
return c, nil
|
|
|
|
}
|
2022-04-04 11:25:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
switch av {
|
|
|
|
|
|
|
|
case actors.Version0:
|
|
|
|
return builtin0.StorageMarketActorCodeID, nil
|
|
|
|
|
|
|
|
case actors.Version2:
|
|
|
|
return builtin2.StorageMarketActorCodeID, nil
|
|
|
|
|
|
|
|
case actors.Version3:
|
|
|
|
return builtin3.StorageMarketActorCodeID, nil
|
|
|
|
|
|
|
|
case actors.Version4:
|
|
|
|
return builtin4.StorageMarketActorCodeID, nil
|
|
|
|
|
2021-05-27 10:27:47 +00:00
|
|
|
case actors.Version5:
|
|
|
|
return builtin5.StorageMarketActorCodeID, nil
|
|
|
|
|
2021-09-15 15:22:25 +00:00
|
|
|
case actors.Version6:
|
|
|
|
return builtin6.StorageMarketActorCodeID, nil
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
case actors.Version7:
|
|
|
|
return builtin7.StorageMarketActorCodeID, nil
|
|
|
|
|
2022-03-01 03:57:40 +00:00
|
|
|
case actors.Version8:
|
|
|
|
return builtin8.StorageMarketActorCodeID, nil
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cid.Undef, xerrors.Errorf("unknown actor version %d", av)
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
type State interface {
|
|
|
|
cbor.Marshaler
|
2020-09-23 05:19:43 +00:00
|
|
|
BalancesChanged(State) (bool, error)
|
2020-09-12 03:07:52 +00:00
|
|
|
EscrowTable() (BalanceTable, error)
|
|
|
|
LockedTable() (BalanceTable, error)
|
|
|
|
TotalLocked() (abi.TokenAmount, error)
|
2020-09-23 05:19:43 +00:00
|
|
|
StatesChanged(State) (bool, error)
|
2020-09-17 02:13:12 +00:00
|
|
|
States() (DealStates, error)
|
2020-09-23 05:19:43 +00:00
|
|
|
ProposalsChanged(State) (bool, error)
|
2020-09-17 02:13:12 +00:00
|
|
|
Proposals() (DealProposals, error)
|
2020-09-15 23:47:58 +00:00
|
|
|
VerifyDealsForActivation(
|
|
|
|
minerAddr address.Address, deals []abi.DealID, currEpoch, sectorExpiry abi.ChainEpoch,
|
|
|
|
) (weight, verifiedWeight abi.DealWeight, err error)
|
2021-03-21 07:11:42 +00:00
|
|
|
NextID() (abi.DealID, error)
|
2021-05-15 01:11:23 +00:00
|
|
|
GetState() interface{}
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BalanceTable interface {
|
2020-09-17 07:32:10 +00:00
|
|
|
ForEach(cb func(address.Address, abi.TokenAmount) error) error
|
2020-09-12 03:07:52 +00:00
|
|
|
Get(key address.Address) (abi.TokenAmount, error)
|
|
|
|
}
|
2020-09-17 02:13:12 +00:00
|
|
|
|
|
|
|
type DealStates interface {
|
2020-09-22 21:09:33 +00:00
|
|
|
ForEach(cb func(id abi.DealID, ds DealState) error) error
|
2020-09-17 07:32:10 +00:00
|
|
|
Get(id abi.DealID) (*DealState, bool, error)
|
2020-09-17 23:08:54 +00:00
|
|
|
|
|
|
|
array() adt.Array
|
|
|
|
decode(*cbg.Deferred) (*DealState, error)
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DealProposals interface {
|
2020-09-17 07:32:10 +00:00
|
|
|
ForEach(cb func(id abi.DealID, dp DealProposal) error) error
|
|
|
|
Get(id abi.DealID) (*DealProposal, bool, error)
|
2020-09-17 23:08:54 +00:00
|
|
|
|
|
|
|
array() adt.Array
|
|
|
|
decode(*cbg.Deferred) (*DealProposal, error)
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 18:07:35 +00:00
|
|
|
type PublishStorageDealsParams = market8.PublishStorageDealsParams
|
2021-09-29 20:16:19 +00:00
|
|
|
|
|
|
|
type PublishStorageDealsReturn interface {
|
2022-04-04 11:30:42 +00:00
|
|
|
DealIDs() ([]abi.DealID, error)
|
2021-09-29 20:16:19 +00:00
|
|
|
// Note that this index is based on the batch of deals that were published, NOT the DealID
|
|
|
|
IsDealValid(index uint64) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodePublishStorageDealsReturn(b []byte, nv network.Version) (PublishStorageDealsReturn, error) {
|
|
|
|
av, err := actors.VersionForNetwork(nv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch av {
|
|
|
|
|
|
|
|
case actors.Version0:
|
|
|
|
return decodePublishStorageDealsReturn0(b)
|
|
|
|
|
|
|
|
case actors.Version2:
|
|
|
|
return decodePublishStorageDealsReturn2(b)
|
|
|
|
|
|
|
|
case actors.Version3:
|
|
|
|
return decodePublishStorageDealsReturn3(b)
|
|
|
|
|
|
|
|
case actors.Version4:
|
|
|
|
return decodePublishStorageDealsReturn4(b)
|
|
|
|
|
|
|
|
case actors.Version5:
|
|
|
|
return decodePublishStorageDealsReturn5(b)
|
|
|
|
|
|
|
|
case actors.Version6:
|
|
|
|
return decodePublishStorageDealsReturn6(b)
|
|
|
|
|
2021-11-04 15:59:29 +00:00
|
|
|
case actors.Version7:
|
|
|
|
return decodePublishStorageDealsReturn7(b)
|
|
|
|
|
2022-03-01 03:57:40 +00:00
|
|
|
case actors.Version8:
|
|
|
|
return decodePublishStorageDealsReturn8(b)
|
|
|
|
|
2022-04-04 11:30:42 +00:00
|
|
|
}
|
2021-09-29 20:16:19 +00:00
|
|
|
return nil, xerrors.Errorf("unknown actor version %d", av)
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:18:52 +00:00
|
|
|
type VerifyDealsForActivationParams = market0.VerifyDealsForActivationParams
|
2020-11-06 15:58:45 +00:00
|
|
|
type WithdrawBalanceParams = market0.WithdrawBalanceParams
|
2020-09-23 06:18:52 +00:00
|
|
|
|
2022-03-12 18:07:35 +00:00
|
|
|
type ClientDealProposal = market8.ClientDealProposal
|
2020-09-23 06:18:52 +00:00
|
|
|
|
2020-09-17 07:32:10 +00:00
|
|
|
type DealState struct {
|
|
|
|
SectorStartEpoch abi.ChainEpoch // -1 if not yet included in proven sector
|
|
|
|
LastUpdatedEpoch abi.ChainEpoch // -1 if deal state never updated
|
|
|
|
SlashEpoch abi.ChainEpoch // -1 if deal never slashed
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 07:32:10 +00:00
|
|
|
type DealProposal struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
PieceCID cid.Cid
|
|
|
|
PieceSize abi.PaddedPieceSize
|
|
|
|
VerifiedDeal bool
|
|
|
|
Client address.Address
|
|
|
|
Provider address.Address
|
2022-03-12 18:07:35 +00:00
|
|
|
Label market8.DealLabel
|
2022-04-04 11:30:42 +00:00
|
|
|
StartEpoch abi.ChainEpoch
|
|
|
|
EndEpoch abi.ChainEpoch
|
2020-09-17 07:32:10 +00:00
|
|
|
StoragePricePerEpoch abi.TokenAmount
|
2022-04-04 11:30:42 +00:00
|
|
|
ProviderCollateral abi.TokenAmount
|
|
|
|
ClientCollateral abi.TokenAmount
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DealStateChanges struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
Added []DealIDState
|
2020-09-17 02:13:12 +00:00
|
|
|
Modified []DealStateChange
|
2022-04-04 11:30:42 +00:00
|
|
|
Removed []DealIDState
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DealIDState struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
ID abi.DealID
|
2020-09-17 02:13:12 +00:00
|
|
|
Deal DealState
|
|
|
|
}
|
|
|
|
|
|
|
|
// DealStateChange is a change in deal state from -> to
|
|
|
|
type DealStateChange struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
ID abi.DealID
|
2020-09-17 07:32:10 +00:00
|
|
|
From *DealState
|
2022-04-04 11:30:42 +00:00
|
|
|
To *DealState
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DealProposalChanges struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
Added []ProposalIDState
|
2020-09-17 02:13:12 +00:00
|
|
|
Removed []ProposalIDState
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProposalIDState struct {
|
2022-04-04 11:30:42 +00:00
|
|
|
ID abi.DealID
|
2020-09-17 02:13:12 +00:00
|
|
|
Proposal DealProposal
|
|
|
|
}
|
2020-09-17 07:32:10 +00:00
|
|
|
|
|
|
|
func EmptyDealState() *DealState {
|
|
|
|
return &DealState{
|
|
|
|
SectorStartEpoch: -1,
|
2022-04-04 11:30:42 +00:00
|
|
|
SlashEpoch: -1,
|
2020-09-17 07:32:10 +00:00
|
|
|
LastUpdatedEpoch: -1,
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 06:08:19 +00:00
|
|
|
|
|
|
|
// returns the earned fees and pending fees for a given deal
|
|
|
|
func (deal DealProposal) GetDealFees(height abi.ChainEpoch) (abi.TokenAmount, abi.TokenAmount) {
|
2022-04-04 11:30:42 +00:00
|
|
|
tf := big.Mul(deal.StoragePricePerEpoch, big.NewInt(int64(deal.EndEpoch-deal.StartEpoch)))
|
2021-01-05 06:08:19 +00:00
|
|
|
|
2022-04-04 11:30:42 +00:00
|
|
|
ef := big.Mul(deal.StoragePricePerEpoch, big.NewInt(int64(height-deal.StartEpoch)))
|
|
|
|
if ef.LessThan(big.Zero()) {
|
|
|
|
ef = big.Zero()
|
|
|
|
}
|
2021-01-05 06:08:19 +00:00
|
|
|
|
2022-04-04 11:30:42 +00:00
|
|
|
if ef.GreaterThan(tf) {
|
|
|
|
ef = tf
|
|
|
|
}
|
2021-01-05 06:08:19 +00:00
|
|
|
|
2022-04-04 11:30:42 +00:00
|
|
|
return ef, big.Sub(tf, ef)
|
2021-01-05 06:08:19 +00:00
|
|
|
}
|
2022-03-12 18:07:35 +00:00
|
|
|
|
|
|
|
func labelFromGoString(s string) (market8.DealLabel, error) {
|
|
|
|
if utf8.ValidString(s) {
|
|
|
|
return market8.NewLabelFromString(s)
|
|
|
|
} else {
|
|
|
|
return market8.NewLabelFromBytes([]byte(s))
|
|
|
|
}
|
|
|
|
}
|