Skip to content

Commit

Permalink
refactor: add default TestVector to TestDriver (#208)
Browse files Browse the repository at this point in the history
This PR adds a `TestVector` to the `TestDriver` so that test driver methods that apply messages can be recorded in the test vector.

refs #194
  • Loading branch information
Alan Shaw authored Aug 7, 2020
1 parent 7d118e1 commit 81ea0ec
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 136 deletions.
2 changes: 1 addition & 1 deletion tvx/chain/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func MustNewActorAddr(data string) addr.Address {
return address
}

func MustIdFromAddress(a addr.Address) uint64 {
func MustIDFromAddress(a addr.Address) uint64 {
if a.Protocol() != addr.ID {
panic("must be ID protocol address")
}
Expand Down
2 changes: 1 addition & 1 deletion tvx/drivers/state_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (d *StateDriver) newMinerAccountActor(sealProofType abi_spec.RegisteredSeal
// creat a miner, owner, and its worker
minerOwnerPk, minerOwnerID := d.NewAccountActor(address.SECP256K1, big_spec.NewInt(1_000_000_000))
minerWorkerPk, minerWorkerID := d.NewAccountActor(address.BLS, big_spec.Zero())
expectedMinerActorIDAddress := chain.MustNewIDAddr(chain.MustIdFromAddress(minerWorkerID) + 1)
expectedMinerActorIDAddress := chain.MustNewIDAddr(chain.MustIDFromAddress(minerWorkerID) + 1)
minerActorAddrs := computeInitActorExecReturn(minerWorkerPk, 0, 1, expectedMinerActorIDAddress)

d.minerInfo = &MinerInfo{
Expand Down
24 changes: 24 additions & 0 deletions tvx/drivers/test_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/oni/tvx/chain"
vtypes "github.com/filecoin-project/oni/tvx/chain/types"
"github.com/filecoin-project/oni/tvx/schema"
)

var (
Expand Down Expand Up @@ -235,6 +236,25 @@ func NewTestDriver() *TestDriver {
checkRet := true
config := NewConfig(checkExit, checkRet)

vector := schema.TestVector{
Class: schema.ClassMessage,
Selector: "",
Meta: &schema.Metadata{
ID: "TK",
Version: "TK",
Gen: schema.GenerationData{
Source: "TK",
Version: "TK",
},
},
Pre: &schema.Preconditions{
StateTree: &schema.StateTree{},
},
Post: &schema.Postconditions{
StateTree: &schema.StateTree{},
},
}

return &TestDriver{
StateDriver: sd,

Expand All @@ -244,6 +264,7 @@ func NewTestDriver() *TestDriver {
SysCalls: syscalls,

applier: applier,
Vector: &vector,
}
}

Expand All @@ -265,6 +286,9 @@ type TestDriver struct {
Config *Config

SysCalls *ChainValidationSysCalls
// Vector is the test vector that is used when methods are called on the
// driver that apply messages.
Vector *schema.TestVector
}

//
Expand Down
3 changes: 2 additions & 1 deletion tvx/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"
"github.com/filecoin-project/specs-actors/actors/util/adt"

"github.com/filecoin-project/oni/tvx/schema"
"github.com/filecoin-project/oni/tvx/state"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func runExamineCmd(_ *cli.Context) error {
return err
}

var tv TestVector
var tv schema.TestVector
if err := json.NewDecoder(file).Decode(&tv); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions tvx/exec_lotus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/urfave/cli/v2"

"github.com/filecoin-project/oni/tvx/lotus"
"github.com/filecoin-project/oni/tvx/schema"
)

var execLotusFlags struct {
Expand Down Expand Up @@ -44,7 +45,7 @@ func runExecLotus(_ *cli.Context) error {

var (
dec = json.NewDecoder(file)
tv TestVector
tv schema.TestVector
)

if err = dec.Decode(&tv); err != nil {
Expand All @@ -55,7 +56,7 @@ func runExecLotus(_ *cli.Context) error {
default:
dec := json.NewDecoder(os.Stdin)
for {
var tv TestVector
var tv schema.TestVector

err := dec.Decode(&tv)
if err == io.EOF {
Expand All @@ -73,7 +74,7 @@ func runExecLotus(_ *cli.Context) error {
}
}

func executeTestVector(tv TestVector) error {
func executeTestVector(tv schema.TestVector) error {
fmt.Println("executing test vector")
switch tv.Class {
case "message":
Expand Down
19 changes: 10 additions & 9 deletions tvx/extract_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/urfave/cli/v2"

"github.com/filecoin-project/oni/tvx/lotus"
"github.com/filecoin-project/oni/tvx/schema"
"github.com/filecoin-project/oni/tvx/state"
)

Expand Down Expand Up @@ -178,27 +179,27 @@ func runExtractMsg(c *cli.Context) error {
}

// Write out the test vector.
vector := TestVector{
Class: ClassMessage,
vector := schema.TestVector{
Class: schema.ClassMessage,
Selector: "",
Meta: &Metadata{
Meta: &schema.Metadata{
ID: "TK",
Version: "TK",
Gen: GenerationData{
Gen: schema.GenerationData{
Source: "TK",
Version: version.String(),
},
},
CAR: out.Bytes(),
Pre: &Preconditions{
Pre: &schema.Preconditions{
Epoch: execTs.Height(),
StateTree: &StateTree{
StateTree: &schema.StateTree{
RootCID: preroot,
},
},
ApplyMessages: []Message{{Bytes: msgBytes}},
Post: &Postconditions{
StateTree: &StateTree{
ApplyMessages: []schema.Message{{Bytes: msgBytes}},
Post: &schema.Postconditions{
StateTree: &schema.StateTree{
RootCID: postroot,
},
},
Expand Down
2 changes: 1 addition & 1 deletion tvx/schema.go → tvx/schema/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package schema

import (
"encoding/hex"
Expand Down
54 changes: 15 additions & 39 deletions tvx/suite_messages_create_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (

"github.com/filecoin-project/oni/tvx/chain"
"github.com/filecoin-project/oni/tvx/drivers"
"github.com/filecoin-project/oni/tvx/schema"
)

var suiteMessagesCmd = &cli.Command{
Name: "suite-messages",
Description: "",
Description: "generate test vectors from the messages test suite adapted from github.com/filecoin-project/chain-validation",
Action: suiteMessages,
}

Expand Down Expand Up @@ -90,8 +91,6 @@ func MessageTest_AccountActorCreation() error {
err := func() error {
td := drivers.NewTestDriver()

v := newEmptyMessageVector()

existingAccountAddr, _ := td.NewAccountActor(tc.existingActorType, tc.existingActorBal)

preroot := td.GetStateRoot()
Expand All @@ -101,7 +100,7 @@ func MessageTest_AccountActorCreation() error {
if err != nil {
return err
}
v.ApplyMessages = []Message{{Bytes: b}}
td.Vector.ApplyMessages = []schema.Message{{Bytes: b}}
result := td.ApplyFailure(
msg,
tc.expExitCode,
Expand All @@ -115,13 +114,13 @@ func MessageTest_AccountActorCreation() error {

postroot := td.GetStateRoot()

v.CAR = td.MustMarshalGzippedCAR(preroot, postroot)
v.Pre.StateTree.RootCID = preroot
v.Post.StateTree.RootCID = postroot
td.Vector.CAR = td.MustMarshalGzippedCAR(preroot, postroot)
td.Vector.Pre.StateTree.RootCID = preroot
td.Vector.Post.StateTree.RootCID = postroot

// encode and output
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(&v); err != nil {
if err := enc.Encode(&td.Vector); err != nil {
return err
}

Expand All @@ -139,17 +138,15 @@ func MessageTest_AccountActorCreation() error {
func MessageTest_InitActorSequentialIDAddressCreate() error {
td := drivers.NewTestDriver()

v := newEmptyMessageVector()

var initialBal = abi_spec.NewTokenAmount(200_000_000_000)
var toSend = abi_spec.NewTokenAmount(10_000)

sender, _ := td.NewAccountActor(drivers.SECP, initialBal)

receiver, receiverID := td.NewAccountActor(drivers.SECP, initialBal)

firstPaychAddr := chain.MustNewIDAddr(chain.MustIdFromAddress(receiverID) + 1)
secondPaychAddr := chain.MustNewIDAddr(chain.MustIdFromAddress(receiverID) + 2)
firstPaychAddr := chain.MustNewIDAddr(chain.MustIDFromAddress(receiverID) + 1)
secondPaychAddr := chain.MustNewIDAddr(chain.MustIDFromAddress(receiverID) + 2)

firstInitRet := td.ComputeInitActorExecReturn(sender, 0, 0, firstPaychAddr)
secondInitRet := td.ComputeInitActorExecReturn(sender, 1, 0, secondPaychAddr)
Expand All @@ -166,7 +163,7 @@ func MessageTest_InitActorSequentialIDAddressCreate() error {
if err != nil {
return err
}
v.ApplyMessages = append(v.ApplyMessages, Message{Bytes: b1})
td.Vector.ApplyMessages = append(td.Vector.ApplyMessages, schema.Message{Bytes: b1})

msg2 := td.MessageProducer.CreatePaymentChannelActor(sender, receiver, chain.Value(toSend), chain.Nonce(1))
td.ApplyExpect(
Expand All @@ -178,40 +175,19 @@ func MessageTest_InitActorSequentialIDAddressCreate() error {
if err != nil {
return err
}
v.ApplyMessages = append(v.ApplyMessages, Message{Bytes: b2})
td.Vector.ApplyMessages = append(td.Vector.ApplyMessages, schema.Message{Bytes: b2})

postroot := td.GetStateRoot()

v.CAR = td.MustMarshalGzippedCAR(preroot, postroot)
v.Pre.StateTree.RootCID = preroot
v.Post.StateTree.RootCID = postroot
td.Vector.CAR = td.MustMarshalGzippedCAR(preroot, postroot)
td.Vector.Pre.StateTree.RootCID = preroot
td.Vector.Post.StateTree.RootCID = postroot

// encode and output
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(&v); err != nil {
if err := enc.Encode(&td.Vector); err != nil {
return err
}

return nil
}

func newEmptyMessageVector() TestVector {
return TestVector{
Class: ClassMessage,
Selector: "",
Meta: &Metadata{
ID: "TK",
Version: "TK",
Gen: GenerationData{
Source: "TK",
Version: "TK",
},
},
Pre: &Preconditions{
StateTree: &StateTree{},
},
Post: &Postconditions{
StateTree: &StateTree{},
},
}
}
Loading

0 comments on commit 81ea0ec

Please sign in to comment.