eth/tracers: refactor transaction creation and improve bad block setup

This commit refactors the transaction creation logic in `createTestTransactions` to use a range loop for better readability. It also updates the bad block setup to use the latest parent block's properties consistently. Additionally, it removes unused functions and comments to clean up the code.

Signed-off-by: katsumata <12413150+winor30@users.noreply.github.com>
This commit is contained in:
katsumata 2025-06-09 02:01:35 +09:00
parent 0e136dbf48
commit 297c4e7fd1
No known key found for this signature in database
GPG key ID: 8987A425F9AD03B0

View file

@ -1346,7 +1346,7 @@ func createTestTransactions(t *testing.T, count int) []*types.Transaction {
) )
var transactions []*types.Transaction var transactions []*types.Transaction
for i := 0; i < count; i++ { for i := range count {
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{ tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: uint64(i), Nonce: uint64(i),
To: &testAddr, To: &testAddr,
@ -1361,27 +1361,6 @@ func createTestTransactions(t *testing.T, count int) []*types.Transaction {
return transactions return transactions
} }
// createSingleTransaction creates a single transaction for testing
func createSingleTransaction(t *testing.T, nonce uint64, value *big.Int, gasLimit uint64, data []byte) *types.Transaction {
t.Helper()
var (
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: nonce,
To: &testAddr,
Value: value,
Gas: gasLimit,
GasPrice: big.NewInt(1000000000),
Data: data,
}), types.HomesteadSigner{}, key)
return tx
}
// setupBadBlock creates a bad block and stores it in the database for testing // setupBadBlock creates a bad block and stores it in the database for testing
func setupBadBlock(t *testing.T, backend *testBackend, transactions []*types.Transaction) (*types.Block, []common.Hash) { func setupBadBlock(t *testing.T, backend *testBackend, transactions []*types.Transaction) (*types.Block, []common.Hash) {
t.Helper() t.Helper()
@ -1392,23 +1371,23 @@ func setupBadBlock(t *testing.T, backend *testBackend, transactions []*types.Tra
} }
// Get the latest block from the test chain as the parent // Get the latest block from the test chain as the parent
latestBlock := backend.chain.CurrentBlock() parentBlock := backend.chain.CurrentBlock()
parentHash := latestBlock.Hash() parentHash := parentBlock.Hash()
parentNumber := latestBlock.Number.Uint64() parentNumber := parentBlock.Number.Uint64()
// Create a bad block header that references the existing parent // Create a bad block header that references the existing parent
badHeader := &types.Header{ badHeader := &types.Header{
Number: big.NewInt(int64(parentNumber + 1)), Number: big.NewInt(int64(parentNumber + 1)),
Time: latestBlock.Time + 1, Time: parentBlock.Time + 1,
Extra: []byte("bad block for testing"), Extra: []byte("bad block for testing"),
GasLimit: latestBlock.GasLimit, GasLimit: parentBlock.GasLimit,
GasUsed: uint64(len(transactions) * 21000), GasUsed: uint64(len(transactions) * 21000),
Difficulty: big.NewInt(1000), Difficulty: big.NewInt(1000),
UncleHash: types.EmptyUncleHash, UncleHash: types.EmptyUncleHash,
TxHash: types.DeriveSha(types.Transactions(transactions), trie.NewStackTrie(nil)), TxHash: types.DeriveSha(types.Transactions(transactions), trie.NewStackTrie(nil)),
ReceiptHash: types.EmptyReceiptsHash, ReceiptHash: types.EmptyReceiptsHash,
ParentHash: parentHash, ParentHash: parentHash,
BaseFee: latestBlock.BaseFee, // Copy base fee to avoid nil pointer BaseFee: parentBlock.BaseFee, // Copy base fee to avoid nil pointer
} }
// Create the bad block // Create the bad block
@ -1438,7 +1417,6 @@ func TestStandardTraceBadBlockToFile(t *testing.T) {
testAddr := common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43") testAddr := common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
testCode := []byte{byte(vm.PUSH1), 0x42, byte(vm.POP), byte(vm.STOP)} testCode := []byte{byte(vm.PUSH1), 0x42, byte(vm.POP), byte(vm.STOP)}
// Create test backend with proper accounts
genesis := &core.Genesis{ genesis := &core.Genesis{
Config: params.TestChainConfig, Config: params.TestChainConfig,
Alloc: types.GenesisAlloc{ Alloc: types.GenesisAlloc{
@ -1562,7 +1540,6 @@ func TestStandardTraceBadBlockToFile(t *testing.T) {
t.Fatalf("Expected %d trace files, got %d", len(tc.expectedTraces), len(files)) t.Fatalf("Expected %d trace files, got %d", len(tc.expectedTraces), len(files))
} }
// Verify trace content if files are expected
for i, fileName := range files { for i, fileName := range files {
data, err := os.ReadFile(fileName) data, err := os.ReadFile(fileName)
if err != nil { if err != nil {