2019-08-20 16:48:33 +00:00
package full
import (
2019-10-22 19:29:05 +00:00
"bytes"
2019-08-20 16:48:33 +00:00
"context"
2019-10-22 19:29:05 +00:00
"strconv"
2019-09-03 04:36:07 +00:00
2019-09-19 20:25:18 +00:00
cid "github.com/ipfs/go-cid"
2019-09-16 20:11:17 +00:00
"go.uber.org/fx"
"golang.org/x/xerrors"
2019-12-19 20:13:17 +00:00
"github.com/filecoin-project/go-address"
2020-07-14 12:32:17 +00:00
"github.com/filecoin-project/go-bitfield"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
2020-10-08 23:27:38 +00:00
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/network"
2020-08-17 13:26:18 +00:00
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
2020-06-26 13:49:39 +00:00
2019-10-18 04:47:41 +00:00
"github.com/filecoin-project/lotus/api"
2020-10-08 23:27:38 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin"
2020-09-17 07:32:10 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
2020-09-15 11:04:45 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
2020-09-15 21:44:03 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
2020-09-15 23:47:58 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
2020-10-08 23:27:38 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/actors/policy"
2020-04-30 22:11:14 +00:00
"github.com/filecoin-project/lotus/chain/beacon"
2019-10-18 04:47:41 +00:00
"github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
2020-04-13 21:05:34 +00:00
"github.com/filecoin-project/lotus/node/modules/dtypes"
2019-08-20 16:48:33 +00:00
)
2020-10-01 15:51:27 +00:00
type StateModuleAPI interface {
2020-10-09 11:41:09 +00:00
MsigGetAvailableBalance ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( types . BigInt , error )
MsigGetVested ( ctx context . Context , addr address . Address , start types . TipSetKey , end types . TipSetKey ) ( types . BigInt , error )
2021-02-04 04:46:10 +00:00
MsigGetPending ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( [ ] * api . MsigTransaction , error )
2020-10-01 15:51:27 +00:00
StateAccountKey ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( address . Address , error )
2020-10-09 11:41:09 +00:00
StateDealProviderCollateralBounds ( ctx context . Context , size abi . PaddedPieceSize , verified bool , tsk types . TipSetKey ) ( api . DealCollateralBounds , error )
2020-10-01 15:51:27 +00:00
StateGetActor ( ctx context . Context , actor address . Address , tsk types . TipSetKey ) ( * types . Actor , error )
2020-10-09 11:41:09 +00:00
StateListMiners ( ctx context . Context , tsk types . TipSetKey ) ( [ ] address . Address , error )
2020-10-01 15:51:27 +00:00
StateLookupID ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( address . Address , error )
2020-10-09 11:41:09 +00:00
StateMarketBalance ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( api . MarketBalance , error )
StateMarketStorageDeal ( ctx context . Context , dealId abi . DealID , tsk types . TipSetKey ) ( * api . MarketDeal , error )
StateMinerInfo ( ctx context . Context , actor address . Address , tsk types . TipSetKey ) ( miner . MinerInfo , error )
2020-10-23 14:51:27 +00:00
StateMinerProvingDeadline ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * dline . Info , error )
StateMinerPower ( context . Context , address . Address , types . TipSetKey ) ( * api . MinerPower , error )
2020-10-09 11:41:09 +00:00
StateNetworkVersion ( ctx context . Context , key types . TipSetKey ) ( network . Version , error )
2021-01-08 15:28:38 +00:00
StateSectorGetInfo ( ctx context . Context , maddr address . Address , n abi . SectorNumber , tsk types . TipSetKey ) ( * miner . SectorOnChainInfo , error )
2020-10-20 15:08:25 +00:00
StateVerifiedClientStatus ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * abi . StoragePower , error )
2021-04-03 11:20:50 +00:00
StateSearchMsg ( ctx context . Context , from types . TipSetKey , msg cid . Cid , limit abi . ChainEpoch , allowReplaced bool ) ( * api . MsgLookup , error )
StateWaitMsg ( ctx context . Context , cid cid . Cid , confidence uint64 , limit abi . ChainEpoch , allowReplaced bool ) ( * api . MsgLookup , error )
2020-10-01 15:51:27 +00:00
}
2021-04-03 11:20:50 +00:00
var _ StateModuleAPI = * new ( api . FullNode )
2020-10-01 15:51:27 +00:00
// StateModule provides a default implementation of StateModuleAPI.
// It can be swapped out with another implementation through Dependency
// Injection (for example with a thin RPC client).
type StateModule struct {
fx . In
StateManager * stmgr . StateManager
Chain * store . ChainStore
}
var _ StateModuleAPI = ( * StateModule ) ( nil )
2019-08-20 16:48:33 +00:00
type StateAPI struct {
fx . In
2019-09-06 06:26:02 +00:00
// TODO: the wallet here is only needed because we have the MinerCreateBlock
// API attached to the state API. It probably should live somewhere better
2021-03-23 12:42:56 +00:00
Wallet api . Wallet
2020-10-08 23:27:38 +00:00
DefWallet wallet . Default
2019-09-06 06:26:02 +00:00
2020-10-01 15:51:27 +00:00
StateModuleAPI
2020-04-17 23:36:54 +00:00
ProofVerifier ffiwrapper . Verifier
StateManager * stmgr . StateManager
Chain * store . ChainStore
2020-09-08 20:28:06 +00:00
Beacon beacon . Schedule
2019-08-20 16:48:33 +00:00
}
2020-03-31 23:13:37 +00:00
func ( a * StateAPI ) StateNetworkName ( ctx context . Context ) ( dtypes . NetworkName , error ) {
return stmgr . GetNetworkName ( ctx , a . StateManager , a . Chain . GetHeaviestTipSet ( ) . ParentState ( ) )
}
2020-09-21 19:05:01 +00:00
func ( a * StateAPI ) StateMinerSectors ( ctx context . Context , addr address . Address , sectorNos * bitfield . BitField , tsk types . TipSetKey ) ( [ ] * miner . SectorOnChainInfo , error ) {
2020-10-22 19:09:30 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , addr , tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-10-22 19:09:30 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-02-11 23:29:45 +00:00
}
2019-09-03 04:36:07 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-07-17 14:21:00 +00:00
if err != nil {
2020-10-22 19:09:30 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-07-17 14:21:00 +00:00
}
2020-10-22 19:09:30 +00:00
return mas . LoadSectors ( sectorNos )
}
func ( a * StateAPI ) StateMinerActiveSectors ( ctx context . Context , maddr address . Address , tsk types . TipSetKey ) ( [ ] * miner . SectorOnChainInfo , error ) { // TODO: only used in cli
2020-09-15 13:29:25 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , maddr , tsk )
2020-07-17 14:21:00 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-07-17 14:21:00 +00:00
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-15 13:29:25 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
}
2020-04-15 20:40:46 +00:00
2020-09-15 13:29:25 +00:00
activeSectors , err := miner . AllPartSectors ( mas , miner . Partition . ActiveSectors )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "merge partition active sets: %w" , err )
2020-02-11 23:29:45 +00:00
}
2020-06-09 01:03:44 +00:00
2020-10-22 19:09:30 +00:00
return mas . LoadSectors ( & activeSectors )
2020-04-15 20:40:46 +00:00
}
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateMinerInfo ( ctx context . Context , actor address . Address , tsk types . TipSetKey ) ( miner . MinerInfo , error ) {
2020-11-12 17:05:28 +00:00
ts , err := m . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return miner . MinerInfo { } , xerrors . Errorf ( "failed to load tipset: %w" , err )
}
act , err := m . StateManager . LoadActor ( ctx , actor , ts )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return miner . MinerInfo { } , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-02-11 23:29:45 +00:00
}
2020-06-09 01:03:44 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( m . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-06-09 01:03:44 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return miner . MinerInfo { } , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-06-09 01:03:44 +00:00
}
2020-09-15 13:29:25 +00:00
2020-11-07 05:05:47 +00:00
info , err := mas . Info ( )
if err != nil {
return miner . MinerInfo { } , err
}
return info , nil
2019-10-16 07:07:16 +00:00
}
2020-09-17 15:30:15 +00:00
func ( a * StateAPI ) StateMinerDeadlines ( ctx context . Context , m address . Address , tsk types . TipSetKey ) ( [ ] api . Deadline , error ) {
2020-09-15 13:29:25 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , m , tsk )
2020-09-12 03:07:52 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-09-12 03:07:52 +00:00
}
2020-09-15 13:29:25 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-12 03:07:52 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-09-12 03:07:52 +00:00
}
2020-09-15 13:29:25 +00:00
deadlines , err := mas . NumDeadlines ( )
2020-09-12 03:07:52 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "getting deadline count: %w" , err )
2020-09-12 03:07:52 +00:00
}
2020-09-15 13:29:25 +00:00
2020-09-17 15:30:15 +00:00
out := make ( [ ] api . Deadline , deadlines )
2020-09-15 13:29:25 +00:00
if err := mas . ForEachDeadline ( func ( i uint64 , dl miner . Deadline ) error {
2021-01-16 05:53:27 +00:00
ps , err := dl . PartitionsPoSted ( )
2020-09-17 15:30:15 +00:00
if err != nil {
return err
}
2021-01-18 08:46:22 +00:00
l , err := dl . DisputableProofCount ( )
2020-09-17 15:30:15 +00:00
if err != nil {
return err
}
out [ i ] = api . Deadline {
2021-01-18 08:46:22 +00:00
PostSubmissions : ps ,
DisputableProofCount : l ,
2020-09-17 15:30:15 +00:00
}
2020-09-12 03:07:52 +00:00
return nil
} ) ; err != nil {
2020-09-15 13:29:25 +00:00
return nil , err
2020-09-12 03:07:52 +00:00
}
2020-09-15 13:29:25 +00:00
return out , nil
2020-07-14 17:10:31 +00:00
}
2020-09-17 15:30:15 +00:00
func ( a * StateAPI ) StateMinerPartitions ( ctx context . Context , m address . Address , dlIdx uint64 , tsk types . TipSetKey ) ( [ ] api . Partition , error ) {
2020-09-15 13:29:25 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , m , tsk )
if err != nil {
return nil , xerrors . Errorf ( "failed to load miner actor: %w" , err )
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-15 13:29:25 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
}
dl , err := mas . LoadDeadline ( dlIdx )
if err != nil {
return nil , xerrors . Errorf ( "failed to load the deadline: %w" , err )
}
2020-09-17 15:30:15 +00:00
var out [ ] api . Partition
2020-09-15 13:29:25 +00:00
err = dl . ForEachPartition ( func ( _ uint64 , part miner . Partition ) error {
2020-09-17 15:30:15 +00:00
allSectors , err := part . AllSectors ( )
if err != nil {
return xerrors . Errorf ( "getting AllSectors: %w" , err )
}
faultySectors , err := part . FaultySectors ( )
if err != nil {
return xerrors . Errorf ( "getting FaultySectors: %w" , err )
}
recoveringSectors , err := part . RecoveringSectors ( )
if err != nil {
return xerrors . Errorf ( "getting RecoveringSectors: %w" , err )
}
liveSectors , err := part . LiveSectors ( )
if err != nil {
return xerrors . Errorf ( "getting LiveSectors: %w" , err )
}
activeSectors , err := part . ActiveSectors ( )
if err != nil {
return xerrors . Errorf ( "getting ActiveSectors: %w" , err )
}
out = append ( out , api . Partition {
AllSectors : allSectors ,
FaultySectors : faultySectors ,
RecoveringSectors : recoveringSectors ,
LiveSectors : liveSectors ,
ActiveSectors : activeSectors ,
} )
2020-09-15 13:29:25 +00:00
return nil
} )
return out , err
2020-04-15 20:22:58 +00:00
}
2020-10-23 14:51:27 +00:00
func ( m * StateModule ) StateMinerProvingDeadline ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * dline . Info , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-04-24 17:12:30 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-23 14:51:27 +00:00
act , err := m . StateManager . LoadActor ( ctx , addr , ts )
2020-04-24 17:12:30 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor: %w" , err )
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( m . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-04-24 17:12:30 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return nil , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-04-24 17:12:30 +00:00
}
2020-09-23 05:19:43 +00:00
di , err := mas . DeadlineInfo ( ts . Height ( ) )
if err != nil {
return nil , xerrors . Errorf ( "failed to get deadline info: %w" , err )
}
return di . NextNotElapsed ( ) , nil
2020-04-24 17:12:30 +00:00
}
2020-09-07 03:49:10 +00:00
func ( a * StateAPI ) StateMinerFaults ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( bitfield . BitField , error ) {
2020-09-15 13:29:25 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , addr , tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return bitfield . BitField { } , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-02-11 23:29:45 +00:00
}
2020-07-14 11:45:45 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return bitfield . BitField { } , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-02-11 23:29:45 +00:00
}
2020-07-14 11:45:45 +00:00
2020-09-15 13:29:25 +00:00
return miner . AllPartSectors ( mas , miner . Partition . FaultySectors )
2020-01-30 00:50:58 +00:00
}
2020-05-21 20:28:59 +00:00
func ( a * StateAPI ) StateAllMinerFaults ( ctx context . Context , lookback abi . ChainEpoch , endTsk types . TipSetKey ) ( [ ] * api . Fault , error ) {
2020-07-14 11:45:45 +00:00
return nil , xerrors . Errorf ( "fixme" )
/ * endTs , err := a . Chain . GetTipSetFromKey ( endTsk )
2020-05-21 20:28:59 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading end tipset %s: %w" , endTsk , err )
}
cutoff := endTs . Height ( ) - lookback
miners , err := stmgr . ListMinerActors ( ctx , a . StateManager , endTs )
if err != nil {
return nil , xerrors . Errorf ( "loading miners: %w" , err )
}
var allFaults [ ] * api . Fault
for _ , m := range miners {
var mas miner . State
_ , err := a . StateManager . LoadActorState ( ctx , m , & mas , endTs )
if err != nil {
return nil , xerrors . Errorf ( "failed to load miner actor state %s: %w" , m , err )
}
2020-08-05 07:37:28 +00:00
err = mas . ForEachFaultEpoch ( a . Chain . Store ( ctx ) , func ( faultStart abi . ChainEpoch , faults abi . BitField ) error {
2020-05-21 20:28:59 +00:00
if faultStart >= cutoff {
allFaults = append ( allFaults , & api . Fault {
Miner : m ,
Epoch : faultStart ,
} )
return nil
}
return nil
} )
if err != nil {
return nil , xerrors . Errorf ( "failure when iterating over miner states: %w" , err )
}
}
2020-07-14 11:45:45 +00:00
return allFaults , nil * /
2020-05-21 20:28:59 +00:00
}
2020-09-07 03:49:10 +00:00
func ( a * StateAPI ) StateMinerRecoveries ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( bitfield . BitField , error ) {
2020-09-15 13:29:25 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , addr , tsk )
2020-05-16 21:50:50 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return bitfield . BitField { } , xerrors . Errorf ( "failed to load miner actor: %w" , err )
2020-05-16 21:50:50 +00:00
}
2020-07-14 11:45:45 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-05-16 21:50:50 +00:00
if err != nil {
2020-09-15 13:29:25 +00:00
return bitfield . BitField { } , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-05-16 21:50:50 +00:00
}
2020-07-14 11:45:45 +00:00
2020-09-15 13:29:25 +00:00
return miner . AllPartSectors ( mas , miner . Partition . RecoveringSectors )
2020-05-16 21:50:50 +00:00
}
2020-10-23 14:51:27 +00:00
func ( m * StateModule ) StateMinerPower ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * api . MinerPower , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-04-15 20:40:46 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-23 14:51:27 +00:00
mp , net , hmp , err := stmgr . GetPower ( ctx , m . StateManager , ts , addr )
2020-04-15 20:40:46 +00:00
if err != nil {
return nil , err
}
return & api . MinerPower {
2020-10-23 14:51:27 +00:00
MinerPower : mp ,
2020-09-17 07:32:10 +00:00
TotalPower : net ,
2020-09-16 05:47:24 +00:00
HasMinPower : hmp ,
2020-04-15 20:40:46 +00:00
} , nil
}
2020-10-07 23:03:42 +00:00
func ( a * StateAPI ) StateCall ( ctx context . Context , msg * types . Message , tsk types . TipSetKey ) ( res * api . InvocResult , err error ) {
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-07 23:03:42 +00:00
for {
res , err = a . StateManager . Call ( ctx , msg , ts )
2020-10-07 23:14:11 +00:00
if err != stmgr . ErrExpensiveFork {
2020-10-07 23:03:42 +00:00
break
}
ts , err = a . Chain . GetTipSetFromKey ( ts . Parents ( ) )
if err != nil {
return nil , xerrors . Errorf ( "getting parent tipset: %w" , err )
}
}
return res , err
2019-09-06 06:26:02 +00:00
}
2020-03-03 23:32:17 +00:00
func ( a * StateAPI ) StateReplay ( ctx context . Context , tsk types . TipSetKey , mc cid . Cid ) ( * api . InvocResult , error ) {
2020-10-16 02:45:11 +00:00
msgToReplay := mc
var ts * types . TipSet
2020-10-19 18:27:04 +00:00
var err error
2020-10-16 02:45:11 +00:00
if tsk == types . EmptyTSK {
2021-04-03 11:20:50 +00:00
mlkp , err := a . StateSearchMsg ( ctx , types . EmptyTSK , mc , stmgr . LookbackNoLimit , true )
2020-10-16 02:45:11 +00:00
if err != nil {
return nil , xerrors . Errorf ( "searching for msg %s: %w" , mc , err )
}
if mlkp == nil {
return nil , xerrors . Errorf ( "didn't find msg %s" , mc )
}
2019-09-19 20:25:18 +00:00
2020-10-16 02:45:11 +00:00
msgToReplay = mlkp . Message
2020-10-15 23:47:02 +00:00
2020-10-16 02:45:11 +00:00
executionTs , err := a . Chain . GetTipSetFromKey ( mlkp . TipSet )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , mlkp . TipSet , err )
}
2020-10-15 23:47:02 +00:00
2020-10-16 02:45:11 +00:00
ts , err = a . Chain . LoadTipSet ( executionTs . Parents ( ) )
if err != nil {
return nil , xerrors . Errorf ( "loading parent tipset %s: %w" , mlkp . TipSet , err )
}
} else {
2020-10-19 18:27:04 +00:00
ts , err = a . Chain . LoadTipSet ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading specified tipset %s: %w" , tsk , err )
}
2020-02-11 23:29:45 +00:00
}
2020-10-15 23:47:02 +00:00
2020-10-16 02:45:11 +00:00
m , r , err := a . StateManager . Replay ( ctx , ts , msgToReplay )
2019-09-19 20:25:18 +00:00
if err != nil {
return nil , err
}
var errstr string
if r . ActorErr != nil {
errstr = r . ActorErr . Error ( )
}
2020-03-03 23:32:17 +00:00
return & api . InvocResult {
2020-10-16 02:45:11 +00:00
MsgCid : msgToReplay ,
2020-06-11 00:47:28 +00:00
Msg : m ,
MsgRct : & r . MessageReceipt ,
2020-10-15 23:47:02 +00:00
GasCost : stmgr . MakeMsgGasCost ( m , r ) ,
2020-06-11 00:47:28 +00:00
ExecutionTrace : r . ExecutionTrace ,
Error : errstr ,
Duration : r . Duration ,
2019-09-19 20:25:18 +00:00
} , nil
}
2021-05-07 14:38:40 +00:00
func ( m * StateModule ) StateGetActor ( ctx context . Context , actor address . Address , tsk types . TipSetKey ) ( a * types . Actor , err error ) {
2020-10-01 15:51:27 +00:00
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-22 19:09:30 +00:00
return m . StateManager . LoadActor ( ctx , actor , ts )
2019-09-06 06:26:02 +00:00
}
2020-10-01 15:51:27 +00:00
func ( m * StateModule ) StateLookupID ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( address . Address , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
return address . Undef , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2019-11-15 16:39:43 +00:00
2020-10-01 15:51:27 +00:00
return m . StateManager . LookupID ( ctx , addr , ts )
2019-11-15 16:39:43 +00:00
}
2020-10-01 15:51:27 +00:00
func ( m * StateModule ) StateAccountKey ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( address . Address , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-04-16 20:38:42 +00:00
if err != nil {
return address . Undef , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-01 15:51:27 +00:00
return m . StateManager . ResolveToKeyAddress ( ctx , addr , ts )
2020-04-16 20:38:42 +00:00
}
2020-06-17 05:10:29 +00:00
func ( a * StateAPI ) StateReadState ( ctx context . Context , actor address . Address , tsk types . TipSetKey ) ( * api . ActorState , error ) {
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-22 19:09:30 +00:00
act , err := a . StateManager . LoadActor ( ctx , actor , ts )
2020-06-17 05:10:29 +00:00
if err != nil {
2020-08-07 14:07:34 +00:00
return nil , xerrors . Errorf ( "getting actor: %w" , err )
2020-06-17 05:10:29 +00:00
}
2021-02-28 22:48:36 +00:00
blk , err := a . Chain . StateBlockstore ( ) . Get ( act . Head )
2019-09-06 06:26:02 +00:00
if err != nil {
2020-08-07 14:07:34 +00:00
return nil , xerrors . Errorf ( "getting actor head: %w" , err )
2019-09-06 06:26:02 +00:00
}
2020-09-25 00:51:34 +00:00
oif , err := vm . DumpActorState ( act , blk . RawData ( ) )
2019-09-06 06:26:02 +00:00
if err != nil {
2020-08-07 14:07:34 +00:00
return nil , xerrors . Errorf ( "dumping actor state (a:%s): %w" , actor , err )
2019-09-06 06:26:02 +00:00
}
return & api . ActorState {
Balance : act . Balance ,
2021-02-09 22:18:19 +00:00
Code : act . Code ,
2019-09-06 06:26:02 +00:00
State : oif ,
} , nil
}
2020-09-29 07:01:52 +00:00
func ( a * StateAPI ) StateDecodeParams ( ctx context . Context , toAddr address . Address , method abi . MethodNum , params [ ] byte , tsk types . TipSetKey ) ( interface { } , error ) {
act , err := a . StateGetActor ( ctx , toAddr , tsk )
if err != nil {
return nil , xerrors . Errorf ( "getting actor: %w" , err )
}
paramType , err := stmgr . GetParamType ( act . Code , method )
if err != nil {
return nil , xerrors . Errorf ( "getting params type: %w" , err )
}
if err = paramType . UnmarshalCBOR ( bytes . NewReader ( params ) ) ; err != nil {
return nil , err
}
return paramType , nil
}
2020-04-02 01:10:28 +00:00
// This is on StateAPI because miner.Miner requires this, and MinerAPI requires miner.Miner
2020-04-17 23:36:54 +00:00
func ( a * StateAPI ) MinerGetBaseInfo ( ctx context . Context , maddr address . Address , epoch abi . ChainEpoch , tsk types . TipSetKey ) ( * api . MiningBaseInfo , error ) {
2020-10-22 19:09:30 +00:00
// XXX: Gets the state by computing the tipset state, instead of looking at the parent.
2020-04-30 22:11:14 +00:00
return stmgr . MinerGetBaseInfo ( ctx , a . StateManager , a . Beacon , tsk , epoch , maddr , a . ProofVerifier )
2020-04-02 01:10:28 +00:00
}
2020-04-09 00:24:10 +00:00
func ( a * StateAPI ) MinerCreateBlock ( ctx context . Context , bt * api . BlockTemplate ) ( * types . BlockMsg , error ) {
fblk , err := gen . MinerCreateBlock ( ctx , a . StateManager , a . Wallet , bt )
2019-09-06 06:26:02 +00:00
if err != nil {
return nil , err
}
2019-09-18 02:50:03 +00:00
var out types . BlockMsg
2019-09-06 06:26:02 +00:00
out . Header = fblk . Header
for _ , msg := range fblk . BlsMessages {
out . BlsMessages = append ( out . BlsMessages , msg . Cid ( ) )
}
for _ , msg := range fblk . SecpkMessages {
out . SecpkMessages = append ( out . SecpkMessages , msg . Cid ( ) )
}
return & out , nil
}
2019-10-08 05:51:34 +00:00
2021-04-03 10:55:29 +00:00
func ( m * StateModule ) StateWaitMsg ( ctx context . Context , msg cid . Cid , confidence uint64 , lookbackLimit abi . ChainEpoch , allowReplaced bool ) ( * api . MsgLookup , error ) {
ts , recpt , found , err := m . StateManager . WaitForMessage ( ctx , msg , confidence , lookbackLimit , allowReplaced )
2019-10-08 05:51:34 +00:00
if err != nil {
return nil , err
}
2020-07-03 16:57:58 +00:00
var returndec interface { }
if recpt . ExitCode == 0 && len ( recpt . Return ) > 0 {
2021-04-02 11:52:24 +00:00
cmsg , err := m . Chain . GetCMessage ( msg )
2020-07-03 16:57:58 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to load message after successful receipt search: %w" , err )
}
vmsg := cmsg . VMMessage ( )
2021-04-02 11:52:24 +00:00
t , err := stmgr . GetReturnType ( ctx , m . StateManager , vmsg . To , vmsg . Method , ts )
2020-07-03 16:57:58 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to get return type: %w" , err )
}
if err := t . UnmarshalCBOR ( bytes . NewReader ( recpt . Return ) ) ; err != nil {
return nil , err
}
returndec = t
}
2020-03-18 23:06:53 +00:00
return & api . MsgLookup {
2020-08-10 12:55:27 +00:00
Message : found ,
2020-07-03 16:57:58 +00:00
Receipt : * recpt ,
ReturnDec : returndec ,
2020-07-12 03:54:25 +00:00
TipSet : ts . Key ( ) ,
Height : ts . Height ( ) ,
2019-10-08 05:51:34 +00:00
} , nil
}
2019-10-12 06:45:48 +00:00
2021-04-02 11:52:24 +00:00
func ( m * StateModule ) StateSearchMsg ( ctx context . Context , tsk types . TipSetKey , msg cid . Cid , lookbackLimit abi . ChainEpoch , allowReplaced bool ) ( * api . MsgLookup , error ) {
fromTs , err := m . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
ts , recpt , found , err := m . StateManager . SearchForMessage ( ctx , fromTs , msg , lookbackLimit , allowReplaced )
2020-03-18 23:06:53 +00:00
if err != nil {
return nil , err
}
if ts != nil {
return & api . MsgLookup {
2020-08-10 12:55:27 +00:00
Message : found ,
2020-03-18 23:06:53 +00:00
Receipt : * recpt ,
2020-07-12 03:54:25 +00:00
TipSet : ts . Key ( ) ,
Height : ts . Height ( ) ,
2020-03-18 23:06:53 +00:00
} , nil
}
2020-08-20 04:49:10 +00:00
return nil , nil
2020-03-18 23:06:53 +00:00
}
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateListMiners ( ctx context . Context , tsk types . TipSetKey ) ( [ ] address . Address , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-09 11:41:09 +00:00
return stmgr . ListMinerActors ( ctx , m . StateManager , ts )
2019-10-12 06:45:48 +00:00
}
2020-02-11 23:29:45 +00:00
func ( a * StateAPI ) StateListActors ( ctx context . Context , tsk types . TipSetKey ) ( [ ] address . Address , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2019-10-12 06:45:48 +00:00
return a . StateManager . ListAllActors ( ctx , ts )
}
2019-10-22 10:09:36 +00:00
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateMarketBalance ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( api . MarketBalance , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
2020-02-24 17:32:02 +00:00
return api . MarketBalance { } , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
2020-02-11 23:29:45 +00:00
}
2020-10-09 11:41:09 +00:00
return m . StateManager . MarketBalance ( ctx , addr , ts )
2019-10-22 10:09:36 +00:00
}
2019-10-22 19:29:05 +00:00
2020-02-24 17:32:02 +00:00
func ( a * StateAPI ) StateMarketParticipants ( ctx context . Context , tsk types . TipSetKey ) ( map [ string ] api . MarketBalance , error ) {
2020-02-08 02:18:32 +00:00
out := map [ string ] api . MarketBalance { }
2019-10-22 19:29:05 +00:00
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-17 07:32:10 +00:00
state , err := a . StateManager . GetMarketState ( ctx , ts )
if err != nil {
2019-10-22 19:29:05 +00:00
return nil , err
}
2020-09-17 07:32:10 +00:00
escrow , err := state . EscrowTable ( )
2020-02-08 02:18:32 +00:00
if err != nil {
return nil , err
}
2020-09-17 07:32:10 +00:00
locked , err := state . LockedTable ( )
2019-10-22 19:29:05 +00:00
if err != nil {
return nil , err
}
2020-09-17 07:32:10 +00:00
err = escrow . ForEach ( func ( a address . Address , es abi . TokenAmount ) error {
2020-02-08 02:18:32 +00:00
2020-09-17 07:32:10 +00:00
lk , err := locked . Get ( a )
if err != nil {
2019-10-22 19:29:05 +00:00
return err
}
2020-02-08 02:18:32 +00:00
out [ a . String ( ) ] = api . MarketBalance {
Escrow : es ,
Locked : lk ,
}
2019-10-22 19:29:05 +00:00
return nil
} )
if err != nil {
return nil , err
}
return out , nil
}
2020-02-24 17:32:02 +00:00
func ( a * StateAPI ) StateMarketDeals ( ctx context . Context , tsk types . TipSetKey ) ( map [ string ] api . MarketDeal , error ) {
2020-02-09 06:06:32 +00:00
out := map [ string ] api . MarketDeal { }
2019-10-22 19:29:05 +00:00
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-17 07:32:10 +00:00
state , err := a . StateManager . GetMarketState ( ctx , ts )
if err != nil {
2019-10-22 19:29:05 +00:00
return nil , err
}
2020-09-17 07:32:10 +00:00
da , err := state . Proposals ( )
2019-10-22 19:29:05 +00:00
if err != nil {
return nil , err
}
2020-09-17 07:32:10 +00:00
sa , err := state . States ( )
2020-02-09 06:06:32 +00:00
if err != nil {
return nil , err
}
2020-09-17 07:32:10 +00:00
if err := da . ForEach ( func ( dealID abi . DealID , d market . DealProposal ) error {
s , found , err := sa . Get ( dealID )
if err != nil {
2020-07-23 00:14:54 +00:00
return xerrors . Errorf ( "failed to get state for deal in proposals array: %w" , err )
} else if ! found {
2020-09-17 07:32:10 +00:00
s = market . EmptyDealState ( )
2020-02-09 06:06:32 +00:00
}
2020-09-17 07:32:10 +00:00
out [ strconv . FormatInt ( int64 ( dealID ) , 10 ) ] = api . MarketDeal {
2020-02-09 06:06:32 +00:00
Proposal : d ,
2020-09-17 07:32:10 +00:00
State : * s ,
2020-02-09 06:06:32 +00:00
}
2019-10-22 19:29:05 +00:00
return nil
} ) ; err != nil {
return nil , err
}
return out , nil
}
2019-11-07 07:57:10 +00:00
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateMarketStorageDeal ( ctx context . Context , dealId abi . DealID , tsk types . TipSetKey ) ( * api . MarketDeal , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-09 11:41:09 +00:00
return stmgr . GetStorageDeal ( ctx , m . StateManager , dealId , ts )
2019-11-07 07:57:10 +00:00
}
2019-11-15 18:38:09 +00:00
func ( a * StateAPI ) StateChangedActors ( ctx context . Context , old cid . Cid , new cid . Cid ) ( map [ string ] types . Actor , error ) {
2021-02-28 22:48:36 +00:00
store := a . Chain . ActorStore ( ctx )
2019-11-15 18:38:09 +00:00
2020-09-21 19:50:12 +00:00
oldTree , err := state . LoadStateTree ( store , old )
2019-11-15 18:38:09 +00:00
if err != nil {
2020-09-21 19:50:12 +00:00
return nil , xerrors . Errorf ( "failed to load old state tree: %w" , err )
2019-11-15 18:38:09 +00:00
}
2020-09-21 19:50:12 +00:00
newTree , err := state . LoadStateTree ( store , new )
2019-11-15 18:38:09 +00:00
if err != nil {
2020-09-21 19:50:12 +00:00
return nil , xerrors . Errorf ( "failed to load new state tree: %w" , err )
2019-11-15 18:38:09 +00:00
}
2021-06-30 23:40:43 +00:00
return state . Diff ( ctx , oldTree , newTree )
2019-11-15 18:38:09 +00:00
}
2019-12-11 23:31:59 +00:00
2020-02-11 23:29:45 +00:00
func ( a * StateAPI ) StateMinerSectorCount ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( api . MinerSectors , error ) {
2020-09-15 19:09:39 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , addr , tsk )
2020-07-18 12:54:21 +00:00
if err != nil {
return api . MinerSectors { } , err
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . Chain . ActorStore ( ctx ) , act )
2020-09-15 19:09:39 +00:00
if err != nil {
return api . MinerSectors { } , err
}
var activeCount , liveCount , faultyCount uint64
if err := mas . ForEachDeadline ( func ( _ uint64 , dl miner . Deadline ) error {
return dl . ForEachPartition ( func ( _ uint64 , part miner . Partition ) error {
if active , err := part . ActiveSectors ( ) ; err != nil {
return err
} else if count , err := active . Count ( ) ; err != nil {
return err
} else {
activeCount += count
}
if live , err := part . LiveSectors ( ) ; err != nil {
return err
} else if count , err := live . Count ( ) ; err != nil {
return err
} else {
liveCount += count
}
if faulty , err := part . FaultySectors ( ) ; err != nil {
return err
} else if count , err := faulty . Count ( ) ; err != nil {
return err
} else {
faultyCount += count
}
return nil
} )
} ) ; err != nil {
return api . MinerSectors { } , err
}
return api . MinerSectors { Live : liveCount , Active : activeCount , Faulty : faultyCount } , nil
2019-12-11 23:31:59 +00:00
}
2020-01-07 19:03:11 +00:00
2020-04-04 02:55:19 +00:00
func ( a * StateAPI ) StateSectorPreCommitInfo ( ctx context . Context , maddr address . Address , n abi . SectorNumber , tsk types . TipSetKey ) ( miner . SectorPreCommitOnChainInfo , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return miner . SectorPreCommitOnChainInfo { } , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-17 08:17:14 +00:00
pci , err := stmgr . PreCommitInfo ( ctx , a . StateManager , maddr , n , ts )
if err != nil {
return miner . SectorPreCommitOnChainInfo { } , err
2020-09-29 01:37:47 +00:00
} else if pci == nil {
2020-11-23 11:14:49 +00:00
return miner . SectorPreCommitOnChainInfo { } , xerrors . Errorf ( "precommit info is not exists" )
2020-09-17 08:17:14 +00:00
}
return * pci , err
2020-04-04 02:55:19 +00:00
}
2020-11-20 16:30:17 +00:00
func ( m * StateModule ) StateSectorGetInfo ( ctx context . Context , maddr address . Address , n abi . SectorNumber , tsk types . TipSetKey ) ( * miner . SectorOnChainInfo , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-05-28 00:06:29 +00:00
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-11-20 16:30:17 +00:00
return stmgr . MinerSectorInfo ( ctx , m . StateManager , maddr , n , ts )
2020-05-28 00:06:29 +00:00
}
2020-09-15 19:09:39 +00:00
func ( a * StateAPI ) StateSectorExpiration ( ctx context . Context , maddr address . Address , sectorNumber abi . SectorNumber , tsk types . TipSetKey ) ( * miner . SectorExpiration , error ) {
2020-09-15 17:44:44 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , maddr , tsk )
2020-07-16 15:24:41 +00:00
if err != nil {
return nil , err
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-15 17:44:44 +00:00
if err != nil {
return nil , err
2020-07-16 15:24:41 +00:00
}
2020-09-15 17:44:44 +00:00
return mas . GetSectorExpiration ( sectorNumber )
2020-07-16 15:24:41 +00:00
}
2020-09-15 19:09:39 +00:00
func ( a * StateAPI ) StateSectorPartition ( ctx context . Context , maddr address . Address , sectorNumber abi . SectorNumber , tsk types . TipSetKey ) ( * miner . SectorLocation , error ) {
2020-09-15 17:44:44 +00:00
act , err := a . StateManager . LoadActorTsk ( ctx , maddr , tsk )
2020-07-14 12:32:17 +00:00
if err != nil {
return nil , err
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-15 17:44:44 +00:00
if err != nil {
return nil , err
}
return mas . FindSector ( sectorNumber )
2020-07-14 12:32:17 +00:00
}
2020-10-15 15:54:36 +00:00
func ( a * StateAPI ) StateListMessages ( ctx context . Context , match * api . MessageMatch , tsk types . TipSetKey , toheight abi . ChainEpoch ) ( [ ] cid . Cid , error ) {
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-01-07 19:03:11 +00:00
if ts == nil {
ts = a . Chain . GetHeaviestTipSet ( )
}
if match . To == address . Undef && match . From == address . Undef {
return nil , xerrors . Errorf ( "must specify at least To or From in message filter" )
2021-04-10 06:33:34 +00:00
} else if match . To != address . Undef {
_ , err := a . StateLookupID ( ctx , match . To , tsk )
// if the recipient doesn't exist at the start point, we're not gonna find any matches
if xerrors . Is ( err , types . ErrActorNotFound ) {
return nil , nil
}
if err != nil {
return nil , xerrors . Errorf ( "looking up match.To: %w" , err )
}
} else if match . From != address . Undef {
_ , err := a . StateLookupID ( ctx , match . From , tsk )
// if the sender doesn't exist at the start point, we're not gonna find any matches
if xerrors . Is ( err , types . ErrActorNotFound ) {
return nil , nil
}
if err != nil {
return nil , xerrors . Errorf ( "looking up match.From: %w" , err )
}
2020-01-07 19:03:11 +00:00
}
2021-04-10 06:33:34 +00:00
// TODO: This should probably match on both ID and robust address, no?
2020-01-07 19:03:11 +00:00
matchFunc := func ( msg * types . Message ) bool {
if match . From != address . Undef && match . From != msg . From {
return false
}
if match . To != address . Undef && match . To != msg . To {
return false
}
return true
}
var out [ ] cid . Cid
for ts . Height ( ) >= toheight {
msgs , err := a . Chain . MessagesForTipset ( ts )
if err != nil {
return nil , xerrors . Errorf ( "failed to get messages for tipset (%s): %w" , ts . Key ( ) , err )
}
for _ , msg := range msgs {
if matchFunc ( msg . VMMessage ( ) ) {
out = append ( out , msg . Cid ( ) )
}
}
if ts . Height ( ) == 0 {
break
}
next , err := a . Chain . LoadTipSet ( ts . Parents ( ) )
if err != nil {
return nil , xerrors . Errorf ( "loading next tipset: %w" , err )
}
ts = next
}
return out , nil
}
2020-01-17 02:33:43 +00:00
2020-03-08 02:31:36 +00:00
func ( a * StateAPI ) StateCompute ( ctx context . Context , height abi . ChainEpoch , msgs [ ] * types . Message , tsk types . TipSetKey ) ( * api . ComputeStateOutput , error ) {
2020-02-11 23:29:45 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
2020-03-08 02:31:36 +00:00
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
st , t , err := stmgr . ComputeState ( ctx , a . StateManager , height , msgs , ts )
if err != nil {
return nil , err
2020-02-11 23:29:45 +00:00
}
2020-03-08 02:31:36 +00:00
return & api . ComputeStateOutput {
Root : st ,
Trace : t ,
} , nil
2020-01-17 02:33:43 +00:00
}
2020-01-30 01:23:16 +00:00
2020-10-07 16:14:12 +00:00
func ( m * StateModule ) MsigGetAvailableBalance ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( types . BigInt , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-02-11 23:29:45 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
2020-01-30 01:23:16 +00:00
}
2020-10-07 16:14:12 +00:00
act , err := m . StateManager . LoadActor ( ctx , addr , ts )
2020-01-30 01:23:16 +00:00
if err != nil {
2020-09-15 21:44:03 +00:00
return types . EmptyInt , xerrors . Errorf ( "failed to load multisig actor: %w" , err )
2020-01-30 01:23:16 +00:00
}
2021-02-28 22:48:36 +00:00
msas , err := multisig . Load ( m . Chain . ActorStore ( ctx ) , act )
2020-01-30 01:23:16 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to load multisig actor state: %w" , err )
}
2020-09-15 21:44:03 +00:00
locked , err := msas . LockedBalance ( ts . Height ( ) )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to compute locked multisig balance: %w" , err )
2020-01-30 01:23:16 +00:00
}
2020-09-15 21:44:03 +00:00
return types . BigSub ( act . Balance , locked ) , nil
2020-01-30 01:23:16 +00:00
}
2020-04-23 19:39:34 +00:00
2020-10-08 19:19:39 +00:00
func ( a * StateAPI ) MsigGetVestingSchedule ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( api . MsigVesting , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
act , err := a . StateManager . LoadActor ( ctx , addr , ts )
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "failed to load multisig actor: %w" , err )
}
2021-02-28 22:48:36 +00:00
msas , err := multisig . Load ( a . Chain . ActorStore ( ctx ) , act )
2020-10-08 19:19:39 +00:00
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "failed to load multisig actor state: %w" , err )
}
ib , err := msas . InitialBalance ( )
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "failed to load multisig initial balance: %w" , err )
}
se , err := msas . StartEpoch ( )
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "failed to load multisig start epoch: %w" , err )
}
ud , err := msas . UnlockDuration ( )
if err != nil {
return api . EmptyVesting , xerrors . Errorf ( "failed to load multisig unlock duration: %w" , err )
}
return api . MsigVesting {
InitialBalance : ib ,
StartEpoch : se ,
UnlockDuration : ud ,
} , nil
}
2020-10-07 16:14:12 +00:00
func ( m * StateModule ) MsigGetVested ( ctx context . Context , addr address . Address , start types . TipSetKey , end types . TipSetKey ) ( types . BigInt , error ) {
startTs , err := m . Chain . GetTipSetFromKey ( start )
2020-09-06 00:29:26 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading start tipset %s: %w" , start , err )
}
2020-10-07 16:14:12 +00:00
endTs , err := m . Chain . GetTipSetFromKey ( end )
2020-09-06 00:29:26 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading end tipset %s: %w" , end , err )
}
if startTs . Height ( ) > endTs . Height ( ) {
return types . EmptyInt , xerrors . Errorf ( "start tipset %d is after end tipset %d" , startTs . Height ( ) , endTs . Height ( ) )
} else if startTs . Height ( ) == endTs . Height ( ) {
return big . Zero ( ) , nil
}
2020-01-30 01:23:16 +00:00
2020-10-07 16:14:12 +00:00
act , err := m . StateManager . LoadActor ( ctx , addr , endTs )
2020-09-06 00:29:26 +00:00
if err != nil {
2020-09-15 21:44:03 +00:00
return types . EmptyInt , xerrors . Errorf ( "failed to load multisig actor at end epoch: %w" , err )
2020-01-30 01:23:16 +00:00
}
2021-02-28 22:48:36 +00:00
msas , err := multisig . Load ( m . Chain . ActorStore ( ctx ) , act )
2020-09-15 21:44:03 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to load multisig actor state: %w" , err )
2020-01-30 01:23:16 +00:00
}
2020-09-15 21:44:03 +00:00
startLk , err := msas . LockedBalance ( startTs . Height ( ) )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to compute locked balance at start height: %w" , err )
2020-01-30 01:23:16 +00:00
}
2020-09-15 21:44:03 +00:00
endLk , err := msas . LockedBalance ( endTs . Height ( ) )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to compute locked balance at end height: %w" , err )
2020-09-06 00:29:26 +00:00
}
2020-09-15 21:44:03 +00:00
return types . BigSub ( startLk , endLk ) , nil
2020-01-30 01:23:16 +00:00
}
2020-04-23 19:39:34 +00:00
2021-02-04 04:46:10 +00:00
func ( m * StateModule ) MsigGetPending ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( [ ] * api . MsigTransaction , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return nil , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
act , err := m . StateManager . LoadActor ( ctx , addr , ts )
if err != nil {
return nil , xerrors . Errorf ( "failed to load multisig actor: %w" , err )
}
2021-02-28 22:48:36 +00:00
msas , err := multisig . Load ( m . Chain . ActorStore ( ctx ) , act )
2021-02-04 04:46:10 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to load multisig actor state: %w" , err )
}
var out = [ ] * api . MsigTransaction { }
if err := msas . ForEachPendingTxn ( func ( id int64 , txn multisig . Transaction ) error {
out = append ( out , & api . MsigTransaction {
ID : id ,
To : txn . To ,
Value : txn . Value ,
Method : txn . Method ,
Params : txn . Params ,
Approved : txn . Approved ,
} )
return nil
} ) ; err != nil {
return nil , err
}
return out , nil
}
2020-07-30 00:06:22 +00:00
var initialPledgeNum = types . NewInt ( 110 )
2020-05-15 14:53:35 +00:00
var initialPledgeDen = types . NewInt ( 100 )
2020-07-28 18:55:20 +00:00
func ( a * StateAPI ) StateMinerPreCommitDepositForPower ( ctx context . Context , maddr address . Address , pci miner . SectorPreCommitInfo , tsk types . TipSetKey ) ( types . BigInt , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-15 23:47:58 +00:00
state , err := a . StateManager . ParentState ( ts )
2020-07-28 18:55:20 +00:00
if err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading state %s: %w" , tsk , err )
2020-07-28 18:55:20 +00:00
}
2020-09-15 23:47:58 +00:00
ssize , err := pci . SealProof . SectorSize ( )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to get resolve size: %w" , err )
2020-07-28 18:55:20 +00:00
}
2021-02-28 22:48:36 +00:00
store := a . Chain . ActorStore ( ctx )
2020-07-28 18:55:20 +00:00
2020-09-15 23:47:58 +00:00
var sectorWeight abi . StoragePower
if act , err := state . GetActor ( market . Address ) ; err != nil {
2020-09-22 04:12:07 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading market actor %s: %w" , maddr , err )
2020-09-17 07:32:10 +00:00
} else if s , err := market . Load ( store , act ) ; err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading market actor state %s: %w" , maddr , err )
} else if w , vw , err := s . VerifyDealsForActivation ( maddr , pci . DealIDs , ts . Height ( ) , pci . Expiration ) ; err != nil {
2020-09-17 07:32:10 +00:00
return types . EmptyInt , xerrors . Errorf ( "verifying deals for activation: %w" , err )
2020-09-15 23:47:58 +00:00
} else {
// NB: not exactly accurate, but should always lead us to *over* estimate, not under
duration := pci . Expiration - ts . Height ( )
2020-09-30 20:30:24 +00:00
sectorWeight = builtin . QAPowerForWeight ( ssize , duration , w , vw )
2020-07-28 18:55:20 +00:00
}
2020-09-30 20:30:24 +00:00
var powerSmoothed builtin . FilterEstimate
2020-09-15 23:47:58 +00:00
if act , err := state . GetActor ( power . Address ) ; err != nil {
2020-09-22 04:12:07 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading power actor: %w" , err )
2020-09-17 07:32:10 +00:00
} else if s , err := power . Load ( store , act ) ; err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading power actor state: %w" , err )
2020-09-17 07:32:10 +00:00
} else if p , err := s . TotalPowerSmoothed ( ) ; err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "failed to determine total power: %w" , err )
} else {
powerSmoothed = p
2020-07-28 18:55:20 +00:00
}
2020-09-19 04:30:24 +00:00
rewardActor , err := state . GetActor ( reward . Address )
2020-07-28 18:55:20 +00:00
if err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading miner actor: %w" , err )
2020-07-28 18:55:20 +00:00
}
2020-09-19 04:30:24 +00:00
rewardState , err := reward . Load ( store , rewardActor )
if err != nil {
2020-09-15 23:47:58 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading reward actor state: %w" , err )
}
2020-07-28 18:55:20 +00:00
2020-09-19 04:30:24 +00:00
deposit , err := rewardState . PreCommitDepositForPower ( powerSmoothed , sectorWeight )
if err != nil {
return big . Zero ( ) , xerrors . Errorf ( "calculating precommit deposit: %w" , err )
}
2020-07-28 18:55:20 +00:00
return types . BigDiv ( types . BigMul ( deposit , initialPledgeNum ) , initialPledgeDen ) , nil
}
2020-06-26 15:59:34 +00:00
func ( a * StateAPI ) StateMinerInitialPledgeCollateral ( ctx context . Context , maddr address . Address , pci miner . SectorPreCommitInfo , tsk types . TipSetKey ) ( types . BigInt , error ) {
2020-09-16 01:05:33 +00:00
// TODO: this repeats a lot of the previous function. Fix that.
2020-04-23 19:39:34 +00:00
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-16 01:05:33 +00:00
state , err := a . StateManager . ParentState ( ts )
2020-07-14 11:45:45 +00:00
if err != nil {
2020-09-16 01:05:33 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading state %s: %w" , tsk , err )
2020-04-23 19:39:34 +00:00
}
2020-09-16 01:05:33 +00:00
ssize , err := pci . SealProof . SectorSize ( )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to get resolve size: %w" , err )
2020-07-24 22:21:34 +00:00
}
2021-02-28 22:48:36 +00:00
store := a . Chain . ActorStore ( ctx )
2020-04-23 19:39:34 +00:00
2020-09-16 01:05:33 +00:00
var sectorWeight abi . StoragePower
if act , err := state . GetActor ( market . Address ) ; err != nil {
2021-02-28 06:12:22 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading market actor: %w" , err )
2020-09-17 07:32:10 +00:00
} else if s , err := market . Load ( store , act ) ; err != nil {
2021-02-28 06:12:22 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading market actor state: %w" , err )
2020-09-16 01:05:33 +00:00
} else if w , vw , err := s . VerifyDealsForActivation ( maddr , pci . DealIDs , ts . Height ( ) , pci . Expiration ) ; err != nil {
2020-09-17 07:32:10 +00:00
return types . EmptyInt , xerrors . Errorf ( "verifying deals for activation: %w" , err )
2020-09-16 01:05:33 +00:00
} else {
// NB: not exactly accurate, but should always lead us to *over* estimate, not under
duration := pci . Expiration - ts . Height ( )
2020-09-30 20:30:24 +00:00
sectorWeight = builtin . QAPowerForWeight ( ssize , duration , w , vw )
2020-04-23 19:39:34 +00:00
}
2020-09-16 01:05:33 +00:00
var (
2020-09-30 20:30:24 +00:00
powerSmoothed builtin . FilterEstimate
2020-09-17 08:17:14 +00:00
pledgeCollateral abi . TokenAmount
2020-09-16 01:05:33 +00:00
)
if act , err := state . GetActor ( power . Address ) ; err != nil {
2021-02-28 06:12:22 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading power actor: %w" , err )
2020-09-17 07:32:10 +00:00
} else if s , err := power . Load ( store , act ) ; err != nil {
2020-09-16 01:05:33 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading power actor state: %w" , err )
2020-09-17 07:32:10 +00:00
} else if p , err := s . TotalPowerSmoothed ( ) ; err != nil {
2020-09-16 01:05:33 +00:00
return types . EmptyInt , xerrors . Errorf ( "failed to determine total power: %w" , err )
2020-09-17 07:32:10 +00:00
} else if c , err := s . TotalLocked ( ) ; err != nil {
2020-09-16 01:05:33 +00:00
return types . EmptyInt , xerrors . Errorf ( "failed to determine pledge collateral: %w" , err )
} else {
powerSmoothed = p
pledgeCollateral = c
2020-04-23 19:39:34 +00:00
}
2020-09-19 04:30:24 +00:00
rewardActor , err := state . GetActor ( reward . Address )
2020-06-26 13:49:39 +00:00
if err != nil {
2021-02-28 06:12:22 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading reward actor: %w" , err )
2020-06-26 13:49:39 +00:00
}
2020-04-23 19:39:34 +00:00
2020-09-19 04:30:24 +00:00
rewardState , err := reward . Load ( store , rewardActor )
if err != nil {
2020-09-16 01:05:33 +00:00
return types . EmptyInt , xerrors . Errorf ( "loading reward actor state: %w" , err )
}
2020-04-23 19:39:34 +00:00
2020-10-12 20:41:27 +00:00
circSupply , err := a . StateVMCirculatingSupplyInternal ( ctx , ts . Key ( ) )
2020-06-26 13:49:39 +00:00
if err != nil {
return big . Zero ( ) , xerrors . Errorf ( "getting circulating supply: %w" , err )
2020-04-23 19:39:34 +00:00
}
2020-09-19 04:30:24 +00:00
initialPledge , err := rewardState . InitialPledgeForPower (
2020-07-28 14:36:32 +00:00
sectorWeight ,
2020-09-16 01:05:33 +00:00
pledgeCollateral ,
2020-09-17 08:17:14 +00:00
& powerSmoothed ,
2020-08-14 20:44:33 +00:00
circSupply . FilCirculating ,
2020-07-28 18:55:20 +00:00
)
2020-09-19 04:30:24 +00:00
if err != nil {
return big . Zero ( ) , xerrors . Errorf ( "calculating initial pledge: %w" , err )
}
2020-06-26 13:49:39 +00:00
2020-05-15 14:53:35 +00:00
return types . BigDiv ( types . BigMul ( initialPledge , initialPledgeNum ) , initialPledgeDen ) , nil
2020-04-23 19:39:34 +00:00
}
func ( a * StateAPI ) StateMinerAvailableBalance ( ctx context . Context , maddr address . Address , tsk types . TipSetKey ) ( types . BigInt , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-09-17 08:17:14 +00:00
act , err := a . StateManager . LoadActor ( ctx , maddr , ts )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to load miner actor: %w" , err )
}
2020-04-23 19:39:34 +00:00
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-09-17 08:17:14 +00:00
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
2020-04-23 19:39:34 +00:00
}
2020-09-17 08:17:14 +00:00
vested , err := mas . VestedFunds ( ts . Height ( ) )
2020-04-23 19:39:34 +00:00
if err != nil {
return types . EmptyInt , err
}
2020-09-17 08:17:14 +00:00
abal , err := mas . AvailableBalance ( act . Balance )
2020-04-23 19:39:34 +00:00
if err != nil {
return types . EmptyInt , err
}
2020-09-17 08:17:14 +00:00
return types . BigAdd ( abal , vested ) , nil
2020-04-23 19:39:34 +00:00
}
2020-07-02 15:57:10 +00:00
2020-10-13 19:35:29 +00:00
func ( a * StateAPI ) StateMinerSectorAllocated ( ctx context . Context , maddr address . Address , s abi . SectorNumber , tsk types . TipSetKey ) ( bool , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return false , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
act , err := a . StateManager . LoadActor ( ctx , maddr , ts )
if err != nil {
return false , xerrors . Errorf ( "failed to load miner actor: %w" , err )
}
2021-02-28 22:48:36 +00:00
mas , err := miner . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-10-13 19:35:29 +00:00
if err != nil {
return false , xerrors . Errorf ( "failed to load miner actor state: %w" , err )
}
return mas . IsAllocated ( s )
}
2020-07-02 18:49:08 +00:00
// StateVerifiedClientStatus returns the data cap for the given address.
2020-10-01 07:14:59 +00:00
// Returns zero if there is no entry in the data cap table for the
2020-07-02 18:49:08 +00:00
// address.
2020-10-01 07:14:59 +00:00
func ( a * StateAPI ) StateVerifierStatus ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * abi . StoragePower , error ) {
act , err := a . StateGetActor ( ctx , verifreg . Address , tsk )
2020-07-02 15:57:10 +00:00
if err != nil {
2020-07-07 13:59:14 +00:00
return nil , err
2020-07-02 15:57:10 +00:00
}
2020-08-19 12:27:50 +00:00
aid , err := a . StateLookupID ( ctx , addr , tsk )
if err != nil {
log . Warnf ( "lookup failure %v" , err )
return nil , err
}
2021-02-28 22:48:36 +00:00
vrs , err := verifreg . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-10-01 07:14:59 +00:00
if err != nil {
return nil , xerrors . Errorf ( "failed to load verified registry state: %w" , err )
}
2020-07-02 15:57:10 +00:00
2020-10-01 07:14:59 +00:00
verified , dcap , err := vrs . VerifierDataCap ( aid )
if err != nil {
return nil , xerrors . Errorf ( "looking up verifier: %w" , err )
}
if ! verified {
return nil , nil
2020-07-02 15:57:10 +00:00
}
2020-10-01 07:14:59 +00:00
return & dcap , nil
}
2020-07-02 18:49:08 +00:00
// StateVerifiedClientStatus returns the data cap for the given address.
2020-09-17 07:48:39 +00:00
// Returns zero if there is no entry in the data cap table for the
2020-07-02 18:49:08 +00:00
// address.
2020-10-20 15:08:25 +00:00
func ( m * StateModule ) StateVerifiedClientStatus ( ctx context . Context , addr address . Address , tsk types . TipSetKey ) ( * abi . StoragePower , error ) {
act , err := m . StateGetActor ( ctx , verifreg . Address , tsk )
2020-07-02 15:57:10 +00:00
if err != nil {
2020-07-07 13:59:14 +00:00
return nil , err
2020-07-02 15:57:10 +00:00
}
2020-07-07 14:01:08 +00:00
2020-10-20 15:08:25 +00:00
aid , err := m . StateLookupID ( ctx , addr , tsk )
2020-08-19 12:27:50 +00:00
if err != nil {
log . Warnf ( "lookup failure %v" , err )
2020-07-07 13:59:14 +00:00
return nil , err
2020-08-19 12:27:50 +00:00
}
2021-02-28 22:48:36 +00:00
vrs , err := verifreg . Load ( m . StateManager . ChainStore ( ) . ActorStore ( ctx ) , act )
2020-07-02 15:57:10 +00:00
if err != nil {
2020-09-17 15:30:15 +00:00
return nil , xerrors . Errorf ( "failed to load verified registry state: %w" , err )
2020-07-02 15:57:10 +00:00
}
2020-07-07 14:01:08 +00:00
2020-09-17 15:30:15 +00:00
verified , dcap , err := vrs . VerifiedClientDataCap ( aid )
2020-09-17 07:48:39 +00:00
if err != nil {
2020-09-17 15:30:15 +00:00
return nil , xerrors . Errorf ( "looking up verified client: %w" , err )
}
if ! verified {
2020-07-23 00:14:54 +00:00
return nil , nil
2020-07-02 15:57:10 +00:00
}
2020-07-07 13:59:14 +00:00
return & dcap , nil
2020-07-06 20:47:39 +00:00
}
2020-07-30 04:55:37 +00:00
2020-10-01 07:14:59 +00:00
func ( a * StateAPI ) StateVerifiedRegistryRootKey ( ctx context . Context , tsk types . TipSetKey ) ( address . Address , error ) {
vact , err := a . StateGetActor ( ctx , verifreg . Address , tsk )
if err != nil {
return address . Undef , err
}
2021-02-28 22:48:36 +00:00
vst , err := verifreg . Load ( a . StateManager . ChainStore ( ) . ActorStore ( ctx ) , vact )
2020-10-01 07:14:59 +00:00
if err != nil {
return address . Undef , err
}
return vst . RootKey ( )
}
2020-07-30 04:55:37 +00:00
var dealProviderCollateralNum = types . NewInt ( 110 )
var dealProviderCollateralDen = types . NewInt ( 100 )
// StateDealProviderCollateralBounds returns the min and max collateral a storage provider
// can issue. It takes the deal size and verified status as parameters.
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateDealProviderCollateralBounds ( ctx context . Context , size abi . PaddedPieceSize , verified bool , tsk types . TipSetKey ) ( api . DealCollateralBounds , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-07-30 04:55:37 +00:00
if err != nil {
2020-07-30 12:31:31 +00:00
return api . DealCollateralBounds { } , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
2020-07-30 04:55:37 +00:00
}
2020-10-09 11:41:09 +00:00
pact , err := m . StateGetActor ( ctx , power . Address , tsk )
2020-09-17 08:17:14 +00:00
if err != nil {
return api . DealCollateralBounds { } , xerrors . Errorf ( "failed to load power actor: %w" , err )
}
2020-07-30 04:55:37 +00:00
2020-10-09 11:41:09 +00:00
ract , err := m . StateGetActor ( ctx , reward . Address , tsk )
2020-09-17 08:17:14 +00:00
if err != nil {
return api . DealCollateralBounds { } , xerrors . Errorf ( "failed to load reward actor: %w" , err )
}
2020-07-30 04:55:37 +00:00
2021-02-28 22:48:36 +00:00
pst , err := power . Load ( m . StateManager . ChainStore ( ) . ActorStore ( ctx ) , pact )
2020-09-17 08:17:14 +00:00
if err != nil {
return api . DealCollateralBounds { } , xerrors . Errorf ( "failed to load power actor state: %w" , err )
}
2020-07-30 04:55:37 +00:00
2021-02-28 22:48:36 +00:00
rst , err := reward . Load ( m . StateManager . ChainStore ( ) . ActorStore ( ctx ) , ract )
2020-07-30 04:55:37 +00:00
if err != nil {
2020-09-17 08:17:14 +00:00
return api . DealCollateralBounds { } , xerrors . Errorf ( "failed to load reward actor state: %w" , err )
2020-07-30 04:55:37 +00:00
}
2020-10-09 11:41:09 +00:00
circ , err := stateVMCirculatingSupplyInternal ( ctx , ts . Key ( ) , m . Chain , m . StateManager )
2020-07-30 04:55:37 +00:00
if err != nil {
2020-08-20 04:49:10 +00:00
return api . DealCollateralBounds { } , xerrors . Errorf ( "getting total circulating supply: %w" , err )
2020-07-30 04:55:37 +00:00
}
2020-09-17 08:17:14 +00:00
powClaim , err := pst . TotalPower ( )
if err != nil {
return api . DealCollateralBounds { } , xerrors . Errorf ( "getting total power: %w" , err )
}
rewPow , err := rst . ThisEpochBaselinePower ( )
if err != nil {
return api . DealCollateralBounds { } , xerrors . Errorf ( "getting reward baseline power: %w" , err )
}
2020-09-30 20:30:24 +00:00
min , max := policy . DealProviderCollateralBounds ( size ,
2020-09-07 20:01:09 +00:00
verified ,
2020-09-17 08:17:14 +00:00
powClaim . RawBytePower ,
powClaim . QualityAdjPower ,
rewPow ,
2020-09-07 20:01:09 +00:00
circ . FilCirculating ,
2020-10-09 11:41:09 +00:00
m . StateManager . GetNtwkVersion ( ctx , ts . Height ( ) ) )
2020-07-30 12:31:31 +00:00
return api . DealCollateralBounds {
Min : types . BigDiv ( types . BigMul ( min , dealProviderCollateralNum ) , dealProviderCollateralDen ) ,
Max : max ,
} , nil
2020-07-30 04:55:37 +00:00
}
2020-08-07 19:57:03 +00:00
2020-10-11 22:17:28 +00:00
func ( a * StateAPI ) StateCirculatingSupply ( ctx context . Context , tsk types . TipSetKey ) ( abi . TokenAmount , error ) {
ts , err := a . Chain . GetTipSetFromKey ( tsk )
if err != nil {
return types . EmptyInt , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
}
2020-10-22 19:09:30 +00:00
sTree , err := a . StateManager . ParentState ( ts )
2020-10-11 22:17:28 +00:00
if err != nil {
return types . EmptyInt , err
}
2020-10-22 19:09:30 +00:00
return a . StateManager . GetCirculatingSupply ( ctx , ts . Height ( ) - 1 , sTree )
2020-10-11 22:17:28 +00:00
}
2020-10-12 20:41:27 +00:00
func ( a * StateAPI ) StateVMCirculatingSupplyInternal ( ctx context . Context , tsk types . TipSetKey ) ( api . CirculatingSupply , error ) {
2020-10-09 11:41:09 +00:00
return stateVMCirculatingSupplyInternal ( ctx , tsk , a . Chain , a . StateManager )
}
func stateVMCirculatingSupplyInternal (
ctx context . Context ,
tsk types . TipSetKey ,
cstore * store . ChainStore ,
smgr * stmgr . StateManager ,
) ( api . CirculatingSupply , error ) {
ts , err := cstore . GetTipSetFromKey ( tsk )
2020-08-07 19:57:03 +00:00
if err != nil {
2020-08-14 20:44:33 +00:00
return api . CirculatingSupply { } , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
2020-08-07 19:57:03 +00:00
}
2020-10-22 19:09:30 +00:00
sTree , err := smgr . ParentState ( ts )
2020-08-09 22:49:38 +00:00
if err != nil {
2020-08-14 20:44:33 +00:00
return api . CirculatingSupply { } , err
2020-08-09 22:49:38 +00:00
}
2020-10-09 11:41:09 +00:00
return smgr . GetVMCirculatingSupplyDetailed ( ctx , ts . Height ( ) , sTree )
2020-08-07 19:57:03 +00:00
}
2020-08-09 22:49:38 +00:00
2020-10-09 11:41:09 +00:00
func ( m * StateModule ) StateNetworkVersion ( ctx context . Context , tsk types . TipSetKey ) ( network . Version , error ) {
ts , err := m . Chain . GetTipSetFromKey ( tsk )
2020-08-20 04:49:10 +00:00
if err != nil {
2020-09-17 08:17:14 +00:00
return network . VersionMax , xerrors . Errorf ( "loading tipset %s: %w" , tsk , err )
2020-08-20 04:49:10 +00:00
}
2020-08-09 22:49:38 +00:00
2020-10-22 19:09:30 +00:00
// TODO: Height-1 to be consistent with the rest of the APIs?
// But that's likely going to break a bunch of stuff.
2020-10-09 11:41:09 +00:00
return m . StateManager . GetNtwkVersion ( ctx , ts . Height ( ) ) , nil
2020-09-17 01:56:02 +00:00
}