Skip to content

Commit 2e69629

Browse files
committed
Merge commit '7705d13ed492a6291b2d7aa7f7c15b70749e9a65' into merge_v1.15.11
2 parents ede177e + 7705d13 commit 2e69629

File tree

9 files changed

+418
-46
lines changed

9 files changed

+418
-46
lines changed

core/filtermaps/indexer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func (f *FilterMaps) tryIndexHead() error {
254254
((!f.loggedHeadIndex && time.Since(f.startedHeadIndexAt) > headLogDelay) ||
255255
time.Since(f.lastLogHeadIndex) > logFrequency) {
256256
log.Info("Log index head rendering in progress",
257-
"first block", f.indexedRange.blocks.First(), "last block", f.indexedRange.blocks.Last(),
257+
"firstblock", f.indexedRange.blocks.First(), "lastblock", f.indexedRange.blocks.Last(),
258258
"processed", f.indexedRange.blocks.AfterLast()-f.ptrHeadIndex,
259259
"remaining", f.indexedView.HeadNumber()-f.indexedRange.blocks.Last(),
260260
"elapsed", common.PrettyDuration(time.Since(f.startedHeadIndexAt)))
@@ -266,7 +266,7 @@ func (f *FilterMaps) tryIndexHead() error {
266266
}
267267
if f.loggedHeadIndex && f.indexedRange.hasIndexedBlocks() {
268268
log.Info("Log index head rendering finished",
269-
"first block", f.indexedRange.blocks.First(), "last block", f.indexedRange.blocks.Last(),
269+
"firstblock", f.indexedRange.blocks.First(), "lastblock", f.indexedRange.blocks.Last(),
270270
"processed", f.indexedRange.blocks.AfterLast()-f.ptrHeadIndex,
271271
"elapsed", common.PrettyDuration(time.Since(f.startedHeadIndexAt)))
272272
}
@@ -323,7 +323,7 @@ func (f *FilterMaps) tryIndexTail() (bool, error) {
323323
if f.indexedRange.hasIndexedBlocks() && f.ptrTailIndex >= f.indexedRange.blocks.First() &&
324324
(!f.loggedTailIndex || time.Since(f.lastLogTailIndex) > logFrequency) {
325325
log.Info("Log index tail rendering in progress",
326-
"first block", f.indexedRange.blocks.First(), "last block", f.indexedRange.blocks.Last(),
326+
"firstblock", f.indexedRange.blocks.First(), "last block", f.indexedRange.blocks.Last(),
327327
"processed", f.ptrTailIndex-f.indexedRange.blocks.First()+tpb,
328328
"remaining", remaining,
329329
"next tail epoch percentage", f.indexedRange.tailPartialEpoch*100/f.mapsPerEpoch,
@@ -346,7 +346,7 @@ func (f *FilterMaps) tryIndexTail() (bool, error) {
346346
}
347347
if f.loggedTailIndex && f.indexedRange.hasIndexedBlocks() {
348348
log.Info("Log index tail rendering finished",
349-
"first block", f.indexedRange.blocks.First(), "last block", f.indexedRange.blocks.Last(),
349+
"firstblock", f.indexedRange.blocks.First(), "lastblock", f.indexedRange.blocks.Last(),
350350
"processed", f.ptrTailIndex-f.indexedRange.blocks.First(),
351351
"elapsed", common.PrettyDuration(time.Since(f.startedTailIndexAt)))
352352
f.loggedTailIndex = false

eth/tracers/api.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
784784
// Note: This copies the config, to not screw up the main config
785785
chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
786786
}
787+
787788
evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
788789
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
789790
core.ProcessBeaconBlockRoot(*beaconRoot, evm)
@@ -793,42 +794,45 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
793794
}
794795
for i, tx := range block.Transactions() {
795796
// Prepare the transaction for un-traced execution
796-
var (
797-
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee(), core.MessageReplayMode)
798-
vmConf vm.Config
799-
dump *os.File
800-
writer *bufio.Writer
801-
err error
802-
)
803-
// If the transaction needs tracing, swap out the configs
804-
if tx.Hash() == txHash || txHash == (common.Hash{}) {
805-
// Generate a unique temporary file to dump it into
806-
prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
807-
if !canon {
808-
prefix = fmt.Sprintf("%valt-", prefix)
809-
}
810-
dump, err = os.CreateTemp(os.TempDir(), prefix)
797+
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), core.MessageReplayMode)
798+
if txHash != (common.Hash{}) && tx.Hash() != txHash {
799+
// Process the tx to update state, but don't trace it.
800+
_, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
811801
if err != nil {
812-
return nil, err
813-
}
814-
dumps = append(dumps, dump.Name())
815-
816-
// Swap out the noop logger to the standard tracer
817-
writer = bufio.NewWriter(dump)
818-
vmConf = vm.Config{
819-
Tracer: logger.NewJSONLogger(&logConfig, writer),
820-
EnablePreimageRecording: true,
802+
return dumps, err
821803
}
804+
// Finalize the state so any modifications are written to the trie
805+
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
806+
statedb.Finalise(evm.ChainConfig().IsEIP158(block.Number()))
807+
continue
822808
}
809+
// The transaction should be traced.
810+
// Generate a unique temporary file to dump it into.
811+
prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
812+
if !canon {
813+
prefix = fmt.Sprintf("%valt-", prefix)
814+
}
815+
var dump *os.File
816+
dump, err := os.CreateTemp(os.TempDir(), prefix)
817+
if err != nil {
818+
return nil, err
819+
}
820+
dumps = append(dumps, dump.Name())
821+
// Set up the tracer and EVM for the transaction.
822+
var (
823+
writer = bufio.NewWriter(dump)
824+
tracer = logger.NewJSONLogger(&logConfig, writer)
825+
evm = vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{
826+
Tracer: tracer,
827+
NoBaseFee: true,
828+
})
829+
)
823830
// Execute the transaction and flush any traces to disk
824831
statedb.SetTxContext(tx.Hash(), i)
825-
if vmConf.Tracer.OnTxStart != nil {
826-
vmConf.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
827-
}
828-
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
829-
if vmConf.Tracer.OnTxEnd != nil {
830-
vmConf.Tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err)
832+
if tracer.OnTxStart != nil {
833+
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
831834
}
835+
_, err = core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
832836
if writer != nil {
833837
writer.Flush()
834838
}

eth/tracers/api_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"errors"
2424
"fmt"
2525
"math/big"
26+
"os"
2627
"reflect"
2728
"slices"
2829
"sync/atomic"
@@ -1223,3 +1224,118 @@ func TestTraceBlockWithBasefee(t *testing.T) {
12231224
}
12241225
}
12251226
}
1227+
1228+
func TestStandardTraceBlockToFile(t *testing.T) {
1229+
var (
1230+
// A sender who makes transactions, has some funds
1231+
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
1232+
address = crypto.PubkeyToAddress(key.PublicKey)
1233+
funds = big.NewInt(1000000000000000)
1234+
1235+
// first contract the sender transacts with
1236+
aa = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
1237+
aaCode = []byte{byte(vm.PUSH1), 0x00, byte(vm.POP)}
1238+
1239+
// second contract the sender transacts with
1240+
bb = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f44")
1241+
bbCode = []byte{byte(vm.PUSH2), 0x00, 0x01, byte(vm.POP)}
1242+
)
1243+
1244+
genesis := &core.Genesis{
1245+
Config: params.TestChainConfig,
1246+
Alloc: types.GenesisAlloc{
1247+
address: {Balance: funds},
1248+
aa: {
1249+
Code: aaCode,
1250+
Nonce: 1,
1251+
Balance: big.NewInt(0),
1252+
},
1253+
bb: {
1254+
Code: bbCode,
1255+
Nonce: 1,
1256+
Balance: big.NewInt(0),
1257+
},
1258+
},
1259+
}
1260+
txHashs := make([]common.Hash, 0, 2)
1261+
backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
1262+
b.SetCoinbase(common.Address{1})
1263+
// first tx to aa
1264+
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
1265+
Nonce: 0,
1266+
To: &aa,
1267+
Value: big.NewInt(0),
1268+
Gas: 50000,
1269+
GasPrice: b.BaseFee(),
1270+
Data: nil,
1271+
}), types.HomesteadSigner{}, key)
1272+
b.AddTx(tx)
1273+
txHashs = append(txHashs, tx.Hash())
1274+
// second tx to bb
1275+
tx, _ = types.SignTx(types.NewTx(&types.LegacyTx{
1276+
Nonce: 1,
1277+
To: &bb,
1278+
Value: big.NewInt(1),
1279+
Gas: 100000,
1280+
GasPrice: b.BaseFee(),
1281+
Data: nil,
1282+
}), types.HomesteadSigner{}, key)
1283+
b.AddTx(tx)
1284+
txHashs = append(txHashs, tx.Hash())
1285+
})
1286+
defer backend.chain.Stop()
1287+
1288+
var testSuite = []struct {
1289+
blockNumber rpc.BlockNumber
1290+
config *StdTraceConfig
1291+
want []string
1292+
}{
1293+
{
1294+
// test that all traces in the block were outputted if no trace config is specified
1295+
blockNumber: rpc.LatestBlockNumber,
1296+
config: nil,
1297+
want: []string{
1298+
`{"pc":0,"op":96,"gas":"0x7148","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
1299+
{"pc":2,"op":80,"gas":"0x7145","gasCost":"0x2","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"POP"}
1300+
{"pc":3,"op":0,"gas":"0x7143","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
1301+
{"output":"","gasUsed":"0x5"}
1302+
`,
1303+
`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
1304+
{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
1305+
{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
1306+
{"output":"","gasUsed":"0x5"}
1307+
`,
1308+
},
1309+
},
1310+
{
1311+
// test that only a specific tx is traced if specified
1312+
blockNumber: rpc.LatestBlockNumber,
1313+
config: &StdTraceConfig{TxHash: txHashs[1]},
1314+
want: []string{
1315+
`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
1316+
{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
1317+
{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
1318+
{"output":"","gasUsed":"0x5"}
1319+
`,
1320+
},
1321+
},
1322+
}
1323+
1324+
api := NewAPI(backend)
1325+
for i, tc := range testSuite {
1326+
block, _ := api.blockByNumber(context.Background(), tc.blockNumber)
1327+
txTraces, err := api.StandardTraceBlockToFile(context.Background(), block.Hash(), tc.config)
1328+
if err != nil {
1329+
t.Fatalf("test index %d received error %v", i, err)
1330+
}
1331+
for j, traceFileName := range txTraces {
1332+
traceReceived, err := os.ReadFile(traceFileName)
1333+
if err != nil {
1334+
t.Fatalf("could not read trace file: %v", err)
1335+
}
1336+
if tc.want[j] != string(traceReceived) {
1337+
t.Fatalf("unexpected trace result. expected\n'%s'\n\nreceived\n'%s'\n", tc.want[j], string(traceReceived))
1338+
}
1339+
}
1340+
}
1341+
}

internal/ethapi/api_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,77 @@ func TestSimulateV1ChainLinkage(t *testing.T) {
24982498
require.Equal(t, block2.Hash().Bytes(), []byte(results[2].Calls[1].ReturnValue), "returned blockhash for block2 does not match")
24992499
}
25002500

2501+
func TestSimulateV1TxSender(t *testing.T) {
2502+
var (
2503+
sender = common.Address{0xaa, 0xaa}
2504+
sender2 = common.Address{0xaa, 0xab}
2505+
sender3 = common.Address{0xaa, 0xac}
2506+
recipient = common.Address{0xbb, 0xbb}
2507+
gspec = &core.Genesis{
2508+
Config: params.MergedTestChainConfig,
2509+
Alloc: types.GenesisAlloc{
2510+
sender: {Balance: big.NewInt(params.Ether)},
2511+
sender2: {Balance: big.NewInt(params.Ether)},
2512+
sender3: {Balance: big.NewInt(params.Ether)},
2513+
},
2514+
}
2515+
ctx = context.Background()
2516+
)
2517+
backend := newTestBackend(t, 0, gspec, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {})
2518+
stateDB, baseHeader, err := backend.StateAndHeaderByNumberOrHash(ctx, rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
2519+
if err != nil {
2520+
t.Fatalf("failed to get state and header: %v", err)
2521+
}
2522+
2523+
sim := &simulator{
2524+
b: backend,
2525+
state: stateDB,
2526+
base: baseHeader,
2527+
chainConfig: backend.ChainConfig(),
2528+
gp: new(core.GasPool).AddGas(math.MaxUint64),
2529+
traceTransfers: false,
2530+
validate: false,
2531+
fullTx: true,
2532+
}
2533+
2534+
results, err := sim.execute(ctx, []simBlock{
2535+
{Calls: []TransactionArgs{
2536+
{From: &sender, To: &recipient, Value: (*hexutil.Big)(big.NewInt(1000))},
2537+
{From: &sender2, To: &recipient, Value: (*hexutil.Big)(big.NewInt(2000))},
2538+
{From: &sender3, To: &recipient, Value: (*hexutil.Big)(big.NewInt(3000))},
2539+
}},
2540+
{Calls: []TransactionArgs{
2541+
{From: &sender2, To: &recipient, Value: (*hexutil.Big)(big.NewInt(4000))},
2542+
}},
2543+
})
2544+
if err != nil {
2545+
t.Fatalf("simulation execution failed: %v", err)
2546+
}
2547+
require.Len(t, results, 2, "expected 2 simulated blocks")
2548+
require.Len(t, results[0].Block.Transactions(), 3, "expected 3 transaction in simulated block")
2549+
require.Len(t, results[1].Block.Transactions(), 1, "expected 1 transaction in 2nd simulated block")
2550+
enc, err := json.Marshal(results)
2551+
if err != nil {
2552+
t.Fatalf("failed to marshal results: %v", err)
2553+
}
2554+
type resultType struct {
2555+
Transactions []struct {
2556+
From common.Address `json:"from"`
2557+
}
2558+
}
2559+
var summary []resultType
2560+
if err := json.Unmarshal(enc, &summary); err != nil {
2561+
t.Fatalf("failed to unmarshal results: %v", err)
2562+
}
2563+
require.Len(t, summary, 2, "expected 2 simulated blocks")
2564+
require.Len(t, summary[0].Transactions, 3, "expected 3 transaction in simulated block")
2565+
require.Equal(t, sender, summary[0].Transactions[0].From, "sender address mismatch")
2566+
require.Equal(t, sender2, summary[0].Transactions[1].From, "sender address mismatch")
2567+
require.Equal(t, sender3, summary[0].Transactions[2].From, "sender address mismatch")
2568+
require.Len(t, summary[1].Transactions, 1, "expected 1 transaction in simulated block")
2569+
require.Equal(t, sender2, summary[1].Transactions[0].From, "sender address mismatch")
2570+
}
2571+
25012572
func TestSignTransaction(t *testing.T) {
25022573
t.Parallel()
25032574
// Initialize test accounts

0 commit comments

Comments
 (0)