Skip to content

Commit

Permalink
VM: Circ supply should be constant per epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Dec 29, 2021
1 parent 1030cd4 commit b4abbff
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 30 deletions.
3 changes: 2 additions & 1 deletion pkg/consensus/call_invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consensus
import (
"context"
"fmt"

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

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *Expected) CallWithGas(ctx context.Context, msg *types.UnsignedMessage,
Fork: c.fork,
}

vmi, err := vm.NewVM(vmOption)
vmi, err := vm.NewVM(ctx, vmOption)
if err != nil {
return nil, err
}
Expand Down
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 @@ -489,9 +489,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 @@ -502,7 +506,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 @@ -112,7 +112,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
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
1 change: 1 addition & 0 deletions pkg/vm/vmcontext/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vmcontext

import (
"context"

acrypto "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/venus/pkg/state"
Expand Down
31 changes: 24 additions & 7 deletions pkg/vm/vmcontext/vmcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type VM struct {
debugger *VMDebugMsg
vmOption VmOption

baseCircSupply abi.TokenAmount

State tree.Tree
}

Expand Down Expand Up @@ -90,7 +92,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 @@ -108,13 +110,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 @@ -763,6 +771,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
19 changes: 7 additions & 12 deletions tools/conformance/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package conformance

import (
"context"
"github.com/filecoin-project/venus/pkg/util/ffiwrapper/impl"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
gobig "math/big"
"os"

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

"github.com/filecoin-project/venus/pkg/vm/gas"
cbor "github.com/ipfs/go-ipld-cbor"

Expand Down Expand Up @@ -113,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 @@ -136,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 @@ -292,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
7 changes: 4 additions & 3 deletions tools/gengen/util/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"context"
"fmt"
"github.com/filecoin-project/venus/pkg/util/ffiwrapper/impl"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
"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/go-address"
Expand Down Expand Up @@ -97,7 +98,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

0 comments on commit b4abbff

Please sign in to comment.