From dcfc044ff123ebcf9727bf27e98fb2372469c8fe Mon Sep 17 00:00:00 2001 From: Igor Crevar Date: Wed, 13 Sep 2023 11:42:53 +0200 Subject: [PATCH] Removing tx hash fork (#1904) --- blockchain/blockchain.go | 2 +- blockchain/blockchain_test.go | 14 +-- blockchain/storage/keyvalue.go | 8 +- blockchain/storage/leveldb/leveldb_test.go | 2 +- blockchain/storage/testing.go | 4 +- chain/params.go | 4 - consensus/polybft/fsm.go | 12 +-- consensus/polybft/fsm_test.go | 20 ++-- consensus/polybft/state_sync_manager_test.go | 2 +- consensus/utils.go | 2 +- contracts/staking/query_test.go | 4 +- jsonrpc/debug_endpoint.go | 2 +- jsonrpc/debug_endpoint_test.go | 2 +- jsonrpc/eth_endpoint.go | 4 +- jsonrpc/eth_endpoint_test.go | 8 +- jsonrpc/eth_txpool_test.go | 4 +- jsonrpc/helper.go | 4 +- jsonrpc/helper_test.go | 4 +- jsonrpc/txpool_endpoint_test.go | 4 +- server/server.go | 5 - txpool/txpool.go | 11 +-- txpool/txpool_test.go | 6 +- types/buildroot/buildroot.go | 6 +- types/rlp_encoding_test.go | 8 +- types/rlp_unmarshal.go | 2 +- types/transaction.go | 7 +- types/transaction_fork_hash.go | 97 -------------------- 27 files changed, 64 insertions(+), 184 deletions(-) delete mode 100644 types/transaction_fork_hash.go diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index b1df0f3f98..bc5cacc7f3 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -703,7 +703,7 @@ func (b *Blockchain) verifyBlockBody(block *types.Block) ([]*types.Receipt, erro } // Make sure the transactions root matches up - if hash := buildroot.CalculateTransactionsRoot(block.Transactions, block.Number()); hash != block.Header.TxRoot { + if hash := buildroot.CalculateTransactionsRoot(block.Transactions); hash != block.Header.TxRoot { b.logger.Error(fmt.Sprintf( "incorrect tx root (expected: %s, actual: %s)", hash, diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index f85d8ea59a..ee42e590b7 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -594,7 +594,7 @@ func TestBlockchainWriteBody(t *testing.T) { }, } - tx.ComputeHash(1) + tx.ComputeHash() block.Header.ComputeHash() txFromByTxHash := map[types.Hash]types.Address{} @@ -625,7 +625,7 @@ func TestBlockchainWriteBody(t *testing.T) { }, } - tx.ComputeHash(1) + tx.ComputeHash() block.Header.ComputeHash() txFromByTxHash := map[types.Hash]types.Address{} @@ -657,7 +657,7 @@ func TestBlockchainWriteBody(t *testing.T) { }, } - tx.ComputeHash(1) + tx.ComputeHash() block.Header.ComputeHash() txFromByTxHash := map[types.Hash]types.Address{ @@ -692,7 +692,7 @@ func Test_recoverFromFieldsInBlock(t *testing.T) { computeTxHashes := func(txs ...*types.Transaction) { for _, tx := range txs { - tx.ComputeHash(1) + tx.ComputeHash() } } @@ -777,7 +777,7 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) { computeTxHashes := func(txs ...*types.Transaction) { for _, tx := range txs { - tx.ComputeHash(1) + tx.ComputeHash() } } @@ -896,7 +896,7 @@ func TestBlockchainReadBody(t *testing.T) { V: big.NewInt(1), } - tx.ComputeHash(1) + tx.ComputeHash() block := &types.Block{ Header: &types.Header{}, @@ -1595,7 +1595,7 @@ func TestBlockchain_WriteFullBlock(t *testing.T) { Value: big.NewInt(1), } - tx.ComputeHash(1) + tx.ComputeHash() header.ComputeHash() existingHeader.ComputeHash() bc.currentHeader.Store(existingHeader) diff --git a/blockchain/storage/keyvalue.go b/blockchain/storage/keyvalue.go index 1b8202a42f..123d22b733 100644 --- a/blockchain/storage/keyvalue.go +++ b/blockchain/storage/keyvalue.go @@ -147,14 +147,8 @@ func (s *KeyValueStorage) ReadBody(hash types.Hash) (*types.Body, error) { return nil, err } - // must read header because block number is needed in order to calculate each tx hash - header := &types.Header{} - if err := s.readRLP(HEADER, hash.Bytes(), header); err != nil { - return nil, err - } - for _, tx := range body.Transactions { - tx.ComputeHash(header.Number) + tx.ComputeHash() } return body, nil diff --git a/blockchain/storage/leveldb/leveldb_test.go b/blockchain/storage/leveldb/leveldb_test.go index eea53de605..95f24c48ae 100644 --- a/blockchain/storage/leveldb/leveldb_test.go +++ b/blockchain/storage/leveldb/leveldb_test.go @@ -70,7 +70,7 @@ func generateTxs(t *testing.T, startNonce, count int, from types.Address, to *ty require.NoError(t, err) - tx.ComputeHash(1) + tx.ComputeHash() txs[i] = tx } diff --git a/blockchain/storage/testing.go b/blockchain/storage/testing.go index 05bcc47bf3..45615363ad 100644 --- a/blockchain/storage/testing.go +++ b/blockchain/storage/testing.go @@ -279,7 +279,7 @@ func testBody(t *testing.T, m PlaceholderStorage) { Input: []byte{1, 2}, V: big.NewInt(1), } - t0.ComputeHash(1) + t0.ComputeHash() addr2 := types.StringToAddress("22") t1 := &types.Transaction{ @@ -291,7 +291,7 @@ func testBody(t *testing.T, m PlaceholderStorage) { Input: []byte{4, 5}, V: big.NewInt(2), } - t1.ComputeHash(1) + t1.ComputeHash() block := types.Block{ Header: header, diff --git a/chain/params.go b/chain/params.go index e1973de6a7..7e2f99aba6 100644 --- a/chain/params.go +++ b/chain/params.go @@ -92,7 +92,6 @@ const ( EIP158 = "EIP158" EIP155 = "EIP155" QuorumCalcAlignment = "quorumcalcalignment" - TxHashWithType = "txHashWithType" LondonFix = "londonfix" Governance = "governance" DoubleSignSlashing = "doubleSignSlashing" @@ -130,7 +129,6 @@ func (f *Forks) At(block uint64) ForksInTime { EIP158: f.IsActive(EIP158, block), EIP155: f.IsActive(EIP155, block), QuorumCalcAlignment: f.IsActive(QuorumCalcAlignment, block), - TxHashWithType: f.IsActive(TxHashWithType, block), LondonFix: f.IsActive(LondonFix, block), Governance: f.IsActive(Governance, block), DoubleSignSlashing: f.IsActive(DoubleSignSlashing, block), @@ -162,7 +160,6 @@ type ForksInTime struct { EIP158, EIP155, QuorumCalcAlignment, - TxHashWithType, LondonFix, Governance, DoubleSignSlashing bool @@ -180,7 +177,6 @@ var AllForksEnabled = &Forks{ Istanbul: NewFork(0), London: NewFork(0), QuorumCalcAlignment: NewFork(0), - TxHashWithType: NewFork(0), LondonFix: NewFork(0), Governance: NewFork(0), DoubleSignSlashing: NewFork(0), diff --git a/consensus/polybft/fsm.go b/consensus/polybft/fsm.go index cf8a23c61f..ed757ed3b8 100644 --- a/consensus/polybft/fsm.go +++ b/consensus/polybft/fsm.go @@ -256,7 +256,7 @@ func (f *fsm) createBridgeCommitmentTx() (*types.Transaction, error) { return nil, fmt.Errorf("failed to encode input data for bridge commitment registration: %w", err) } - return createStateTransactionWithData(f.Height(), contracts.StateReceiverContract, inputData), nil + return createStateTransactionWithData(contracts.StateReceiverContract, inputData), nil } // applySlashingTx adds state transaction to the block to slash the double signing validators @@ -285,7 +285,7 @@ func (f *fsm) createSlashingTx() (*types.Transaction, error) { return nil, fmt.Errorf("failed to encode input data for slashing: %w", err) } - return createStateTransactionWithData(f.Height(), contracts.ValidatorSetContract, inputData), nil + return createStateTransactionWithData(contracts.ValidatorSetContract, inputData), nil } // getValidatorsTransition applies delta to the current validators, @@ -308,7 +308,7 @@ func (f *fsm) createCommitEpochTx() (*types.Transaction, error) { return nil, err } - return createStateTransactionWithData(f.Height(), contracts.ValidatorSetContract, input), nil + return createStateTransactionWithData(contracts.ValidatorSetContract, input), nil } // createDistributeRewardsTx create a StateTransaction, which invokes RewardPool smart contract @@ -319,7 +319,7 @@ func (f *fsm) createDistributeRewardsTx() (*types.Transaction, error) { return nil, err } - return createStateTransactionWithData(f.Height(), contracts.RewardPoolContract, input), nil + return createStateTransactionWithData(contracts.RewardPoolContract, input), nil } // ValidateCommit is used to validate that a given commit is valid @@ -793,7 +793,7 @@ func validateHeaderFields(parent *types.Header, header *types.Header, blockTimeD // createStateTransactionWithData creates a state transaction // with provided target address and inputData parameter which is ABI encoded byte array. -func createStateTransactionWithData(blockNumber uint64, target types.Address, inputData []byte) *types.Transaction { +func createStateTransactionWithData(target types.Address, inputData []byte) *types.Transaction { tx := &types.Transaction{ From: contracts.SystemCaller, To: &target, @@ -803,7 +803,7 @@ func createStateTransactionWithData(blockNumber uint64, target types.Address, in GasPrice: big.NewInt(0), } - tx.ComputeHash(blockNumber) + tx.ComputeHash() return tx } diff --git a/consensus/polybft/fsm_test.go b/consensus/polybft/fsm_test.go index 91c6bef968..35105042c0 100644 --- a/consensus/polybft/fsm_test.go +++ b/consensus/polybft/fsm_test.go @@ -541,7 +541,7 @@ func TestFSM_VerifyStateTransactions_CommitEpoch(t *testing.T) { commitEpochInput, err := createTestCommitEpochInput(t, 1, 5).EncodeAbi() require.NoError(t, err) - commitEpochTx := createStateTransactionWithData(10, contracts.ValidatorSetContract, commitEpochInput) + commitEpochTx := createStateTransactionWithData(contracts.ValidatorSetContract, commitEpochInput) err = fsm.VerifyStateTransactions([]*types.Transaction{commitEpochTx}) require.ErrorContains(t, err, "invalid commit epoch transaction") }) @@ -565,7 +565,7 @@ func TestFSM_VerifyStateTransactions_CommitEpoch(t *testing.T) { input, err := commitEpochTxTwo.EncodeAbi() require.NoError(t, err) - txs[1] = createStateTransactionWithData(10, types.ZeroAddress, input) + txs[1] = createStateTransactionWithData(types.ZeroAddress, input) assert.ErrorIs(t, fsm.VerifyStateTransactions(txs), errCommitEpochTxSingleExpected) }) @@ -683,7 +683,7 @@ func TestFSM_VerifyStateTransactions_SlashingTx(t *testing.T) { inputData, err := differentDoubleSigners.EncodeAbi() require.NoError(t, err) - slashingTx3 := createStateTransactionWithData(fsm.Height(), contracts.ValidatorSetContract, inputData) + slashingTx3 := createStateTransactionWithData(contracts.ValidatorSetContract, inputData) err = fsm.VerifyStateTransactions([]*types.Transaction{slashingTx3}) assert.ErrorContains(t, err, "invalid slashing transaction") @@ -732,7 +732,7 @@ func TestFSM_VerifyStateTransaction_Commitments(t *testing.T) { require.NoError(t, err) if i == 0 { - tx := createStateTransactionWithData(10, contracts.StateReceiverContract, inputData) + tx := createStateTransactionWithData(contracts.StateReceiverContract, inputData) txns = append(txns, tx) } } @@ -822,7 +822,7 @@ func TestFSM_VerifyStateTransaction_Commitments(t *testing.T) { encodedCommitment, err := createTestCommitmentMessage(t, 1).EncodeAbi() require.NoError(t, err) - tx := createStateTransactionWithData(0, contracts.StateReceiverContract, encodedCommitment) + tx := createStateTransactionWithData(contracts.StateReceiverContract, encodedCommitment) assert.ErrorContains(t, fsm.VerifyStateTransactions([]*types.Transaction{tx}), "found commitment tx in block which should not contain it") }) @@ -853,12 +853,12 @@ func TestFSM_VerifyStateTransaction_Commitments(t *testing.T) { require.NoError(t, err) txns = append(txns, - createStateTransactionWithData(0, contracts.StateReceiverContract, inputData)) + createStateTransactionWithData(contracts.StateReceiverContract, inputData)) inputData, err = commitmentMessageSigned.EncodeAbi() require.NoError(t, err) txns = append(txns, - createStateTransactionWithData(0, contracts.StateReceiverContract, inputData)) + createStateTransactionWithData(contracts.StateReceiverContract, inputData)) err = f.VerifyStateTransactions(txns) require.ErrorContains(t, err, "only one commitment tx is allowed per block") }) @@ -1084,7 +1084,7 @@ func TestFSM_Validate_EpochEndingBlock_MismatchInDeltas(t *testing.T) { stateBlock.Block.Header.ParentHash = parent.Hash stateBlock.Block.Header.Timestamp = uint64(time.Now().UTC().Unix()) stateBlock.Block.Transactions = []*types.Transaction{ - createStateTransactionWithData(0, contracts.ValidatorSetContract, commitEpochTxInput), + createStateTransactionWithData(contracts.ValidatorSetContract, commitEpochTxInput), } proposal := stateBlock.Block.MarshalRLP() @@ -1560,7 +1560,7 @@ func TestFSM_DecodeCommitEpochStateTx(t *testing.T) { require.NoError(t, err) require.NotNil(t, input) - tx := createStateTransactionWithData(1, contracts.ValidatorSetContract, input) + tx := createStateTransactionWithData(contracts.ValidatorSetContract, input) decodedInputData, err := decodeStateTransaction(tx.Input) require.NoError(t, err) @@ -1581,7 +1581,7 @@ func TestFSM_VerifyStateTransaction_InvalidTypeOfStateTransactions(t *testing.T) var txns []*types.Transaction txns = append(txns, - createStateTransactionWithData(1, contracts.StateReceiverContract, []byte{9, 3, 1, 1})) + createStateTransactionWithData(contracts.StateReceiverContract, []byte{9, 3, 1, 1})) require.ErrorContains(t, f.VerifyStateTransactions(txns), "unknown state transaction") } diff --git a/consensus/polybft/state_sync_manager_test.go b/consensus/polybft/state_sync_manager_test.go index 407b5760be..a31d4d6d61 100644 --- a/consensus/polybft/state_sync_manager_test.go +++ b/consensus/polybft/state_sync_manager_test.go @@ -305,7 +305,7 @@ func TestStateSyncerManager_BuildProofs(t *testing.T) { txData, err := mockMsg.EncodeAbi() require.NoError(t, err) - tx := createStateTransactionWithData(1, types.Address{}, txData) + tx := createStateTransactionWithData(types.Address{}, txData) req := &common.PostBlockRequest{ FullBlock: &types.FullBlock{ diff --git a/consensus/utils.go b/consensus/utils.go index cafa23316a..093c3d9202 100644 --- a/consensus/utils.go +++ b/consensus/utils.go @@ -20,7 +20,7 @@ func BuildBlock(params BuildBlockParams) *types.Block { if len(txs) == 0 { header.TxRoot = types.EmptyRootHash } else { - header.TxRoot = buildroot.CalculateTransactionsRoot(txs, header.Number) + header.TxRoot = buildroot.CalculateTransactionsRoot(txs) } if len(params.Receipts) == 0 { diff --git a/contracts/staking/query_test.go b/contracts/staking/query_test.go index 1f6147bb76..60a4aa13e2 100644 --- a/contracts/staking/query_test.go +++ b/contracts/staking/query_test.go @@ -48,7 +48,7 @@ func (m *TxMock) Apply(tx *types.Transaction) (*runtime.ExecutionResult, error) return nil, nil } - tx.ComputeHash(1) + tx.ComputeHash() res, ok := m.hashToRes[tx.Hash] if ok { @@ -203,7 +203,7 @@ func TestQueryValidators(t *testing.T) { mock := &TxMock{ hashToRes: map[types.Hash]*runtime.ExecutionResult{ - tt.mockArgs.tx.ComputeHash(1).Hash: tt.mockReturns.res, + tt.mockArgs.tx.ComputeHash().Hash: tt.mockReturns.res, }, nonce: map[types.Address]uint64{ tt.mockArgs.addr: tt.mockReturns.nonce, diff --git a/jsonrpc/debug_endpoint.go b/jsonrpc/debug_endpoint.go index 577b9a1622..8c486bba6a 100644 --- a/jsonrpc/debug_endpoint.go +++ b/jsonrpc/debug_endpoint.go @@ -186,7 +186,7 @@ func (d *Debug) TraceCall( return nil, ErrHeaderNotFound } - tx, err := DecodeTxn(arg, header.Number, d.store, true) + tx, err := DecodeTxn(arg, d.store, true) if err != nil { return nil, err } diff --git a/jsonrpc/debug_endpoint_test.go b/jsonrpc/debug_endpoint_test.go index 13c3868a94..bb9e051d1d 100644 --- a/jsonrpc/debug_endpoint_test.go +++ b/jsonrpc/debug_endpoint_test.go @@ -600,7 +600,7 @@ func TestTraceCall(t *testing.T) { } ) - decodedTx.ComputeHash(1) + decodedTx.ComputeHash() tests := []struct { name string diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index b6d209adb6..7396711a12 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -486,7 +486,7 @@ func (e *Eth) Call(arg *txnArgs, filter BlockNumberOrHash, apiOverride *stateOve return nil, err } - transaction, err := DecodeTxn(arg, header.Number, e.store, true) + transaction, err := DecodeTxn(arg, e.store, true) if err != nil { return nil, err } @@ -541,7 +541,7 @@ func (e *Eth) EstimateGas(arg *txnArgs, rawNum *BlockNumber) (interface{}, error } // testTransaction should execute tx with nonce always set to the current expected nonce for the account - transaction, err := DecodeTxn(arg, header.Number, e.store, true) + transaction, err := DecodeTxn(arg, e.store, true) if err != nil { return nil, err } diff --git a/jsonrpc/eth_endpoint_test.go b/jsonrpc/eth_endpoint_test.go index 65ebc743cc..19a71f6af7 100644 --- a/jsonrpc/eth_endpoint_test.go +++ b/jsonrpc/eth_endpoint_test.go @@ -161,7 +161,7 @@ func TestEth_DecodeTxn(t *testing.T) { t.Parallel() if tt.res != nil { - tt.res.ComputeHash(1) + tt.res.ComputeHash() } store := newMockStore() @@ -169,7 +169,7 @@ func TestEth_DecodeTxn(t *testing.T) { store.SetAccount(addr, acc) } - res, err := DecodeTxn(tt.arg, 1, store, false) + res, err := DecodeTxn(tt.arg, store, false) assert.Equal(t, tt.res, res) assert.Equal(t, tt.err, err) }) @@ -289,9 +289,9 @@ func TestEth_TxnType(t *testing.T) { Nonce: 0, Type: types.DynamicFeeTx, } - res, err := DecodeTxn(args, 1, store, false) + res, err := DecodeTxn(args, store, false) - expectedRes.ComputeHash(1) + expectedRes.ComputeHash() assert.NoError(t, err) assert.Equal(t, expectedRes, res) } diff --git a/jsonrpc/eth_txpool_test.go b/jsonrpc/eth_txpool_test.go index d7f9d109e3..39099cdbac 100644 --- a/jsonrpc/eth_txpool_test.go +++ b/jsonrpc/eth_txpool_test.go @@ -16,7 +16,7 @@ func TestEth_TxnPool_SendRawTransaction(t *testing.T) { From: addr0, V: big.NewInt(1), } - txn.ComputeHash(1) + txn.ComputeHash() data := txn.MarshalRLP() _, err := eth.SendRawTransaction(data) @@ -55,7 +55,7 @@ type mockStoreTxn struct { func (m *mockStoreTxn) AddTx(tx *types.Transaction) error { m.txn = tx - tx.ComputeHash(1) + tx.ComputeHash() return nil } diff --git a/jsonrpc/helper.go b/jsonrpc/helper.go index bd0d72b531..eabfbbdcfb 100644 --- a/jsonrpc/helper.go +++ b/jsonrpc/helper.go @@ -162,7 +162,7 @@ func GetNextNonce(address types.Address, number BlockNumber, store nonceGetter) return acc.Nonce, nil } -func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) { +func DecodeTxn(arg *txnArgs, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) { if arg == nil { return nil, errors.New("missing value for required argument 0") } @@ -235,7 +235,7 @@ func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonc txn.To = arg.To } - txn.ComputeHash(blockNumber) + txn.ComputeHash() return txn, nil } diff --git a/jsonrpc/helper_test.go b/jsonrpc/helper_test.go index 1f2d4f64de..5cc2b06490 100644 --- a/jsonrpc/helper_test.go +++ b/jsonrpc/helper_test.go @@ -825,11 +825,11 @@ func TestDecodeTxn(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - tx, err := DecodeTxn(test.arg, 1, test.store, false) + tx, err := DecodeTxn(test.arg, test.store, false) // DecodeTxn computes hash of tx if !test.err { - test.expected.ComputeHash(1) + test.expected.ComputeHash() } assert.Equal(t, test.expected, tx) diff --git a/jsonrpc/txpool_endpoint_test.go b/jsonrpc/txpool_endpoint_test.go index a52f7dc2de..6e4e4cd5b3 100644 --- a/jsonrpc/txpool_endpoint_test.go +++ b/jsonrpc/txpool_endpoint_test.go @@ -308,7 +308,7 @@ func newTestTransaction(nonce uint64, from types.Address) *types.Transaction { S: big.NewInt(1), } - txn.ComputeHash(1) + txn.ComputeHash() return txn } @@ -330,7 +330,7 @@ func newTestDynamicFeeTransaction(nonce uint64, from types.Address) *types.Trans S: big.NewInt(1), } - txn.ComputeHash(1) + txn.ComputeHash() return txn } diff --git a/server/server.go b/server/server.go index 32aebe669a..7a6299f9d2 100644 --- a/server/server.go +++ b/server/server.go @@ -1041,11 +1041,6 @@ func initForkManager(engineName string, config *chain.Chain) error { fm.RegisterFork(name, f.Params) } - // Register handlers and additional forks here - if err := types.RegisterTxHashFork(chain.TxHashWithType); err != nil { - return err - } - // Register Handler for London fork fix if err := state.RegisterLondonFixFork(chain.LondonFix); err != nil { return err diff --git a/txpool/txpool.go b/txpool/txpool.go index 564cfc96fe..9ebe199add 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -9,7 +9,6 @@ import ( "github.com/0xPolygon/polygon-edge/blockchain" "github.com/0xPolygon/polygon-edge/chain" - "github.com/0xPolygon/polygon-edge/forkmanager" "github.com/0xPolygon/polygon-edge/network" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/state/runtime" @@ -624,14 +623,6 @@ func (p *TxPool) validateTx(tx *types.Transaction) error { return ErrTxTypeNotSupported } - // DynamicFeeTx should be rejected if TxHashWithType fork is registered but not enabled for current block - blockNumber, err := forkmanager.GetInstance().GetForkBlock(chain.TxHashWithType) - if err == nil && blockNumber > currentBlockNumber { - metrics.IncrCounter([]string{txPoolMetrics, "dynamic_tx_not_allowed"}, 1) - - return ErrDynamicTxNotAllowed - } - // Check EIP-1559-related fields and make sure they are correct if tx.GasFeeCap == nil || tx.GasTipCap == nil { metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) @@ -782,7 +773,7 @@ func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error { } // calculate tx hash - tx.ComputeHash(p.store.Header().Number) + tx.ComputeHash() // initialize account for this address once or retrieve existing one account := p.getOrCreateAccount(tx.From) diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index b1ff4063f5..7dee8f1fce 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -3409,7 +3409,7 @@ func TestBatchTx_SingleAccount(t *testing.T) { go func(i uint64) { tx := newTx(addr, i, 1) - tx.ComputeHash(1) + tx.ComputeHash() // add transaction hash to map mux.Lock() @@ -3614,7 +3614,7 @@ func TestAddTx_TxReplacement(t *testing.T) { singedTx, err := poolSigner.SignTx(tx, key) require.NoError(t, err) - singedTx.ComputeHash(0) + singedTx.ComputeHash() return singedTx } @@ -3631,7 +3631,7 @@ func TestAddTx_TxReplacement(t *testing.T) { singedTx, err := poolSigner.SignTx(tx, key) require.NoError(t, err) - singedTx.ComputeHash(0) + singedTx.ComputeHash() return singedTx } diff --git a/types/buildroot/buildroot.go b/types/buildroot/buildroot.go index ad99ffbfa1..613e33987c 100644 --- a/types/buildroot/buildroot.go +++ b/types/buildroot/buildroot.go @@ -25,11 +25,9 @@ func CalculateReceiptsRoot(receipts []*types.Receipt) types.Hash { } // CalculateTransactionsRoot calculates the root of a list of transactions -func CalculateTransactionsRoot(transactions []*types.Transaction, blockNumber uint64) types.Hash { - handler := types.GetTransactionHashHandler(blockNumber) - +func CalculateTransactionsRoot(transactions []*types.Transaction) types.Hash { return CalculateRoot(len(transactions), func(indx int) []byte { - return handler.SerializeForRootCalculation(transactions[indx], &arenaPool) + return transactions[indx].MarshalRLPTo(nil) }) } diff --git a/types/rlp_encoding_test.go b/types/rlp_encoding_test.go index aaf5724afb..be4ef7c446 100644 --- a/types/rlp_encoding_test.go +++ b/types/rlp_encoding_test.go @@ -55,7 +55,7 @@ func TestRLPMarshall_And_Unmarshall_Transaction(t *testing.T) { R: big.NewInt(27), } - txn.ComputeHash(1) + txn.ComputeHash() unmarshalledTxn := new(Transaction) marshaledRlp := txn.MarshalRLP() @@ -64,7 +64,7 @@ func TestRLPMarshall_And_Unmarshall_Transaction(t *testing.T) { t.Fatal(err) } - unmarshalledTxn.ComputeHash(1) + unmarshalledTxn.ComputeHash() assert.Equal(t, txn, unmarshalledTxn, "[ERROR] Unmarshalled transaction not equal to base transaction") } @@ -161,14 +161,14 @@ func TestRLPMarshall_And_Unmarshall_TypedTransaction(t *testing.T) { for _, v := range txTypes { t.Run(v.String(), func(t *testing.T) { originalTx.Type = v - originalTx.ComputeHash(1) + originalTx.ComputeHash() txRLP := originalTx.MarshalRLP() unmarshalledTx := new(Transaction) assert.NoError(t, unmarshalledTx.UnmarshalRLP(txRLP)) - unmarshalledTx.ComputeHash(1) + unmarshalledTx.ComputeHash() assert.Equal(t, originalTx.Type, unmarshalledTx.Type) assert.Equal(t, originalTx.Hash, unmarshalledTx.Hash) }) diff --git a/types/rlp_unmarshal.go b/types/rlp_unmarshal.go index 52a9e55977..94f106e63e 100644 --- a/types/rlp_unmarshal.go +++ b/types/rlp_unmarshal.go @@ -113,7 +113,7 @@ func (b *Block) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { return err } - bTxn = bTxn.ComputeHash(b.Header.Number) + bTxn = bTxn.ComputeHash() b.Transactions = append(b.Transactions, bTxn) diff --git a/types/transaction.go b/types/transaction.go index 126545f585..db60ac6652 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -6,6 +6,7 @@ import ( "sync/atomic" "github.com/0xPolygon/polygon-edge/helper/common" + "github.com/0xPolygon/polygon-edge/helper/keccak" ) const ( @@ -75,8 +76,10 @@ func (t *Transaction) IsContractCreation() bool { } // ComputeHash computes the hash of the transaction -func (t *Transaction) ComputeHash(blockNumber uint64) *Transaction { - GetTransactionHashHandler(blockNumber).ComputeHash(t) +func (t *Transaction) ComputeHash() *Transaction { + hash := keccak.DefaultKeccakPool.Get() + hash.WriteFn(t.Hash[:0], t.MarshalRLPTo) + keccak.DefaultKeccakPool.Put(hash) return t } diff --git a/types/transaction_fork_hash.go b/types/transaction_fork_hash.go deleted file mode 100644 index 6c84461e5d..0000000000 --- a/types/transaction_fork_hash.go +++ /dev/null @@ -1,97 +0,0 @@ -package types - -import ( - "math/big" - - "github.com/0xPolygon/polygon-edge/forkmanager" - "github.com/0xPolygon/polygon-edge/helper/keccak" - "github.com/umbracle/fastrlp" -) - -const txHashHandler = "txHash" - -type TransactionHashFork interface { - SerializeForRootCalculation(*Transaction, *fastrlp.ArenaPool) []byte - ComputeHash(*Transaction) -} - -var ( - _ TransactionHashFork = (*TransactionHashForkV1)(nil) - _ TransactionHashFork = (*TransactionHashForkV2)(nil) -) - -type TransactionHashForkV1 struct { -} - -func (th *TransactionHashForkV1) SerializeForRootCalculation(t *Transaction, ap *fastrlp.ArenaPool) []byte { - ar := ap.Get() - chainID := t.ChainID - t.ChainID = big.NewInt(0) - - defer func() { - ap.Put(ar) - - t.ChainID = chainID - }() - - ar.Reset() - - return t.MarshalRLPWith(ar).MarshalTo(nil) -} - -func (th *TransactionHashForkV1) ComputeHash(t *Transaction) { - ar := marshalArenaPool.Get() - hash := keccak.DefaultKeccakPool.Get() - - chainID := t.ChainID - t.ChainID = big.NewInt(0) - - v := t.MarshalRLPWith(ar) - hash.WriteRlp(t.Hash[:0], v) - - t.ChainID = chainID - - marshalArenaPool.Put(ar) - keccak.DefaultKeccakPool.Put(hash) -} - -type TransactionHashForkV2 struct { -} - -func (th *TransactionHashForkV2) SerializeForRootCalculation(t *Transaction, _ *fastrlp.ArenaPool) []byte { - return t.MarshalRLPTo(nil) -} - -func (th *TransactionHashForkV2) ComputeHash(t *Transaction) { - hash := keccak.DefaultKeccakPool.Get() - hash.WriteFn(t.Hash[:0], t.MarshalRLPTo) - keccak.DefaultKeccakPool.Put(hash) -} - -func RegisterTxHashFork(txHashWithTypeFork string) error { - fh := forkmanager.GetInstance() - - if err := fh.RegisterHandler( - forkmanager.InitialFork, txHashHandler, &TransactionHashForkV1{}); err != nil { - return err - } - - if fh.IsForkRegistered(txHashWithTypeFork) { - if err := fh.RegisterHandler( - txHashWithTypeFork, txHashHandler, &TransactionHashForkV2{}); err != nil { - return err - } - } - - return nil -} - -func GetTransactionHashHandler(blockNumber uint64) TransactionHashFork { - if h := forkmanager.GetInstance().GetHandler(txHashHandler, blockNumber); h != nil { - //nolint:forcetypeassert - return h.(TransactionHashFork) - } - - // because of tests - return &TransactionHashForkV1{} -}