Skip to content

Commit

Permalink
Merge pull request #4211 from filecoin-project/conformance/minor-refa…
Browse files Browse the repository at this point in the history
…ctors

conformance: minor driver refactors.
  • Loading branch information
magik6k authored Oct 7, 2020
2 parents cfd126e + d3dc560 commit 81fb552
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
12 changes: 6 additions & 6 deletions cmd/tvx/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 33 additions & 14 deletions conformance/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conformance

import (
"context"
gobig "math/big"
"os"

"github.com/filecoin-project/lotus/chain/state"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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,
}

Expand Down Expand Up @@ -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)
}
28 changes: 28 additions & 0 deletions conformance/rand_fixed.go
Original file line number Diff line number Diff line change
@@ -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.
}
20 changes: 4 additions & 16 deletions conformance/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 1 addition & 13 deletions conformance/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 81fb552

Please sign in to comment.