Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: fix network version not correct #4712

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions pkg/consensus/expected.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,17 @@ func (c *Expected) RunStateTransition(ctx context.Context, ts *types.TipSet) (ci
}
return dertail.FilCirculating, nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, c.chainState, c.fork, ts),
NetworkVersion: c.fork.GetNetworkVersion(ctx, ts.At(0).Height),
Rnd: NewHeadRandomness(c.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Fork: c.fork,
Epoch: ts.At(0).Height,
GasPriceSchedule: c.gasPirceSchedule,
Bsstore: c.bstore,
PRoot: ts.At(0).ParentStateRoot,
SysCallsImpl: c.syscallsImpl,
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, c.chainState, c.fork, ts),
NetworkVersion: c.fork.GetNetworkVersion(ctx, ts.At(0).Height),
NetworkVersionGetter: c.fork.GetNetworkVersion,
Rnd: NewHeadRandomness(c.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Fork: c.fork,
Epoch: ts.At(0).Height,
GasPriceSchedule: c.gasPirceSchedule,
Bsstore: c.bstore,
PRoot: ts.At(0).ParentStateRoot,
SysCallsImpl: c.syscallsImpl,
}
root, receipts, err := c.processor.ProcessTipSet(ctx, pts, ts, blockMessageInfo, vmOption)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fork/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"encoding/binary"
"errors"
"github.com/docker/go-units"
"runtime"
"sort"
"sync"
"time"

"github.com/docker/go-units"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
Expand Down
17 changes: 10 additions & 7 deletions pkg/gen/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,16 @@ func VerifyPreSealedData(ctx context.Context, cs *chain.Store, stateroot cid.Cid
vmopt := vm.VmOption{
CircSupplyCalculator: csc,
NetworkVersion: nv,
Rnd: &fakeRand{},
BaseFee: big.NewInt(0),
Epoch: 0,
PRoot: stateroot,
Bsstore: cs.Blockstore(),
SysCallsImpl: mkFakedSigSyscalls(syscalls),
GasPriceSchedule: gasPriceSchedule,
NetworkVersionGetter: func(ctx context.Context, height abi.ChainEpoch) network.Version {
return nv
},
Rnd: &fakeRand{},
BaseFee: big.NewInt(0),
Epoch: 0,
PRoot: stateroot,
Bsstore: cs.Blockstore(),
SysCallsImpl: mkFakedSigSyscalls(syscalls),
GasPriceSchedule: gasPriceSchedule,
}

vm, err := vm.NewVM(ctx, vmopt)
Expand Down
17 changes: 10 additions & 7 deletions pkg/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ func SetupStorageMiners(ctx context.Context, cs *chain.Store, sroot cid.Cid, min
vmopt := vm.VmOption{
CircSupplyCalculator: csc,
NetworkVersion: nv,
Rnd: &fakeRand{},
BaseFee: big.NewInt(0),
Epoch: 0,
PRoot: sroot,
Bsstore: cs.Blockstore(),
SysCallsImpl: mkFakedSigSyscalls(syscalls),
GasPriceSchedule: gasPirceSchedule,
NetworkVersionGetter: func(ctx context.Context, height abi.ChainEpoch) network.Version {
return nv
},
Rnd: &fakeRand{},
BaseFee: big.NewInt(0),
Epoch: 0,
PRoot: sroot,
Bsstore: cs.Blockstore(),
SysCallsImpl: mkFakedSigSyscalls(syscalls),
GasPriceSchedule: gasPirceSchedule,
}

vmi, err := vm.NewVM(ctx, vmopt)
Expand Down
4 changes: 1 addition & 3 deletions pkg/state/tree/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,7 @@ func (st *State) At(root Root) error {
return err
}

st.root = newState.root
st.version = newState.version
st.info = newState.info
*st = *newState
return nil
}
func Diff(oldTree, newTree *State) (map[string]types.Actor, error) {
Expand Down
42 changes: 22 additions & 20 deletions pkg/statemanger/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,17 @@ func (s *Stmgr) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs [
}
return cs.FilCirculating, nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, s.cs, s.fork, ts),
NetworkVersion: s.fork.GetNetworkVersion(ctx, ts.Height()+1),
Rnd: consensus.NewHeadRandomness(s.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Epoch: ts.Height() + 1,
GasPriceSchedule: s.gasSchedule,
PRoot: stateRoot,
Bsstore: s.cs.Blockstore(),
SysCallsImpl: s.syscallsImpl,
Fork: s.fork,
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, s.cs, s.fork, ts),
NetworkVersion: s.fork.GetNetworkVersion(ctx, ts.Height()+1),
NetworkVersionGetter: s.fork.GetNetworkVersion,
Rnd: consensus.NewHeadRandomness(s.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Epoch: ts.Height() + 1,
GasPriceSchedule: s.gasSchedule,
PRoot: stateRoot,
Bsstore: s.cs.Blockstore(),
SysCallsImpl: s.syscallsImpl,
Fork: s.fork,
}

vmi, err := vm.NewVM(ctx, vmOption)
Expand Down Expand Up @@ -196,16 +197,17 @@ func (s *Stmgr) Call(ctx context.Context, msg *types.Message, ts *types.TipSet)
}
return dertail.FilCirculating, nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, s.cs, s.fork, ts),
NetworkVersion: s.fork.GetNetworkVersion(ctx, pheight+1),
Rnd: consensus.NewHeadRandomness(s.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Epoch: pheight + 1,
GasPriceSchedule: s.gasSchedule,
Fork: s.fork,
PRoot: ts.At(0).ParentStateRoot,
Bsstore: s.cs.Blockstore(),
SysCallsImpl: s.syscallsImpl,
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, s.cs, s.fork, ts),
NetworkVersion: s.fork.GetNetworkVersion(ctx, pheight+1),
NetworkVersionGetter: s.fork.GetNetworkVersion,
Rnd: consensus.NewHeadRandomness(s.rnd, ts.Key()),
BaseFee: ts.At(0).ParentBaseFee,
Epoch: pheight + 1,
GasPriceSchedule: s.gasSchedule,
Fork: s.fork,
PRoot: ts.At(0).ParentStateRoot,
Bsstore: s.cs.Blockstore(),
SysCallsImpl: s.syscallsImpl,
}

v, err := vm.NewVM(ctx, vmOption)
Expand Down
2 changes: 2 additions & 0 deletions pkg/vm/vmcontext/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import (
type ExecCallBack func(cid.Cid, VmMessage, *Ret) error
type CircSupplyCalculator func(context.Context, abi.ChainEpoch, tree.Tree) (abi.TokenAmount, error)
type LookbackStateGetter func(context.Context, abi.ChainEpoch) (*state.View, error)
type NetworkVersionGetter func(ctx context.Context, height abi.ChainEpoch) network.Version

type VmOption struct { //nolint
CircSupplyCalculator CircSupplyCalculator
LookbackStateGetter LookbackStateGetter
NetworkVersion network.Version
NetworkVersionGetter NetworkVersionGetter
Rnd HeadChainRandomness
BaseFee abi.TokenAmount
Fork fork.IFork
Expand Down
8 changes: 6 additions & 2 deletions pkg/vm/vmcontext/vmcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (vm *VM) ApplyTipSetMessages(blocks []types.BlockMessagesInfo, ts *types.Ti
pstate, _ := vm.State.Flush(vm.context)
for i := parentEpoch; i < epoch; i++ {
if i > parentEpoch {
// fix: https://github.com/filecoin-project/lotus/pull/7966
vm.vmOption.NetworkVersion = vm.vmOption.NetworkVersionGetter(vm.context, i)
// run cron for null rounds if any
cronMessage := makeCronTickMessage()
ret, err := vm.applyImplicitMessage(cronMessage)
Expand Down Expand Up @@ -249,6 +251,9 @@ func (vm *VM) ApplyTipSetMessages(blocks []types.BlockMessagesInfo, ts *types.Ti
return cid.Undef, nil, xerrors.Errorf("error advancing vm an epoch: %w", err)
}
}
// as above
vm.vmOption.NetworkVersion = vm.vmOption.NetworkVersionGetter(vm.context, epoch)

vmlog.Debugf("process tipset fork: %v\n", time.Since(toProcessTipset).Milliseconds())
// create message tracker
// Note: the same message could have been included by more than one miner
Expand Down Expand Up @@ -309,7 +314,6 @@ func (vm *VM) ApplyTipSetMessages(blocks []types.BlockMessagesInfo, ts *types.Ti
vm.debugger.Println(string(tracesBytes))
}
}

// Pay block reward.
// Dragons: missing final protocol design on if/how To determine the nominal power
rewardMessage := makeBlockRewardMessage(blkInfo.Block.Miner, minerPenaltyTotal, minerGasRewardTotal, blkInfo.Block.ElectionProof.WinCount)
Expand Down Expand Up @@ -853,7 +857,7 @@ func (vm *VM) StateTree() tree.Tree {
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.vmOption.NetworkVersion <= network.Version14 {
return vm.vmOption.CircSupplyCalculator(ctx, vm.vmOption.Epoch, vm.State)
return vm.vmOption.CircSupplyCalculator(ctx, vm.currentEpoch, vm.State)
}

return vm.baseCircSupply, nil
Expand Down
44 changes: 23 additions & 21 deletions tools/conformance/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ func (d *Driver) ExecuteTipset(bs blockstore.Blockstore, chainDs ds.Batching, pr
CircSupplyCalculator: func(context.Context, abi.ChainEpoch, tree.Tree) (abi.TokenAmount, error) {
return big.Zero(), nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, chainStore, chainFork, nil),
NetworkVersion: chainFork.GetNetworkVersion(ctx, execEpoch),
Rnd: NewFixedRand(),
BaseFee: big.NewFromGo(&tipset.BaseFee),
Fork: chainFork,
Epoch: execEpoch,
GasPriceSchedule: gas.NewPricesSchedule(mainNetParams.Network.ForkUpgradeParam),
PRoot: preroot,
Bsstore: bs,
SysCallsImpl: syscalls,
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, chainStore, chainFork, nil),
NetworkVersion: chainFork.GetNetworkVersion(ctx, execEpoch),
NetworkVersionGetter: chainFork.GetNetworkVersion,
Rnd: NewFixedRand(),
BaseFee: big.NewFromGo(&tipset.BaseFee),
Fork: chainFork,
Epoch: execEpoch,
GasPriceSchedule: gas.NewPricesSchedule(mainNetParams.Network.ForkUpgradeParam),
PRoot: preroot,
Bsstore: bs,
SysCallsImpl: syscalls,
}
)

Expand Down Expand Up @@ -302,17 +303,18 @@ func (d *Driver) ExecuteMessage(bs blockstore.Blockstore, params ExecuteMessageP
CircSupplyCalculator: func(ctx context.Context, epoch abi.ChainEpoch, tree tree.Tree) (abi.TokenAmount, error) {
return params.CircSupply, nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, chainStore, chainFork, nil),
NetworkVersion: params.NetworkVersion,
Rnd: params.Rand,
BaseFee: params.BaseFee,
Fork: chainFork,
ActorCodeLoader: &coderLoader,
Epoch: params.Epoch,
GasPriceSchedule: gas.NewPricesSchedule(mainNetParams.Network.ForkUpgradeParam),
PRoot: params.Preroot,
Bsstore: bs,
SysCallsImpl: syscalls,
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(ctx, chainStore, chainFork, nil),
NetworkVersion: params.NetworkVersion,
NetworkVersionGetter: chainFork.GetNetworkVersion,
Rnd: params.Rand,
BaseFee: params.BaseFee,
Fork: chainFork,
ActorCodeLoader: &coderLoader,
Epoch: params.Epoch,
GasPriceSchedule: gas.NewPricesSchedule(mainNetParams.Network.ForkUpgradeParam),
PRoot: params.Preroot,
Bsstore: bs,
SysCallsImpl: syscalls,
}
)

Expand Down
3 changes: 3 additions & 0 deletions tools/gengen/util/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func NewGenesisGenerator(bs blockstore.Blockstore) *GenesisGenerator {
SysCallsImpl: syscallImpl,
Fork: chainFork,
}
vmOption.NetworkVersionGetter = func(ctx context.Context, height abi.ChainEpoch) network.Version {
return vmOption.NetworkVersion
}
vm, err := vm.NewVM(context.Background(), vmOption)
if err != nil {
panic(xerrors.Errorf("create state error, should never come here"))
Expand Down