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/tx.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 9580bc0..fd52222 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -4,14 +4,17 @@ package evm import ( + "bytes" "errors" "fmt" + "sort" "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" + "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/codec" "github.com/ava-labs/avalanchego/utils/crypto" "github.com/ava-labs/avalanchego/utils/hashing" @@ -115,3 +118,31 @@ func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) erro tx.Initialize(unsignedBytes, signedBytes) return nil } + +// innerSortInputs implements sort.Interface for EVMInput +type innerSortInputs struct { + inputs []EVMInput + signers [][]*crypto.PrivateKeySECP256K1R +} + +func (ins *innerSortInputs) Less(i, j int) bool { + return bytes.Compare(ins.inputs[i].Address.Bytes(), ins.inputs[j].Address.Bytes()) < 0 +} + +func (ins *innerSortInputs) Len() int { return len(ins.inputs) } + +func (ins *innerSortInputs) Swap(i, j int) { + ins.inputs[j], ins.inputs[i] = ins.inputs[i], ins.inputs[j] + ins.signers[j], ins.signers[i] = ins.signers[i], ins.signers[j] +} + +// SortEVMInputsAndSigners sorts the list of EVMInputs based solely on the address of the input +func SortEVMInputsAndSigners(inputs []EVMInput, signers [][]*crypto.PrivateKeySECP256K1R) { + sort.Sort(&innerSortInputs{inputs: inputs, signers: signers}) +} + +// IsSortedAndUniqueEVMInputs returns true if the EVM Inputs are sorted and unique +// based on the account addresses +func IsSortedAndUniqueEVMInputs(inputs []EVMInput) bool { + return utils.IsSortedAndUnique(&innerSortInputs{inputs: inputs}) +} -- cgit v1.2.3 From 332dc311e9a21f21f141cfb262c7c23f5ea61ecb Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 23 Sep 2020 00:38:51 -0400 Subject: Change name of EVMInputs inner sort struct --- plugin/evm/tx.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index fd52222..db49980 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -119,30 +119,30 @@ func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) erro return nil } -// innerSortInputs implements sort.Interface for EVMInput -type innerSortInputs struct { +// innerSortInputsAndSigners implements sort.Interface for EVMInput +type innerSortInputsAndSigners struct { inputs []EVMInput signers [][]*crypto.PrivateKeySECP256K1R } -func (ins *innerSortInputs) Less(i, j int) bool { +func (ins *innerSortInputsAndSigners) Less(i, j int) bool { return bytes.Compare(ins.inputs[i].Address.Bytes(), ins.inputs[j].Address.Bytes()) < 0 } -func (ins *innerSortInputs) Len() int { return len(ins.inputs) } +func (ins *innerSortInputsAndSigners) Len() int { return len(ins.inputs) } -func (ins *innerSortInputs) Swap(i, j int) { +func (ins *innerSortInputsAndSigners) Swap(i, j int) { ins.inputs[j], ins.inputs[i] = ins.inputs[i], ins.inputs[j] ins.signers[j], ins.signers[i] = ins.signers[i], ins.signers[j] } // SortEVMInputsAndSigners sorts the list of EVMInputs based solely on the address of the input func SortEVMInputsAndSigners(inputs []EVMInput, signers [][]*crypto.PrivateKeySECP256K1R) { - sort.Sort(&innerSortInputs{inputs: inputs, signers: signers}) + sort.Sort(&innerSortInputsAndSigners{inputs: inputs, signers: signers}) } // IsSortedAndUniqueEVMInputs returns true if the EVM Inputs are sorted and unique // based on the account addresses func IsSortedAndUniqueEVMInputs(inputs []EVMInput) bool { - return utils.IsSortedAndUnique(&innerSortInputs{inputs: inputs}) + return utils.IsSortedAndUnique(&innerSortInputsAndSigners{inputs: inputs}) } -- cgit v1.2.3 From 6cc9d3c61ee803f37cbc6e65799797c0dc51e9e5 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Wed, 23 Sep 2020 01:35:28 -0400 Subject: Sort evm outputs of import tx --- plugin/evm/tx.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index db49980..7dcebc8 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -126,7 +126,11 @@ type innerSortInputsAndSigners struct { } func (ins *innerSortInputsAndSigners) Less(i, j int) bool { - return bytes.Compare(ins.inputs[i].Address.Bytes(), ins.inputs[j].Address.Bytes()) < 0 + addrComp := bytes.Compare(ins.inputs[i].Address.Bytes(), ins.inputs[j].Address.Bytes()) + if addrComp != 0 { + return addrComp < 0 + } + return bytes.Compare(ins.inputs[i].AssetID.Bytes(), ins.inputs[j].AssetID.Bytes()) < 0 } func (ins *innerSortInputsAndSigners) Len() int { return len(ins.inputs) } @@ -136,7 +140,7 @@ func (ins *innerSortInputsAndSigners) Swap(i, j int) { ins.signers[j], ins.signers[i] = ins.signers[i], ins.signers[j] } -// SortEVMInputsAndSigners sorts the list of EVMInputs based solely on the address of the input +// SortEVMInputsAndSigners sorts the list of EVMInputs based on the addresses and assetIDs func SortEVMInputsAndSigners(inputs []EVMInput, signers [][]*crypto.PrivateKeySECP256K1R) { sort.Sort(&innerSortInputsAndSigners{inputs: inputs, signers: signers}) } @@ -146,3 +150,34 @@ func SortEVMInputsAndSigners(inputs []EVMInput, signers [][]*crypto.PrivateKeySE func IsSortedAndUniqueEVMInputs(inputs []EVMInput) bool { return utils.IsSortedAndUnique(&innerSortInputsAndSigners{inputs: inputs}) } + +// innerSortEVMOutputs implements sort.Interface for EVMOutput +type innerSortEVMOutputs struct { + outputs []EVMOutput +} + +func (outs *innerSortEVMOutputs) Less(i, j int) bool { + addrComp := bytes.Compare(outs.outputs[i].Address.Bytes(), outs.outputs[j].Address.Bytes()) + if addrComp != 0 { + return addrComp < 0 + } + return bytes.Compare(outs.outputs[i].AssetID.Bytes(), outs.outputs[j].AssetID.Bytes()) < 0 +} + +func (outs *innerSortEVMOutputs) Len() int { return len(outs.outputs) } + +func (outs *innerSortEVMOutputs) Swap(i, j int) { + outs.outputs[j], outs.outputs[i] = outs.outputs[i], outs.outputs[j] +} + +// SortEVMOutputs sorts the list of EVMOutputs based on the addresses and assetIDs +// of the outputs +func SortEVMOutputs(outputs []EVMOutput) { + sort.Sort(&innerSortEVMOutputs{outputs: outputs}) +} + +// IsSortedAndUniqueEVMOutputs returns true if the EVMOutputs are sorted and unique +// based on the account addresses and assetIDs +func IsSortedAndUniqueEVMOutputs(outputs []EVMOutput) bool { + return utils.IsSortedAndUnique(&innerSortEVMOutputs{outputs: outputs}) +} -- 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/tx.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 7dcebc8..7c2ebf1 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -33,12 +33,14 @@ var ( errNilTx = errors.New("tx is nil") ) +// EVMOutput defines an output from EVM State created from export transactions type EVMOutput struct { Address common.Address `serialize:"true" json:"address"` Amount uint64 `serialize:"true" json:"amount"` AssetID ids.ID `serialize:"true" json:"assetID"` } +// EVMInput defines an input for the EVM State to be used in import transactions type EVMInput struct { Address common.Address `serialize:"true" json:"address"` Amount uint64 `serialize:"true" json:"amount"` @@ -46,10 +48,12 @@ type EVMInput struct { Nonce uint64 `serialize:"true" json:"nonce"` } +// Verify ... func (out *EVMOutput) Verify() error { return nil } +// Verify ... func (in *EVMInput) Verify() error { return nil } -- cgit v1.2.3