From a725a6a16c53f508681484e36729ebe65a5b33b1 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 22 Sep 2020 16:45:12 -0400 Subject: Add test for ExportTx Verify --- plugin/evm/export_tx_test.go | 144 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 plugin/evm/export_tx_test.go (limited to 'plugin/evm/export_tx_test.go') diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go new file mode 100644 index 0000000..2af123f --- /dev/null +++ b/plugin/evm/export_tx_test.go @@ -0,0 +1,144 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package evm + +import ( + "testing" + + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/utils/crypto" + "github.com/ava-labs/avalanchego/vms/components/avax" + "github.com/ava-labs/avalanchego/vms/secp256k1fx" +) + +func TestExportTxVerifyNil(t *testing.T) { + var exportTx UnsignedExportTx + if err := exportTx.Verify(testXChainID, NewContext(), testTxFee, testAvaxAssetID); err == nil { + t.Fatal("Verify should have failed due to nil transaction") + } +} + +func TestExportTxVerify(t *testing.T) { + var exportAmount uint64 = 10000000 + exportTx := &UnsignedExportTx{ + NetworkID: testNetworkID, + BlockchainID: testCChainID, + DestinationChain: testXChainID, + Ins: []EVMInput{ + { + Address: testEthAddrs[0], + Amount: exportAmount, + AssetID: testAvaxAssetID, + Nonce: 0, + }, + { + Address: testEthAddrs[2], + Amount: exportAmount, + AssetID: testAvaxAssetID, + Nonce: 0, + }, + }, + ExportedOutputs: []*avax.TransferableOutput{ + { + Asset: avax.Asset{ID: testAvaxAssetID}, + Out: &secp256k1fx.TransferOutput{ + Amt: exportAmount - testTxFee, + OutputOwners: secp256k1fx.OutputOwners{ + Locktime: 0, + Threshold: 1, + Addrs: []ids.ShortID{testShortIDAddrs[0]}, + }, + }, + }, + { + Asset: avax.Asset{ID: testAvaxAssetID}, + Out: &secp256k1fx.TransferOutput{ + Amt: exportAmount, // only subtract fee from one output + OutputOwners: secp256k1fx.OutputOwners{ + Locktime: 0, + Threshold: 1, + Addrs: []ids.ShortID{testShortIDAddrs[1]}, + }, + }, + }, + }, + } + + // Sort the inputs and outputs to ensure the transaction is canonical + avax.SortTransferableOutputs(exportTx.ExportedOutputs, Codec) + // Pass in a list of signers here with the appropriate length + // to avoid causing a nil-pointer error in the helper method + emptySigners := make([][]*crypto.PrivateKeySECP256K1R, 2) + SortEVMInputsAndSigners(exportTx.Ins, emptySigners) + + ctx := NewContext() + + // Test Valid Export Tx + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err != nil { + t.Fatalf("Failed to verify valid ExportTx: %w", err) + } + + exportTx.syntacticallyVerified = false + exportTx.NetworkID = testNetworkID + 1 + + // Test Incorrect Network ID Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to incorrect network ID") + } + + exportTx.syntacticallyVerified = false + exportTx.NetworkID = testNetworkID + exportTx.BlockchainID = nonExistentID + // Test Incorrect Blockchain ID Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to incorrect blockchain ID") + } + + exportTx.syntacticallyVerified = false + exportTx.BlockchainID = testCChainID + exportTx.DestinationChain = nonExistentID + // Test Incorrect Destination Chain ID Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to incorrect destination chain") + } + + exportTx.syntacticallyVerified = false + exportTx.DestinationChain = testXChainID + exportedOuts := exportTx.ExportedOutputs + exportTx.ExportedOutputs = nil + // Test No Exported Outputs Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to no exported outputs") + } + + exportTx.syntacticallyVerified = false + exportTx.ExportedOutputs = []*avax.TransferableOutput{exportedOuts[1], exportedOuts[0]} + // Test Unsorted outputs Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to no exported outputs") + } + + exportTx.syntacticallyVerified = false + exportTx.ExportedOutputs = exportedOuts + inputs := exportTx.Ins + exportTx.Ins = nil + // Test No Exported Outputs Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to no inputs") + } + + exportTx.syntacticallyVerified = false + exportTx.Ins = []EVMInput{inputs[1], inputs[0]} + // Test unsorted EVM Inputs Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to unsorted inputs") + } + + exportTx.syntacticallyVerified = false + exportTx.Ins = []EVMInput{inputs[0], inputs[0]} + // Test non-unique EVM Inputs Errors + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to non-unique inputs") + } +} -- cgit v1.2.3 From affb9c79b05ce6610c4b397fc6998df23cf599bd Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 23 Sep 2020 15:41:05 -0400 Subject: remove requirement for evm inputs and outputs to be sorted and unique --- plugin/evm/export_tx_test.go | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'plugin/evm/export_tx_test.go') diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go index 2af123f..b17f719 100644 --- a/plugin/evm/export_tx_test.go +++ b/plugin/evm/export_tx_test.go @@ -121,24 +121,9 @@ func TestExportTxVerify(t *testing.T) { exportTx.syntacticallyVerified = false exportTx.ExportedOutputs = exportedOuts - inputs := exportTx.Ins exportTx.Ins = nil // Test No Exported Outputs Errors if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { t.Fatal("ExportTx should have failed verification due to no inputs") } - - exportTx.syntacticallyVerified = false - exportTx.Ins = []EVMInput{inputs[1], inputs[0]} - // Test unsorted EVM Inputs Errors - if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { - t.Fatal("ExportTx should have failed verification due to unsorted inputs") - } - - exportTx.syntacticallyVerified = false - exportTx.Ins = []EVMInput{inputs[0], inputs[0]} - // Test non-unique EVM Inputs Errors - if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { - t.Fatal("ExportTx should have failed verification due to non-unique inputs") - } } -- cgit v1.2.3 From 100f358d61d60d1196e3a68517c25147b45d965b Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 23 Sep 2020 15:43:35 -0400 Subject: remove requirement that evm inputs/outputs are non-empty list --- plugin/evm/export_tx_test.go | 8 -------- 1 file changed, 8 deletions(-) (limited to 'plugin/evm/export_tx_test.go') diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go index b17f719..40670ff 100644 --- a/plugin/evm/export_tx_test.go +++ b/plugin/evm/export_tx_test.go @@ -118,12 +118,4 @@ func TestExportTxVerify(t *testing.T) { if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { t.Fatal("ExportTx should have failed verification due to no exported outputs") } - - exportTx.syntacticallyVerified = false - exportTx.ExportedOutputs = exportedOuts - exportTx.Ins = nil - // Test No Exported Outputs Errors - if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { - t.Fatal("ExportTx should have failed verification due to no inputs") - } } -- cgit v1.2.3 From a5b80ffc01821becffbe4f36d937f778c911af5f Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 6 Oct 2020 16:12:56 -0400 Subject: Add semantic verification tests for import tx --- plugin/evm/export_tx_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'plugin/evm/export_tx_test.go') diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go index 40670ff..319c6dd 100644 --- a/plugin/evm/export_tx_test.go +++ b/plugin/evm/export_tx_test.go @@ -13,7 +13,7 @@ import ( ) func TestExportTxVerifyNil(t *testing.T) { - var exportTx UnsignedExportTx + var exportTx *UnsignedExportTx if err := exportTx.Verify(testXChainID, NewContext(), testTxFee, testAvaxAssetID); err == nil { t.Fatal("Verify should have failed due to nil transaction") } @@ -116,6 +116,17 @@ func TestExportTxVerify(t *testing.T) { exportTx.ExportedOutputs = []*avax.TransferableOutput{exportedOuts[1], exportedOuts[0]} // Test Unsorted outputs Errors if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { - t.Fatal("ExportTx should have failed verification due to no exported outputs") + t.Fatal("ExportTx should have failed verification due to no unsorted exported outputs") + } + + exportTx.syntacticallyVerified = false + exportTx.ExportedOutputs = []*avax.TransferableOutput{exportedOuts[0], nil} + // Test invalid exported output + if err := exportTx.Verify(testXChainID, ctx, testTxFee, testAvaxAssetID); err == nil { + t.Fatal("ExportTx should have failed verification due to invalid output") } } + +func TestExportTxSemanticVerify(t *testing.T) { + +} -- cgit v1.2.3