From d3dc560f53aee4eb843a3c081b44282e36c6dca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Wed, 7 Oct 2020 12:45:11 +0100 Subject: [PATCH] conformance: minor refactors. --- cmd/tvx/extract.go | 12 +++++----- conformance/driver.go | 47 +++++++++++++++++++++++++++------------ conformance/rand_fixed.go | 28 +++++++++++++++++++++++ conformance/runner.go | 20 ++++------------- conformance/stubs.go | 14 +----------- 5 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 conformance/rand_fixed.go diff --git a/cmd/tvx/extract.go b/cmd/tvx/extract.go index fef24585803..afdeb9540e3 100644 --- a/cmd/tvx/extract.go +++ b/cmd/tvx/extract.go @@ -198,8 +198,8 @@ func doExtract(ctx context.Context, fapi api.FullNode, opts extractOpts) error { Preroot: root, Epoch: execTs.Height(), Message: m, - CircSupply: &circSupplyDetail.FilCirculating, - BaseFee: &basefee, + CircSupply: circSupplyDetail.FilCirculating, + BaseFee: basefee, }) if err != nil { return fmt.Errorf("failed to execute precursor message: %w", err) @@ -229,8 +229,8 @@ func doExtract(ctx context.Context, fapi api.FullNode, opts extractOpts) error { Preroot: preroot, Epoch: execTs.Height(), Message: msg, - CircSupply: &circSupplyDetail.FilCirculating, - BaseFee: &basefee, + CircSupply: circSupplyDetail.FilCirculating, + BaseFee: basefee, }) if err != nil { return fmt.Errorf("failed to execute message: %w", err) @@ -260,8 +260,8 @@ func doExtract(ctx context.Context, fapi api.FullNode, opts extractOpts) error { Preroot: preroot, Epoch: execTs.Height(), Message: msg, - CircSupply: &circSupplyDetail.FilCirculating, - BaseFee: &basefee, + CircSupply: circSupplyDetail.FilCirculating, + BaseFee: basefee, }) if err != nil { return fmt.Errorf("failed to execute message: %w", err) diff --git a/conformance/driver.go b/conformance/driver.go index 9ced12d745a..d51798a525c 100644 --- a/conformance/driver.go +++ b/conformance/driver.go @@ -2,6 +2,7 @@ package conformance import ( "context" + gobig "math/big" "os" "github.com/filecoin-project/lotus/chain/state" @@ -14,6 +15,7 @@ import ( "github.com/filecoin-project/lotus/lib/blockstore" "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/test-vectors/schema" @@ -80,7 +82,7 @@ type ExecuteTipsetResult struct { func (d *Driver) ExecuteTipset(bs blockstore.Blockstore, ds ds.Batching, preroot cid.Cid, parentEpoch abi.ChainEpoch, tipset *schema.Tipset) (*ExecuteTipsetResult, error) { var ( syscalls = mkFakedSigSyscalls(vm.Syscalls(ffiwrapper.ProofVerifier)) - vmRand = new(testRand) + vmRand = NewFixedRand() cs = store.NewChainStore(bs, ds, syscalls) sm = stmgr.NewStateManager(cs) @@ -143,8 +145,12 @@ type ExecuteMessageParams struct { Preroot cid.Cid Epoch abi.ChainEpoch Message *types.Message - CircSupply *abi.TokenAmount - BaseFee *abi.TokenAmount + CircSupply abi.TokenAmount + BaseFee abi.TokenAmount + + // Rand is an optional vm.Rand implementation to use. If nil, the driver + // will use a vm.Rand that returns a fixed value for all calls. + Rand vm.Rand } // ExecuteMessage executes a conformance test vector message in a temporary VM. @@ -155,14 +161,8 @@ func (d *Driver) ExecuteMessage(bs blockstore.Blockstore, params ExecuteMessageP _ = os.Setenv("LOTUS_DISABLE_VM_BUF", "iknowitsabadidea") } - basefee := DefaultBaseFee - if params.BaseFee != nil { - basefee = *params.BaseFee - } - - circSupply := DefaultCirculatingSupply - if params.CircSupply != nil { - circSupply = *params.CircSupply + if params.Rand == nil { + params.Rand = NewFixedRand() } // dummy state manager; only to reference the GetNetworkVersion method, @@ -172,13 +172,13 @@ func (d *Driver) ExecuteMessage(bs blockstore.Blockstore, params ExecuteMessageP vmOpts := &vm.VMOpts{ StateBase: params.Preroot, Epoch: params.Epoch, - Rand: &testRand{}, // TODO always succeeds; need more flexibility. Bstore: bs, Syscalls: mkFakedSigSyscalls(vm.Syscalls(ffiwrapper.ProofVerifier)), // TODO always succeeds; need more flexibility. CircSupplyCalc: func(_ context.Context, _ abi.ChainEpoch, _ *state.StateTree) (abi.TokenAmount, error) { - return circSupply, nil + return params.CircSupply, nil }, - BaseFee: basefee, + Rand: params.Rand, + BaseFee: params.BaseFee, NtwkVersion: sm.GetNtwkVersion, } @@ -231,3 +231,22 @@ func toChainMsg(msg *types.Message) (ret types.ChainMsg) { } return ret } + +// BaseFeeOrDefault converts a basefee as passed in a test vector (go *big.Int +// type) to an abi.TokenAmount, or if nil it returns the DefaultBaseFee. +func BaseFeeOrDefault(basefee *gobig.Int) abi.TokenAmount { + if basefee == nil { + return DefaultBaseFee + } + return big.NewFromGo(basefee) +} + +// CircSupplyOrDefault converts a circulating supply as passed in a test vector +// (go *big.Int type) to an abi.TokenAmount, or if nil it returns the +// DefaultCirculatingSupply. +func CircSupplyOrDefault(circSupply *gobig.Int) abi.TokenAmount { + if circSupply == nil { + return DefaultBaseFee + } + return big.NewFromGo(circSupply) +} diff --git a/conformance/rand_fixed.go b/conformance/rand_fixed.go new file mode 100644 index 00000000000..d356b53d049 --- /dev/null +++ b/conformance/rand_fixed.go @@ -0,0 +1,28 @@ +package conformance + +import ( + "context" + + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/crypto" + + "github.com/filecoin-project/lotus/chain/vm" +) + +type fixedRand struct{} + +var _ vm.Rand = (*fixedRand)(nil) + +// NewFixedRand creates a test vm.Rand that always returns fixed bytes value +// of utf-8 string 'i_am_random_____i_am_random_____'. +func NewFixedRand() vm.Rand { + return &fixedRand{} +} + +func (r *fixedRand) GetChainRandomness(_ context.Context, _ crypto.DomainSeparationTag, _ abi.ChainEpoch, _ []byte) ([]byte, error) { + return []byte("i_am_random_____i_am_random_____"), nil // 32 bytes. +} + +func (r *fixedRand) GetBeaconRandomness(_ context.Context, _ crypto.DomainSeparationTag, _ abi.ChainEpoch, _ []byte) ([]byte, error) { + return []byte("i_am_random_____i_am_random_____"), nil // 32 bytes. +} diff --git a/conformance/runner.go b/conformance/runner.go index 2db53b3e4c3..a240c90678a 100644 --- a/conformance/runner.go +++ b/conformance/runner.go @@ -13,9 +13,7 @@ import ( "github.com/fatih/color" "github.com/filecoin-project/go-state-types/abi" - "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/exitcode" - "github.com/filecoin-project/test-vectors/schema" "github.com/ipfs/go-blockservice" "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" @@ -24,6 +22,8 @@ import ( "github.com/ipfs/go-merkledag" "github.com/ipld/go-car" + "github.com/filecoin-project/test-vectors/schema" + "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/lib/blockstore" @@ -46,18 +46,6 @@ func ExecuteMessageVector(r Reporter, vector *schema.TestVector) { // Create a new Driver. driver := NewDriver(ctx, vector.Selector, DriverOpts{DisableVMFlush: true}) - var circSupply *abi.TokenAmount - if cs := vector.Pre.CircSupply; cs != nil { - ta := big.NewFromGo(cs) - circSupply = &ta - } - - var basefee *abi.TokenAmount - if bf := vector.Pre.BaseFee; bf != nil { - ta := big.NewFromGo(bf) - basefee = &ta - } - // Apply every message. for i, m := range vector.ApplyMessages { msg, err := types.DecodeMessage(m.Bytes) @@ -76,8 +64,8 @@ func ExecuteMessageVector(r Reporter, vector *schema.TestVector) { Preroot: root, Epoch: abi.ChainEpoch(epoch), Message: msg, - CircSupply: circSupply, - BaseFee: basefee, + BaseFee: BaseFeeOrDefault(vector.Pre.BaseFee), + CircSupply: CircSupplyOrDefault(vector.Pre.CircSupply), }) if err != nil { r.Fatalf("fatal failure when executing message: %s", err) diff --git a/conformance/stubs.go b/conformance/stubs.go index a7100892f9c..9307fdd65a2 100644 --- a/conformance/stubs.go +++ b/conformance/stubs.go @@ -6,28 +6,16 @@ import ( "github.com/filecoin-project/specs-actors/actors/runtime/proof" "github.com/filecoin-project/go-address" + "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/vm" - "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/specs-actors/actors/runtime" cbor "github.com/ipfs/go-ipld-cbor" ) -type testRand struct{} - -var _ vm.Rand = (*testRand)(nil) - -func (r *testRand) GetChainRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) { - return []byte("i_am_random_____i_am_random_____"), nil // 32 bytes. -} - -func (r *testRand) GetBeaconRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) { - return []byte("i_am_random_____i_am_random_____"), nil // 32 bytes. -} - type testSyscalls struct { runtime.Syscalls }