Skip to content

Commit

Permalink
Removing tx hash fork (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Sep 13, 2023
1 parent 498ef15 commit dcfc044
Show file tree
Hide file tree
Showing 27 changed files with 64 additions and 184 deletions.
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func TestBlockchainWriteBody(t *testing.T) {
},
}

tx.ComputeHash(1)
tx.ComputeHash()
block.Header.ComputeHash()

txFromByTxHash := map[types.Hash]types.Address{}
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestBlockchainWriteBody(t *testing.T) {
},
}

tx.ComputeHash(1)
tx.ComputeHash()
block.Header.ComputeHash()

txFromByTxHash := map[types.Hash]types.Address{}
Expand Down Expand Up @@ -657,7 +657,7 @@ func TestBlockchainWriteBody(t *testing.T) {
},
}

tx.ComputeHash(1)
tx.ComputeHash()
block.Header.ComputeHash()

txFromByTxHash := map[types.Hash]types.Address{
Expand Down Expand Up @@ -692,7 +692,7 @@ func Test_recoverFromFieldsInBlock(t *testing.T) {

computeTxHashes := func(txs ...*types.Transaction) {
for _, tx := range txs {
tx.ComputeHash(1)
tx.ComputeHash()
}
}

Expand Down Expand Up @@ -777,7 +777,7 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) {

computeTxHashes := func(txs ...*types.Transaction) {
for _, tx := range txs {
tx.ComputeHash(1)
tx.ComputeHash()
}
}

Expand Down Expand Up @@ -896,7 +896,7 @@ func TestBlockchainReadBody(t *testing.T) {
V: big.NewInt(1),
}

tx.ComputeHash(1)
tx.ComputeHash()

block := &types.Block{
Header: &types.Header{},
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions blockchain/storage/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion blockchain/storage/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions blockchain/storage/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const (
EIP158 = "EIP158"
EIP155 = "EIP155"
QuorumCalcAlignment = "quorumcalcalignment"
TxHashWithType = "txHashWithType"
LondonFix = "londonfix"
Governance = "governance"
DoubleSignSlashing = "doubleSignSlashing"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -162,7 +160,6 @@ type ForksInTime struct {
EIP158,
EIP155,
QuorumCalcAlignment,
TxHashWithType,
LondonFix,
Governance,
DoubleSignSlashing bool
Expand All @@ -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),
Expand Down
12 changes: 6 additions & 6 deletions consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -803,7 +803,7 @@ func createStateTransactionWithData(blockNumber uint64, target types.Address, in
GasPrice: big.NewInt(0),
}

tx.ComputeHash(blockNumber)
tx.ComputeHash()

return tx
}
20 changes: 10 additions & 10 deletions consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand All @@ -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)
})
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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")
})
Expand Down Expand Up @@ -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")
})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/state_sync_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion consensus/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions contracts/staking/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/debug_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/debug_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func TestTraceCall(t *testing.T) {
}
)

decodedTx.ComputeHash(1)
decodedTx.ComputeHash()

tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ func TestEth_DecodeTxn(t *testing.T) {
t.Parallel()

if tt.res != nil {
tt.res.ComputeHash(1)
tt.res.ComputeHash()
}

store := newMockStore()
for addr, acc := range tt.accounts {
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)
})
Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit dcfc044

Please sign in to comment.