Skip to content

Commit f9d4e32

Browse files
committed
feat(stylus): decode event into topics and find its signature
1 parent ad8d1ca commit f9d4e32

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

cmd/wazero/wazero.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
214214
flags.StringVar(&stylusTracePath, "stylus", "",
215215
"Imports the EVM hook functions and mocks their IO according the result of debug_traceTransaction in the path provided.")
216216

217+
var stylusSignatureMapPath string
218+
flags.StringVar(&stylusSignatureMapPath, "stylus-signature-map", "",
219+
"Signature map of the EVM events. Used to decode events.")
220+
217221
var traceDir string
218222
flags.StringVar(&traceDir, "trace-dir", "",
219223
"Directory where to save the trace record. If empty - no trace is produced. Default \"\".")
@@ -344,7 +348,7 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer) int {
344348

345349
var stylusState *stylus.StylusTrace
346350
if stylusTracePath != "" {
347-
stylusState, err = stylus.Instantiate(ctx, rt, stylusTracePath, traceRecordPtr)
351+
stylusState, err = stylus.Instantiate(ctx, rt, stylusTracePath, stylusSignatureMapPath, traceRecordPtr)
348352
if err != nil {
349353
fmt.Fprintf(stdErr, "error reading stylus trace: %v\n", err)
350354
return 1

internal/stylus/stylus.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ package stylus
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"os"
78

89
"github.com/metacraft-labs/trace_record"
910
"github.com/tetratelabs/wazero"
1011
)
1112

12-
func Instantiate(ctx context.Context, r wazero.Runtime, stylusTracePath string, record *trace_record.TraceRecord) (*StylusTrace, error) {
13+
func Instantiate(ctx context.Context, r wazero.Runtime, stylusTracePath string, stylusSignatureMapPath string, record *trace_record.TraceRecord) (*StylusTrace, error) {
14+
stylusState := StylusTrace{}
15+
1316
stylusTraceJson, err := os.ReadFile(stylusTracePath)
1417
if err != nil {
1518
return nil, err
1619
}
1720

18-
stylusState := StylusTrace{}
19-
2021
if err := json.Unmarshal(stylusTraceJson, &stylusState.events); err != nil {
2122
return nil, err
2223
}
2324

25+
stylusSignatureMapJson, err := os.ReadFile(stylusSignatureMapPath)
26+
if err != nil {
27+
fmt.Printf("Can't read signature map. Error: %v\n", err)
28+
}
29+
30+
if err := json.Unmarshal(stylusSignatureMapJson, &stylusState.eventSignatureMap); err != nil {
31+
fmt.Printf("Can't parse signature map. Error: %v\n", err)
32+
}
33+
2434
moduleBuilder := r.NewHostModuleBuilder("vm_hooks")
2535
moduleBuilder = exportSylusFunctions(moduleBuilder, &stylusState, record)
2636

internal/stylus/stylus_funcs.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,37 @@ func exportEmitLog(mb wazero.HostModuleBuilder, trace *StylusTrace, record *trac
376376
func(m api.Module, stack []uint64, event evmEvent) {
377377
mem := m.Memory()
378378
dataPtr := uint32(stack[0])
379-
len := uint32(stack[1])
380-
data := readMemoryBytes(mem, dataPtr, len)
379+
rawDataLen := uint32(stack[1])
380+
numTopics := stack[2]
381+
382+
rawData := readMemoryBytes(mem, dataPtr, rawDataLen)
381383
_ = event
382384

383-
// TODO: convert this to human readable format
384-
record.RegisterRecordEvent(trace_record.EventKindEvmEvent, "emit_log", hexBytes(data))
385+
topics := make([][]byte, numTopics)
386+
for i := range numTopics {
387+
topics[i] = rawData[i*32 : (i*32 + 32)]
388+
}
389+
390+
data := rawData[numTopics*32:]
391+
392+
content := ""
393+
394+
if signature, hasSignature := trace.eventSignatureMap[hexBytes(topics[0])]; hasSignature {
395+
content += signature
396+
// TODO: display topics and data in human-readable way
397+
} else {
398+
// TODO: discuss if using https://www.4byte.directory/ as fallback for resolving signatures is a good idea
399+
}
400+
401+
for i := range numTopics {
402+
content = fmt.Sprintf("%s\nTOPIC[%d] = %s", content, i, hexBytes(topics[i]))
403+
}
404+
405+
if len(data) > 0 {
406+
content = fmt.Sprintf("%s\nDATA = %s", content, hexBytes(data))
407+
}
408+
409+
record.RegisterRecordEvent(trace_record.EventKindEvmEvent, "emit_log", content)
385410
})
386411
}
387412

internal/stylus/stylus_trace.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func (e *evmEvent) UnmarshalJSON(data []byte) error {
6565
type StylusTrace struct {
6666
events []evmEvent
6767
current int
68+
69+
eventSignatureMap map[string]string
6870
}
6971

7072
func (st *StylusTrace) nextEvent(event string) (evmEvent, error) {

0 commit comments

Comments
 (0)