2019-09-06 06:26:02 +00:00
|
|
|
package stmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-08 16:38:22 +00:00
|
|
|
"errors"
|
2019-10-08 05:51:34 +00:00
|
|
|
"fmt"
|
2019-09-06 20:03:28 +00:00
|
|
|
"sync"
|
2021-04-18 14:17:31 +00:00
|
|
|
"sync/atomic"
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2021-05-30 18:54:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2021-04-15 17:43:16 +00:00
|
|
|
"go.opencensus.io/stats"
|
2020-09-14 11:45:20 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"golang.org/x/xerrors"
|
2020-09-07 20:01:09 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-08-06 17:09:03 +00:00
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
// Used for genesis.
|
|
|
|
msig0 "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
|
2021-01-26 20:46:57 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/v3/actors/migration/nv10"
|
2020-10-08 01:09:33 +00:00
|
|
|
|
2021-01-19 00:06:16 +00:00
|
|
|
// we use the same adt for all receipts
|
|
|
|
blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-07-17 17:49:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-03-04 21:21:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-09-22 18:09:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2020-10-08 01:09:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-10-08 20:32:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/cron"
|
2020-10-08 01:09:33 +00:00
|
|
|
_init "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2020-10-08 01:09:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2020-09-22 18:09:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
|
2020-09-22 18:09:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
|
2020-10-08 01:09:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2021-04-15 17:43:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
2019-09-06 06:26:02 +00:00
|
|
|
)
|
|
|
|
|
2021-04-05 11:23:46 +00:00
|
|
|
const LookbackNoLimit = api.LookbackNoLimit
|
2021-01-16 04:12:31 +00:00
|
|
|
const ReceiptAmtBitwidth = 3
|
2020-10-02 14:14:30 +00:00
|
|
|
|
2019-09-09 16:18:27 +00:00
|
|
|
var log = logging.Logger("statemgr")
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2020-09-29 15:25:45 +00:00
|
|
|
type StateManagerAPI interface {
|
2020-10-09 11:41:09 +00:00
|
|
|
Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error)
|
|
|
|
GetPaychState(ctx context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, paych.State, error)
|
2020-09-29 15:25:45 +00:00
|
|
|
LoadActorTsk(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error)
|
2020-09-30 14:36:16 +00:00
|
|
|
LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
|
2020-09-29 15:25:45 +00:00
|
|
|
ResolveToKeyAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error)
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:09:56 +00:00
|
|
|
type versionSpec struct {
|
|
|
|
networkVersion network.Version
|
|
|
|
atOrBelow abi.ChainEpoch
|
|
|
|
}
|
|
|
|
|
2021-01-26 20:46:57 +00:00
|
|
|
type migration struct {
|
2021-01-26 23:15:14 +00:00
|
|
|
upgrade MigrationFunc
|
|
|
|
preMigrations []PreMigration
|
2021-01-27 01:08:06 +00:00
|
|
|
cache *nv10.MemMigrationCache
|
2021-01-26 20:46:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
type StateManager struct {
|
|
|
|
cs *store.ChainStore
|
2019-09-06 20:03:28 +00:00
|
|
|
|
2021-01-26 23:11:31 +00:00
|
|
|
cancel context.CancelFunc
|
|
|
|
shutdown chan struct{}
|
2021-01-26 20:46:57 +00:00
|
|
|
|
2020-10-03 00:09:56 +00:00
|
|
|
// Determines the network version at any given epoch.
|
|
|
|
networkVersions []versionSpec
|
|
|
|
latestVersion network.Version
|
|
|
|
|
2021-01-26 20:46:57 +00:00
|
|
|
// Maps chain epochs to migrations.
|
|
|
|
stateMigrations map[abi.ChainEpoch]*migration
|
2020-10-07 23:14:11 +00:00
|
|
|
// A set of potentially expensive/time consuming upgrades. Explicit
|
|
|
|
// calls for, e.g., gas estimation fail against this epoch with
|
|
|
|
// ErrExpensiveFork.
|
|
|
|
expensiveUpgrades map[abi.ChainEpoch]struct{}
|
2020-10-01 22:02:40 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
stCache map[string][]cid.Cid
|
2021-05-31 22:12:42 +00:00
|
|
|
tCache treeCache
|
2020-11-13 08:25:29 +00:00
|
|
|
compWait map[string]chan struct{}
|
|
|
|
stlk sync.Mutex
|
|
|
|
genesisMsigLk sync.Mutex
|
|
|
|
newVM func(context.Context, *vm.VMOpts) (*vm.VM, error)
|
|
|
|
preIgnitionVesting []msig0.State
|
|
|
|
postIgnitionVesting []msig0.State
|
|
|
|
postCalicoVesting []msig0.State
|
|
|
|
|
|
|
|
genesisPledge abi.TokenAmount
|
|
|
|
genesisMarketFunds abi.TokenAmount
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 22:12:42 +00:00
|
|
|
// Caches a single state tree
|
|
|
|
type treeCache struct {
|
|
|
|
root cid.Cid
|
|
|
|
tree *state.StateTree
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:18:59 +00:00
|
|
|
func NewStateManager(cs *store.ChainStore) *StateManager {
|
2020-10-03 00:09:56 +00:00
|
|
|
sm, err := NewStateManagerWithUpgradeSchedule(cs, DefaultUpgradeSchedule())
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("default upgrade schedule is invalid: %s", err))
|
|
|
|
}
|
|
|
|
return sm
|
2020-10-01 22:18:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:09:56 +00:00
|
|
|
func NewStateManagerWithUpgradeSchedule(cs *store.ChainStore, us UpgradeSchedule) (*StateManager, error) {
|
|
|
|
// If we have upgrades, make sure they're in-order and make sense.
|
|
|
|
if err := us.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-01 22:02:40 +00:00
|
|
|
|
2021-01-26 20:46:57 +00:00
|
|
|
stateMigrations := make(map[abi.ChainEpoch]*migration, len(us))
|
2020-10-07 23:14:11 +00:00
|
|
|
expensiveUpgrades := make(map[abi.ChainEpoch]struct{}, len(us))
|
2020-10-03 00:09:56 +00:00
|
|
|
var networkVersions []versionSpec
|
|
|
|
lastVersion := network.Version0
|
|
|
|
if len(us) > 0 {
|
|
|
|
// If we have any upgrades, process them and create a version
|
|
|
|
// schedule.
|
|
|
|
for _, upgrade := range us {
|
2021-01-26 23:15:14 +00:00
|
|
|
if upgrade.Migration != nil || upgrade.PreMigrations != nil {
|
2021-01-26 20:46:57 +00:00
|
|
|
migration := &migration{
|
|
|
|
upgrade: upgrade.Migration,
|
2021-01-26 23:15:14 +00:00
|
|
|
preMigrations: upgrade.PreMigrations,
|
2021-01-26 20:46:57 +00:00
|
|
|
cache: nv10.NewMemMigrationCache(),
|
|
|
|
}
|
|
|
|
stateMigrations[upgrade.Height] = migration
|
2020-10-03 00:09:56 +00:00
|
|
|
}
|
2020-10-07 23:14:11 +00:00
|
|
|
if upgrade.Expensive {
|
|
|
|
expensiveUpgrades[upgrade.Height] = struct{}{}
|
|
|
|
}
|
2020-10-03 00:09:56 +00:00
|
|
|
networkVersions = append(networkVersions, versionSpec{
|
|
|
|
networkVersion: lastVersion,
|
|
|
|
atOrBelow: upgrade.Height,
|
|
|
|
})
|
|
|
|
lastVersion = upgrade.Network
|
2020-10-01 22:02:40 +00:00
|
|
|
}
|
2020-10-03 00:09:56 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, go directly to the latest version.
|
|
|
|
lastVersion = build.NewestNetworkVersion
|
2020-10-01 22:02:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 20:03:28 +00:00
|
|
|
return &StateManager{
|
2020-10-07 23:14:11 +00:00
|
|
|
networkVersions: networkVersions,
|
|
|
|
latestVersion: lastVersion,
|
|
|
|
stateMigrations: stateMigrations,
|
|
|
|
expensiveUpgrades: expensiveUpgrades,
|
|
|
|
newVM: vm.NewVM,
|
|
|
|
cs: cs,
|
|
|
|
stCache: make(map[string][]cid.Cid),
|
2021-05-31 22:12:42 +00:00
|
|
|
tCache: treeCache{
|
|
|
|
root: cid.Undef,
|
|
|
|
tree: nil,
|
|
|
|
},
|
|
|
|
compWait: make(map[string]chan struct{}),
|
2020-10-03 00:09:56 +00:00
|
|
|
}, nil
|
2019-09-06 20:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cidsToKey(cids []cid.Cid) string {
|
|
|
|
var out string
|
|
|
|
for _, c := range cids {
|
|
|
|
out += c.KeyString()
|
|
|
|
}
|
|
|
|
return out
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 20:46:57 +00:00
|
|
|
// Start starts the state manager's optional background processes. At the moment, this schedules
|
|
|
|
// pre-migration functions to run ahead of network upgrades.
|
|
|
|
//
|
2021-01-27 23:41:26 +00:00
|
|
|
// This method is not safe to invoke from multiple threads or concurrently with Stop.
|
2021-01-26 20:46:57 +00:00
|
|
|
func (sm *StateManager) Start(context.Context) error {
|
2021-01-26 23:11:31 +00:00
|
|
|
var ctx context.Context
|
|
|
|
ctx, sm.cancel = context.WithCancel(context.Background())
|
|
|
|
sm.shutdown = make(chan struct{})
|
|
|
|
go sm.preMigrationWorker(ctx)
|
2021-01-26 20:46:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop starts the state manager's background processes.
|
|
|
|
//
|
2021-01-27 23:41:26 +00:00
|
|
|
// This method is not safe to invoke concurrently with Start.
|
2021-01-26 23:11:31 +00:00
|
|
|
func (sm *StateManager) Stop(ctx context.Context) error {
|
2021-01-26 20:46:57 +00:00
|
|
|
if sm.cancel != nil {
|
|
|
|
sm.cancel()
|
2021-01-26 23:11:31 +00:00
|
|
|
select {
|
|
|
|
case <-sm.shutdown:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-01-26 20:46:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 17:07:44 +00:00
|
|
|
func (sm *StateManager) TipSetState(ctx context.Context, ts *types.TipSet) (st cid.Cid, rec cid.Cid, err error) {
|
2019-10-12 09:44:56 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "tipSetState")
|
|
|
|
defer span.End()
|
2019-11-13 06:44:29 +00:00
|
|
|
if span.IsRecordingEvents() {
|
|
|
|
span.AddAttributes(trace.StringAttribute("tipset", fmt.Sprint(ts.Cids())))
|
|
|
|
}
|
2019-09-19 20:25:18 +00:00
|
|
|
|
2019-10-02 20:03:27 +00:00
|
|
|
ck := cidsToKey(ts.Cids())
|
2019-09-06 20:03:28 +00:00
|
|
|
sm.stlk.Lock()
|
2019-12-08 17:07:44 +00:00
|
|
|
cw, cwok := sm.compWait[ck]
|
|
|
|
if cwok {
|
|
|
|
sm.stlk.Unlock()
|
|
|
|
span.AddAttributes(trace.BoolAttribute("waited", true))
|
|
|
|
select {
|
|
|
|
case <-cw:
|
|
|
|
sm.stlk.Lock()
|
|
|
|
case <-ctx.Done():
|
|
|
|
return cid.Undef, cid.Undef, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 20:03:28 +00:00
|
|
|
cached, ok := sm.stCache[ck]
|
|
|
|
if ok {
|
2019-12-08 17:07:44 +00:00
|
|
|
sm.stlk.Unlock()
|
2019-10-13 01:05:43 +00:00
|
|
|
span.AddAttributes(trace.BoolAttribute("cache", true))
|
2019-09-27 23:55:15 +00:00
|
|
|
return cached[0], cached[1], nil
|
2019-09-06 20:03:28 +00:00
|
|
|
}
|
2019-12-08 17:07:44 +00:00
|
|
|
ch := make(chan struct{})
|
|
|
|
sm.compWait[ck] = ch
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
sm.stlk.Lock()
|
|
|
|
delete(sm.compWait, ck)
|
|
|
|
if st != cid.Undef {
|
|
|
|
sm.stCache[ck] = []cid.Cid{st, rec}
|
|
|
|
}
|
|
|
|
sm.stlk.Unlock()
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
sm.stlk.Unlock()
|
2019-09-06 20:03:28 +00:00
|
|
|
|
2019-10-02 18:00:08 +00:00
|
|
|
if ts.Height() == 0 {
|
2019-10-02 20:27:41 +00:00
|
|
|
// NB: This is here because the process that executes blocks requires that the
|
|
|
|
// block miner reference a valid miner in the state tree. Unless we create some
|
|
|
|
// magical genesis miner, this won't work properly, so we short circuit here
|
|
|
|
// This avoids the question of 'who gets paid the genesis block reward'
|
2019-10-02 18:00:08 +00:00
|
|
|
return ts.Blocks()[0].ParentStateRoot, ts.Blocks()[0].ParentMessageReceipts, nil
|
|
|
|
}
|
|
|
|
|
2020-08-09 01:37:49 +00:00
|
|
|
st, rec, err = sm.computeTipSetState(ctx, ts, nil)
|
2019-09-06 20:03:28 +00:00
|
|
|
if err != nil {
|
2019-09-27 23:55:15 +00:00
|
|
|
return cid.Undef, cid.Undef, err
|
2019-09-06 20:03:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
return st, rec, nil
|
2019-09-06 20:03:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
func traceFunc(trace *[]*api.InvocResult) func(mcid cid.Cid, msg *types.Message, ret *vm.ApplyRet) error {
|
|
|
|
return func(mcid cid.Cid, msg *types.Message, ret *vm.ApplyRet) error {
|
2020-03-08 02:31:36 +00:00
|
|
|
ir := &api.InvocResult{
|
2020-10-14 03:45:47 +00:00
|
|
|
MsgCid: mcid,
|
2020-06-11 00:47:28 +00:00
|
|
|
Msg: msg,
|
|
|
|
MsgRct: &ret.MessageReceipt,
|
|
|
|
ExecutionTrace: ret.ExecutionTrace,
|
|
|
|
Duration: ret.Duration,
|
2020-03-08 02:31:36 +00:00
|
|
|
}
|
|
|
|
if ret.ActorErr != nil {
|
|
|
|
ir.Error = ret.ActorErr.Error()
|
|
|
|
}
|
2020-10-16 01:12:38 +00:00
|
|
|
if ret.GasCosts != nil {
|
|
|
|
ir.GasCost = MakeMsgGasCost(msg, ret)
|
|
|
|
}
|
2020-09-24 21:30:11 +00:00
|
|
|
*trace = append(*trace, ir)
|
2020-03-08 02:31:36 +00:00
|
|
|
return nil
|
2020-09-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StateManager) ExecutionTrace(ctx context.Context, ts *types.TipSet) (cid.Cid, []*api.InvocResult, error) {
|
|
|
|
var trace []*api.InvocResult
|
|
|
|
st, _, err := sm.computeTipSetState(ctx, ts, traceFunc(&trace))
|
2020-03-08 02:31:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st, trace, nil
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:24:07 +00:00
|
|
|
type ExecCallback func(cid.Cid, *types.Message, *vm.ApplyRet) error
|
2019-09-20 02:54:52 +00:00
|
|
|
|
2020-09-08 20:45:44 +00:00
|
|
|
func (sm *StateManager) ApplyBlocks(ctx context.Context, parentEpoch abi.ChainEpoch, pstate cid.Cid, bms []store.BlockMessages, epoch abi.ChainEpoch, r vm.Rand, cb ExecCallback, baseFee abi.TokenAmount, ts *types.TipSet) (cid.Cid, cid.Cid, error) {
|
2021-04-15 17:43:16 +00:00
|
|
|
done := metrics.Timer(ctx, metrics.VMApplyBlocksTotal)
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
partDone := metrics.Timer(ctx, metrics.VMApplyEarly)
|
2021-04-18 14:17:31 +00:00
|
|
|
defer func() {
|
|
|
|
partDone()
|
|
|
|
}()
|
2020-08-06 17:09:03 +00:00
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
makeVmWithBaseState := func(base cid.Cid) (*vm.VM, error) {
|
|
|
|
vmopt := &vm.VMOpts{
|
|
|
|
StateBase: base,
|
|
|
|
Epoch: epoch,
|
|
|
|
Rand: r,
|
2021-02-28 22:48:36 +00:00
|
|
|
Bstore: sm.cs.StateBlockstore(),
|
2020-09-24 21:30:11 +00:00
|
|
|
Syscalls: sm.cs.VMSys(),
|
2020-10-11 22:17:28 +00:00
|
|
|
CircSupplyCalc: sm.GetVMCirculatingSupply,
|
2020-09-24 21:30:11 +00:00
|
|
|
NtwkVersion: sm.GetNtwkVersion,
|
|
|
|
BaseFee: baseFee,
|
2020-10-24 09:57:17 +00:00
|
|
|
LookbackState: LookbackStateGetterForTipset(sm, ts),
|
2020-09-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sm.newVM(ctx, vmopt)
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
vmi, err := makeVmWithBaseState(pstate)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2020-09-24 21:30:11 +00:00
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("making vm: %w", err)
|
2019-09-27 23:55:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 05:05:28 +00:00
|
|
|
runCron := func(epoch abi.ChainEpoch) error {
|
2020-07-28 00:25:04 +00:00
|
|
|
cronMsg := &types.Message{
|
2020-10-08 20:32:54 +00:00
|
|
|
To: cron.Address,
|
|
|
|
From: builtin.SystemActorAddr,
|
2020-10-13 05:05:28 +00:00
|
|
|
Nonce: uint64(epoch),
|
2020-08-06 21:08:42 +00:00
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasFeeCap: types.NewInt(0),
|
|
|
|
GasPremium: types.NewInt(0),
|
|
|
|
GasLimit: build.BlockGasLimit * 10000, // Make super sure this is never too little
|
2020-10-08 20:32:54 +00:00
|
|
|
Method: cron.Methods.EpochTick,
|
2020-08-06 21:08:42 +00:00
|
|
|
Params: nil,
|
2020-07-28 00:25:04 +00:00
|
|
|
}
|
|
|
|
ret, err := vmi.ApplyImplicitMessage(ctx, cronMsg)
|
|
|
|
if err != nil {
|
2020-07-28 00:28:22 +00:00
|
|
|
return err
|
2020-07-28 00:25:04 +00:00
|
|
|
}
|
|
|
|
if cb != nil {
|
|
|
|
if err := cb(cronMsg.Cid(), cronMsg, ret); err != nil {
|
2020-07-28 00:28:22 +00:00
|
|
|
return xerrors.Errorf("callback failed on cron message: %w", err)
|
2020-07-28 00:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ret.ExitCode != 0 {
|
2020-07-28 00:28:22 +00:00
|
|
|
return xerrors.Errorf("CheckProofSubmissions exit was non-zero: %d", ret.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:31:13 +00:00
|
|
|
for i := parentEpoch; i < epoch; i++ {
|
2020-09-30 20:54:30 +00:00
|
|
|
if i > parentEpoch {
|
|
|
|
// run cron for null rounds if any
|
2020-10-13 05:05:28 +00:00
|
|
|
if err := runCron(i); err != nil {
|
2020-10-02 21:12:12 +00:00
|
|
|
return cid.Undef, cid.Undef, err
|
2020-09-30 20:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pstate, err = vmi.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:31:13 +00:00
|
|
|
// handle state forks
|
2020-09-16 05:00:00 +00:00
|
|
|
// XXX: The state tree
|
2020-09-24 21:30:11 +00:00
|
|
|
newState, err := sm.handleStateForks(ctx, pstate, i, cb, ts)
|
2020-07-28 12:31:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("error handling state forks: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
if pstate != newState {
|
|
|
|
vmi, err = makeVmWithBaseState(newState)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("making vm: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:25:04 +00:00
|
|
|
vmi.SetBlockHeight(i + 1)
|
2020-09-24 21:30:11 +00:00
|
|
|
pstate = newState
|
2020-07-28 00:25:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:43:16 +00:00
|
|
|
partDone()
|
|
|
|
partDone = metrics.Timer(ctx, metrics.VMApplyMessages)
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
var receipts []cbg.CBORMarshaler
|
2020-10-13 14:34:07 +00:00
|
|
|
processedMsgs := make(map[cid.Cid]struct{})
|
2020-03-03 01:24:07 +00:00
|
|
|
for _, b := range bms {
|
2020-03-04 21:21:24 +00:00
|
|
|
penalty := types.NewInt(0)
|
2020-03-24 06:50:20 +00:00
|
|
|
gasReward := big.Zero()
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2020-03-04 21:21:24 +00:00
|
|
|
for _, cm := range append(b.BlsMessages, b.SecpkMessages...) {
|
2019-09-27 23:55:15 +00:00
|
|
|
m := cm.VMMessage()
|
2020-03-25 10:48:17 +00:00
|
|
|
if _, found := processedMsgs[m.Cid()]; found {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-25 19:13:09 +00:00
|
|
|
r, err := vmi.ApplyMessage(ctx, cm)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-09-27 23:55:15 +00:00
|
|
|
return cid.Undef, cid.Undef, err
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
2019-09-19 20:25:18 +00:00
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
receipts = append(receipts, &r.MessageReceipt)
|
2020-09-13 05:26:44 +00:00
|
|
|
gasReward = big.Add(gasReward, r.GasCosts.MinerTip)
|
|
|
|
penalty = big.Add(penalty, r.GasCosts.MinerPenalty)
|
2019-09-27 23:55:15 +00:00
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
if cb != nil {
|
2019-09-27 23:55:15 +00:00
|
|
|
if err := cb(cm.Cid(), m, r); err != nil {
|
|
|
|
return cid.Undef, cid.Undef, err
|
2019-09-19 20:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 14:34:07 +00:00
|
|
|
processedMsgs[m.Cid()] = struct{}{}
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
2020-03-04 21:21:24 +00:00
|
|
|
|
2020-09-22 04:12:07 +00:00
|
|
|
params, err := actors.SerializeParams(&reward.AwardBlockRewardParams{
|
|
|
|
Miner: b.Miner,
|
|
|
|
Penalty: penalty,
|
|
|
|
GasReward: gasReward,
|
|
|
|
WinCount: b.WinCount,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("failed to serialize award params: %w", err)
|
2020-03-04 21:21:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 02:31:36 +00:00
|
|
|
rwMsg := &types.Message{
|
2020-10-08 20:32:54 +00:00
|
|
|
From: builtin.SystemActorAddr,
|
2020-09-22 04:12:07 +00:00
|
|
|
To: reward.Address,
|
2020-10-13 05:05:28 +00:00
|
|
|
Nonce: uint64(epoch),
|
2020-08-06 21:08:42 +00:00
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasFeeCap: types.NewInt(0),
|
|
|
|
GasPremium: types.NewInt(0),
|
|
|
|
GasLimit: 1 << 30,
|
2020-10-08 20:32:54 +00:00
|
|
|
Method: reward.Methods.AwardBlockReward,
|
2020-08-06 21:08:42 +00:00
|
|
|
Params: params,
|
2020-03-08 02:31:36 +00:00
|
|
|
}
|
2020-09-22 04:12:07 +00:00
|
|
|
ret, actErr := vmi.ApplyImplicitMessage(ctx, rwMsg)
|
|
|
|
if actErr != nil {
|
2020-09-30 22:04:16 +00:00
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("failed to apply reward message for miner %s: %w", b.Miner, actErr)
|
2020-03-04 21:21:24 +00:00
|
|
|
}
|
2020-03-08 02:31:36 +00:00
|
|
|
if cb != nil {
|
|
|
|
if err := cb(rwMsg.Cid(), rwMsg, ret); err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("callback failed on reward message: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:21:24 +00:00
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("reward application message failed (exit %d): %s", ret.ExitCode, ret.ActorErr)
|
|
|
|
}
|
2019-11-14 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:43:16 +00:00
|
|
|
partDone()
|
|
|
|
partDone = metrics.Timer(ctx, metrics.VMApplyCron)
|
|
|
|
|
2020-10-13 05:05:28 +00:00
|
|
|
if err := runCron(epoch); err != nil {
|
2020-07-28 00:28:22 +00:00
|
|
|
return cid.Cid{}, cid.Cid{}, err
|
2019-11-13 23:14:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:43:16 +00:00
|
|
|
partDone()
|
|
|
|
partDone = metrics.Timer(ctx, metrics.VMApplyFlush)
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
rectarr := blockadt.MakeEmptyArray(sm.cs.ActorStore(ctx))
|
2020-07-23 00:14:54 +00:00
|
|
|
for i, receipt := range receipts {
|
|
|
|
if err := rectarr.Set(uint64(i), receipt); err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("failed to build receipts amt: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rectroot, err := rectarr.Root()
|
2019-09-27 23:55:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("failed to build receipts amt: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := vmi.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("vm flush failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:43:16 +00:00
|
|
|
stats.Record(ctx, metrics.VMSends.M(int64(atomic.LoadUint64(&vm.StatSends))),
|
|
|
|
metrics.VMApplied.M(int64(atomic.LoadUint64(&vm.StatApplied))))
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
return st, rectroot, nil
|
2020-03-03 01:24:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 01:37:49 +00:00
|
|
|
func (sm *StateManager) computeTipSetState(ctx context.Context, ts *types.TipSet, cb ExecCallback) (cid.Cid, cid.Cid, error) {
|
2020-03-03 01:24:07 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "computeTipSetState")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-08-09 01:37:49 +00:00
|
|
|
blks := ts.Blocks()
|
|
|
|
|
2020-03-03 01:24:07 +00:00
|
|
|
for i := 0; i < len(blks); i++ {
|
|
|
|
for j := i + 1; j < len(blks); j++ {
|
|
|
|
if blks[i].Miner == blks[j].Miner {
|
|
|
|
return cid.Undef, cid.Undef,
|
|
|
|
xerrors.Errorf("duplicate miner in a tipset (%s %s)",
|
|
|
|
blks[i].Miner, blks[j].Miner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:25:04 +00:00
|
|
|
var parentEpoch abi.ChainEpoch
|
2020-03-03 01:24:07 +00:00
|
|
|
pstate := blks[0].ParentStateRoot
|
2020-08-18 01:54:49 +00:00
|
|
|
if blks[0].Height > 0 {
|
2020-03-03 01:24:07 +00:00
|
|
|
parent, err := sm.cs.GetBlock(blks[0].Parents[0])
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("getting parent block: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:25:04 +00:00
|
|
|
parentEpoch = parent.Height
|
2020-03-03 01:24:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 02:46:41 +00:00
|
|
|
r := store.NewChainRand(sm.cs, ts.Cids())
|
2020-03-03 01:24:07 +00:00
|
|
|
|
2020-08-09 01:37:49 +00:00
|
|
|
blkmsgs, err := sm.cs.BlockMsgsForTipset(ts)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, cid.Undef, xerrors.Errorf("getting block messages for tipset: %w", err)
|
2020-03-03 01:24:07 +00:00
|
|
|
}
|
2020-08-09 01:37:49 +00:00
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
baseFee := blks[0].ParentBaseFee
|
2020-03-03 01:24:07 +00:00
|
|
|
|
2020-09-08 20:45:44 +00:00
|
|
|
return sm.ApplyBlocks(ctx, parentEpoch, pstate, blkmsgs, blks[0].Height, r, cb, baseFee, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 02:33:27 +00:00
|
|
|
func (sm *StateManager) parentState(ts *types.TipSet) cid.Cid {
|
2019-09-17 22:43:47 +00:00
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2020-02-11 02:33:27 +00:00
|
|
|
return ts.ParentState()
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (sm *StateManager) ChainStore() *store.ChainStore {
|
|
|
|
return sm.cs
|
|
|
|
}
|
|
|
|
|
2020-06-02 16:38:41 +00:00
|
|
|
// ResolveToKeyAddress is similar to `vm.ResolveToKeyAddr` but does not allow `Actor` type of addresses.
|
2020-06-02 14:29:39 +00:00
|
|
|
// Uses the `TipSet` `ts` to generate the VM state.
|
2019-10-09 02:58:49 +00:00
|
|
|
func (sm *StateManager) ResolveToKeyAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
|
|
|
switch addr.Protocol() {
|
|
|
|
case address.BLS, address.SECP256K1:
|
|
|
|
return addr, nil
|
|
|
|
case address.Actor:
|
|
|
|
return address.Undef, xerrors.New("cannot resolve actor address to key address")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
cst := cbor.NewCborStore(sm.cs.StateBlockstore())
|
2021-01-28 00:37:32 +00:00
|
|
|
|
|
|
|
// First try to resolve the actor in the parent state, so we don't have to compute anything.
|
|
|
|
tree, err := state.LoadStateTree(cst, ts.ParentState())
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to load parent state tree: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved, err := vm.ResolveToKeyAddr(tree, cst, addr)
|
|
|
|
if err == nil {
|
|
|
|
return resolved, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that fails, compute the tip-set and try again.
|
2019-10-10 11:13:26 +00:00
|
|
|
st, _, err := sm.TipSetState(ctx, ts)
|
2019-10-09 02:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("resolve address failed to get tipset state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-28 00:37:32 +00:00
|
|
|
tree, err = state.LoadStateTree(cst, st)
|
2019-10-09 02:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to load state tree")
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm.ResolveToKeyAddr(tree, cst, addr)
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:12:42 +00:00
|
|
|
// ResolveToKeyAddressAtFinality is similar to stmgr.ResolveToKeyAddress but fails if the ID address being resolved isn't reorg-stable yet.
|
2021-05-30 18:54:09 +00:00
|
|
|
// It should not be used for consensus-critical subsystems.
|
2021-05-31 22:12:42 +00:00
|
|
|
func (sm *StateManager) ResolveToKeyAddressAtFinality(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
2021-05-30 18:54:09 +00:00
|
|
|
switch addr.Protocol() {
|
|
|
|
case address.BLS, address.SECP256K1:
|
|
|
|
return addr, nil
|
|
|
|
case address.Actor:
|
|
|
|
return address.Undef, xerrors.New("cannot resolve actor address to key address")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:12:42 +00:00
|
|
|
var err error
|
2021-05-30 18:54:09 +00:00
|
|
|
if ts.Height() > policy.ChainFinality {
|
|
|
|
ts, err = sm.ChainStore().GetTipsetByHeight(ctx, ts.Height()-policy.ChainFinality, ts, true)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to load lookback tipset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cst := cbor.NewCborStore(sm.cs.StateBlockstore())
|
2021-05-31 22:12:42 +00:00
|
|
|
tree := sm.tCache.tree
|
2021-05-30 18:54:09 +00:00
|
|
|
|
2021-05-31 22:12:42 +00:00
|
|
|
if tree == nil || sm.tCache.root != ts.ParentState() {
|
|
|
|
tree, err = state.LoadStateTree(cst, ts.ParentState())
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed to load parent state tree: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.tCache = treeCache{
|
|
|
|
root: ts.ParentState(),
|
|
|
|
tree: tree,
|
|
|
|
}
|
2021-05-30 18:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolved, err := vm.ResolveToKeyAddr(tree, cst, addr)
|
|
|
|
if err == nil {
|
|
|
|
return resolved, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.Undef, xerrors.New("ID address not found in lookback state")
|
|
|
|
}
|
|
|
|
|
2020-07-26 01:38:18 +00:00
|
|
|
func (sm *StateManager) GetBlsPublicKey(ctx context.Context, addr address.Address, ts *types.TipSet) (pubk []byte, err error) {
|
2019-10-09 02:58:49 +00:00
|
|
|
kaddr, err := sm.ResolveToKeyAddress(ctx, addr, ts)
|
|
|
|
if err != nil {
|
|
|
|
return pubk, xerrors.Errorf("failed to resolve address to key address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if kaddr.Protocol() != address.BLS {
|
|
|
|
return pubk, xerrors.Errorf("address must be BLS address to load bls public key")
|
|
|
|
}
|
|
|
|
|
2020-07-26 01:38:18 +00:00
|
|
|
return kaddr.Payload(), nil
|
2019-10-09 02:58:49 +00:00
|
|
|
}
|
2019-10-08 05:51:34 +00:00
|
|
|
|
2020-02-23 15:50:36 +00:00
|
|
|
func (sm *StateManager) LookupID(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
2021-02-28 22:48:36 +00:00
|
|
|
cst := cbor.NewCborStore(sm.cs.StateBlockstore())
|
2020-09-14 22:43:12 +00:00
|
|
|
state, err := state.LoadStateTree(cst, sm.parentState(ts))
|
2020-02-23 15:50:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("load state tree: %w", err)
|
|
|
|
}
|
|
|
|
return state.LookupID(addr)
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:38:37 +00:00
|
|
|
// WaitForMessage blocks until a message appears on chain. It looks backwards in the chain to see if this has already
|
2020-10-02 14:14:30 +00:00
|
|
|
// happened, with an optional limit to how many epochs it will search. It guarantees that the message has been on
|
|
|
|
// chain for at least confidence epochs without being reverted before returning.
|
2021-04-02 11:52:24 +00:00
|
|
|
func (sm *StateManager) WaitForMessage(ctx context.Context, mcid cid.Cid, confidence uint64, lookbackLimit abi.ChainEpoch, allowReplaced bool) (*types.TipSet, *types.MessageReceipt, cid.Cid, error) {
|
2019-10-08 05:51:34 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
msg, err := sm.cs.GetCMessage(mcid)
|
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, fmt.Errorf("failed to load message: %w", err)
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tsub := sm.cs.SubHeadChanges(ctx)
|
|
|
|
|
|
|
|
head, ok := <-tsub
|
|
|
|
if !ok {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, fmt.Errorf("SubHeadChanges stream was invalid")
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(head) != 1 {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, fmt.Errorf("SubHeadChanges first entry should have been one item")
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if head[0].Type != store.HCCurrent {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, fmt.Errorf("expected current head on SHC stream (got %s)", head[0].Type)
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:52:24 +00:00
|
|
|
r, foundMsg, err := sm.tipsetExecutedMessage(head[0].Val, mcid, msg.VMMessage(), allowReplaced)
|
2019-10-08 05:51:34 +00:00
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, err
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return head[0].Val, r, foundMsg, nil
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var backTs *types.TipSet
|
|
|
|
var backRcp *types.MessageReceipt
|
2020-08-10 12:55:27 +00:00
|
|
|
var backFm cid.Cid
|
2019-10-08 05:51:34 +00:00
|
|
|
backSearchWait := make(chan struct{})
|
|
|
|
go func() {
|
2021-04-02 11:52:24 +00:00
|
|
|
fts, r, foundMsg, err := sm.searchBackForMsg(ctx, head[0].Val, msg, lookbackLimit, allowReplaced)
|
2019-10-08 05:51:34 +00:00
|
|
|
if err != nil {
|
2021-02-11 11:00:26 +00:00
|
|
|
log.Warnf("failed to look back through chain for message: %v", err)
|
2019-10-08 05:51:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backTs = fts
|
|
|
|
backRcp = r
|
2020-08-10 12:55:27 +00:00
|
|
|
backFm = foundMsg
|
2019-10-08 05:51:34 +00:00
|
|
|
close(backSearchWait)
|
|
|
|
}()
|
|
|
|
|
2020-06-03 19:38:37 +00:00
|
|
|
var candidateTs *types.TipSet
|
|
|
|
var candidateRcp *types.MessageReceipt
|
2020-08-10 12:55:27 +00:00
|
|
|
var candidateFm cid.Cid
|
2020-06-03 19:38:37 +00:00
|
|
|
heightOfHead := head[0].Val.Height()
|
|
|
|
reverts := map[types.TipSetKey]bool{}
|
|
|
|
|
2019-10-08 05:51:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case notif, ok := <-tsub:
|
|
|
|
if !ok {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, ctx.Err()
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
for _, val := range notif {
|
|
|
|
switch val.Type {
|
|
|
|
case store.HCRevert:
|
2020-06-03 19:38:37 +00:00
|
|
|
if val.Val.Equals(candidateTs) {
|
|
|
|
candidateTs = nil
|
|
|
|
candidateRcp = nil
|
2020-08-10 12:55:27 +00:00
|
|
|
candidateFm = cid.Undef
|
2020-06-03 19:38:37 +00:00
|
|
|
}
|
|
|
|
if backSearchWait != nil {
|
|
|
|
reverts[val.Val.Key()] = true
|
|
|
|
}
|
2019-10-08 05:51:34 +00:00
|
|
|
case store.HCApply:
|
2020-06-04 02:30:09 +00:00
|
|
|
if candidateTs != nil && val.Val.Height() >= candidateTs.Height()+abi.ChainEpoch(confidence) {
|
2020-08-10 12:55:27 +00:00
|
|
|
return candidateTs, candidateRcp, candidateFm, nil
|
2020-06-03 19:38:37 +00:00
|
|
|
}
|
2021-04-02 11:52:24 +00:00
|
|
|
r, foundMsg, err := sm.tipsetExecutedMessage(val.Val, mcid, msg.VMMessage(), allowReplaced)
|
2019-10-08 05:51:34 +00:00
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, err
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
if r != nil {
|
2020-06-03 19:38:37 +00:00
|
|
|
if confidence == 0 {
|
2020-08-10 12:55:27 +00:00
|
|
|
return val.Val, r, foundMsg, err
|
2020-06-03 19:38:37 +00:00
|
|
|
}
|
|
|
|
candidateTs = val.Val
|
|
|
|
candidateRcp = r
|
2020-08-10 12:55:27 +00:00
|
|
|
candidateFm = foundMsg
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
2020-06-03 19:38:37 +00:00
|
|
|
heightOfHead = val.Val.Height()
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-backSearchWait:
|
2020-06-03 19:38:37 +00:00
|
|
|
// check if we found the message in the chain and that is hasn't been reverted since we started searching
|
|
|
|
if backTs != nil && !reverts[backTs.Key()] {
|
|
|
|
// if head is at or past confidence interval, return immediately
|
2020-06-04 02:30:09 +00:00
|
|
|
if heightOfHead >= backTs.Height()+abi.ChainEpoch(confidence) {
|
2020-08-10 12:55:27 +00:00
|
|
|
return backTs, backRcp, backFm, nil
|
2020-06-03 19:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for confidence interval
|
|
|
|
candidateTs = backTs
|
|
|
|
candidateRcp = backRcp
|
2020-08-10 12:55:27 +00:00
|
|
|
candidateFm = backFm
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
2020-06-03 19:38:37 +00:00
|
|
|
reverts = nil
|
2019-10-08 05:51:34 +00:00
|
|
|
backSearchWait = nil
|
|
|
|
case <-ctx.Done():
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, ctx.Err()
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:52:24 +00:00
|
|
|
func (sm *StateManager) SearchForMessage(ctx context.Context, head *types.TipSet, mcid cid.Cid, lookbackLimit abi.ChainEpoch, allowReplaced bool) (*types.TipSet, *types.MessageReceipt, cid.Cid, error) {
|
2020-03-18 23:06:53 +00:00
|
|
|
msg, err := sm.cs.GetCMessage(mcid)
|
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, fmt.Errorf("failed to load message: %w", err)
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:52:24 +00:00
|
|
|
r, foundMsg, err := sm.tipsetExecutedMessage(head, mcid, msg.VMMessage(), allowReplaced)
|
2020-03-18 23:06:53 +00:00
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, err
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return head, r, foundMsg, nil
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:52:24 +00:00
|
|
|
fts, r, foundMsg, err := sm.searchBackForMsg(ctx, head, msg, lookbackLimit, allowReplaced)
|
2020-03-18 23:06:53 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to look back through chain for message %s", mcid)
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, err
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fts == nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, nil
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:55:27 +00:00
|
|
|
return fts, r, foundMsg, nil
|
2020-03-18 23:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:14:30 +00:00
|
|
|
// searchBackForMsg searches up to limit tipsets backwards from the given
|
|
|
|
// tipset for a message receipt.
|
|
|
|
// If limit is
|
|
|
|
// - 0 then no tipsets are searched
|
|
|
|
// - 5 then five tipset are searched
|
|
|
|
// - LookbackNoLimit then there is no limit
|
2021-04-02 11:52:24 +00:00
|
|
|
func (sm *StateManager) searchBackForMsg(ctx context.Context, from *types.TipSet, m types.ChainMsg, limit abi.ChainEpoch, allowReplaced bool) (*types.TipSet, *types.MessageReceipt, cid.Cid, error) {
|
2020-10-02 14:14:30 +00:00
|
|
|
limitHeight := from.Height() - limit
|
|
|
|
noLimit := limit == LookbackNoLimit
|
2019-10-08 05:51:34 +00:00
|
|
|
|
|
|
|
cur := from
|
2020-10-08 16:38:22 +00:00
|
|
|
curActor, err := sm.LoadActor(ctx, m.VMMessage().From, cur)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, cid.Undef, xerrors.Errorf("failed to load initital tipset")
|
|
|
|
}
|
|
|
|
|
|
|
|
mFromId, err := sm.LookupID(ctx, m.VMMessage().From, from)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, cid.Undef, xerrors.Errorf("looking up From id address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mNonce := m.VMMessage().Nonce
|
|
|
|
|
2019-10-08 05:51:34 +00:00
|
|
|
for {
|
2020-10-02 14:14:30 +00:00
|
|
|
// If we've reached the genesis block, or we've reached the limit of
|
|
|
|
// how far back to look
|
|
|
|
if cur.Height() == 0 || !noLimit && cur.Height() <= limitHeight {
|
2019-10-09 08:56:47 +00:00
|
|
|
// it ain't here!
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, nil
|
2019-10-09 08:56:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:51:34 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, nil
|
2019-10-08 05:51:34 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:08:05 +00:00
|
|
|
// we either have no messages from the sender, or the latest message we found has a lower nonce than the one being searched for,
|
|
|
|
// either way, no reason to lookback, it ain't there
|
2020-10-08 16:38:22 +00:00
|
|
|
if curActor == nil || curActor.Nonce == 0 || curActor.Nonce < mNonce {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, nil, cid.Undef, nil
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:38:22 +00:00
|
|
|
pts, err := sm.cs.LoadTipSet(cur.Parents())
|
2019-10-08 05:51:34 +00:00
|
|
|
if err != nil {
|
2020-10-08 16:38:22 +00:00
|
|
|
return nil, nil, cid.Undef, xerrors.Errorf("failed to load tipset during msg wait searchback: %w", err)
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:38:22 +00:00
|
|
|
act, err := sm.LoadActor(ctx, mFromId, pts)
|
|
|
|
actorNoExist := errors.Is(err, types.ErrActorNotFound)
|
|
|
|
if err != nil && !actorNoExist {
|
|
|
|
return nil, nil, cid.Cid{}, xerrors.Errorf("failed to load the actor: %w", err)
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:38:22 +00:00
|
|
|
// check that between cur and parent tipset the nonce fell into range of our message
|
|
|
|
if actorNoExist || (curActor.Nonce > mNonce && act.Nonce <= mNonce) {
|
2021-04-02 11:52:24 +00:00
|
|
|
r, foundMsg, err := sm.tipsetExecutedMessage(cur, m.Cid(), m.VMMessage(), allowReplaced)
|
2020-10-08 16:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, cid.Undef, xerrors.Errorf("checking for message execution during lookback: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
2020-10-13 15:48:54 +00:00
|
|
|
return cur, r, foundMsg, nil
|
2020-10-08 16:38:22 +00:00
|
|
|
}
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:38:22 +00:00
|
|
|
cur = pts
|
|
|
|
curActor = act
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:52:24 +00:00
|
|
|
func (sm *StateManager) tipsetExecutedMessage(ts *types.TipSet, msg cid.Cid, vmm *types.Message, allowReplaced bool) (*types.MessageReceipt, cid.Cid, error) {
|
2019-10-08 05:51:34 +00:00
|
|
|
// The genesis block did not execute any messages
|
|
|
|
if ts.Height() == 0 {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, nil
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := sm.cs.LoadTipSet(ts.Parents())
|
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, err
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm, err := sm.cs.MessagesForTipset(pts)
|
|
|
|
if err != nil {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, err
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 22:42:51 +00:00
|
|
|
for ii := range cm {
|
|
|
|
// iterate in reverse because we going backwards through the chain
|
|
|
|
i := len(cm) - ii - 1
|
|
|
|
m := cm[i]
|
|
|
|
|
2019-11-24 19:16:18 +00:00
|
|
|
if m.VMMessage().From == vmm.From { // cheaper to just check origin first
|
|
|
|
if m.VMMessage().Nonce == vmm.Nonce {
|
2021-04-02 11:52:24 +00:00
|
|
|
if allowReplaced && m.VMMessage().EqualCall(vmm) {
|
2020-08-10 12:55:27 +00:00
|
|
|
if m.Cid() != msg {
|
|
|
|
log.Warnw("found message with equal nonce and call params but different CID",
|
|
|
|
"wanted", msg, "found", m.Cid(), "nonce", vmm.Nonce, "from", vmm.From)
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, err := sm.cs.GetParentReceipt(ts.Blocks()[0], i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, cid.Undef, err
|
|
|
|
}
|
|
|
|
return pr, m.Cid(), nil
|
2019-11-24 19:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this should be that message
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, xerrors.Errorf("found message with equal nonce as the one we are looking for (F:%s n %d, TS: %s n%d)",
|
2019-11-24 19:16:18 +00:00
|
|
|
msg, vmm.Nonce, m.Cid(), m.VMMessage().Nonce)
|
|
|
|
}
|
|
|
|
if m.VMMessage().Nonce < vmm.Nonce {
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, nil // don't bother looking further
|
2019-11-24 19:16:18 +00:00
|
|
|
}
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:55:27 +00:00
|
|
|
return nil, cid.Undef, nil
|
2019-10-08 05:51:34 +00:00
|
|
|
}
|
2019-10-12 06:45:48 +00:00
|
|
|
|
|
|
|
func (sm *StateManager) ListAllActors(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
|
|
|
|
if ts == nil {
|
2019-11-14 06:34:54 +00:00
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
2019-10-12 06:45:48 +00:00
|
|
|
}
|
2021-04-29 06:35:27 +00:00
|
|
|
st := ts.ParentState()
|
2019-10-12 06:45:48 +00:00
|
|
|
|
2020-09-22 18:09:56 +00:00
|
|
|
stateTree, err := sm.StateTree(st)
|
2019-10-12 06:45:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []address.Address
|
2020-09-22 18:09:56 +00:00
|
|
|
err = stateTree.ForEach(func(addr address.Address, act *types.Actor) error {
|
2019-10-12 06:45:48 +00:00
|
|
|
out = append(out, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-10-23 17:39:14 +00:00
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (sm *StateManager) MarketBalance(ctx context.Context, addr address.Address, ts *types.TipSet) (api.MarketBalance, error) {
|
2020-09-12 03:07:52 +00:00
|
|
|
st, err := sm.ParentState(ts)
|
2020-02-08 02:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MarketBalance{}, err
|
2019-10-23 17:39:14 +00:00
|
|
|
}
|
2020-09-14 12:17:45 +00:00
|
|
|
|
2020-09-22 04:12:07 +00:00
|
|
|
act, err := st.GetActor(market.Address)
|
2020-09-12 03:07:52 +00:00
|
|
|
if err != nil {
|
2020-09-14 12:17:45 +00:00
|
|
|
return api.MarketBalance{}, err
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
mstate, err := market.Load(sm.cs.ActorStore(ctx), act)
|
2020-09-14 12:17:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MarketBalance{}, err
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
2020-02-08 02:18:32 +00:00
|
|
|
|
2020-02-23 15:50:36 +00:00
|
|
|
addr, err = sm.LookupID(ctx, addr, ts)
|
|
|
|
if err != nil {
|
|
|
|
return api.MarketBalance{}, err
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
var out api.MarketBalance
|
2020-02-23 15:50:36 +00:00
|
|
|
|
2020-09-14 12:17:45 +00:00
|
|
|
et, err := mstate.EscrowTable()
|
2020-04-13 21:05:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MarketBalance{}, err
|
|
|
|
}
|
2020-07-28 14:36:32 +00:00
|
|
|
out.Escrow, err = et.Get(addr)
|
2019-10-23 17:39:14 +00:00
|
|
|
if err != nil {
|
2020-07-28 14:36:32 +00:00
|
|
|
return api.MarketBalance{}, xerrors.Errorf("getting escrow balance: %w", err)
|
2019-10-23 17:39:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:17:45 +00:00
|
|
|
lt, err := mstate.LockedTable()
|
2020-04-13 21:05:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.MarketBalance{}, err
|
|
|
|
}
|
2020-07-28 14:36:32 +00:00
|
|
|
out.Locked, err = lt.Get(addr)
|
2020-02-08 02:18:32 +00:00
|
|
|
if err != nil {
|
2020-07-28 14:36:32 +00:00
|
|
|
return api.MarketBalance{}, xerrors.Errorf("getting locked balance: %w", err)
|
2020-02-08 02:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2019-10-23 17:39:14 +00:00
|
|
|
}
|
2020-01-21 01:53:55 +00:00
|
|
|
|
|
|
|
func (sm *StateManager) ValidateChain(ctx context.Context, ts *types.TipSet) error {
|
|
|
|
tschain := []*types.TipSet{ts}
|
|
|
|
for ts.Height() != 0 {
|
|
|
|
next, err := sm.cs.LoadTipSet(ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tschain = append(tschain, next)
|
|
|
|
ts = next
|
|
|
|
}
|
|
|
|
|
|
|
|
lastState := tschain[len(tschain)-1].ParentState()
|
|
|
|
for i := len(tschain) - 1; i >= 0; i-- {
|
|
|
|
cur := tschain[i]
|
|
|
|
log.Infof("computing state (height: %d, ts=%s)", cur.Height(), cur.Cids())
|
|
|
|
if cur.ParentState() != lastState {
|
|
|
|
return xerrors.Errorf("tipset chain had state mismatch at height %d", cur.Height())
|
|
|
|
}
|
|
|
|
st, _, err := sm.TipSetState(ctx, cur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lastState = st
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-31 23:51:15 +00:00
|
|
|
|
2020-09-14 12:17:45 +00:00
|
|
|
func (sm *StateManager) SetVMConstructor(nvm func(context.Context, *vm.VMOpts) (*vm.VM, error)) {
|
2020-01-31 23:51:15 +00:00
|
|
|
sm.newVM = nvm
|
|
|
|
}
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// sets up information about the vesting schedule
|
|
|
|
func (sm *StateManager) setupGenesisVestingSchedule(ctx context.Context) error {
|
2020-08-09 23:31:00 +00:00
|
|
|
|
2020-07-25 22:29:33 +00:00
|
|
|
gb, err := sm.cs.GetGenesis()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting genesis block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gts, err := types.NewTipSet([]*types.BlockHeader{gb})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting genesis tipset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, _, err := sm.TipSetState(ctx, gts)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting genesis tipset state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
cst := cbor.NewCborStore(sm.cs.StateBlockstore())
|
2020-09-14 22:43:12 +00:00
|
|
|
sTree, err := state.LoadStateTree(cst, st)
|
2020-08-09 21:45:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading state tree: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
gmf, err := getFilMarketLocked(ctx, sTree)
|
2020-08-09 23:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("setting up genesis market funds: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
gp, err := getFilPowerLocked(ctx, sTree)
|
2020-08-09 23:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("setting up genesis pledge: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.genesisMarketFunds = gmf
|
|
|
|
sm.genesisPledge = gp
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
totalsByEpoch := make(map[abi.ChainEpoch]abi.TokenAmount)
|
2020-09-23 05:19:43 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 6 months
|
|
|
|
sixMonths := abi.ChainEpoch(183 * builtin.EpochsInDay)
|
|
|
|
totalsByEpoch[sixMonths] = big.NewInt(49_929_341)
|
|
|
|
totalsByEpoch[sixMonths] = big.Add(totalsByEpoch[sixMonths], big.NewInt(32_787_700))
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 1 year
|
|
|
|
oneYear := abi.ChainEpoch(365 * builtin.EpochsInDay)
|
|
|
|
totalsByEpoch[oneYear] = big.NewInt(22_421_712)
|
2020-08-09 21:45:45 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 2 years
|
|
|
|
twoYears := abi.ChainEpoch(2 * 365 * builtin.EpochsInDay)
|
|
|
|
totalsByEpoch[twoYears] = big.NewInt(7_223_364)
|
2020-09-22 18:09:56 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 3 years
|
|
|
|
threeYears := abi.ChainEpoch(3 * 365 * builtin.EpochsInDay)
|
|
|
|
totalsByEpoch[threeYears] = big.NewInt(87_637_883)
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 6 years
|
|
|
|
sixYears := abi.ChainEpoch(6 * 365 * builtin.EpochsInDay)
|
|
|
|
totalsByEpoch[sixYears] = big.NewInt(100_000_000)
|
|
|
|
totalsByEpoch[sixYears] = big.Add(totalsByEpoch[sixYears], big.NewInt(300_000_000))
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.preIgnitionVesting = make([]msig0.State, 0, len(totalsByEpoch))
|
2020-07-25 22:29:33 +00:00
|
|
|
for k, v := range totalsByEpoch {
|
2020-09-18 21:59:27 +00:00
|
|
|
ns := msig0.State{
|
2020-07-25 22:29:33 +00:00
|
|
|
InitialBalance: v,
|
|
|
|
UnlockDuration: k,
|
|
|
|
PendingTxns: cid.Undef,
|
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.preIgnitionVesting = append(sm.preIgnitionVesting, ns)
|
2020-07-25 22:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// sets up information about the vesting schedule post the ignition upgrade
|
|
|
|
func (sm *StateManager) setupPostIgnitionVesting(ctx context.Context) error {
|
2020-08-18 03:44:56 +00:00
|
|
|
|
|
|
|
totalsByEpoch := make(map[abi.ChainEpoch]abi.TokenAmount)
|
|
|
|
|
|
|
|
// 6 months
|
2020-10-08 20:32:54 +00:00
|
|
|
sixMonths := abi.ChainEpoch(183 * builtin.EpochsInDay)
|
2020-08-18 03:44:56 +00:00
|
|
|
totalsByEpoch[sixMonths] = big.NewInt(49_929_341)
|
|
|
|
totalsByEpoch[sixMonths] = big.Add(totalsByEpoch[sixMonths], big.NewInt(32_787_700))
|
|
|
|
|
|
|
|
// 1 year
|
2020-10-08 20:32:54 +00:00
|
|
|
oneYear := abi.ChainEpoch(365 * builtin.EpochsInDay)
|
2020-08-18 03:44:56 +00:00
|
|
|
totalsByEpoch[oneYear] = big.NewInt(22_421_712)
|
|
|
|
|
|
|
|
// 2 years
|
2020-10-08 20:32:54 +00:00
|
|
|
twoYears := abi.ChainEpoch(2 * 365 * builtin.EpochsInDay)
|
2020-08-18 03:44:56 +00:00
|
|
|
totalsByEpoch[twoYears] = big.NewInt(7_223_364)
|
|
|
|
|
|
|
|
// 3 years
|
2020-10-08 20:32:54 +00:00
|
|
|
threeYears := abi.ChainEpoch(3 * 365 * builtin.EpochsInDay)
|
2020-08-18 03:44:56 +00:00
|
|
|
totalsByEpoch[threeYears] = big.NewInt(87_637_883)
|
|
|
|
|
|
|
|
// 6 years
|
2020-10-08 20:32:54 +00:00
|
|
|
sixYears := abi.ChainEpoch(6 * 365 * builtin.EpochsInDay)
|
2020-08-18 03:44:56 +00:00
|
|
|
totalsByEpoch[sixYears] = big.NewInt(100_000_000)
|
|
|
|
totalsByEpoch[sixYears] = big.Add(totalsByEpoch[sixYears], big.NewInt(300_000_000))
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.postIgnitionVesting = make([]msig0.State, 0, len(totalsByEpoch))
|
2020-08-18 03:44:56 +00:00
|
|
|
for k, v := range totalsByEpoch {
|
2020-09-18 21:59:27 +00:00
|
|
|
ns := msig0.State{
|
2020-11-13 08:25:29 +00:00
|
|
|
// In the pre-ignition logic, we incorrectly set this value in Fil, not attoFil, an off-by-10^18 error
|
|
|
|
InitialBalance: big.Mul(v, big.NewInt(int64(build.FilecoinPrecision))),
|
2020-08-18 03:44:56 +00:00
|
|
|
UnlockDuration: k,
|
|
|
|
PendingTxns: cid.Undef,
|
2020-11-13 08:25:29 +00:00
|
|
|
// In the pre-ignition logic, the start epoch was 0. This changes in the fork logic of the Ignition upgrade itself.
|
|
|
|
StartEpoch: build.UpgradeLiftoffHeight,
|
2020-08-18 03:44:56 +00:00
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.postIgnitionVesting = append(sm.postIgnitionVesting, ns)
|
2020-08-18 03:44:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// sets up information about the vesting schedule post the calico upgrade
|
|
|
|
func (sm *StateManager) setupPostCalicoVesting(ctx context.Context) error {
|
2020-09-24 21:30:11 +00:00
|
|
|
|
|
|
|
totalsByEpoch := make(map[abi.ChainEpoch]abi.TokenAmount)
|
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
// 0 days
|
|
|
|
zeroDays := abi.ChainEpoch(0)
|
|
|
|
totalsByEpoch[zeroDays] = big.NewInt(10_632_000)
|
|
|
|
|
2020-09-24 21:30:11 +00:00
|
|
|
// 6 months
|
2020-10-08 20:32:54 +00:00
|
|
|
sixMonths := abi.ChainEpoch(183 * builtin.EpochsInDay)
|
2020-11-13 08:25:29 +00:00
|
|
|
totalsByEpoch[sixMonths] = big.NewInt(19_015_887)
|
2020-09-24 21:30:11 +00:00
|
|
|
totalsByEpoch[sixMonths] = big.Add(totalsByEpoch[sixMonths], big.NewInt(32_787_700))
|
|
|
|
|
|
|
|
// 1 year
|
2020-10-08 20:32:54 +00:00
|
|
|
oneYear := abi.ChainEpoch(365 * builtin.EpochsInDay)
|
2020-09-24 21:30:11 +00:00
|
|
|
totalsByEpoch[oneYear] = big.NewInt(22_421_712)
|
2020-11-13 08:25:29 +00:00
|
|
|
totalsByEpoch[oneYear] = big.Add(totalsByEpoch[oneYear], big.NewInt(9_400_000))
|
2020-09-24 21:30:11 +00:00
|
|
|
|
|
|
|
// 2 years
|
2020-10-08 20:32:54 +00:00
|
|
|
twoYears := abi.ChainEpoch(2 * 365 * builtin.EpochsInDay)
|
2020-09-24 21:30:11 +00:00
|
|
|
totalsByEpoch[twoYears] = big.NewInt(7_223_364)
|
|
|
|
|
|
|
|
// 3 years
|
2020-10-08 20:32:54 +00:00
|
|
|
threeYears := abi.ChainEpoch(3 * 365 * builtin.EpochsInDay)
|
2020-09-24 21:30:11 +00:00
|
|
|
totalsByEpoch[threeYears] = big.NewInt(87_637_883)
|
2020-11-13 08:25:29 +00:00
|
|
|
totalsByEpoch[threeYears] = big.Add(totalsByEpoch[threeYears], big.NewInt(898_958))
|
2020-09-24 21:30:11 +00:00
|
|
|
|
|
|
|
// 6 years
|
2020-10-08 20:32:54 +00:00
|
|
|
sixYears := abi.ChainEpoch(6 * 365 * builtin.EpochsInDay)
|
2020-09-24 21:30:11 +00:00
|
|
|
totalsByEpoch[sixYears] = big.NewInt(100_000_000)
|
|
|
|
totalsByEpoch[sixYears] = big.Add(totalsByEpoch[sixYears], big.NewInt(300_000_000))
|
2020-11-13 08:25:29 +00:00
|
|
|
totalsByEpoch[sixYears] = big.Add(totalsByEpoch[sixYears], big.NewInt(9_805_053))
|
2020-09-24 21:30:11 +00:00
|
|
|
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.postCalicoVesting = make([]msig0.State, 0, len(totalsByEpoch))
|
2020-09-24 21:30:11 +00:00
|
|
|
for k, v := range totalsByEpoch {
|
|
|
|
ns := msig0.State{
|
|
|
|
InitialBalance: big.Mul(v, big.NewInt(int64(build.FilecoinPrecision))),
|
|
|
|
UnlockDuration: k,
|
|
|
|
PendingTxns: cid.Undef,
|
2020-11-13 08:25:29 +00:00
|
|
|
StartEpoch: build.UpgradeLiftoffHeight,
|
2020-09-24 21:30:11 +00:00
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
sm.postCalicoVesting = append(sm.postCalicoVesting, ns)
|
2020-09-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 03:44:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-09 21:45:45 +00:00
|
|
|
// GetVestedFunds returns all funds that have "left" actors that are in the genesis state:
|
|
|
|
// - For Multisigs, it counts the actual amounts that have vested at the given epoch
|
|
|
|
// - For Accounts, it counts max(currentBalance - genesisBalance, 0).
|
2020-08-09 22:49:38 +00:00
|
|
|
func (sm *StateManager) GetFilVested(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-07-25 22:29:33 +00:00
|
|
|
vf := big.Zero()
|
2020-09-24 21:30:11 +00:00
|
|
|
if height <= build.UpgradeIgnitionHeight {
|
2020-11-13 08:25:29 +00:00
|
|
|
for _, v := range sm.preIgnitionVesting {
|
2020-09-24 21:30:11 +00:00
|
|
|
au := big.Sub(v.InitialBalance, v.AmountLocked(height))
|
|
|
|
vf = big.Add(vf, au)
|
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
} else if height <= build.UpgradeCalicoHeight {
|
|
|
|
for _, v := range sm.postIgnitionVesting {
|
2020-09-24 21:30:11 +00:00
|
|
|
// In the pre-ignition logic, we simply called AmountLocked(height), assuming startEpoch was 0.
|
|
|
|
// The start epoch changed in the Ignition upgrade.
|
|
|
|
au := big.Sub(v.InitialBalance, v.AmountLocked(height-v.StartEpoch))
|
|
|
|
vf = big.Add(vf, au)
|
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
} else {
|
|
|
|
for _, v := range sm.postCalicoVesting {
|
|
|
|
// In the pre-ignition logic, we simply called AmountLocked(height), assuming startEpoch was 0.
|
|
|
|
// The start epoch changed in the Ignition upgrade.
|
|
|
|
au := big.Sub(v.InitialBalance, v.AmountLocked(height-v.StartEpoch))
|
|
|
|
vf = big.Add(vf, au)
|
2020-08-09 21:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:44:11 +00:00
|
|
|
// After UpgradeAssemblyHeight these funds are accounted for in GetFilReserveDisbursed
|
|
|
|
if height <= build.UpgradeAssemblyHeight {
|
2020-10-05 01:38:52 +00:00
|
|
|
// continue to use preIgnitionGenInfos, nothing changed at the Ignition epoch
|
2020-11-13 08:25:29 +00:00
|
|
|
vf = big.Add(vf, sm.genesisPledge)
|
2020-10-05 01:38:52 +00:00
|
|
|
// continue to use preIgnitionGenInfos, nothing changed at the Ignition epoch
|
2020-11-13 08:25:29 +00:00
|
|
|
vf = big.Add(vf, sm.genesisMarketFunds)
|
2020-10-05 01:38:52 +00:00
|
|
|
}
|
2020-08-12 19:24:30 +00:00
|
|
|
|
2020-07-25 22:29:33 +00:00
|
|
|
return vf, nil
|
|
|
|
}
|
2020-08-09 22:49:38 +00:00
|
|
|
|
2020-10-05 01:38:52 +00:00
|
|
|
func GetFilReserveDisbursed(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
|
|
|
ract, err := st.GetActor(builtin.ReserveAddress)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to get reserve actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If money enters the reserve actor, this could lead to a negative term
|
|
|
|
return big.Sub(big.NewFromGo(build.InitialFilReserved), ract.Balance), nil
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:49:38 +00:00
|
|
|
func GetFilMined(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-09-22 04:12:07 +00:00
|
|
|
ractor, err := st.GetActor(reward.Address)
|
2020-08-09 22:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to load reward actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:30:15 +00:00
|
|
|
rst, err := reward.Load(adt.WrapStore(ctx, st.Store), ractor)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), err
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
2020-08-10 20:13:09 +00:00
|
|
|
|
2020-09-17 08:17:14 +00:00
|
|
|
return rst.TotalStoragePowerReward()
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:21:21 +00:00
|
|
|
func getFilMarketLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-09-22 04:12:07 +00:00
|
|
|
act, err := st.GetActor(market.Address)
|
2020-08-09 22:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to load market actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
mst, err := market.Load(adt.WrapStore(ctx, st.Store), act)
|
|
|
|
if err != nil {
|
2020-08-09 22:49:38 +00:00
|
|
|
return big.Zero(), xerrors.Errorf("failed to load market state: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
return mst.TotalLocked()
|
2020-08-09 23:21:21 +00:00
|
|
|
}
|
2020-08-09 22:49:38 +00:00
|
|
|
|
2020-08-09 23:21:21 +00:00
|
|
|
func getFilPowerLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-09-22 04:12:07 +00:00
|
|
|
pactor, err := st.GetActor(power.Address)
|
2020-08-09 22:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to load power actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:17:45 +00:00
|
|
|
pst, err := power.Load(adt.WrapStore(ctx, st.Store), pactor)
|
2020-09-12 03:07:52 +00:00
|
|
|
if err != nil {
|
2020-08-09 22:49:38 +00:00
|
|
|
return big.Zero(), xerrors.Errorf("failed to load power state: %w", err)
|
|
|
|
}
|
2020-09-12 03:07:52 +00:00
|
|
|
|
2020-09-14 12:17:45 +00:00
|
|
|
return pst.TotalLocked()
|
2020-08-09 23:21:21 +00:00
|
|
|
}
|
2020-08-09 22:49:38 +00:00
|
|
|
|
2020-08-09 23:21:21 +00:00
|
|
|
func (sm *StateManager) GetFilLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
|
|
|
|
|
|
|
filMarketLocked, err := getFilMarketLocked(ctx, st)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to get filMarketLocked: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filPowerLocked, err := getFilPowerLocked(ctx, st)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to get filPowerLocked: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.BigAdd(filMarketLocked, filPowerLocked), nil
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetFilBurnt(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-10-08 20:32:54 +00:00
|
|
|
burnt, err := st.GetActor(builtin.BurntFundsActorAddr)
|
2020-08-09 22:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), xerrors.Errorf("failed to load burnt actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return burnt.Balance, nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:17:28 +00:00
|
|
|
func (sm *StateManager) GetVMCirculatingSupply(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (abi.TokenAmount, error) {
|
|
|
|
cs, err := sm.GetVMCirculatingSupplyDetailed(ctx, height, st)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.FilCirculating, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StateManager) GetVMCirculatingSupplyDetailed(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (api.CirculatingSupply, error) {
|
2020-08-09 23:21:21 +00:00
|
|
|
sm.genesisMsigLk.Lock()
|
|
|
|
defer sm.genesisMsigLk.Unlock()
|
2020-11-13 08:25:29 +00:00
|
|
|
if sm.preIgnitionVesting == nil || sm.genesisPledge.IsZero() || sm.genesisMarketFunds.IsZero() {
|
|
|
|
err := sm.setupGenesisVestingSchedule(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to setup pre-ignition vesting schedule: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sm.postIgnitionVesting == nil {
|
|
|
|
err := sm.setupPostIgnitionVesting(ctx)
|
2020-08-09 23:21:21 +00:00
|
|
|
if err != nil {
|
2020-11-13 08:25:29 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to setup post-ignition vesting schedule: %w", err)
|
2020-09-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 08:25:29 +00:00
|
|
|
if sm.postCalicoVesting == nil {
|
|
|
|
err := sm.setupPostCalicoVesting(ctx)
|
2020-09-24 21:30:11 +00:00
|
|
|
if err != nil {
|
2020-11-13 08:25:29 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to setup post-calico vesting schedule: %w", err)
|
2020-08-09 23:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:49:38 +00:00
|
|
|
filVested, err := sm.GetFilVested(ctx, height, st)
|
|
|
|
if err != nil {
|
2020-08-14 20:44:33 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filVested: %w", err)
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 14:53:49 +00:00
|
|
|
filReserveDisbursed := big.Zero()
|
2021-05-06 05:44:11 +00:00
|
|
|
if height > build.UpgradeAssemblyHeight {
|
2020-10-06 14:53:49 +00:00
|
|
|
filReserveDisbursed, err = GetFilReserveDisbursed(ctx, st)
|
|
|
|
if err != nil {
|
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filReserveDisbursed: %w", err)
|
|
|
|
}
|
2020-10-05 01:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:49:38 +00:00
|
|
|
filMined, err := GetFilMined(ctx, st)
|
|
|
|
if err != nil {
|
2020-08-14 20:44:33 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filMined: %w", err)
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filBurnt, err := GetFilBurnt(ctx, st)
|
|
|
|
if err != nil {
|
2020-08-14 20:44:33 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filBurnt: %w", err)
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 23:21:21 +00:00
|
|
|
filLocked, err := sm.GetFilLocked(ctx, st)
|
2020-08-09 22:49:38 +00:00
|
|
|
if err != nil {
|
2020-08-14 20:44:33 +00:00
|
|
|
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filLocked: %w", err)
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret := types.BigAdd(filVested, filMined)
|
2020-10-05 01:38:52 +00:00
|
|
|
ret = types.BigAdd(ret, filReserveDisbursed)
|
2020-08-09 22:49:38 +00:00
|
|
|
ret = types.BigSub(ret, filBurnt)
|
|
|
|
ret = types.BigSub(ret, filLocked)
|
|
|
|
|
|
|
|
if ret.LessThan(big.Zero()) {
|
|
|
|
ret = big.Zero()
|
|
|
|
}
|
|
|
|
|
2020-08-14 20:44:33 +00:00
|
|
|
return api.CirculatingSupply{
|
2021-04-16 22:15:38 +00:00
|
|
|
FilVested: filVested,
|
|
|
|
FilMined: filMined,
|
|
|
|
FilBurnt: filBurnt,
|
|
|
|
FilLocked: filLocked,
|
|
|
|
FilCirculating: ret,
|
|
|
|
FilReserveDisbursed: filReserveDisbursed,
|
2020-08-14 20:44:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StateManager) GetCirculatingSupply(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (abi.TokenAmount, error) {
|
2020-10-11 22:17:28 +00:00
|
|
|
circ := big.Zero()
|
|
|
|
unCirc := big.Zero()
|
|
|
|
err := st.ForEach(func(a address.Address, actor *types.Actor) error {
|
|
|
|
switch {
|
|
|
|
case actor.Balance.IsZero():
|
|
|
|
// Do nothing for zero-balance actors
|
|
|
|
break
|
|
|
|
case a == _init.Address ||
|
|
|
|
a == reward.Address ||
|
|
|
|
a == verifreg.Address ||
|
|
|
|
// The power actor itself should never receive funds
|
|
|
|
a == power.Address ||
|
|
|
|
a == builtin.SystemActorAddr ||
|
|
|
|
a == builtin.CronActorAddr ||
|
|
|
|
a == builtin.BurntFundsActorAddr ||
|
|
|
|
a == builtin.SaftAddress ||
|
|
|
|
a == builtin.ReserveAddress:
|
|
|
|
|
|
|
|
unCirc = big.Add(unCirc, actor.Balance)
|
|
|
|
|
|
|
|
case a == market.Address:
|
2021-02-28 22:48:36 +00:00
|
|
|
mst, err := market.Load(sm.cs.ActorStore(ctx), actor)
|
2020-10-11 22:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lb, err := mst.TotalLocked()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
circ = big.Add(circ, big.Sub(actor.Balance, lb))
|
|
|
|
unCirc = big.Add(unCirc, lb)
|
|
|
|
|
|
|
|
case builtin.IsAccountActor(actor.Code) || builtin.IsPaymentChannelActor(actor.Code):
|
|
|
|
circ = big.Add(circ, actor.Balance)
|
|
|
|
|
|
|
|
case builtin.IsStorageMinerActor(actor.Code):
|
2021-02-28 22:48:36 +00:00
|
|
|
mst, err := miner.Load(sm.cs.ActorStore(ctx), actor)
|
2020-10-11 22:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ab, err := mst.AvailableBalance(actor.Balance)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
circ = big.Add(circ, ab)
|
|
|
|
unCirc = big.Add(unCirc, big.Sub(actor.Balance, ab))
|
|
|
|
} else {
|
|
|
|
// Assume any error is because the miner state is "broken" (lower actor balance than locked funds)
|
|
|
|
// In this case, the actor's entire balance is considered "uncirculating"
|
|
|
|
unCirc = big.Add(unCirc, actor.Balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
case builtin.IsMultisigActor(actor.Code):
|
2021-02-28 22:48:36 +00:00
|
|
|
mst, err := multisig.Load(sm.cs.ActorStore(ctx), actor)
|
2020-10-11 22:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lb, err := mst.LockedBalance(height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ab := big.Sub(actor.Balance, lb)
|
|
|
|
circ = big.Add(circ, big.Max(ab, big.Zero()))
|
|
|
|
unCirc = big.Add(unCirc, big.Min(actor.Balance, lb))
|
|
|
|
default:
|
|
|
|
return xerrors.Errorf("unexpected actor: %s", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-08-14 20:44:33 +00:00
|
|
|
if err != nil {
|
2020-10-11 22:17:28 +00:00
|
|
|
return types.EmptyInt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
total := big.Add(circ, unCirc)
|
|
|
|
if !total.Equals(types.TotalFilecoinInt) {
|
|
|
|
return types.EmptyInt, xerrors.Errorf("total filecoin didn't add to expected amount: %s != %s", total, types.TotalFilecoinInt)
|
2020-08-14 20:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 22:17:28 +00:00
|
|
|
return circ, nil
|
2020-08-09 22:49:38 +00:00
|
|
|
}
|
2020-09-07 20:01:09 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
func (sm *StateManager) GetNtwkVersion(ctx context.Context, height abi.ChainEpoch) network.Version {
|
2020-10-01 22:33:32 +00:00
|
|
|
// The epochs here are the _last_ epoch for every version, or -1 if the
|
|
|
|
// version is disabled.
|
2020-10-03 00:09:56 +00:00
|
|
|
for _, spec := range sm.networkVersions {
|
|
|
|
if height <= spec.atOrBelow {
|
|
|
|
return spec.networkVersion
|
2020-10-01 22:02:40 +00:00
|
|
|
}
|
2020-09-28 22:58:07 +00:00
|
|
|
}
|
2020-10-03 00:09:56 +00:00
|
|
|
return sm.latestVersion
|
2020-09-07 20:01:09 +00:00
|
|
|
}
|
2020-09-16 04:06:04 +00:00
|
|
|
|
|
|
|
func (sm *StateManager) GetPaychState(ctx context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, paych.State, error) {
|
|
|
|
st, err := sm.ParentState(ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := st.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
actState, err := paych.Load(sm.cs.ActorStore(ctx), act)
|
2020-09-16 04:06:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return act, actState, nil
|
|
|
|
}
|
2020-09-17 07:32:10 +00:00
|
|
|
|
|
|
|
func (sm *StateManager) GetMarketState(ctx context.Context, ts *types.TipSet) (market.State, error) {
|
|
|
|
st, err := sm.ParentState(ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:22:38 +00:00
|
|
|
act, err := st.GetActor(market.Address)
|
2020-09-17 07:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
actState, err := market.Load(sm.cs.ActorStore(ctx), act)
|
2020-09-17 07:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return actState, nil
|
|
|
|
}
|
2020-09-29 15:25:45 +00:00
|
|
|
|
|
|
|
var _ StateManagerAPI = (*StateManager)(nil)
|