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: Circ supply should be constant per epoch #4656

Merged
merged 1 commit into from
Dec 31, 2021
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
4 changes: 2 additions & 2 deletions pkg/consensus/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p *DefaultProcessor) ProcessTipSet(ctx context.Context,
parentEpoch = parent.Height()
}

v, err := vm.NewVM(vmOption)
v, err := vm.NewVM(ctx, vmOption)
if err != nil {
return cid.Undef, nil, err
}
Expand All @@ -78,7 +78,7 @@ func (p *DefaultProcessor) ProcessImplicitMessage(ctx context.Context, msg *type
span.AddAttributes(trace.StringAttribute("message", msg.String()))
defer tracing.AddErrorEndSpan(ctx, span, &err)

v, err := vm.NewVM(vmOption)
v, err := vm.NewVM(ctx, vmOption)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/gen/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,13 @@ func VerifyPreSealedData(ctx context.Context, cs *chain.Store, stateroot cid.Cid
return nv
}

csc := func(context.Context, abi.ChainEpoch, tree.Tree) (abi.TokenAmount, error) {
return big.Zero(), nil
}

gasPriceSchedule := gas.NewPricesSchedule(para)
vmopt := vm.VmOption{
CircSupplyCalculator: nil,
CircSupplyCalculator: csc,
NtwkVersionGetter: genesisNetworkVersion,
Rnd: &fakeRand{},
BaseFee: big.NewInt(0),
Expand All @@ -501,7 +505,7 @@ func VerifyPreSealedData(ctx context.Context, cs *chain.Store, stateroot cid.Cid
GasPriceSchedule: gasPriceSchedule,
}

vm, err := vm.NewVM(vmopt)
vm, err := vm.NewVM(ctx, vmopt)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func SetupStorageMiners(ctx context.Context, cs *chain.Store, sroot cid.Cid, min
GasPriceSchedule: gasPirceSchedule,
}

vmi, err := vm.NewVM(vmopt)
vmi, err := vm.NewVM(ctx, vmopt)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/statemanger/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Stmgr) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs [
Fork: s.fork,
}

vmi, err := vm.NewVM(vmOption)
vmi, err := vm.NewVM(ctx, vmOption)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (s *Stmgr) Call(ctx context.Context, msg *types.Message, ts *types.TipSet)
SysCallsImpl: s.syscallsImpl,
}

v, err := vm.NewVM(vmOption)
v, err := vm.NewVM(ctx, vmOption)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/vm/vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package vm

import (
"context"

"github.com/filecoin-project/venus/pkg/vm/dispatch"
"github.com/filecoin-project/venus/pkg/vm/register"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
Expand All @@ -24,12 +26,12 @@ type FakeSyscalls = vmcontext.FakeSyscalls
type ChainRandomness = vmcontext.HeadChainRandomness

// NewVM creates a new VM interpreter.
func NewVM(option VmOption) (Interpreter, error) {
func NewVM(ctx context.Context, option VmOption) (Interpreter, error) {
if option.ActorCodeLoader == nil {
option.ActorCodeLoader = &DefaultActors
}

return vmcontext.NewVM(option.ActorCodeLoader, option)
return vmcontext.NewVM(ctx, option.ActorCodeLoader, option)
}

// DefaultActors is a code loader with the built-in actors that come with the system.
Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/vmcontext/syscallsStateView.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func (vm *syscallsStateView) GetNtwkVersion(ctx context.Context, ce abi.ChainEpo

//GetNtwkVersion get network version
func (vm *syscallsStateView) TotalFilCircSupply(height abi.ChainEpoch, st tree.Tree) (abi.TokenAmount, error) {
return vm.vmOption.CircSupplyCalculator(context.TODO(), height, st)
return vm.GetCircSupply(context.TODO())
}
31 changes: 24 additions & 7 deletions pkg/vm/vmcontext/vmcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type VM struct {
debugger *VMDebugMsg
vmOption VmOption

baseCircSupply abi.TokenAmount

State tree.Tree
}

Expand Down Expand Up @@ -91,7 +93,7 @@ var _ VMInterpreter = (*VM)(nil)

// NewVM creates a new runtime for executing messages.
// Dragons: change To take a root and the store, build the tree internally
func NewVM(actorImpls ActorImplLookup, vmOption VmOption) (*VM, error) {
func NewVM(ctx context.Context, actorImpls ActorImplLookup, vmOption VmOption) (*VM, error) {
buf := blockstoreutil.NewBufferedBstore(vmOption.Bsstore)
cst := cbor.NewCborStore(buf)
var st tree.Tree
Expand All @@ -109,13 +111,19 @@ func NewVM(actorImpls ActorImplLookup, vmOption VmOption) (*VM, error) {
}
}

baseCirc, err := vmOption.CircSupplyCalculator(ctx, vmOption.Epoch, st)
if err != nil {
return nil, err
}

return &VM{
context: context.Background(),
actorImpls: actorImpls,
bsstore: buf,
store: cst,
State: st,
vmOption: vmOption,
context: context.Background(),
actorImpls: actorImpls,
bsstore: buf,
store: cst,
State: st,
vmOption: vmOption,
baseCircSupply: baseCirc,
// loaded during execution
// currentEpoch: ..,
}, nil
Expand Down Expand Up @@ -764,6 +772,15 @@ func (vm *VM) StateTree() tree.Tree {
return vm.State
}

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.NtwkVersionGetter(ctx, vm.vmOption.Epoch) <= network.Version14 {
return vm.vmOption.CircSupplyCalculator(ctx, vm.vmOption.Epoch, vm.State)
}

return vm.baseCircSupply, nil
}

func deductFunds(act *types.Actor, amt abi.TokenAmount) error {
if act.Balance.LessThan(amt) {
return fmt.Errorf("not enough funds")
Expand Down
14 changes: 4 additions & 10 deletions tools/conformance/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,9 @@ func (d *Driver) ExecuteTipset(bs blockstore.Blockstore, chainDs ds.Batching, pr
return nil, err
}
var (
caculator = chain.NewCirculatingSupplyCalculator(bs, cid.Undef, mainNetParams.Network.ForkUpgradeParam)

vmOption = vm.VmOption{
CircSupplyCalculator: func(ctx context.Context, epoch abi.ChainEpoch, tree tree.Tree) (abi.TokenAmount, error) {
dertail, err := caculator.GetCirculatingSupplyDetailed(ctx, epoch, tree)
if err != nil {
return abi.TokenAmount{}, err
}
return dertail.FilCirculating, nil
CircSupplyCalculator: func(context.Context, abi.ChainEpoch, tree.Tree) (abi.TokenAmount, error) {
return big.Zero(), nil
},
LookbackStateGetter: vmcontext.LookbackStateGetterForTipset(chainStore, chainFork, nil),
NtwkVersionGetter: chainFork.GetNtwkVersion,
Expand All @@ -137,7 +131,7 @@ func (d *Driver) ExecuteTipset(bs blockstore.Blockstore, chainDs ds.Batching, pr
}
)

lvm, err := vm.NewVM(vmOption)
lvm, err := vm.NewVM(context.Background(), vmOption)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,7 +287,7 @@ func (d *Driver) ExecuteMessage(bs blockstore.Blockstore, params ExecuteMessageP
}
)

lvm, err := vm.NewVM(vmOption)
lvm, err := vm.NewVM(context.TODO(), vmOption)
if err != nil {
return nil, cid.Undef, err
}
Expand Down
5 changes: 4 additions & 1 deletion tools/gengen/util/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"io"
mrand "math/rand"

"github.com/filecoin-project/venus/pkg/util/ffiwrapper/impl"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"

"github.com/filecoin-project/venus/pkg/fork"
"github.com/filecoin-project/venus/pkg/util/ffiwrapper/impl"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
Expand Down Expand Up @@ -97,7 +100,7 @@ func NewGenesisGenerator(bs blockstore.Blockstore) *GenesisGenerator {
SysCallsImpl: syscallImpl,
Fork: chainFork,
}
vm, err := vm.NewVM(vmOption)
vm, err := vm.NewVM(context.Background(), vmOption)
if err != nil {
panic(xerrors.Errorf("create state error, should never come here"))
}
Expand Down