From 670bd993f3c94286b33d7d4e0073f8e1404d818d Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Fri, 17 Dec 2021 19:05:59 -0500 Subject: [PATCH] Refactor: VM: Remove the NetworkVersionGetter --- chain/consensus/filcns/compute_state.go | 2 +- chain/gen/genesis/genesis.go | 6 ++---- chain/gen/genesis/miners.go | 6 ++---- chain/stmgr/call.go | 4 ++-- chain/stmgr/utils.go | 2 +- chain/vm/invoker_test.go | 9 ++------- chain/vm/runtime.go | 2 +- chain/vm/vm.go | 20 ++++++++----------- .../simulation/blockbuilder/blockbuilder.go | 2 +- conformance/driver.go | 6 +++--- 10 files changed, 23 insertions(+), 36 deletions(-) diff --git a/chain/consensus/filcns/compute_state.go b/chain/consensus/filcns/compute_state.go index ba4cc0335dd..3c70f61db15 100644 --- a/chain/consensus/filcns/compute_state.go +++ b/chain/consensus/filcns/compute_state.go @@ -101,7 +101,7 @@ func (t *TipSetExecutor) ApplyBlocks(ctx context.Context, sm *stmgr.StateManager Actors: NewActorRegistry(), Syscalls: sm.Syscalls, CircSupplyCalc: sm.GetVMCirculatingSupply, - NtwkVersion: sm.GetNetworkVersion, + NetworkVersion: sm.GetNetworkVersion(ctx, epoch), BaseFee: baseFee, LookbackState: stmgr.LookbackStateGetterForTipset(sm, ts), } diff --git a/chain/gen/genesis/genesis.go b/chain/gen/genesis/genesis.go index 64fd76d2b3f..6ab101e7893 100644 --- a/chain/gen/genesis/genesis.go +++ b/chain/gen/genesis/genesis.go @@ -491,10 +491,8 @@ func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, sys vm.Sysca Actors: filcns.NewActorRegistry(), Syscalls: mkFakedSigSyscalls(sys), CircSupplyCalc: csc, - NtwkVersion: func(_ context.Context, _ abi.ChainEpoch) network.Version { - return nv - }, - BaseFee: types.NewInt(0), + NetworkVersion: nv, + BaseFee: types.NewInt(0), } vm, err := vm.NewVM(ctx, &vmopt) if err != nil { diff --git a/chain/gen/genesis/miners.go b/chain/gen/genesis/miners.go index 101f1d3b509..2749181470c 100644 --- a/chain/gen/genesis/miners.go +++ b/chain/gen/genesis/miners.go @@ -94,10 +94,8 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal Actors: filcns.NewActorRegistry(), Syscalls: mkFakedSigSyscalls(sys), CircSupplyCalc: csc, - NtwkVersion: func(_ context.Context, _ abi.ChainEpoch) network.Version { - return nv - }, - BaseFee: types.NewInt(0), + NetworkVersion: nv, + BaseFee: types.NewInt(0), } vm, err := vm.NewVM(ctx, vmopt) diff --git a/chain/stmgr/call.go b/chain/stmgr/call.go index 7f76628e826..31639701dad 100644 --- a/chain/stmgr/call.go +++ b/chain/stmgr/call.go @@ -80,7 +80,7 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types. Actors: sm.tsExec.NewActorRegistry(), Syscalls: sm.Syscalls, CircSupplyCalc: sm.GetVMCirculatingSupply, - NtwkVersion: sm.GetNetworkVersion, + NetworkVersion: sm.GetNetworkVersion(ctx, pheight+1), BaseFee: types.NewInt(0), LookbackState: LookbackStateGetterForTipset(sm, ts), } @@ -204,7 +204,7 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri Actors: sm.tsExec.NewActorRegistry(), Syscalls: sm.Syscalls, CircSupplyCalc: sm.GetVMCirculatingSupply, - NtwkVersion: sm.GetNetworkVersion, + NetworkVersion: sm.GetNetworkVersion(ctx, ts.Height()+1), BaseFee: ts.Blocks()[0].ParentBaseFee, LookbackState: LookbackStateGetterForTipset(sm, ts), } diff --git a/chain/stmgr/utils.go b/chain/stmgr/utils.go index 698ffc13f66..2a84c777b3a 100644 --- a/chain/stmgr/utils.go +++ b/chain/stmgr/utils.go @@ -88,7 +88,7 @@ func ComputeState(ctx context.Context, sm *StateManager, height abi.ChainEpoch, Actors: sm.tsExec.NewActorRegistry(), Syscalls: sm.Syscalls, CircSupplyCalc: sm.GetVMCirculatingSupply, - NtwkVersion: sm.GetNetworkVersion, + NetworkVersion: sm.GetNetworkVersion(ctx, height), BaseFee: ts.Blocks()[0].ParentBaseFee, LookbackState: LookbackStateGetterForTipset(sm, ts), } diff --git a/chain/vm/invoker_test.go b/chain/vm/invoker_test.go index 8499f001a59..fb9910ecdd0 100644 --- a/chain/vm/invoker_test.go +++ b/chain/vm/invoker_test.go @@ -1,7 +1,6 @@ package vm import ( - "context" "fmt" "io" "testing" @@ -136,9 +135,7 @@ func TestInvokerBasic(t *testing.T) { { _, aerr := code[1](&Runtime{ - vm: &VM{ntwkVersion: func(ctx context.Context, epoch abi.ChainEpoch) network.Version { - return network.Version0 - }}, + vm: &VM{networkVersion: network.Version0}, Message: &basicRtMessage{}, }, []byte{99}) if aerrors.IsFatal(aerr) { @@ -149,9 +146,7 @@ func TestInvokerBasic(t *testing.T) { { _, aerr := code[1](&Runtime{ - vm: &VM{ntwkVersion: func(ctx context.Context, epoch abi.ChainEpoch) network.Version { - return network.Version7 - }}, + vm: &VM{networkVersion: network.Version7}, Message: &basicRtMessage{}, }, []byte{99}) if aerrors.IsFatal(aerr) { diff --git a/chain/vm/runtime.go b/chain/vm/runtime.go index 844f5d2539e..0e2adc87983 100644 --- a/chain/vm/runtime.go +++ b/chain/vm/runtime.go @@ -92,7 +92,7 @@ func (rt *Runtime) BaseFee() abi.TokenAmount { } func (rt *Runtime) NetworkVersion() network.Version { - return rt.vm.GetNtwkVersion(rt.ctx, rt.CurrEpoch()) + return rt.vm.networkVersion } func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount { diff --git a/chain/vm/vm.go b/chain/vm/vm.go index c0b730774de..7e9e972ae40 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -169,7 +169,7 @@ func (vm *VM) makeRuntime(ctx context.Context, msg *types.Message, parent *Runti } vmm.From = resF - if vm.ntwkVersion(ctx, vm.blockHeight) <= network.Version3 { + if vm.networkVersion <= network.Version3 { rt.Message = &vmm } else { resT, _ := rt.ResolveAddress(msg.To) @@ -209,7 +209,7 @@ type VM struct { areg *ActorRegistry rand Rand circSupplyCalc CircSupplyCalculator - ntwkVersion NtwkVersionGetter + networkVersion network.Version baseFee abi.TokenAmount lbStateGet LookbackStateGetter baseCircSupply abi.TokenAmount @@ -225,7 +225,7 @@ type VMOpts struct { Actors *ActorRegistry Syscalls SyscallBuilder CircSupplyCalc CircSupplyCalculator - NtwkVersion NtwkVersionGetter // TODO: stebalien: In what cases do we actually need this? It seems like even when creating new networks we want to use the 'global'/build-default version getter + NetworkVersion network.Version BaseFee abi.TokenAmount LookbackState LookbackStateGetter } @@ -251,7 +251,7 @@ func NewVM(ctx context.Context, opts *VMOpts) (*VM, error) { areg: opts.Actors, rand: opts.Rand, // TODO: Probably should be a syscall circSupplyCalc: opts.CircSupplyCalc, - ntwkVersion: opts.NtwkVersion, + networkVersion: opts.NetworkVersion, Syscalls: opts.Syscalls, baseFee: opts.BaseFee, baseCircSupply: baseCirc, @@ -313,7 +313,7 @@ func (vm *VM) send(ctx context.Context, msg *types.Message, parent *Runtime, return nil, aerrors.Wrapf(err, "could not create account") } toActor = a - if vm.ntwkVersion(ctx, vm.blockHeight) <= network.Version3 { + if vm.networkVersion <= network.Version3 { // Leave the rt.Message as is } else { nmsg := Message{ @@ -340,7 +340,7 @@ func (vm *VM) send(ctx context.Context, msg *types.Message, parent *Runtime, defer rt.chargeGasSafe(newGasCharge("OnMethodInvocationDone", 0, 0)) if types.BigCmp(msg.Value, types.NewInt(0)) != 0 { - if err := vm.transfer(msg.From, msg.To, msg.Value, vm.ntwkVersion(ctx, vm.blockHeight)); err != nil { + if err := vm.transfer(msg.From, msg.To, msg.Value, vm.networkVersion); err != nil { return nil, aerrors.Wrap(err, "failed to transfer funds") } } @@ -617,7 +617,7 @@ func (vm *VM) ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (*ApplyRet, } func (vm *VM) ShouldBurn(ctx context.Context, st *state.StateTree, msg *types.Message, errcode exitcode.ExitCode) (bool, error) { - if vm.ntwkVersion(ctx, vm.blockHeight) <= network.Version12 { + if vm.networkVersion <= network.Version12 { // Check to see if we should burn funds. We avoid burning on successful // window post. This won't catch _indirect_ window post calls, but this // is the best we can get for now. @@ -855,13 +855,9 @@ func (vm *VM) SetInvoker(i *ActorRegistry) { vm.areg = i } -func (vm *VM) GetNtwkVersion(ctx context.Context, ce abi.ChainEpoch) network.Version { - return vm.ntwkVersion(ctx, ce) -} - func (vm *VM) GetCircSupply(ctx context.Context) (abi.TokenAmount, error) { // Before v15, this was recalculated on each invocation as the state tree was mutated - if vm.GetNtwkVersion(ctx, vm.blockHeight) <= network.Version14 { + if vm.networkVersion <= network.Version14 { return vm.circSupplyCalc(ctx, vm.blockHeight, vm.cstate) } diff --git a/cmd/lotus-sim/simulation/blockbuilder/blockbuilder.go b/cmd/lotus-sim/simulation/blockbuilder/blockbuilder.go index 360368b8222..fb822eb6e77 100644 --- a/cmd/lotus-sim/simulation/blockbuilder/blockbuilder.go +++ b/cmd/lotus-sim/simulation/blockbuilder/blockbuilder.go @@ -88,7 +88,7 @@ func NewBlockBuilder(ctx context.Context, logger *zap.SugaredLogger, sm *stmgr.S Actors: filcns.NewActorRegistry(), Syscalls: sm.VMSys(), CircSupplyCalc: sm.GetVMCirculatingSupply, - NtwkVersion: sm.GetNetworkVersion, + NetworkVersion: sm.GetNetworkVersion(ctx, parentTs.Height()+1), BaseFee: abi.NewTokenAmount(0), LookbackState: stmgr.LookbackStateGetterForTipset(sm, parentTs), } diff --git a/conformance/driver.go b/conformance/driver.go index 9bca9c1fbfc..6df7b911549 100644 --- a/conformance/driver.go +++ b/conformance/driver.go @@ -225,9 +225,9 @@ func (d *Driver) ExecuteMessage(bs blockstore.Blockstore, params ExecuteMessageP CircSupplyCalc: func(_ context.Context, _ abi.ChainEpoch, _ *state.StateTree) (abi.TokenAmount, error) { return params.CircSupply, nil }, - Rand: params.Rand, - BaseFee: params.BaseFee, - NtwkVersion: sm.GetNetworkVersion, + Rand: params.Rand, + BaseFee: params.BaseFee, + NetworkVersion: sm.GetNetworkVersion(context.Background(), params.Epoch), } lvm, err := vm.NewVM(context.TODO(), vmOpts)