From eeb62be039927d461bcd5bebc456e3ab1a31307c Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 20 Aug 2020 00:27:32 -0400 Subject: move nonce to EVMOutput/Input --- plugin/evm/export_tx.go | 6 ++---- plugin/evm/import_tx.go | 17 ++++++++--------- plugin/evm/tx.go | 1 + plugin/evm/vm.go | 5 +++++ 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go index 9af11e0..423c754 100644 --- a/plugin/evm/export_tx.go +++ b/plugin/evm/export_tx.go @@ -33,8 +33,6 @@ type UnsignedExportTx struct { Ins []EVMInput `serialize:"true" json:"inputs"` // Outputs that are exported to the chain ExportedOutputs []*avax.TransferableOutput `serialize:"true" json:"exportedOutputs"` - // EVM nonce - nonce uint64 } // InputUTXOs returns an empty set @@ -157,10 +155,10 @@ func (tx *UnsignedExportTx) EVMStateTransfer(state *state.StateDB) error { return errInsufficientFunds } state.SubBalance(from.Address, amount) - if state.GetNonce(from.Address) != tx.nonce { + if state.GetNonce(from.Address) != from.Nonce { return errInvalidNonce } - state.SetNonce(from.Address, tx.nonce+1) + state.SetNonce(from.Address, from.Nonce+1) } return nil } diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go index 68e5fed..2b4f995 100644 --- a/plugin/evm/import_tx.go +++ b/plugin/evm/import_tx.go @@ -34,8 +34,6 @@ type UnsignedImportTx struct { ImportedInputs []*avax.TransferableInput `serialize:"true" json:"importedInputs"` // Outputs Outs []EVMOutput `serialize:"true" json:"outputs"` - // EVM nonce - nonce uint64 } // InputUTXOs returns the UTXOIDs of the imported funds @@ -182,6 +180,11 @@ func (vm *VM) newImportTx( return nil, errNoFunds // No imported UTXOs were spendable } + nonce, err := vm.GetAcceptedNonce(to) + if err != nil { + return nil, err + } + outs := []EVMOutput{} if importedAmount < vm.txFee { // imported amount goes toward paying tx fee // TODO: spend EVM balance to compensate vm.txFee-importedAmount @@ -190,13 +193,10 @@ func (vm *VM) newImportTx( outs = append(outs, EVMOutput{ Address: to, Amount: importedAmount - vm.txFee, + Nonce: nonce, }) } - nonce, err := vm.GetAcceptedNonce(to) - if err != nil { - return nil, err - } // Create the transaction utx := &UnsignedImportTx{ NetworkID: vm.ctx.NetworkID, @@ -204,7 +204,6 @@ func (vm *VM) newImportTx( Outs: outs, ImportedInputs: importedInputs, SourceChain: chainID, - nonce: nonce, } tx := &Tx{UnsignedTx: utx} if err := tx.Sign(vm.codec, signers); err != nil { @@ -218,10 +217,10 @@ func (tx *UnsignedImportTx) EVMStateTransfer(state *state.StateDB) error { state.AddBalance(to.Address, new(big.Int).Mul( new(big.Int).SetUint64(to.Amount), x2cRate)) - if state.GetNonce(to.Address) != tx.nonce { + if state.GetNonce(to.Address) != to.Nonce { return errInvalidNonce } - state.SetNonce(to.Address, tx.nonce+1) + state.SetNonce(to.Address, to.Nonce+1) } return nil } diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 7e60c79..789ce56 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -33,6 +33,7 @@ var ( type EVMOutput struct { Address common.Address `serialize:"true" json:"address"` Amount uint64 `serialize:"true" json:"amount"` + Nonce uint64 `serialize:"true" json:"nonce"` } func (out *EVMOutput) Verify() error { diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index f62bc7b..38646c9 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -737,9 +737,14 @@ func (vm *VM) GetSpendableCanonical(keys []*crypto.PrivateKeySECP256K1R, amount if amount < balance { balance = amount } + nonce, err := vm.GetAcceptedNonce(addr) + if err != nil { + return nil, nil, err + } inputs = append(inputs, EVMInput{ Address: addr, Amount: balance, + Nonce: nonce, }) signers = append(signers, []*crypto.PrivateKeySECP256K1R{key}) amount -= balance -- cgit v1.2.3