Skip to content

Commit 12bc0da

Browse files
committed
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>
1 parent 9adad59 commit 12bc0da

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

eth/tracers/api_test.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ func createTestTransactions(t *testing.T, count int) []*types.Transaction {
13461346
)
13471347

13481348
var transactions []*types.Transaction
1349-
for i := 0; i < count; i++ {
1349+
for i := range count {
13501350
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
13511351
Nonce: uint64(i),
13521352
To: &testAddr,
@@ -1361,27 +1361,6 @@ func createTestTransactions(t *testing.T, count int) []*types.Transaction {
13611361
return transactions
13621362
}
13631363

1364-
// createSingleTransaction creates a single transaction for testing
1365-
func createSingleTransaction(t *testing.T, nonce uint64, value *big.Int, gasLimit uint64, data []byte) *types.Transaction {
1366-
t.Helper()
1367-
1368-
var (
1369-
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
1370-
testAddr = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
1371-
)
1372-
1373-
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
1374-
Nonce: nonce,
1375-
To: &testAddr,
1376-
Value: value,
1377-
Gas: gasLimit,
1378-
GasPrice: big.NewInt(1000000000),
1379-
Data: data,
1380-
}), types.HomesteadSigner{}, key)
1381-
1382-
return tx
1383-
}
1384-
13851364
// setupBadBlock creates a bad block and stores it in the database for testing
13861365
func setupBadBlock(t *testing.T, backend *testBackend, transactions []*types.Transaction) (*types.Block, []common.Hash) {
13871366
t.Helper()
@@ -1392,23 +1371,23 @@ func setupBadBlock(t *testing.T, backend *testBackend, transactions []*types.Tra
13921371
}
13931372

13941373
// Get the latest block from the test chain as the parent
1395-
latestBlock := backend.chain.CurrentBlock()
1396-
parentHash := latestBlock.Hash()
1397-
parentNumber := latestBlock.Number.Uint64()
1374+
parentBlock := backend.chain.CurrentBlock()
1375+
parentHash := parentBlock.Hash()
1376+
parentNumber := parentBlock.Number.Uint64()
13981377

13991378
// Create a bad block header that references the existing parent
14001379
badHeader := &types.Header{
14011380
Number: big.NewInt(int64(parentNumber + 1)),
1402-
Time: latestBlock.Time + 1,
1381+
Time: parentBlock.Time + 1,
14031382
Extra: []byte("bad block for testing"),
1404-
GasLimit: latestBlock.GasLimit,
1383+
GasLimit: parentBlock.GasLimit,
14051384
GasUsed: uint64(len(transactions) * 21000),
14061385
Difficulty: big.NewInt(1000),
14071386
UncleHash: types.EmptyUncleHash,
14081387
TxHash: types.DeriveSha(types.Transactions(transactions), trie.NewStackTrie(nil)),
14091388
ReceiptHash: types.EmptyReceiptsHash,
14101389
ParentHash: parentHash,
1411-
BaseFee: latestBlock.BaseFee, // Copy base fee to avoid nil pointer
1390+
BaseFee: parentBlock.BaseFee, // Copy base fee to avoid nil pointer
14121391
}
14131392

14141393
// Create the bad block
@@ -1438,7 +1417,6 @@ func TestStandardTraceBadBlockToFile(t *testing.T) {
14381417
testAddr := common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
14391418
testCode := []byte{byte(vm.PUSH1), 0x42, byte(vm.POP), byte(vm.STOP)}
14401419

1441-
// Create test backend with proper accounts
14421420
genesis := &core.Genesis{
14431421
Config: params.TestChainConfig,
14441422
Alloc: types.GenesisAlloc{
@@ -1562,7 +1540,6 @@ func TestStandardTraceBadBlockToFile(t *testing.T) {
15621540
t.Fatalf("Expected %d trace files, got %d", len(tc.expectedTraces), len(files))
15631541
}
15641542

1565-
// Verify trace content if files are expected
15661543
for i, fileName := range files {
15671544
data, err := os.ReadFile(fileName)
15681545
if err != nil {

0 commit comments

Comments
 (0)