Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethcoder: fix event decode byte16 #152

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ethcoder/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ func (d *EventDecoder) DecodeLogAsHex(log types.Log) (ABISignature, []string, bo
return eventDef, nil, false, fmt.Errorf("indexed argument out of range: %d > %d", idx+1, len(log.Topics)-1)
}
data := log.Topics[idx+1].Bytes()
arg := data[32-byteSize:]
eventValues = append(eventValues, HexEncode(arg))

var argVal []byte
if arg.Type.T == abi.BytesTy || arg.Type.T == abi.FixedBytesTy {
argVal = data[:byteSize]
} else {
argVal = data[32-byteSize:]
}
eventValues = append(eventValues, HexEncode(argVal))
idx++
} else {
byteSize := abi.GetTypeSize(arg.Type)
Expand Down
48 changes: 48 additions & 0 deletions ethcoder/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,54 @@ func TestDecodeTransactionLogByEventSig6(t *testing.T) {
require.Equal(t, "0x00000000000000000000000000000000000000000000000000000000003fab53", eventHexValues[3])
}

func TestDecodeTransactionLogByEventSig7(t *testing.T) {
logTopics := []string{
"0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a",
"0x98442bdf09d14ed3859c77b17990a0fc00000000000000000000000000000000",
}
logData := "0x"

txnLog := types.Log{}
txnLog.Topics = []common.Hash{}

for _, topic := range logTopics {
txnLog.Topics = append(txnLog.Topics, common.HexToHash(topic))
}
txnLog.Data, _ = hexutil.Decode(logData)

var eventSig = "JobExecuted(bytes16 indexed)"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkieltyka indexed is required in order to properly decode? Is that right? Otherwise decoder does not know where to look for values?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct


eventDef, eventValues, ok, err := ethcoder.DecodeTransactionLogByEventSig(txnLog, eventSig)
require.NoError(t, err)
require.True(t, ok)

require.Equal(t, "0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a", eventDef.Hash)
require.Equal(t, "JobExecuted", eventDef.Name)
require.Equal(t, "JobExecuted(bytes16)", eventDef.Signature)
require.Equal(t, []string{"bytes16"}, eventDef.ArgTypes)
require.Equal(t, []bool{true}, eventDef.ArgIndexed)

src := eventValues[0].([16]uint8)
dst := make([]byte, 16)
copy(dst[:], src[:])

require.Equal(t, 1, len(eventValues))
require.Equal(t, "0x98442bdf09d14ed3859c77b17990a0fc", ethcoder.HexEncode(dst))

eventDef, eventValuesHex, ok, err := ethcoder.DecodeTransactionLogByEventSigAsHex(txnLog, eventSig)
require.NoError(t, err)
require.True(t, ok)

require.Equal(t, "0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a", eventDef.Hash)
require.Equal(t, "JobExecuted", eventDef.Name)
require.Equal(t, "JobExecuted(bytes16)", eventDef.Signature)
require.Equal(t, []string{"bytes16"}, eventDef.ArgTypes)
require.Equal(t, []bool{true}, eventDef.ArgIndexed)

require.Equal(t, 1, len(eventValuesHex))
require.Equal(t, "0x98442bdf09d14ed3859c77b17990a0fc", eventValuesHex[0])
}

func TestEventDecoder(t *testing.T) {
ed := ethcoder.NewEventDecoder()

Expand Down
Loading