aboutsummaryrefslogtreecommitdiff
path: root/plugin/evm/export_tx.go
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2020-08-20 00:03:12 -0400
committerDeterminant <[email protected]>2020-08-20 00:03:12 -0400
commit0c417aaa02c5cc8bb6a9629ac2502e0c4d200071 (patch)
tree8aa8c257ed613872131527b7ec036e548da60128 /plugin/evm/export_tx.go
parent2d409cb9790e55fd014546222f448786bbefa46b (diff)
impl state transfer for C-to-X Tx
Diffstat (limited to 'plugin/evm/export_tx.go')
-rw-r--r--plugin/evm/export_tx.go24
1 files changed, 22 insertions, 2 deletions
diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go
index 249b7cf..9af11e0 100644
--- a/plugin/evm/export_tx.go
+++ b/plugin/evm/export_tx.go
@@ -5,15 +5,17 @@ package evm
import (
"fmt"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/gecko/database"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow"
"github.com/ava-labs/gecko/utils/crypto"
+ safemath "github.com/ava-labs/gecko/utils/math"
"github.com/ava-labs/gecko/vms/components/avax"
"github.com/ava-labs/gecko/vms/secp256k1fx"
-
- safemath "github.com/ava-labs/gecko/utils/math"
)
// UnsignedExportTx is an unsigned ExportTx
@@ -31,6 +33,8 @@ 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
@@ -144,3 +148,19 @@ func (vm *VM) newExportTx(
}
return tx, utx.Verify(vm.ctx.XChainID, vm.ctx, vm.txFee, vm.ctx.AVAXAssetID)
}
+
+func (tx *UnsignedExportTx) EVMStateTransfer(state *state.StateDB) error {
+ for _, from := range tx.Ins {
+ amount := new(big.Int).Mul(
+ new(big.Int).SetUint64(from.Amount), x2cRate)
+ if state.GetBalance(from.Address).Cmp(amount) < 0 {
+ return errInsufficientFunds
+ }
+ state.SubBalance(from.Address, amount)
+ if state.GetNonce(from.Address) != tx.nonce {
+ return errInvalidNonce
+ }
+ state.SetNonce(from.Address, tx.nonce+1)
+ }
+ return nil
+}