2020-09-12 03:07:52 +00:00
|
|
|
package market
|
|
|
|
|
|
|
|
import (
|
2020-09-17 02:13:12 +00:00
|
|
|
"bytes"
|
|
|
|
|
2020-09-15 23:47:58 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-14 11:14:06 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-24 00:40:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
2020-09-17 02:13:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2020-09-14 11:14:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-24 00:40:29 +00:00
|
|
|
|
|
|
|
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
|
2020-09-18 21:59:27 +00:00
|
|
|
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
|
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
|
|
|
|
}
|
|
|
|
|
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-26 14:48:20 +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
|
|
|
}
|
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
func fromV0DealState(v0 market0.DealState) DealState {
|
2020-09-17 21:59:10 +00:00
|
|
|
return (DealState)(v0)
|
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
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
proposal := fromV0DealProposal(proposal0)
|
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 {
|
|
|
|
return cb(abi.DealID(idx), fromV0DealProposal(dp0))
|
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
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
dp := fromV0DealProposal(dp0)
|
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
|
|
|
}
|
|
|
|
|
2020-09-24 00:40:29 +00:00
|
|
|
func fromV0DealProposal(v0 market0.DealProposal) DealProposal {
|
2020-09-17 23:08:54 +00:00
|
|
|
return (DealProposal)(v0)
|
2020-09-17 02:13:12 +00:00
|
|
|
}
|