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/import_tx.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'plugin/evm/import_tx.go') diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go index 0b0d348..4d7f219 100644 --- a/plugin/evm/import_tx.go +++ b/plugin/evm/import_tx.go @@ -75,6 +75,9 @@ func (tx *UnsignedImportTx) Verify( return err } } + if !IsSortedAndUniqueEVMOutputs(tx.Outs) { + return errExportOutputsNotSortedAndUnique + } for _, in := range tx.ImportedInputs { if err := in.Verify(); err != nil { @@ -127,15 +130,6 @@ func (tx *UnsignedImportTx) SemanticVerify( return tempError{err} } - utxos := make([]*avax.UTXO, len(tx.ImportedInputs)) - for i, utxoBytes := range allUTXOBytes { - utxo := &avax.UTXO{} - if err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil { - return tempError{err} - } - utxos[i] = utxo - } - for i, in := range tx.ImportedInputs { utxoBytes := allUTXOBytes[i] @@ -173,7 +167,7 @@ func (tx *UnsignedImportTx) Accept(ctx *snow.Context, _ database.Batch) error { return ctx.SharedMemory.Remove(tx.SourceChain, utxoIDs) } -// Create a new transaction +// newImportTx returns a new ImportTx func (vm *VM) newImportTx( chainID ids.ID, // chain to import from to common.Address, // Address of recipient @@ -240,20 +234,23 @@ func (vm *VM) newImportTx( // }) //} - // non-AVAX asset outputs - for aidKey, amount := range importedAmount { - aid := ids.NewID(aidKey) - //if aid.Equals(vm.ctx.AVAXAssetID) || amount == 0 { + // This will create unique outputs (in the context of sorting) + // since each output will have a unique assetID + for assetKey, amount := range importedAmount { + assetID := ids.NewID(assetKey) + //if assetID.Equals(vm.ctx.AVAXAssetID) || amount == 0 { if amount == 0 { continue } outs = append(outs, EVMOutput{ Address: to, Amount: amount, - AssetID: aid, + AssetID: assetID, }) } + SortEVMOutputs(outs) + // Create the transaction utx := &UnsignedImportTx{ NetworkID: vm.ctx.NetworkID, @@ -269,6 +266,8 @@ func (vm *VM) newImportTx( return tx, utx.Verify(vm.ctx.XChainID, vm.ctx, vm.txFee, vm.ctx.AVAXAssetID) } +// EVMStateTransfer performs the state transfer to increase the balances of +// accounts accordingly with the imported EVMOutputs func (tx *UnsignedImportTx) EVMStateTransfer(vm *VM, state *state.StateDB) error { for _, to := range tx.Outs { log.Info("crosschain X->C", "addr", to.Address, "amount", to.Amount) -- cgit v1.2.3