@@ -85,15 +85,14 @@ var (
85
85
loggerPool = sync.Pool {
86
86
New : func () interface {} {
87
87
return & StructLog {
88
- Stack : make ([]uint256. Int , 0 ),
89
- ExtraData : types . NewExtraData ( ),
88
+ // init arrays here; other types are inited with default values
89
+ Stack : make ([]uint256. Int , 0 ),
90
90
}
91
91
},
92
92
}
93
93
)
94
94
95
95
func NewStructlog (pc uint64 , op OpCode , gas , cost uint64 , depth int ) * StructLog {
96
-
97
96
structlog := loggerPool .Get ().(* StructLog )
98
97
structlog .Pc , structlog .Op , structlog .Gas , structlog .GasCost , structlog .Depth = pc , op , gas , cost , depth
99
98
@@ -109,7 +108,14 @@ func (s *StructLog) clean() {
109
108
s .Stack = s .Stack [:0 ]
110
109
s .ReturnData .Reset ()
111
110
s .Storage = nil
112
- s .ExtraData .Clean ()
111
+ s .ExtraData = nil
112
+ }
113
+
114
+ func (s * StructLog ) getOrInitExtraData () * types.ExtraData {
115
+ if s .ExtraData == nil {
116
+ s .ExtraData = & types.ExtraData {}
117
+ }
118
+ return s .ExtraData
113
119
}
114
120
115
121
// overrides for gencodec
@@ -236,7 +242,7 @@ func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scop
236
242
l.storage [contractAddress ][storageKey ] = storageValue
237
243
structlog .Storage = l .storage [contractAddress ].Copy ()
238
244
239
- if err := traceStorageProof (l , scope , structlog .ExtraData ); err != nil {
245
+ if err := traceStorageProof (l , scope , structlog .getOrInitExtraData () ); err != nil {
240
246
log .Error ("Failed to trace data" , "opcode" , op .String (), "err" , err )
241
247
}
242
248
}
@@ -247,7 +253,7 @@ func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scop
247
253
if ok {
248
254
// execute trace func list.
249
255
for _ , exec := range execFuncList {
250
- if err = exec (l , scope , structlog .ExtraData ); err != nil {
256
+ if err = exec (l , scope , structlog .getOrInitExtraData () ); err != nil {
251
257
log .Error ("Failed to trace data" , "opcode" , op .String (), "err" , err )
252
258
}
253
259
}
@@ -441,63 +447,28 @@ func (t *mdLogger) CaptureEnter(typ OpCode, from common.Address, to common.Addre
441
447
442
448
func (t * mdLogger ) CaptureExit (output []byte , gasUsed uint64 , err error ) {}
443
449
444
- var (
445
- formatPool = sync.Pool {
446
- New : func () interface {} {
447
- return make ([]types.StructLogRes , 0 , 128 )
448
- },
449
- }
450
- )
451
-
452
450
// FormatLogs formats EVM returned structured logs for json output
453
- func FormatLogs (logs []StructLog ) []types.StructLogRes {
454
- formatted := formatPool .Get ().([]types.StructLogRes )
455
- runtime .SetFinalizer (& formatted , func (format * []types.StructLogRes ) {
456
- for _ , res := range * format {
457
- res .ExtraData = nil
458
- res .Storage = nil
459
- res .Stack = res .Stack [:0 ]
460
- res .Memory = res .Memory [:0 ]
461
- }
462
- formatPool .Put (* format )
463
- })
451
+ func FormatLogs (logs []StructLog ) []* types.StructLogRes {
452
+ formatted := make ([]* types.StructLogRes , 0 , len (logs ))
464
453
465
- for index , trace := range logs {
466
- formatted = append (formatted , types.StructLogRes {
467
- Pc : trace .Pc ,
468
- Op : trace .Op .String (),
469
- Gas : trace .Gas ,
470
- GasCost : trace .GasCost ,
471
- Depth : trace .Depth ,
472
- RefundCounter : trace .RefundCounter ,
473
- Error : trace .ErrorString (),
474
- })
475
- if len (trace .Stack ) != 0 {
476
- if formatted [index ].Stack == nil {
477
- formatted [index ].Stack = make ([]string , 0 , len (trace .Stack ))
478
- }
479
- for _ , stackValue := range trace .Stack {
480
- formatted [index ].Stack = append (formatted [index ].Stack , stackValue .Hex ())
481
- }
454
+ for _ , trace := range logs {
455
+ logRes := types .NewStructLogResBasic (trace .Pc , trace .Op .String (), trace .Gas , trace .GasCost , trace .Depth , trace .RefundCounter , trace .Err )
456
+ for _ , stackValue := range trace .Stack {
457
+ logRes .Stack = append (logRes .Stack , stackValue .Hex ())
482
458
}
483
- if trace .Memory .Len () != 0 {
484
- if formatted [index ].Memory == nil {
485
- formatted [index ].Memory = make ([]string , 0 , (trace .Memory .Len ()+ 31 )/ 32 )
486
- }
487
- for i := 0 ; i + 32 <= trace .Memory .Len (); i += 32 {
488
- formatted [index ].Memory = append (formatted [index ].Memory , common .Bytes2Hex (trace .Memory .Bytes ()[i :i + 32 ]))
489
- }
459
+ for i := 0 ; i + 32 <= trace .Memory .Len (); i += 32 {
460
+ logRes .Memory = append (logRes .Memory , common .Bytes2Hex (trace .Memory .Bytes ()[i :i + 32 ]))
490
461
}
491
462
if len (trace .Storage ) != 0 {
492
463
storage := make (map [string ]string )
493
464
for i , storageValue := range trace .Storage {
494
465
storage [i .Hex ()] = storageValue .Hex ()
495
466
}
496
- formatted [index ].Storage = storage
497
- }
498
- if trace .ExtraData != nil {
499
- formatted [index ].ExtraData = trace .ExtraData .SealExtraData ()
467
+ logRes .Storage = storage
500
468
}
469
+ logRes .ExtraData = trace .ExtraData
470
+
471
+ formatted = append (formatted , logRes )
501
472
}
502
473
return formatted
503
474
}
0 commit comments