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"
|
2020-01-30 01:23:16 +00:00
|
|
|
"fmt"
|
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
|
|
|
"github.com/ipfs/go-hamt-ipld"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-09-16 20:11:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-02-25 20:54:58 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2019-10-22 19:29:05 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
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-02-25 20:54:58 +00:00
|
|
|
"github.com/filecoin-project/go-amt-ipld/v2"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
|
|
|
samsig "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
|
2020-03-06 05:32:52 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/reward"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2020-02-08 02:18:32 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"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"
|
|
|
|
"github.com/filecoin-project/lotus/lib/bufbstore"
|
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
|
|
|
|
Wallet *wallet.Wallet
|
|
|
|
|
|
|
|
StateManager *stmgr.StateManager
|
|
|
|
Chain *store.ChainStore
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateMinerSectors(ctx context.Context, addr address.Address, tsk types.TipSetKey) ([]*api.ChainSectorInfo, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-27 10:27:21 +00:00
|
|
|
return stmgr.GetMinerSectorSet(ctx, a.StateManager, ts, addr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateMinerProvingSet(ctx context.Context, addr address.Address, tsk types.TipSetKey) ([]*api.ChainSectorInfo, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-09-18 02:50:03 +00:00
|
|
|
return stmgr.GetMinerProvingSet(ctx, a.StateManager, ts, addr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
2019-08-21 16:31:14 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, tsk types.TipSetKey) (api.MinerPower, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return api.MinerPower{}, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-09-09 20:03:10 +00:00
|
|
|
mpow, tpow, err := stmgr.GetPower(ctx, a.StateManager, ts, maddr)
|
2019-08-21 16:31:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MinerPower{}, err
|
|
|
|
}
|
|
|
|
|
2019-11-15 01:26:25 +00:00
|
|
|
if maddr != address.Undef {
|
|
|
|
slashed, err := stmgr.GetMinerSlashed(ctx, a.StateManager, ts, maddr)
|
|
|
|
if err != nil {
|
|
|
|
return api.MinerPower{}, err
|
|
|
|
}
|
2020-03-02 01:09:38 +00:00
|
|
|
if slashed {
|
2019-11-15 01:26:25 +00:00
|
|
|
mpow = types.NewInt(0)
|
|
|
|
}
|
2019-11-15 01:01:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:31:14 +00:00
|
|
|
return api.MinerPower{
|
|
|
|
MinerPower: mpow,
|
|
|
|
TotalPower: tpow,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateMinerWorker(ctx context.Context, m address.Address, tsk types.TipSetKey) (address.Address, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-23 17:39:14 +00:00
|
|
|
return stmgr.GetMinerWorker(ctx, a.StateManager, ts, m)
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateMinerPeerID(ctx context.Context, m address.Address, tsk types.TipSetKey) (peer.ID, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return "", xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-09-17 08:00:38 +00:00
|
|
|
return stmgr.GetMinerPeerID(ctx, a.StateManager, ts, m)
|
2019-09-16 20:11:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateMinerPostState(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*miner.PoStState, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
2020-02-24 17:32:02 +00:00
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
2020-02-11 23:29:45 +00:00
|
|
|
}
|
2020-02-11 02:33:27 +00:00
|
|
|
return stmgr.GetMinerPostState(ctx, a.StateManager, ts, actor)
|
2019-09-18 00:08:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateMinerSectorSize(ctx context.Context, actor address.Address, tsk types.TipSetKey) (abi.SectorSize, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-16 07:07:16 +00:00
|
|
|
return stmgr.GetMinerSectorSize(ctx, a.StateManager, ts, actor)
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateMinerFaults(ctx context.Context, addr address.Address, tsk types.TipSetKey) ([]abi.SectorNumber, 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-30 00:50:58 +00:00
|
|
|
return stmgr.GetMinerFaults(ctx, a.StateManager, ts, addr)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StatePledgeCollateral(ctx context.Context, tsk types.TipSetKey) (types.BigInt, error) {
|
2020-02-24 17:32:02 +00:00
|
|
|
/*ts, err := a.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)
|
|
|
|
}
|
|
|
|
|
2019-09-25 23:32:33 +00:00
|
|
|
param, err := actors.SerializeParams(&actors.PledgeCollateralParams{Size: types.NewInt(0)})
|
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, aerr := a.StateManager.Call(ctx, &types.Message{
|
2019-10-19 08:06:41 +00:00
|
|
|
From: actors.StoragePowerAddress,
|
|
|
|
To: actors.StoragePowerAddress,
|
2019-10-11 22:59:36 +00:00
|
|
|
Method: actors.SPAMethods.PledgeCollateralForSize,
|
2019-09-25 23:32:33 +00:00
|
|
|
|
|
|
|
Params: param,
|
|
|
|
}, ts)
|
|
|
|
if aerr != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("failed to get miner worker addr: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:32:17 +00:00
|
|
|
if ret.MsgRct.ExitCode != 0 {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("failed to get miner worker addr (exit code %d)", ret.MsgRct.ExitCode)
|
2019-09-25 23:32:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
return types.BigFromBytes(ret.Return), nil*/
|
2020-02-24 17:32:02 +00:00
|
|
|
log.Error("TODO StatePledgeCollateral")
|
2020-02-12 23:52:36 +00:00
|
|
|
return big.Zero(), nil
|
2019-09-25 23:32:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 23:32:17 +00:00
|
|
|
func (a *StateAPI) StateCall(ctx context.Context, msg *types.Message, tsk types.TipSetKey) (*api.InvocResult, 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)
|
|
|
|
}
|
2019-09-09 20:03:10 +00:00
|
|
|
return a.StateManager.Call(ctx, msg, ts)
|
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-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)
|
|
|
|
}
|
2019-09-19 20:25:18 +00:00
|
|
|
m, r, err := a.StateManager.Replay(ctx, ts, mc)
|
|
|
|
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-03-08 00:46:12 +00:00
|
|
|
Msg: m,
|
|
|
|
MsgRct: &r.MessageReceipt,
|
2020-03-04 01:33:55 +00:00
|
|
|
InternalExecutions: r.InternalExecutions,
|
2020-03-08 00:46:12 +00:00
|
|
|
Error: errstr,
|
2019-09-19 20:25:18 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:13:26 +00:00
|
|
|
func (a *StateAPI) stateForTs(ctx context.Context, ts *types.TipSet) (*state.StateTree, error) {
|
2019-09-06 06:26:02 +00:00
|
|
|
if ts == nil {
|
|
|
|
ts = a.Chain.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:13:26 +00:00
|
|
|
st, _, err := a.StateManager.TipSetState(ctx, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bufbstore.NewBufferedBstore(a.Chain.Blockstore())
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(buf)
|
2019-09-06 06:26:02 +00:00
|
|
|
return state.LoadStateTree(cst, st)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-10 11:13:26 +00:00
|
|
|
state, err := a.stateForTs(ctx, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-12-07 16:24:42 +00:00
|
|
|
return nil, xerrors.Errorf("computing tipset state failed: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state.GetActor(actor)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateLookupID(ctx context.Context, addr address.Address, tsk types.TipSetKey) (address.Address, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
state, err := a.stateForTs(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.LookupID(addr)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateReadState(ctx context.Context, act *types.Actor, tsk types.TipSetKey) (*api.ActorState, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-10 11:13:26 +00:00
|
|
|
state, err := a.stateForTs(ctx, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
blk, err := state.Store.(*cbor.BasicIpldStore).Blocks.Get(act.Head)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oif, err := vm.DumpActorState(act.Code, blk.RawData())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ActorState{
|
|
|
|
Balance: act.Balance,
|
|
|
|
State: oif,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is on StateAPI because miner.Miner requires this, and MinerAPI requires miner.Miner
|
2020-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parentsTSK types.TipSetKey, ticket *types.Ticket, proof *types.EPostProof, msgs []*types.SignedMessage, height abi.ChainEpoch, ts uint64) (*types.BlockMsg, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
parents, err := a.Chain.GetTipSetFromKey(parentsTSK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", parentsTSK, err)
|
|
|
|
}
|
2019-11-19 15:53:00 +00:00
|
|
|
fblk, err := gen.MinerCreateBlock(ctx, a.StateManager, a.Wallet, addr, parents, ticket, proof, msgs, height, ts)
|
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
|
|
|
|
|
|
|
func (a *StateAPI) StateWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWait, error) {
|
|
|
|
// TODO: consider using event system for this, expose confidence
|
|
|
|
|
|
|
|
ts, recpt, err := a.StateManager.WaitForMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.MsgWait{
|
|
|
|
Receipt: *recpt,
|
|
|
|
TipSet: ts,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-12 06:45:48 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateGetReceipt(ctx context.Context, msg cid.Cid, tsk types.TipSetKey) (*types.MessageReceipt, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-11-19 21:27:25 +00:00
|
|
|
return a.StateManager.GetReceipt(ctx, msg, ts)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) StateListMiners(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-12-17 06:51:41 +00:00
|
|
|
return stmgr.ListMinerActors(ctx, a.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-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateMarketBalance(ctx context.Context, addr address.Address, tsk types.TipSetKey) (api.MarketBalance, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
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
|
|
|
}
|
2019-10-23 17:39:14 +00:00
|
|
|
return a.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-25 20:35:15 +00:00
|
|
|
var state market.State
|
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-02-25 20:54:58 +00:00
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, builtin.StorageMarketActorAddr, &state, ts); err != nil {
|
2019-10-22 19:29:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(a.StateManager.ChainStore().Blockstore())
|
2020-02-14 14:14:39 +00:00
|
|
|
escrow, err := hamt.LoadNode(ctx, cst, state.EscrowTable, hamt.UseTreeBitWidth(5)) // todo: adt map
|
2020-02-08 02:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-14 14:14:39 +00:00
|
|
|
locked, err := hamt.LoadNode(ctx, cst, state.EscrowTable, hamt.UseTreeBitWidth(5))
|
2019-10-22 19:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
err = escrow.ForEach(ctx, func(k string, val interface{}) error {
|
2019-10-22 19:29:05 +00:00
|
|
|
cv := val.(*cbg.Deferred)
|
|
|
|
a, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-08 02:18:32 +00:00
|
|
|
|
|
|
|
var es abi.TokenAmount
|
|
|
|
if err := es.UnmarshalCBOR(bytes.NewReader(cv.Raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var lk abi.TokenAmount
|
|
|
|
if err := locked.Find(ctx, k, &es); 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-25 20:35:15 +00:00
|
|
|
var state market.State
|
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-02-25 20:54:58 +00:00
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, builtin.StorageMarketActorAddr, &state, ts); err != nil {
|
2019-10-22 19:29:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:42 +00:00
|
|
|
blks := cbor.NewCborStore(a.StateManager.ChainStore().Blockstore())
|
2020-02-08 02:18:32 +00:00
|
|
|
da, err := amt.LoadAMT(ctx, blks, state.Proposals)
|
2019-10-22 19:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
sa, err := amt.LoadAMT(ctx, blks, state.States)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:42 +00:00
|
|
|
if err := da.ForEach(ctx, func(i uint64, v *cbg.Deferred) error {
|
2020-02-08 02:18:32 +00:00
|
|
|
var d market.DealProposal
|
2019-10-22 19:29:05 +00:00
|
|
|
if err := d.UnmarshalCBOR(bytes.NewReader(v.Raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-09 06:06:32 +00:00
|
|
|
|
|
|
|
var s market.DealState
|
|
|
|
if err := sa.Get(ctx, i, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out[strconv.FormatInt(int64(i), 10)] = api.MarketDeal{
|
|
|
|
Proposal: d,
|
|
|
|
State: s,
|
|
|
|
}
|
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-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateMarketStorageDeal(ctx context.Context, dealId abi.DealID, tsk types.TipSetKey) (*api.MarketDeal, 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)
|
|
|
|
}
|
2019-11-07 09:06:06 +00:00
|
|
|
return stmgr.GetStorageDeal(ctx, a.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) {
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(a.Chain.Blockstore())
|
2019-11-15 18:38:09 +00:00
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
nh, err := hamt.LoadNode(ctx, cst, new, hamt.UseTreeBitWidth(5))
|
2019-11-15 18:38:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
oh, err := hamt.LoadNode(ctx, cst, old, hamt.UseTreeBitWidth(5))
|
2019-11-15 18:38:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := map[string]types.Actor{}
|
|
|
|
|
|
|
|
err = nh.ForEach(ctx, func(k string, nval interface{}) error {
|
|
|
|
ncval := nval.(*cbg.Deferred)
|
|
|
|
var act types.Actor
|
|
|
|
|
|
|
|
var ocval cbg.Deferred
|
|
|
|
|
|
|
|
switch err := oh.Find(ctx, k, &ocval); err {
|
|
|
|
case nil:
|
|
|
|
if bytes.Equal(ocval.Raw, ncval.Raw) {
|
|
|
|
return nil // not changed
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case hamt.ErrNotFound:
|
|
|
|
if err := act.UnmarshalCBOR(bytes.NewReader(ncval.Raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("address in state tree was not valid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out[addr.String()] = act
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
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) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return api.MinerSectors{}, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-12-11 23:31:59 +00:00
|
|
|
return stmgr.SectorSetSizes(ctx, a.StateManager, addr, ts)
|
|
|
|
}
|
2020-01-07 19:03:11 +00:00
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
func (a *StateAPI) StateListMessages(ctx context.Context, match *types.Message, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-11 23:29:45 +00:00
|
|
|
func (a *StateAPI) MsigGetAvailableBalance(ctx context.Context, addr 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-01-30 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
var st samsig.State
|
2020-01-30 01:23:16 +00:00
|
|
|
act, err := a.StateManager.LoadActorState(ctx, addr, &st, ts)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, xerrors.Errorf("failed to load multisig actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:54:58 +00:00
|
|
|
if act.Code != builtin.MultisigActorCodeID {
|
2020-01-30 01:23:16 +00:00
|
|
|
return types.EmptyInt, fmt.Errorf("given actor was not a multisig")
|
|
|
|
}
|
|
|
|
|
|
|
|
if st.UnlockDuration == 0 {
|
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
offset := ts.Height() - st.StartEpoch
|
|
|
|
if offset > st.UnlockDuration {
|
2020-01-30 01:23:16 +00:00
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 03:37:55 +00:00
|
|
|
minBalance := types.BigDiv(types.BigInt(st.InitialBalance), types.NewInt(uint64(st.UnlockDuration)))
|
2020-02-08 02:18:32 +00:00
|
|
|
minBalance = types.BigMul(minBalance, types.NewInt(uint64(offset)))
|
2020-01-30 01:23:16 +00:00
|
|
|
return types.BigSub(act.Balance, minBalance), nil
|
|
|
|
}
|
2020-03-06 05:32:52 +00:00
|
|
|
|
|
|
|
func (a *StateAPI) StateListRewards(ctx context.Context, miner address.Address, tsk types.TipSetKey) ([]reward.Reward, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var st reward.State
|
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, builtin.RewardActorAddr, &st, ts); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load reward actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
as := store.ActorStore(ctx, a.Chain.Blockstore())
|
|
|
|
rmap := adt.AsMultimap(as, st.RewardMap)
|
|
|
|
rewards, found, err := rmap.Get(adt.AddrKey(miner))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get rewards set for miner: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return nil, xerrors.Errorf("no rewards found for miner")
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []reward.Reward
|
|
|
|
|
|
|
|
var r reward.Reward
|
|
|
|
err = rewards.ForEach(&r, func(i int64) error {
|
|
|
|
or := r
|
|
|
|
out = append(out, or)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("rewards.ForEach failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|