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-11-07 07:57:10 +00:00
|
|
|
"github.com/filecoin-project/go-amt-ipld"
|
|
|
|
|
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"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
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-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2019-10-27 10:27:21 +00:00
|
|
|
func (a *StateAPI) StateMinerSectors(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*api.SectorInfo, error) {
|
|
|
|
return stmgr.GetMinerSectorSet(ctx, a.StateManager, ts, addr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 02:50:03 +00:00
|
|
|
func (a *StateAPI) StateMinerProvingSet(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*api.SectorInfo, error) {
|
|
|
|
return stmgr.GetMinerProvingSet(ctx, a.StateManager, ts, addr)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
2019-08-21 16:31:14 +00:00
|
|
|
|
|
|
|
func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, ts *types.TipSet) (api.MinerPower, error) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.MinerPower{
|
|
|
|
MinerPower: mpow,
|
|
|
|
TotalPower: tpow,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
|
|
|
func (a *StateAPI) StateMinerWorker(ctx context.Context, m address.Address, ts *types.TipSet) (address.Address, error) {
|
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
|
|
|
|
2019-09-16 20:11:17 +00:00
|
|
|
func (a *StateAPI) StateMinerPeerID(ctx context.Context, m address.Address, ts *types.TipSet) (peer.ID, error) {
|
2019-09-17 08:00:38 +00:00
|
|
|
return stmgr.GetMinerPeerID(ctx, a.StateManager, ts, m)
|
2019-09-16 20:11:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:08:49 +00:00
|
|
|
func (a *StateAPI) StateMinerProvingPeriodEnd(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) {
|
|
|
|
return stmgr.GetMinerProvingPeriodEnd(ctx, a.StateManager, ts, actor)
|
|
|
|
}
|
|
|
|
|
2019-10-16 07:07:16 +00:00
|
|
|
func (a *StateAPI) StateMinerSectorSize(ctx context.Context, actor address.Address, ts *types.TipSet) (uint64, error) {
|
|
|
|
return stmgr.GetMinerSectorSize(ctx, a.StateManager, ts, actor)
|
|
|
|
}
|
|
|
|
|
2019-09-25 23:32:33 +00:00
|
|
|
func (a *StateAPI) StatePledgeCollateral(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("failed to get miner worker addr (exit code %d)", ret.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.BigFromBytes(ret.Return), nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (a *StateAPI) StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) {
|
2019-09-09 20:03:10 +00:00
|
|
|
return a.StateManager.Call(ctx, msg, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
func (a *StateAPI) StateReplay(ctx context.Context, ts *types.TipSet, mc cid.Cid) (*api.ReplayResults, error) {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ReplayResults{
|
|
|
|
Msg: m,
|
|
|
|
Receipt: &r.MessageReceipt,
|
|
|
|
Error: errstr,
|
|
|
|
}, 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())
|
|
|
|
cst := hamt.CSTFromBstore(buf)
|
|
|
|
return state.LoadStateTree(cst, st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *StateAPI) StateGetActor(ctx context.Context, actor address.Address, ts *types.TipSet) (*types.Actor, error) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.GetActor(actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *StateAPI) StateReadState(ctx context.Context, act *types.Actor, ts *types.TipSet) (*api.ActorState, error) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := state.Store.Blocks.GetBlock(ctx, act.Head)
|
|
|
|
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
|
2019-09-18 02:50:03 +00:00
|
|
|
func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *types.TipSet, tickets []*types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage, ts uint64) (*types.BlockMsg, error) {
|
2019-09-06 06:26:02 +00:00
|
|
|
fblk, err := gen.MinerCreateBlock(ctx, a.StateManager, a.Wallet, addr, parents, tickets, proof, msgs, ts)
|
|
|
|
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
|
|
|
|
|
|
|
func (a *StateAPI) StateListMiners(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
|
2019-10-13 22:51:40 +00:00
|
|
|
var state actors.StoragePowerState
|
2019-10-19 08:06:41 +00:00
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, actors.StoragePowerAddress, &state, ts); err != nil {
|
2019-10-12 06:45:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cst := hamt.CSTFromBstore(a.StateManager.ChainStore().Blockstore())
|
|
|
|
miners, err := actors.MinerSetList(ctx, cst, state.Miners)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return miners, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *StateAPI) StateListActors(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
|
|
|
|
return a.StateManager.ListAllActors(ctx, ts)
|
|
|
|
}
|
2019-10-22 10:09:36 +00:00
|
|
|
|
|
|
|
func (a *StateAPI) StateMarketBalance(ctx context.Context, addr address.Address, ts *types.TipSet) (actors.StorageParticipantBalance, error) {
|
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
|
|
|
|
|
|
|
func (a *StateAPI) StateMarketParticipants(ctx context.Context, ts *types.TipSet) (map[string]actors.StorageParticipantBalance, error) {
|
|
|
|
out := map[string]actors.StorageParticipantBalance{}
|
|
|
|
|
|
|
|
var state actors.StorageMarketState
|
2019-10-23 12:59:57 +00:00
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, actors.StorageMarketAddress, &state, ts); err != nil {
|
2019-10-22 19:29:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cst := hamt.CSTFromBstore(a.StateManager.ChainStore().Blockstore())
|
|
|
|
nd, err := hamt.LoadNode(ctx, cst, state.Balances)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nd.ForEach(ctx, func(k string, val interface{}) error {
|
|
|
|
cv := val.(*cbg.Deferred)
|
|
|
|
a, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var b actors.StorageParticipantBalance
|
|
|
|
if err := b.UnmarshalCBOR(bytes.NewReader(cv.Raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out[a.String()] = b
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *StateAPI) StateMarketDeals(ctx context.Context, ts *types.TipSet) (map[string]actors.OnChainDeal, error) {
|
|
|
|
out := map[string]actors.OnChainDeal{}
|
|
|
|
|
|
|
|
var state actors.StorageMarketState
|
2019-10-23 12:59:57 +00:00
|
|
|
if _, err := a.StateManager.LoadActorState(ctx, actors.StorageMarketAddress, &state, ts); err != nil {
|
2019-10-22 19:29:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blks := amt.WrapBlockstore(a.StateManager.ChainStore().Blockstore())
|
|
|
|
da, err := amt.LoadAMT(blks, state.Deals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := da.ForEach(func(i uint64, v *cbg.Deferred) error {
|
|
|
|
var d actors.OnChainDeal
|
|
|
|
if err := d.UnmarshalCBOR(bytes.NewReader(v.Raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out[strconv.FormatInt(int64(i), 10)] = d
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-11-07 07:57:10 +00:00
|
|
|
|
|
|
|
func (a *StateAPI) StateMarketStorageDeal(ctx context.Context, dealId uint64, ts *types.TipSet) (*actors.OnChainDeal, error) {
|
|
|
|
return stmgr.GetStorageDeal(ctx, s.StateManager, dealId, ts)
|
|
|
|
}
|