From 0844c8c6919f6d98ebe8a9501360ef5bc362dff3 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 19 Aug 2020 01:25:20 -0400 Subject: catch up with the new P-Chain cross-chain impl --- plugin/evm/tx.go | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 plugin/evm/tx.go (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go new file mode 100644 index 0000000..024e54d --- /dev/null +++ b/plugin/evm/tx.go @@ -0,0 +1,105 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package evm + +import ( + "fmt" + + "github.com/ava-labs/gecko/database" + "github.com/ava-labs/gecko/database/versiondb" + "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/snow" + "github.com/ava-labs/gecko/utils/codec" + "github.com/ava-labs/gecko/utils/crypto" + "github.com/ava-labs/gecko/utils/hashing" + "github.com/ava-labs/gecko/vms/components/verify" + "github.com/ava-labs/gecko/vms/secp256k1fx" +) + +// UnsignedTx is an unsigned transaction +type UnsignedTx interface { + Initialize(unsignedBytes, signedBytes []byte) + ID() ids.ID + UnsignedBytes() []byte + Bytes() []byte +} + +// UnsignedDecisionTx is an unsigned operation that can be immediately decided +type UnsignedDecisionTx interface { + UnsignedTx + + // Attempts to verify this transaction with the provided state. + SemanticVerify(vm *VM, db database.Database, stx *Tx) ( + onAcceptFunc func() error, + err TxError, + ) +} + +// UnsignedProposalTx is an unsigned operation that can be proposed +type UnsignedProposalTx interface { + UnsignedTx + + // Attempts to verify this transaction with the provided state. + SemanticVerify(vm *VM, db database.Database, stx *Tx) ( + onCommitDB *versiondb.Database, + onAbortDB *versiondb.Database, + onCommitFunc func() error, + onAbortFunc func() error, + err TxError, + ) + InitiallyPrefersCommit(vm *VM) bool +} + +// UnsignedAtomicTx is an unsigned operation that can be atomically accepted +type UnsignedAtomicTx interface { + UnsignedTx + + // UTXOs this tx consumes + InputUTXOs() ids.Set + // Attempts to verify this transaction with the provided state. + SemanticVerify(vm *VM, db database.Database, stx *Tx) TxError + + // Accept this transaction with the additionally provided state transitions. + Accept(ctx *snow.Context, batch database.Batch) error +} + +// Tx is a signed transaction +type Tx struct { + // The body of this transaction + UnsignedTx `serialize:"true" json:"unsignedTx"` + + // The credentials of this transaction + Creds []verify.Verifiable `serialize:"true" json:"credentials"` +} + +// Sign this transaction with the provided signers +func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) error { + unsignedBytes, err := c.Marshal(&tx.UnsignedTx) + if err != nil { + return fmt.Errorf("couldn't marshal UnsignedTx: %w", err) + } + + // Attach credentials + hash := hashing.ComputeHash256(unsignedBytes) + for _, keys := range signers { + cred := &secp256k1fx.Credential{ + Sigs: make([][crypto.SECP256K1RSigLen]byte, len(keys)), + } + for i, key := range keys { + sig, err := key.SignHash(hash) // Sign hash + if err != nil { + return fmt.Errorf("problem generating credential: %w", err) + } + copy(cred.Sigs[i][:], sig) + } + tx.Creds = append(tx.Creds, cred) // Attach credential + } + + signedBytes, err := c.Marshal(tx) + if err != nil { + return fmt.Errorf("couldn't marshal ProposalTx: %w", err) + } + tx.Initialize(unsignedBytes, signedBytes) + return nil +} -- cgit v1.2.3 From 0b934ef60fd652de038a21e6f25691b11cea7121 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 19 Aug 2020 21:20:58 -0400 Subject: remove base_tx.go --- plugin/evm/tx.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 024e54d..6196eb8 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -4,6 +4,7 @@ package evm import ( + "errors" "fmt" "github.com/ava-labs/gecko/database" @@ -15,8 +16,28 @@ import ( "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/vms/components/verify" "github.com/ava-labs/gecko/vms/secp256k1fx" + "github.com/ava-labs/go-ethereum/common" ) +// Max size of memo field +// Don't change without also changing avm.maxMemoSize +const maxMemoSize = 256 + +var ( + errWrongBlockchainID = errors.New("wrong blockchain ID provided") + errWrongNetworkID = errors.New("tx was issued with a different network ID") + errNilTx = errors.New("tx is nil") +) + +type EVMOutput struct { + Address common.Address `serialize:"true" json:"address"` + Amount uint64 `serialize:"true" json:"amount"` +} + +func (out *EVMOutput) Verify() error { + return nil +} + // UnsignedTx is an unsigned transaction type UnsignedTx interface { Initialize(unsignedBytes, signedBytes []byte) -- cgit v1.2.3 From 40879ea67433b73b464bd8b012a9809fbb646cfd Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 19 Aug 2020 23:34:12 -0400 Subject: WIP: C-to-X transfer --- plugin/evm/tx.go | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 6196eb8..90cd232 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/ava-labs/gecko/database" - "github.com/ava-labs/gecko/database/versiondb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/utils/codec" @@ -38,6 +37,12 @@ func (out *EVMOutput) Verify() error { return nil } +type EVMInput EVMOutput + +func (in *EVMInput) Verify() error { + return nil +} + // UnsignedTx is an unsigned transaction type UnsignedTx interface { Initialize(unsignedBytes, signedBytes []byte) @@ -46,32 +51,6 @@ type UnsignedTx interface { Bytes() []byte } -// UnsignedDecisionTx is an unsigned operation that can be immediately decided -type UnsignedDecisionTx interface { - UnsignedTx - - // Attempts to verify this transaction with the provided state. - SemanticVerify(vm *VM, db database.Database, stx *Tx) ( - onAcceptFunc func() error, - err TxError, - ) -} - -// UnsignedProposalTx is an unsigned operation that can be proposed -type UnsignedProposalTx interface { - UnsignedTx - - // Attempts to verify this transaction with the provided state. - SemanticVerify(vm *VM, db database.Database, stx *Tx) ( - onCommitDB *versiondb.Database, - onAbortDB *versiondb.Database, - onCommitFunc func() error, - onAbortFunc func() error, - err TxError, - ) - InitiallyPrefersCommit(vm *VM) bool -} - // UnsignedAtomicTx is an unsigned operation that can be atomically accepted type UnsignedAtomicTx interface { UnsignedTx @@ -79,7 +58,7 @@ type UnsignedAtomicTx interface { // UTXOs this tx consumes InputUTXOs() ids.Set // Attempts to verify this transaction with the provided state. - SemanticVerify(vm *VM, db database.Database, stx *Tx) TxError + SemanticVerify(vm *VM, stx *Tx) TxError // Accept this transaction with the additionally provided state transitions. Accept(ctx *snow.Context, batch database.Batch) error @@ -94,6 +73,8 @@ type Tx struct { Creds []verify.Verifiable `serialize:"true" json:"credentials"` } +// (*secp256k1fx.Credential) + // Sign this transaction with the provided signers func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) error { unsignedBytes, err := c.Marshal(&tx.UnsignedTx) -- cgit v1.2.3 From c6fbdb0dc9453bf4dbf43490d7d83b7a4de2f182 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 19 Aug 2020 23:42:43 -0400 Subject: clean up code --- 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 90cd232..dfd49f0 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" + "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" @@ -62,6 +64,8 @@ type UnsignedAtomicTx interface { // Accept this transaction with the additionally provided state transitions. Accept(ctx *snow.Context, batch database.Batch) error + + EVMStateTransfer(state *state.StateDB) } // Tx is a signed transaction -- cgit v1.2.3 From 0c417aaa02c5cc8bb6a9629ac2502e0c4d200071 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 20 Aug 2020 00:03:12 -0400 Subject: impl state transfer for C-to-X Tx --- plugin/evm/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugin/evm/tx.go') diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index dfd49f0..7e60c79 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -65,7 +65,7 @@ type UnsignedAtomicTx interface { // Accept this transaction with the additionally provided state transitions. Accept(ctx *snow.Context, batch database.Batch) error - EVMStateTransfer(state *state.StateDB) + EVMStateTransfer(state *state.StateDB) error } // Tx is a signed transaction -- cgit v1.2.3 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/tx.go | 1 + 1 file changed, 1 insertion(+) (limited to 'plugin/evm/tx.go') 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 { -- cgit v1.2.3