Skip to content

Commit

Permalink
Merge pull request #8293 from filecoin-project/feat/fvm
Browse files Browse the repository at this point in the history
Filecoin Virtual Machine integration
  • Loading branch information
arajasek authored Mar 16, 2022
2 parents f2c0ba1 + 37539cc commit 90d78f9
Show file tree
Hide file tree
Showing 21 changed files with 507 additions and 99 deletions.
18 changes: 16 additions & 2 deletions chain/consensus/filcns/compute_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filcns

import (
"context"
"os"
"sync/atomic"

"github.com/filecoin-project/lotus/chain/rand"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (t *TipSetExecutor) ApplyBlocks(ctx context.Context, sm *stmgr.StateManager
}()

ctx = blockstore.WithHotView(ctx)
makeVmWithBaseStateAndEpoch := func(base cid.Cid, e abi.ChainEpoch) (*vm.VM, error) {
makeVmWithBaseStateAndEpoch := func(base cid.Cid, e abi.ChainEpoch) (vm.Interface, error) {
vmopt := &vm.VMOpts{
StateBase: base,
Epoch: e,
Expand All @@ -108,10 +109,23 @@ func (t *TipSetExecutor) ApplyBlocks(ctx context.Context, sm *stmgr.StateManager
LookbackState: stmgr.LookbackStateGetterForTipset(sm, ts),
}

if os.Getenv("LOTUS_USE_FVM_EXPERIMENTAL") == "1" {
// This is needed so that the FVM does not have to duplicate the genesis vesting schedule, one
// of the components of the circ supply calc.
// This field is NOT needed by the LegacyVM, and also NOT needed by the FVM from v15 onwards.
filVested, err := sm.GetFilVested(ctx, e)
if err != nil {
return nil, err
}

vmopt.FilVested = filVested
return vm.NewFVM(ctx, vmopt)
}

return sm.VMConstructor()(ctx, vmopt)
}

runCron := func(vmCron *vm.VM, epoch abi.ChainEpoch) error {
runCron := func(vmCron vm.Interface, epoch abi.ChainEpoch) error {
cronMsg := &types.Message{
To: cron.Address,
From: builtin.SystemActorAddr,
Expand Down
7 changes: 4 additions & 3 deletions chain/gen/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,13 @@ func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, sys vm.Sysca
Actors: filcns.NewActorRegistry(),
Syscalls: mkFakedSigSyscalls(sys),
CircSupplyCalc: csc,
FilVested: big.Zero(),
NetworkVersion: nv,
BaseFee: types.NewInt(0),
BaseFee: big.Zero(),
}
vm, err := vm.NewVM(ctx, &vmopt)
vm, err := vm.NewLegacyVM(ctx, &vmopt)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
return cid.Undef, xerrors.Errorf("failed to create NewLegacyVM: %w", err)
}

for mi, m := range template.Miners {
Expand Down
15 changes: 8 additions & 7 deletions chain/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal
Syscalls: mkFakedSigSyscalls(sys),
CircSupplyCalc: csc,
NetworkVersion: nv,
BaseFee: types.NewInt(0),
BaseFee: big.Zero(),
FilVested: big.Zero(),
}

vm, err := vm.NewVM(ctx, vmopt)
vm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
return cid.Undef, xerrors.Errorf("failed to create NewLegacyVM: %w", err)
}

if len(miners) == 0 {
Expand Down Expand Up @@ -520,7 +521,7 @@ func (fr *fakeRand) GetBeaconRandomness(ctx context.Context, personalization cry
return out, nil
}

func currentTotalPower(ctx context.Context, vm *vm.VM, maddr address.Address) (*power0.CurrentTotalPowerReturn, error) {
func currentTotalPower(ctx context.Context, vm *vm.LegacyVM, maddr address.Address) (*power0.CurrentTotalPowerReturn, error) {
pwret, err := doExecValue(ctx, vm, power.Address, maddr, big.Zero(), builtin0.MethodsPower.CurrentTotalPower, nil)
if err != nil {
return nil, err
Expand All @@ -533,7 +534,7 @@ func currentTotalPower(ctx context.Context, vm *vm.VM, maddr address.Address) (*
return &pwr, nil
}

func dealWeight(ctx context.Context, vm *vm.VM, maddr address.Address, dealIDs []abi.DealID, sectorStart, sectorExpiry abi.ChainEpoch, av actors.Version) (abi.DealWeight, abi.DealWeight, error) {
func dealWeight(ctx context.Context, vm *vm.LegacyVM, maddr address.Address, dealIDs []abi.DealID, sectorStart, sectorExpiry abi.ChainEpoch, av actors.Version) (abi.DealWeight, abi.DealWeight, error) {
// TODO: This hack should move to market actor wrapper
if av <= actors.Version2 {
params := &market0.VerifyDealsForActivationParams{
Expand Down Expand Up @@ -593,7 +594,7 @@ func dealWeight(ctx context.Context, vm *vm.VM, maddr address.Address, dealIDs [
return dealWeights.Sectors[0].DealWeight, dealWeights.Sectors[0].VerifiedDealWeight, nil
}

func currentEpochBlockReward(ctx context.Context, vm *vm.VM, maddr address.Address, av actors.Version) (abi.StoragePower, builtin.FilterEstimate, error) {
func currentEpochBlockReward(ctx context.Context, vm *vm.LegacyVM, maddr address.Address, av actors.Version) (abi.StoragePower, builtin.FilterEstimate, error) {
rwret, err := doExecValue(ctx, vm, reward.Address, maddr, big.Zero(), reward.Methods.ThisEpochReward, nil)
if err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
Expand Down Expand Up @@ -628,7 +629,7 @@ func currentEpochBlockReward(ctx context.Context, vm *vm.VM, maddr address.Addre
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
}

func circSupply(ctx context.Context, vmi *vm.VM, maddr address.Address) abi.TokenAmount {
func circSupply(ctx context.Context, vmi *vm.LegacyVM, maddr address.Address) abi.TokenAmount {
unsafeVM := &vm.UnsafeVM{VM: vmi}
rt := unsafeVM.MakeRuntime(ctx, &types.Message{
GasLimit: 1_000_000_000,
Expand Down
2 changes: 1 addition & 1 deletion chain/gen/genesis/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func mustEnc(i cbg.CBORMarshaler) []byte {
return enc
}

func doExecValue(ctx context.Context, vm *vm.VM, to, from address.Address, value types.BigInt, method abi.MethodNum, params []byte) ([]byte, error) {
func doExecValue(ctx context.Context, vm *vm.LegacyVM, to, from address.Address, value types.BigInt, method abi.MethodNum, params []byte) ([]byte, error) {
act, err := vm.StateTree().GetActor(from)
if err != nil {
return nil, xerrors.Errorf("doExec failed to get from actor (%s): %w", from, err)
Expand Down
57 changes: 49 additions & 8 deletions chain/stmgr/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"errors"
"fmt"

cbor "github.com/ipfs/go-ipld-cbor"

"github.com/filecoin-project/lotus/chain/state"

"github.com/filecoin-project/lotus/blockstore"

"github.com/filecoin-project/lotus/chain/rand"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -64,6 +70,8 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
pheight = ts.Height() - 1
}

// Since we're simulating a future message, pretend we're applying it in the "next" tipset
vmHeight := pheight + 1
bstate := ts.ParentState()

// Run the (not expensive) migration.
Expand All @@ -72,16 +80,22 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
return nil, fmt.Errorf("failed to handle fork: %w", err)
}

filVested, err := sm.GetFilVested(ctx, vmHeight)
if err != nil {
return nil, err
}

vmopt := &vm.VMOpts{
StateBase: bstate,
Epoch: pheight + 1,
Epoch: vmHeight,
Rand: rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion),
Bstore: sm.cs.StateBlockstore(),
Actors: sm.tsExec.NewActorRegistry(),
Syscalls: sm.Syscalls,
CircSupplyCalc: sm.GetVMCirculatingSupply,
NetworkVersion: sm.GetNetworkVersion(ctx, pheight+1),
BaseFee: types.NewInt(0),
FilVested: filVested,
LookbackState: LookbackStateGetterForTipset(sm, ts),
}

Expand Down Expand Up @@ -112,7 +126,12 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
)
}

fromActor, err := vmi.StateTree().GetActor(msg.From)
stTree, err := sm.StateTree(bstate)
if err != nil {
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}

fromActor, err := stTree.GetActor(msg.From)
if err != nil {
return nil, xerrors.Errorf("call raw get actor: %s", err)
}
Expand Down Expand Up @@ -175,13 +194,16 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
}
}

state, _, err := sm.TipSetState(ctx, ts)
// Since we're simulating a future message, pretend we're applying it in the "next" tipset
vmHeight := ts.Height() + 1

stateCid, _, err := sm.TipSetState(ctx, ts)
if err != nil {
return nil, xerrors.Errorf("computing tipset state: %w", err)
}

// Technically, the tipset we're passing in here should be ts+1, but that may not exist.
state, err = sm.HandleStateForks(ctx, state, ts.Height(), nil, ts)
stateCid, err = sm.HandleStateForks(ctx, stateCid, ts.Height(), nil, ts)
if err != nil {
return nil, fmt.Errorf("failed to handle fork: %w", err)
}
Expand All @@ -196,16 +218,23 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
)
}

filVested, err := sm.GetFilVested(ctx, vmHeight)
if err != nil {
return nil, err
}

buffStore := blockstore.NewBuffered(sm.cs.StateBlockstore())
vmopt := &vm.VMOpts{
StateBase: state,
Epoch: ts.Height() + 1,
StateBase: stateCid,
Epoch: vmHeight,
Rand: r,
Bstore: sm.cs.StateBlockstore(),
Bstore: buffStore,
Actors: sm.tsExec.NewActorRegistry(),
Syscalls: sm.Syscalls,
CircSupplyCalc: sm.GetVMCirculatingSupply,
NetworkVersion: sm.GetNetworkVersion(ctx, ts.Height()+1),
BaseFee: ts.Blocks()[0].ParentBaseFee,
FilVested: filVested,
LookbackState: LookbackStateGetterForTipset(sm, ts),
}
vmi, err := sm.newVM(ctx, vmopt)
Expand All @@ -219,7 +248,19 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
}
}

fromActor, err := vmi.StateTree().GetActor(msg.From)
// We flush to get the VM's view of the state tree after applying the above messages
// This is needed to get the correct nonce from the actor state to match the VM
stateCid, err = vmi.Flush(ctx)
if err != nil {
return nil, xerrors.Errorf("flushing vm: %w", err)
}

stTree, err := state.LoadStateTree(cbor.NewCborStore(buffStore), stateCid)
if err != nil {
return nil, xerrors.Errorf("loading state tree: %w", err)
}

fromActor, err := stTree.GetActor(msg.From)
if err != nil {
return nil, xerrors.Errorf("call raw get actor: %s", err)
}
Expand Down
12 changes: 6 additions & 6 deletions chain/stmgr/forks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func TestForkHeightTriggers(t *testing.T) {
inv := filcns.NewActorRegistry()
inv.Register(nil, testActor{})

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (*vm.VM, error) {
nvm, err := vm.NewVM(ctx, vmopt)
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -287,8 +287,8 @@ func testForkRefuseCall(t *testing.T, nullsBefore, nullsAfter int) {
inv := filcns.NewActorRegistry()
inv.Register(nil, testActor{})

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (*vm.VM, error) {
nvm, err := vm.NewVM(ctx, vmopt)
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -508,8 +508,8 @@ func TestForkPreMigration(t *testing.T) {
inv := filcns.NewActorRegistry()
inv.Register(nil, testActor{})

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (*vm.VM, error) {
nvm, err := vm.NewVM(ctx, vmopt)
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type StateManager struct {
compWait map[string]chan struct{}
stlk sync.Mutex
genesisMsigLk sync.Mutex
newVM func(context.Context, *vm.VMOpts) (*vm.VM, error)
newVM func(context.Context, *vm.VMOpts) (vm.Interface, error)
Syscalls vm.SyscallBuilder
preIgnitionVesting []msig0.State
postIgnitionVesting []msig0.State
Expand Down Expand Up @@ -347,12 +347,12 @@ func (sm *StateManager) ValidateChain(ctx context.Context, ts *types.TipSet) err
return nil
}

func (sm *StateManager) SetVMConstructor(nvm func(context.Context, *vm.VMOpts) (*vm.VM, error)) {
func (sm *StateManager) SetVMConstructor(nvm func(context.Context, *vm.VMOpts) (vm.Interface, error)) {
sm.newVM = nvm
}

func (sm *StateManager) VMConstructor() func(context.Context, *vm.VMOpts) (*vm.VM, error) {
return func(ctx context.Context, opts *vm.VMOpts) (*vm.VM, error) {
func (sm *StateManager) VMConstructor() func(context.Context, *vm.VMOpts) (vm.Interface, error) {
return func(ctx context.Context, opts *vm.VMOpts) (vm.Interface, error) {
return sm.newVM(ctx, opts)
}
}
Expand Down
53 changes: 28 additions & 25 deletions chain/stmgr/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,32 @@ func (sm *StateManager) setupPostCalicoVesting(ctx context.Context) error {
// 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).
func (sm *StateManager) GetFilVested(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (abi.TokenAmount, error) {
func (sm *StateManager) GetFilVested(ctx context.Context, height abi.ChainEpoch) (abi.TokenAmount, error) {
vf := big.Zero()

sm.genesisMsigLk.Lock()
defer sm.genesisMsigLk.Unlock()

// TODO: combine all this?
if sm.preIgnitionVesting == nil || sm.genesisPledge.IsZero() || sm.genesisMarketFunds.IsZero() {
err := sm.setupGenesisVestingSchedule(ctx)
if err != nil {
return vf, xerrors.Errorf("failed to setup pre-ignition vesting schedule: %w", err)
}
}
if sm.postIgnitionVesting == nil {
err := sm.setupPostIgnitionVesting(ctx)
if err != nil {
return vf, xerrors.Errorf("failed to setup post-ignition vesting schedule: %w", err)
}
}
if sm.postCalicoVesting == nil {
err := sm.setupPostCalicoVesting(ctx)
if err != nil {
return vf, xerrors.Errorf("failed to setup post-calico vesting schedule: %w", err)
}
}

if height <= build.UpgradeIgnitionHeight {
for _, v := range sm.preIgnitionVesting {
au := big.Sub(v.InitialBalance, v.AmountLocked(height))
Expand Down Expand Up @@ -282,7 +306,7 @@ func getFilPowerLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmoun
return pst.TotalLocked()
}

func (sm *StateManager) GetFilLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {
func GetFilLocked(ctx context.Context, st *state.StateTree) (abi.TokenAmount, error) {

filMarketLocked, err := getFilMarketLocked(ctx, st)
if err != nil {
Expand Down Expand Up @@ -316,28 +340,7 @@ func (sm *StateManager) GetVMCirculatingSupply(ctx context.Context, height abi.C
}

func (sm *StateManager) GetVMCirculatingSupplyDetailed(ctx context.Context, height abi.ChainEpoch, st *state.StateTree) (api.CirculatingSupply, error) {
sm.genesisMsigLk.Lock()
defer sm.genesisMsigLk.Unlock()
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)
if err != nil {
return api.CirculatingSupply{}, xerrors.Errorf("failed to setup post-ignition vesting schedule: %w", err)
}
}
if sm.postCalicoVesting == nil {
err := sm.setupPostCalicoVesting(ctx)
if err != nil {
return api.CirculatingSupply{}, xerrors.Errorf("failed to setup post-calico vesting schedule: %w", err)
}
}

filVested, err := sm.GetFilVested(ctx, height, st)
filVested, err := sm.GetFilVested(ctx, height)
if err != nil {
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filVested: %w", err)
}
Expand All @@ -360,7 +363,7 @@ func (sm *StateManager) GetVMCirculatingSupplyDetailed(ctx context.Context, heig
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filBurnt: %w", err)
}

filLocked, err := sm.GetFilLocked(ctx, st)
filLocked, err := GetFilLocked(ctx, st)
if err != nil {
return api.CirculatingSupply{}, xerrors.Errorf("failed to calculate filLocked: %w", err)
}
Expand Down
Loading

0 comments on commit 90d78f9

Please sign in to comment.