From 6954e6cb0ef941d1d61b362bfe1567c36d125d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Sun, 16 Aug 2020 13:40:02 +0100 Subject: [PATCH] migrate actor creation suite. (#239) --- .circleci/config.yml | 9 +- tvx/_suite_messages_create_actor.go | 156 ----------------------- tvx/builders/asserter.go | 4 +- tvx/builders/gas.go | 5 +- tvx/go.mod | 5 - tvx/scripts/actor_creation/addresses.go | 43 +++++++ tvx/scripts/actor_creation/main.go | 82 ++++++++++++ tvx/scripts/actor_creation/on_tranfer.go | 41 ++++++ 8 files changed, 179 insertions(+), 166 deletions(-) delete mode 100644 tvx/_suite_messages_create_actor.go create mode 100644 tvx/scripts/actor_creation/addresses.go create mode 100644 tvx/scripts/actor_creation/main.go create mode 100644 tvx/scripts/actor_creation/on_tranfer.go diff --git a/.circleci/config.yml b/.circleci/config.yml index 3c20b9fae..db5bfd8b9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -53,14 +53,17 @@ jobs: name: "build tvx" command: pushd tvx && go build . - run: - name: "run messages test vector suite (new api) - msg_application" + name: "run messages test vector suite: msg_application" command: pushd tvx/scripts/msg_application && go build . && ./msg_application | ../../tvx exec-lotus - run: - name: "run messages test vector suite (new api) - nested send" + name: "run messages test vector suite: nested send" command: pushd tvx/scripts/nested && go build . && ./nested | ../../tvx exec-lotus - run: - name: "run messages test vector suite (new api) - paych" + name: "run messages test vector suite: paych" command: pushd tvx/scripts/paych && go build . && ./paych | ../../tvx exec-lotus + - run: + name: "run messages test vector suite: actor_creation" + command: pushd tvx/scripts/actor_creation && go build . && ./actor_creation | ../../tvx exec-lotus soup-build-linux: executor: linux steps: diff --git a/tvx/_suite_messages_create_actor.go b/tvx/_suite_messages_create_actor.go deleted file mode 100644 index f52b52ba9..000000000 --- a/tvx/_suite_messages_create_actor.go +++ /dev/null @@ -1,156 +0,0 @@ -package main - -import ( - "os" - - "github.com/hashicorp/go-multierror" - "github.com/urfave/cli/v2" - - "github.com/filecoin-project/go-address" - - abi_spec "github.com/filecoin-project/specs-actors/actors/abi" - big_spec "github.com/filecoin-project/specs-actors/actors/abi/big" - exitcode_spec "github.com/filecoin-project/specs-actors/actors/runtime/exitcode" - - "github.com/filecoin-project/oni/tvx/chain" - "github.com/filecoin-project/oni/tvx/drivers" -) - -var suiteMessagesCmd = &cli.Command{ - Name: "suite-messages", - Description: "generate test vectors from the messages test suite adapted from github.com/filecoin-project/chain-validation", - Action: suiteMessages, -} - -func suiteMessages(c *cli.Context) error { - var err *multierror.Error - err = multierror.Append(MessageTest_AccountActorCreation()) - err = multierror.Append(MessageTest_InitActorSequentialIDAddressCreate()) - err = multierror.Append(MessageTest_MultiSigActor()) - err = multierror.Append(MessageTest_ValueTransferSimple()) - err = multierror.Append(MessageTest_ValueTransferAdvance()) - return err.ErrorOrNil() -} - -func MessageTest_AccountActorCreation() error { - testCases := []struct { - desc string - - existingActorType address.Protocol - existingActorBal abi_spec.TokenAmount - - newActorAddr address.Address - newActorInitBal abi_spec.TokenAmount - - expExitCode exitcode_spec.ExitCode - }{ - { - "success create SECP256K1 account actor", - address.SECP256K1, - abi_spec.NewTokenAmount(10_000_000_000), - - chain.MustNewSECP256K1Addr("publickeyfoo"), - abi_spec.NewTokenAmount(10_000), - - exitcode_spec.Ok, - }, - { - "success create BLS account actor", - address.SECP256K1, - abi_spec.NewTokenAmount(10_000_000_000), - - chain.MustNewBLSAddr(1), - abi_spec.NewTokenAmount(10_000), - - exitcode_spec.Ok, - }, - { - "fail create SECP256K1 account actor insufficient balance", - address.SECP256K1, - abi_spec.NewTokenAmount(9_999), - - chain.MustNewSECP256K1Addr("publickeybar"), - abi_spec.NewTokenAmount(10_000), - - exitcode_spec.SysErrSenderStateInvalid, - }, - { - "fail create BLS account actor insufficient balance", - address.SECP256K1, - abi_spec.NewTokenAmount(9_999), - - chain.MustNewBLSAddr(1), - abi_spec.NewTokenAmount(10_000), - - exitcode_spec.SysErrSenderStateInvalid, - }, - // TODO add edge case tests that have insufficient balance after gas fees - } - for _, tc := range testCases { - err := func() error { - td := drivers.NewTestDriver() - td.Vector.Meta.Desc = tc.desc - - existingAccountAddr, _ := td.NewAccountActor(tc.existingActorType, tc.existingActorBal) - - td.UpdatePreStateRoot() - - msg := td.MessageProducer.Transfer(existingAccountAddr, tc.newActorAddr, chain.Value(tc.newActorInitBal), chain.Nonce(0)) - result := td.ApplyFailure( - msg, - tc.expExitCode, - ) - - // new actor balance will only exist if message was applied successfully. - if tc.expExitCode.IsSuccess() { - td.AssertBalance(tc.newActorAddr, tc.newActorInitBal) - td.AssertBalance(existingAccountAddr, big_spec.Sub(big_spec.Sub(tc.existingActorBal, result.Receipt.GasUsed.Big()), tc.newActorInitBal)) - } - - td.MustSerialize(os.Stdout) - - return nil - }() - - if err != nil { - return err - } - } - - return nil -} - -func MessageTest_InitActorSequentialIDAddressCreate() error { - td := drivers.NewTestDriver() - - 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) - - firstInitRet := td.ComputeInitActorExecReturn(sender, 0, 0, firstPaychAddr) - secondInitRet := td.ComputeInitActorExecReturn(sender, 1, 0, secondPaychAddr) - - td.UpdatePreStateRoot() - - msg1 := td.MessageProducer.CreatePaymentChannelActor(sender, receiver, chain.Value(toSend), chain.Nonce(0)) - td.ApplyExpect( - msg1, - chain.MustSerialize(&firstInitRet), - ) - - msg2 := td.MessageProducer.CreatePaymentChannelActor(sender, receiver, chain.Value(toSend), chain.Nonce(1)) - td.ApplyExpect( - msg2, - chain.MustSerialize(&secondInitRet), - ) - - td.MustSerialize(os.Stdout) - - return nil -} diff --git a/tvx/builders/asserter.go b/tvx/builders/asserter.go index da7fbe03f..d1001c6e2 100644 --- a/tvx/builders/asserter.go +++ b/tvx/builders/asserter.go @@ -137,5 +137,7 @@ func (a *Asserter) FailNow() { } func (a *Asserter) Errorf(format string, args ...interface{}) { - fmt.Printf("%s: "+format, append([]interface{}{a.stage}, args...)) + id := a.b.vector.Meta.ID + stage := a.stage + fmt.Printf("❌ id: %s, stage: %s:"+format, append([]interface{}{id, stage}, args...)...) } diff --git a/tvx/builders/gas.go b/tvx/builders/gas.go index 42292db79..783e53d08 100644 --- a/tvx/builders/gas.go +++ b/tvx/builders/gas.go @@ -15,8 +15,11 @@ const ( // CalculateDeduction returns the balance that shall be deducted from the // sender's account as a result of applying this message. func CalculateDeduction(am *ApplicableMessage) big.Int { - m := am.Message + if am.Result.GasUsed == 0 { + return big.Zero() + } + m := am.Message minerReward := GetMinerReward(m.GasLimit, m.GasPremium) // goes to the miner burn := CalculateBurn(m.GasLimit, am.Result.GasUsed) // vanishes deducted := big.Add(minerReward, burn) // sum of gas accrued diff --git a/tvx/go.mod b/tvx/go.mod index 4f190fe3e..efd9cf8a4 100644 --- a/tvx/go.mod +++ b/tvx/go.mod @@ -3,28 +3,23 @@ module github.com/filecoin-project/oni/tvx go 1.14 require ( - github.com/filecoin-project/filecoin-ffi v0.30.4-0.20200716204036-cddc56607e1d github.com/filecoin-project/go-address v0.0.3 github.com/filecoin-project/go-bitfield v0.2.0 github.com/filecoin-project/go-crypto v0.0.0-20191218222705-effae4ea9f03 - github.com/filecoin-project/go-fil-commcid v0.0.0-20200716160307-8f644712406f github.com/filecoin-project/lotus v0.4.3-0.20200814191300-4a0171d26aa5 github.com/filecoin-project/sector-storage v0.0.0-20200810171746-eac70842d8e0 github.com/filecoin-project/specs-actors v0.9.2 - github.com/hashicorp/go-multierror v1.1.0 github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-blockservice v0.1.4-0.20200624145336-a978cec6e834 github.com/ipfs/go-cid v0.0.7 github.com/ipfs/go-datastore v0.4.4 github.com/ipfs/go-hamt-ipld v0.1.1 - github.com/ipfs/go-ipfs-blockstore v1.0.1 github.com/ipfs/go-ipfs-exchange-interface v0.0.1 github.com/ipfs/go-ipfs-exchange-offline v0.0.1 github.com/ipfs/go-ipld-cbor v0.0.5-0.20200428170625-a0bd04d3cbdf github.com/ipfs/go-ipld-format v0.2.0 github.com/ipfs/go-merkledag v0.3.2 github.com/ipld/go-car v0.1.1-0.20200526133713-1c7508d55aae - github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52 github.com/libp2p/go-libp2p-core v0.6.1 github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 github.com/multiformats/go-multiaddr v0.2.2 diff --git a/tvx/scripts/actor_creation/addresses.go b/tvx/scripts/actor_creation/addresses.go new file mode 100644 index 000000000..307d62321 --- /dev/null +++ b/tvx/scripts/actor_creation/addresses.go @@ -0,0 +1,43 @@ +package main + +import ( + . "github.com/filecoin-project/oni/tvx/builders" + + "github.com/filecoin-project/specs-actors/actors/abi/big" + init_ "github.com/filecoin-project/specs-actors/actors/builtin/init" + + "github.com/filecoin-project/go-address" +) + +func sequentialAddresses(v *Builder) { + v.Messages.SetDefaults(GasLimit(1_000_000_000), GasPremium(1), GasFeeCap(200)) + + initial := big.NewInt(1_000_000_000_000_000) + + // Set up sender and receiver accounts. + var sender, receiver AddressHandle + v.Actors.AccountN(address.SECP256K1, initial, &sender, &receiver) + v.CommitPreconditions() + + // Create 10 payment channels. + for i := uint64(0); i < 10; i++ { + v.Messages.Sugar().CreatePaychActor(sender.Robust, receiver.Robust, Value(big.NewInt(1000)), Nonce(i)) + } + v.CommitApplies() + + for i, am := range v.Messages.All() { + expectedActorAddr := AddressHandle{ + ID: MustNewIDAddr(MustIDFromAddress(receiver.ID) + uint64(i) + 1), + Robust: sender.NextActorAddress(am.Message.Nonce, 0), + } + + // Verify that the return contains the expected addresses. + var ret init_.ExecReturn + MustDeserialize(am.Result.Return, &ret) + v.Assert.Equal(expectedActorAddr.Robust, ret.RobustAddress) + v.Assert.Equal(expectedActorAddr.ID, ret.IDAddress) + } + + v.Assert.EveryMessageSenderSatisfies(BalanceUpdated(big.Zero())) + v.Assert.EveryMessageSenderSatisfies(NonceUpdated()) +} diff --git a/tvx/scripts/actor_creation/main.go b/tvx/scripts/actor_creation/main.go new file mode 100644 index 000000000..460388316 --- /dev/null +++ b/tvx/scripts/actor_creation/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/runtime/exitcode" + + . "github.com/filecoin-project/oni/tvx/builders" + "github.com/filecoin-project/oni/tvx/schema" +) + +func main() { + g := NewGenerator() + + g.MessageVectorGroup("addresses", + &MessageVectorGenItem{ + Metadata: &schema.Metadata{ + ID: "sequential-10", + Version: "v1", + Desc: "actor addresses are sequential", + }, + Func: sequentialAddresses, + }, + ) + + g.MessageVectorGroup("on_transfer", + &MessageVectorGenItem{ + Metadata: &schema.Metadata{ + ID: "ok-create-secp256k1", + Version: "v1", + }, + Func: actorCreationOnTransfer(actorCreationOnTransferParams{ + senderType: address.SECP256K1, + senderBal: abi.NewTokenAmount(1_000_000_000_000_000), + receiverAddr: MustNewSECP256K1Addr("publickeyfoo"), + amount: abi.NewTokenAmount(10_000), + exitCode: exitcode.Ok, + }), + }, + &MessageVectorGenItem{ + Metadata: &schema.Metadata{ + ID: "ok-create-bls", + Version: "v1", + }, + Func: actorCreationOnTransfer(actorCreationOnTransferParams{ + senderType: address.SECP256K1, + senderBal: abi.NewTokenAmount(1_000_000_000_000_000), + receiverAddr: MustNewBLSAddr(1), + amount: abi.NewTokenAmount(10_000), + exitCode: exitcode.Ok, + }), + }, + &MessageVectorGenItem{ + Metadata: &schema.Metadata{ + ID: "fail-secp256k1-insufficient-balance", + Version: "v1", + }, + Func: actorCreationOnTransfer(actorCreationOnTransferParams{ + senderType: address.SECP256K1, + senderBal: abi.NewTokenAmount(9_999), + receiverAddr: MustNewSECP256K1Addr("publickeyfoo"), + amount: abi.NewTokenAmount(10_000), + exitCode: exitcode.SysErrSenderStateInvalid, + }), + }, + &MessageVectorGenItem{ + Metadata: &schema.Metadata{ + ID: "fail-bls-insufficient-balance", + Version: "v1", + }, + Func: actorCreationOnTransfer(actorCreationOnTransferParams{ + senderType: address.SECP256K1, + senderBal: abi.NewTokenAmount(9_999), + receiverAddr: MustNewBLSAddr(1), + amount: abi.NewTokenAmount(10_000), + exitCode: exitcode.SysErrSenderStateInvalid, + }), + }, + ) + + g.Wait() +} diff --git a/tvx/scripts/actor_creation/on_tranfer.go b/tvx/scripts/actor_creation/on_tranfer.go new file mode 100644 index 000000000..ec9a1e90b --- /dev/null +++ b/tvx/scripts/actor_creation/on_tranfer.go @@ -0,0 +1,41 @@ +package main + +import ( + "github.com/filecoin-project/specs-actors/actors/abi" + "github.com/filecoin-project/specs-actors/actors/runtime/exitcode" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/specs-actors/actors/abi/big" + + . "github.com/filecoin-project/oni/tvx/builders" +) + +type actorCreationOnTransferParams struct { + senderType address.Protocol + senderBal abi.TokenAmount + receiverAddr address.Address + amount abi.TokenAmount + exitCode exitcode.ExitCode +} + +func actorCreationOnTransfer(params actorCreationOnTransferParams) func(v *Builder) { + return func(v *Builder) { + v.Messages.SetDefaults(GasLimit(1_000_000_000), GasPremium(1), GasFeeCap(200)) + + // Set up sender account. + sender := v.Actors.Account(params.senderType, params.senderBal) + v.CommitPreconditions() + + // Perform the transfer. + v.Messages.Sugar().Transfer(sender.ID, params.receiverAddr, Value(params.amount), Nonce(0)) + v.CommitApplies() + + v.Assert.EveryMessageResultSatisfies(ExitCode(params.exitCode)) + v.Assert.EveryMessageSenderSatisfies(BalanceUpdated(big.Zero())) + + if params.exitCode.IsSuccess() { + v.Assert.EveryMessageSenderSatisfies(NonceUpdated()) + v.Assert.BalanceEq(params.receiverAddr, params.amount) + } + } +}