From 5c02d0c77bb9e81596a7787b9c4f23f8fe4091e5 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Wed, 20 Dec 2023 11:32:13 -0500 Subject: [PATCH] Initialize transactions once (#2521) --- genesis/genesis.go | 2 +- vms/avm/block/parser.go | 22 ---------------------- vms/avm/block/standard_block.go | 13 ++++++++++++- vms/avm/environment_test.go | 2 +- vms/avm/network/network_test.go | 2 +- vms/avm/service_test.go | 2 +- vms/avm/state/state_test.go | 4 ++-- vms/avm/txs/base_tx_test.go | 2 +- vms/avm/txs/create_asset_tx_test.go | 4 ++-- vms/avm/txs/export_tx_test.go | 2 +- vms/avm/txs/import_tx_test.go | 2 +- vms/avm/txs/parser.go | 27 --------------------------- vms/avm/vm.go | 2 +- vms/avm/vm_regression_test.go | 2 +- vms/avm/vm_test.go | 6 +++--- 15 files changed, 28 insertions(+), 66 deletions(-) diff --git a/genesis/genesis.go b/genesis/genesis.go index 533795b56973..567f21f061be 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -571,7 +571,7 @@ func AVAXAssetID(avmGenesisBytes []byte) (ids.ID, error) { genesisTx := genesis.Txs[0] tx := xchaintxs.Tx{Unsigned: &genesisTx.CreateAssetTx} - if err := parser.InitializeGenesisTx(&tx); err != nil { + if err := tx.Initialize(genesisCodec); err != nil { return ids.Empty, err } return tx.ID(), nil diff --git a/vms/avm/block/parser.go b/vms/avm/block/parser.go index 230568149b6d..2394ea8dec0a 100644 --- a/vms/avm/block/parser.go +++ b/vms/avm/block/parser.go @@ -4,7 +4,6 @@ package block import ( - "fmt" "reflect" "github.com/ava-labs/avalanchego/codec" @@ -25,9 +24,6 @@ type Parser interface { ParseBlock(bytes []byte) (Block, error) ParseGenesisBlock(bytes []byte) (Block, error) - - InitializeBlock(block Block) error - InitializeGenesisBlock(block Block) error } type parser struct { @@ -88,21 +84,3 @@ func parse(cm codec.Manager, bytes []byte) (Block, error) { } return blk, blk.initialize(bytes, cm) } - -func (p *parser) InitializeBlock(block Block) error { - return initialize(block, p.Codec()) -} - -func (p *parser) InitializeGenesisBlock(block Block) error { - return initialize(block, p.GenesisCodec()) -} - -func initialize(blk Block, cm codec.Manager) error { - // We serialize this block as a pointer so that it can be deserialized into - // a Block - bytes, err := cm.Marshal(CodecVersion, &blk) - if err != nil { - return fmt.Errorf("couldn't marshal block: %w", err) - } - return blk.initialize(bytes, cm) -} diff --git a/vms/avm/block/standard_block.go b/vms/avm/block/standard_block.go index be6f1c7456cd..98c676c7ea52 100644 --- a/vms/avm/block/standard_block.go +++ b/vms/avm/block/standard_block.go @@ -88,5 +88,16 @@ func NewStandardBlock( Time: uint64(timestamp.Unix()), Transactions: txs, } - return blk, initialize(blk, cm) + + // We serialize this block as a pointer so that it can be deserialized into + // a Block + var blkIntf Block = blk + bytes, err := cm.Marshal(CodecVersion, &blkIntf) + if err != nil { + return nil, fmt.Errorf("couldn't marshal block: %w", err) + } + + blk.BlockID = hashing.ComputeHash256Array(bytes) + blk.bytes = bytes + return blk, nil } diff --git a/vms/avm/environment_test.go b/vms/avm/environment_test.go index 96a84b2659a7..a5b45b943ad9 100644 --- a/vms/avm/environment_test.go +++ b/vms/avm/environment_test.go @@ -289,7 +289,7 @@ func getCreateTxFromGenesisTest(tb testing.TB, genesisBytes []byte, assetName st tx := &txs.Tx{ Unsigned: &assetTx.CreateAssetTx, } - require.NoError(parser.InitializeGenesisTx(tx)) + require.NoError(tx.Initialize(parser.GenesisCodec())) return tx } diff --git a/vms/avm/network/network_test.go b/vms/avm/network/network_test.go index 0217430a2d22..b900232173f2 100644 --- a/vms/avm/network/network_test.go +++ b/vms/avm/network/network_test.go @@ -45,7 +45,7 @@ func TestNetworkAppGossip(t *testing.T) { &secp256k1fx.Fx{}, }) require.NoError(t, err) - require.NoError(t, parser.InitializeTx(testTx)) + require.NoError(t, testTx.Initialize(parser.Codec())) type test struct { name string diff --git a/vms/avm/service_test.go b/vms/avm/service_test.go index 19cacb158d13..f2928ea2c0d9 100644 --- a/vms/avm/service_test.go +++ b/vms/avm/service_test.go @@ -918,7 +918,7 @@ func newAvaxExportTxWithOutputs(t *testing.T, genesisBytes []byte, vm *VM) *txs. func newAvaxCreateAssetTxWithOutputs(t *testing.T, vm *VM) *txs.Tx { key := keys[0] tx := buildCreateAssetTx(key) - require.NoError(t, vm.parser.InitializeTx(tx)) + require.NoError(t, tx.Initialize(vm.parser.Codec())) return tx } diff --git a/vms/avm/state/state_test.go b/vms/avm/state/state_test.go index c97836aee794..a9d468d436bb 100644 --- a/vms/avm/state/state_test.go +++ b/vms/avm/state/state_test.go @@ -61,7 +61,7 @@ func init() { populatedTx = &txs.Tx{Unsigned: &txs.BaseTx{BaseTx: avax.BaseTx{ BlockchainID: ids.GenerateTestID(), }}} - err = parser.InitializeTx(populatedTx) + err = populatedTx.Initialize(parser.Codec()) if err != nil { panic(err) } @@ -197,7 +197,7 @@ func ChainTxTest(t *testing.T, c Chain) { tx := &txs.Tx{Unsigned: &txs.BaseTx{BaseTx: avax.BaseTx{ BlockchainID: ids.GenerateTestID(), }}} - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) txID := tx.ID() _, err = c.GetTx(txID) diff --git a/vms/avm/txs/base_tx_test.go b/vms/avm/txs/base_tx_test.go index 6ec386a7ab8a..ed1665c707fb 100644 --- a/vms/avm/txs/base_tx_test.go +++ b/vms/avm/txs/base_tx_test.go @@ -130,7 +130,7 @@ func TestBaseTxSerialization(t *testing.T) { }) require.NoError(err) - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) require.Equal(tx.ID().String(), "zeqT8FTnRAxes7QQQYkaWhNkHavd9d6aCdH8TQu2Mx5KEydEz") result := tx.Bytes() diff --git a/vms/avm/txs/create_asset_tx_test.go b/vms/avm/txs/create_asset_tx_test.go index 08d0c46f4d54..dfcfb27c1bdd 100644 --- a/vms/avm/txs/create_asset_tx_test.go +++ b/vms/avm/txs/create_asset_tx_test.go @@ -198,7 +198,7 @@ func TestCreateAssetTxSerialization(t *testing.T) { }) require.NoError(err) - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) result := tx.Bytes() require.Equal(expected, result) @@ -366,7 +366,7 @@ func TestCreateAssetTxSerializationAgain(t *testing.T) { &secp256k1fx.Fx{}, }) require.NoError(err) - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) result := tx.Bytes() require.Equal(expected, result) diff --git a/vms/avm/txs/export_tx_test.go b/vms/avm/txs/export_tx_test.go index a7c1ed16196f..e384b8bfc327 100644 --- a/vms/avm/txs/export_tx_test.go +++ b/vms/avm/txs/export_tx_test.go @@ -113,7 +113,7 @@ func TestExportTxSerialization(t *testing.T) { }) require.NoError(err) - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) require.Equal(tx.ID().String(), "2PKJE4TrKYpgynBFCpNPpV3GHK7d9QTgrL5mpYG6abHKDvNBG3") result := tx.Bytes() diff --git a/vms/avm/txs/import_tx_test.go b/vms/avm/txs/import_tx_test.go index 4172a4047792..6b94ea1c7e11 100644 --- a/vms/avm/txs/import_tx_test.go +++ b/vms/avm/txs/import_tx_test.go @@ -113,7 +113,7 @@ func TestImportTxSerialization(t *testing.T) { }) require.NoError(err) - require.NoError(parser.InitializeTx(tx)) + require.NoError(tx.Initialize(parser.Codec())) require.Equal(tx.ID().String(), "9wdPb5rsThXYLX4WxkNeyYrNMfDE5cuWLgifSjxKiA2dCmgCZ") result := tx.Bytes() diff --git a/vms/avm/txs/parser.go b/vms/avm/txs/parser.go index def42dfed501..55722c75b9cf 100644 --- a/vms/avm/txs/parser.go +++ b/vms/avm/txs/parser.go @@ -31,9 +31,6 @@ type Parser interface { ParseTx(bytes []byte) (*Tx, error) ParseGenesisTx(bytes []byte) (*Tx, error) - - InitializeTx(tx *Tx) error - InitializeGenesisTx(tx *Tx) error } type parser struct { @@ -130,14 +127,6 @@ func (p *parser) ParseGenesisTx(bytes []byte) (*Tx, error) { return parse(p.gcm, bytes) } -func (p *parser) InitializeTx(tx *Tx) error { - return initializeTx(p.cm, tx) -} - -func (p *parser) InitializeGenesisTx(tx *Tx) error { - return initializeTx(p.gcm, tx) -} - func parse(cm codec.Manager, signedBytes []byte) (*Tx, error) { tx := &Tx{} parsedVersion, err := cm.Unmarshal(signedBytes, tx) @@ -157,19 +146,3 @@ func parse(cm codec.Manager, signedBytes []byte) (*Tx, error) { tx.SetBytes(unsignedBytes, signedBytes) return tx, nil } - -func initializeTx(cm codec.Manager, tx *Tx) error { - signedBytes, err := cm.Marshal(CodecVersion, tx) - if err != nil { - return fmt.Errorf("problem creating transaction: %w", err) - } - - unsignedBytesLen, err := cm.Size(CodecVersion, &tx.Unsigned) - if err != nil { - return fmt.Errorf("couldn't calculate UnsignedTx marshal length: %w", err) - } - - unsignedBytes := signedBytes[:unsignedBytesLen] - tx.SetBytes(unsignedBytes, signedBytes) - return nil -} diff --git a/vms/avm/vm.go b/vms/avm/vm.go index 36049befd07c..72951a5890bf 100644 --- a/vms/avm/vm.go +++ b/vms/avm/vm.go @@ -534,7 +534,7 @@ func (vm *VM) initGenesis(genesisBytes []byte) error { tx := &txs.Tx{ Unsigned: &genesisTx.CreateAssetTx, } - if err := vm.parser.InitializeGenesisTx(tx); err != nil { + if err := tx.Initialize(genesisCodec); err != nil { return err } diff --git a/vms/avm/vm_regression_test.go b/vms/avm/vm_regression_test.go index 6c1dd1be1798..69030b737830 100644 --- a/vms/avm/vm_regression_test.go +++ b/vms/avm/vm_regression_test.go @@ -66,7 +66,7 @@ func TestVerifyFxUsage(t *testing.T) { }, }, }} - require.NoError(env.vm.parser.InitializeTx(createAssetTx)) + require.NoError(createAssetTx.Initialize(env.vm.parser.Codec())) issueAndAccept(require, env.vm, env.issuer, createAssetTx) mintNFTTx := &txs.Tx{Unsigned: &txs.OperationTx{ diff --git a/vms/avm/vm_test.go b/vms/avm/vm_test.go index 3b4dc9558807..e59333f03072 100644 --- a/vms/avm/vm_test.go +++ b/vms/avm/vm_test.go @@ -167,7 +167,7 @@ func TestIssueNFT(t *testing.T) { }, }}, }} - require.NoError(env.vm.parser.InitializeTx(createAssetTx)) + require.NoError(createAssetTx.Initialize(env.vm.parser.Codec())) issueAndAccept(require, env.vm, env.issuer, createAssetTx) mintNFTTx := &txs.Tx{Unsigned: &txs.OperationTx{ @@ -222,7 +222,7 @@ func TestIssueNFT(t *testing.T) { }, }, } - require.NoError(env.vm.parser.InitializeTx(transferNFTTx)) + require.NoError(transferNFTTx.Initialize(env.vm.parser.Codec())) issueAndAccept(require, env.vm, env.issuer, transferNFTTx) } @@ -262,7 +262,7 @@ func TestIssueProperty(t *testing.T) { }, }}, }} - require.NoError(env.vm.parser.InitializeTx(createAssetTx)) + require.NoError(createAssetTx.Initialize(env.vm.parser.Codec())) issueAndAccept(require, env.vm, env.issuer, createAssetTx) mintPropertyTx := &txs.Tx{Unsigned: &txs.OperationTx{