lotus/chain/stmgr/utils.go

315 lines
9.4 KiB
Go
Raw Normal View History

package stmgr
import (
"context"
amt "github.com/filecoin-project/go-amt-ipld/v2"
2020-02-08 02:18:32 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
2020-02-11 02:33:27 +00:00
"github.com/filecoin-project/specs-actors/actors/abi/big"
2020-02-08 02:18:32 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/market"
2020-02-11 02:33:27 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/actors/builtin/power"
"github.com/filecoin-project/specs-actors/actors/util/adt"
2020-02-08 02:18:32 +00:00
2020-01-30 00:50:58 +00:00
"github.com/filecoin-project/lotus/chain/actors/aerrors"
2020-01-07 16:23:12 +00:00
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
2020-02-11 02:33:27 +00:00
ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-address"
2020-01-07 16:23:12 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
2019-09-17 22:43:47 +00:00
cid "github.com/ipfs/go-cid"
2019-09-18 02:55:51 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
2019-09-17 22:43:47 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
)
func GetMinerWorkerRaw(ctx context.Context, sm *StateManager, st cid.Cid, maddr address.Address) (address.Address, error) {
2020-02-11 02:33:27 +00:00
var mas miner.State
_, err := sm.LoadActorStateRaw(ctx, maddr, &mas, st)
if err != nil {
2020-02-11 02:33:27 +00:00
return address.Undef, xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
}
2020-02-11 02:33:27 +00:00
return mas.Info.Worker, nil
}
func GetMinerOwner(ctx context.Context, sm *StateManager, st cid.Cid, maddr address.Address) (address.Address, error) {
2020-02-11 02:33:27 +00:00
var mas miner.State
_, err := sm.LoadActorStateRaw(ctx, maddr, &mas, st)
if err != nil {
2020-02-11 02:33:27 +00:00
return address.Undef, xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
}
2020-02-11 02:33:27 +00:00
return mas.Info.Owner, nil
}
2019-09-09 20:03:10 +00:00
func GetPower(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (types.BigInt, types.BigInt, error) {
2020-02-11 02:33:27 +00:00
var ps power.State
_, err := sm.LoadActorState(ctx, maddr, &ps, ts)
if err != nil {
return big.Zero(), big.Zero(), xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
}
2019-09-09 20:03:10 +00:00
2020-02-11 02:33:27 +00:00
var mpow big.Int
2019-09-09 20:03:10 +00:00
if maddr != address.Undef {
2020-02-11 02:33:27 +00:00
var claim power.Claim
if _, err := adt.AsMap(sm.cs.Store(ctx), ps.Claims).Get(adt.AddrKey(maddr), &claim); err != nil {
return big.Zero(), big.Zero(), err
2019-09-09 20:03:10 +00:00
}
2020-02-11 02:33:27 +00:00
mpow = claim.Power
2019-09-09 20:03:10 +00:00
}
2020-02-11 02:33:27 +00:00
return mpow, ps.TotalNetworkPower, nil
2019-09-09 20:03:10 +00:00
}
2019-09-17 08:00:38 +00:00
func GetMinerPeerID(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (peer.ID, error) {
2020-02-11 02:33:27 +00:00
var mas miner.State
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
2019-09-17 08:00:38 +00:00
if err != nil {
2020-02-11 02:33:27 +00:00
return "", xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
2019-09-17 08:00:38 +00:00
}
2020-02-11 02:33:27 +00:00
return mas.Info.PeerId, nil
2019-09-17 08:00:38 +00:00
}
2019-09-17 22:43:47 +00:00
func GetMinerWorker(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (address.Address, error) {
2020-02-11 02:33:27 +00:00
return GetMinerWorkerRaw(ctx, sm, sm.parentState(ts), maddr)
}
2020-02-11 02:33:27 +00:00
func GetMinerPostState(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (*miner.PoStState, error) {
2019-09-17 22:43:47 +00:00
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
2020-02-11 02:33:27 +00:00
return nil, xerrors.Errorf("(get eps) failed to load miner actor state: %w", err)
2019-09-17 22:43:47 +00:00
}
2020-02-11 02:33:27 +00:00
return &mas.PoStState, nil
2019-09-17 22:43:47 +00:00
}
2019-12-11 23:31:59 +00:00
func SectorSetSizes(ctx context.Context, sm *StateManager, maddr address.Address, ts *types.TipSet) (api.MinerSectors, error) {
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
return api.MinerSectors{}, xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
}
blks := cbor.NewCborStore(sm.ChainStore().Blockstore())
ss, err := amt.LoadAMT(ctx, blks, mas.Sectors)
2019-12-11 23:31:59 +00:00
if err != nil {
return api.MinerSectors{}, err
}
ps, err := amt.LoadAMT(ctx, blks, mas.ProvingSet)
2019-12-11 23:31:59 +00:00
if err != nil {
return api.MinerSectors{}, err
}
return api.MinerSectors{
Pset: ps.Count,
Sset: ss.Count,
}, nil
}
2019-11-08 18:15:13 +00:00
func GetMinerProvingSet(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]*api.ChainSectorInfo, error) {
2019-09-17 22:43:47 +00:00
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
return nil, xerrors.Errorf("(get pset) failed to load miner actor state: %w", err)
2019-09-17 22:43:47 +00:00
}
2019-09-18 02:55:51 +00:00
return LoadSectorsFromSet(ctx, sm.ChainStore().Blockstore(), mas.ProvingSet)
}
2019-11-08 18:15:13 +00:00
func GetMinerSectorSet(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]*api.ChainSectorInfo, error) {
2019-09-18 02:55:51 +00:00
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
return nil, xerrors.Errorf("(get sset) failed to load miner actor state: %w", err)
2019-09-18 02:55:51 +00:00
}
return LoadSectorsFromSet(ctx, sm.ChainStore().Blockstore(), mas.Sectors)
}
2019-11-25 04:45:13 +00:00
func GetSectorsForElectionPost(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (*sectorbuilder.SortedPublicSectorInfo, error) {
sectors, err := GetMinerProvingSet(ctx, sm, ts, maddr)
2019-11-21 22:21:45 +00:00
if err != nil {
return nil, xerrors.Errorf("failed to get sector set for miner: %w", err)
}
var uselessOtherArray []ffi.PublicSectorInfo
2019-11-21 22:21:45 +00:00
for _, s := range sectors {
var uselessBuffer [32]byte
copy(uselessBuffer[:], s.CommR)
uselessOtherArray = append(uselessOtherArray, ffi.PublicSectorInfo{
2020-02-11 01:10:50 +00:00
SectorNum: s.SectorID,
2020-02-11 20:48:03 +00:00
CommR: uselessBuffer,
2019-11-21 22:21:45 +00:00
})
}
2019-11-25 04:45:13 +00:00
ssi := sectorbuilder.NewSortedPublicSectorInfo(uselessOtherArray)
2019-11-21 22:21:45 +00:00
return &ssi, nil
}
2020-02-08 02:18:32 +00:00
func GetMinerSectorSize(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (abi.SectorSize, error) {
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
return 0, xerrors.Errorf("(get ssize) failed to load miner actor state: %w", err)
}
2020-02-11 02:33:27 +00:00
return mas.Info.SectorSize, nil
}
func GetMinerSlashed(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) (uint64, error) {
2020-02-11 02:33:27 +00:00
panic("TODO")
}
func GetMinerFaults(ctx context.Context, sm *StateManager, ts *types.TipSet, maddr address.Address) ([]abi.SectorNumber, error) {
2020-01-30 00:50:58 +00:00
var mas actors.StorageMinerActorState
_, err := sm.LoadActorState(ctx, maddr, &mas, ts)
if err != nil {
return nil, xerrors.Errorf("(get ssize) failed to load miner actor state: %w", err)
}
ss, lerr := amt.LoadAMT(ctx, cbor.NewCborStore(sm.cs.Blockstore()), mas.Sectors)
2020-01-30 00:50:58 +00:00
if lerr != nil {
return nil, aerrors.HandleExternalError(lerr, "could not load proving set node")
}
faults, err := mas.FaultSet.All(2 * ss.Count)
if err != nil {
return nil, xerrors.Errorf("reading fault bit set: %w", err)
}
out := make([]abi.SectorNumber, len(faults))
for i, fault := range faults {
out[i] = abi.SectorNumber(fault)
}
return out, nil
2020-01-30 00:50:58 +00:00
}
func GetStorageDeal(ctx context.Context, sm *StateManager, dealId abi.DealID, ts *types.TipSet) (*api.MarketDeal, error) {
var state actors.StorageMarketState
if _, err := sm.LoadActorState(ctx, actors.StorageMarketAddress, &state, ts); err != nil {
return nil, err
}
2020-02-08 02:18:32 +00:00
da, err := amt.LoadAMT(ctx, cbor.NewCborStore(sm.ChainStore().Blockstore()), state.Proposals)
if err != nil {
return nil, err
}
var dp market.DealProposal
if err := da.Get(ctx, uint64(dealId), &dp); err != nil {
return nil, err
}
sa, err := amt.LoadAMT(ctx, cbor.NewCborStore(sm.ChainStore().Blockstore()), state.States)
if err != nil {
return nil, err
}
var st market.DealState
if err := sa.Get(ctx, uint64(dealId), &st); err != nil {
return nil, err
}
return &api.MarketDeal{
Proposal: dp,
State: st,
}, nil
}
func ListMinerActors(ctx context.Context, sm *StateManager, ts *types.TipSet) ([]address.Address, error) {
var state actors.StoragePowerState
if _, err := sm.LoadActorState(ctx, actors.StoragePowerAddress, &state, ts); err != nil {
return nil, err
}
2020-02-11 02:33:27 +00:00
var miners []address.Address
err := adt.AsMap(sm.cs.Store(ctx), state.Claims).ForEach(nil, func(k string) error {
a, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
miners = append(miners, a)
return nil
})
if err != nil {
return nil, err
}
return miners, nil
}
2019-11-08 18:15:13 +00:00
func LoadSectorsFromSet(ctx context.Context, bs blockstore.Blockstore, ssc cid.Cid) ([]*api.ChainSectorInfo, error) {
a, err := amt.LoadAMT(ctx, cbor.NewCborStore(bs), ssc)
2019-09-17 22:43:47 +00:00
if err != nil {
return nil, err
}
2019-11-08 18:15:13 +00:00
var sset []*api.ChainSectorInfo
if err := a.ForEach(ctx, func(i uint64, v *cbg.Deferred) error {
2019-09-17 22:43:47 +00:00
var comms [][]byte
if err := cbor.DecodeInto(v.Raw, &comms); err != nil {
return err
}
2019-11-08 18:15:13 +00:00
sset = append(sset, &api.ChainSectorInfo{
SectorID: abi.SectorNumber(i),
2019-09-17 22:43:47 +00:00
CommR: comms[0],
CommD: comms[1],
})
return nil
}); err != nil {
return nil, err
}
return sset, nil
}
2020-02-08 02:18:32 +00:00
func ComputeState(ctx context.Context, sm *StateManager, height abi.ChainEpoch, msgs []*types.Message, ts *types.TipSet) (cid.Cid, error) {
if ts == nil {
ts = sm.cs.GetHeaviestTipSet()
}
base, _, err := sm.TipSetState(ctx, ts)
if err != nil {
return cid.Undef, err
}
fstate, err := sm.handleStateForks(ctx, base, height, ts.Height())
if err != nil {
return cid.Undef, err
}
r := store.NewChainRand(sm.cs, ts.Cids(), height)
vmi, err := vm.NewVM(fstate, height, r, actors.NetworkAddress, sm.cs.Blockstore(), sm.cs.VMSys())
if err != nil {
return cid.Undef, err
}
for i, msg := range msgs {
ret, err := vmi.ApplyMessage(ctx, msg)
if err != nil {
return cid.Undef, xerrors.Errorf("applying message %s: %w", msg.Cid(), err)
}
if ret.ExitCode != 0 {
2020-01-17 06:14:00 +00:00
log.Infof("compute state apply message %d failed (exit: %d): %s", i, ret.ExitCode, ret.ActorErr)
}
}
return vmi.Flush(ctx)
}