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

chore(evmengine): refactor evmmsgs #106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 6 additions & 32 deletions client/x/evmengine/keeper/evmmsgs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package keeper

import (
"bytes"
"context"
"slices"
"sort"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -31,41 +28,18 @@ func (k *Keeper) evmEvents(ctx context.Context, blockHash common.Hash) ([]*types
return nil, errors.Wrap(err, "filter logs")
}

ll := make([]*types.EVMEvent, 0, len(logs))
events = make([]*types.EVMEvent, 0, len(logs))
for _, l := range logs {
topics := make([][]byte, 0, len(l.Topics))
for _, t := range l.Topics {
topics = append(topics, t.Bytes())
evmEvent, err := types.EthLogToEVMEvent(l)
if err != nil {
return nil, errors.Wrap(err, "convert log")
}
ll = append(ll, &types.EVMEvent{
Address: l.Address.Bytes(),
Topics: topics,
Data: l.Data,
})
}

for _, log := range ll {
if err := log.Verify(); err != nil {
return nil, errors.Wrap(err, "verify log")
}
events = append(events, evmEvent)
}
events = append(events, ll...)

// Sort by Address > Topics > Data
// This avoids dependency on runtime ordering.
sort.Slice(events, func(i, j int) bool {
if cmp := bytes.Compare(events[i].Address, events[j].Address); cmp != 0 {
return cmp < 0
}

topicI := slices.Concat(events[i].Topics...)
topicJ := slices.Concat(events[j].Topics...)
if cmp := bytes.Compare(topicI, topicJ); cmp != 0 {
return cmp < 0
}

return bytes.Compare(events[i].Data, events[j].Data) < 0
})
types.SortEVMEvents(events)

return events, nil
}
74 changes: 74 additions & 0 deletions client/x/evmengine/types/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import (
"bytes"
"slices"
"sort"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"

Expand Down Expand Up @@ -47,3 +51,73 @@ func (l *EVMEvent) Verify() error {

return nil
}

// EthLogToEVMEvent converts an Ethereum Log to an EVMEvent.
func EthLogToEVMEvent(ethLog ethtypes.Log) (*EVMEvent, error) {
topics := make([][]byte, 0, len(ethLog.Topics))
for _, t := range ethLog.Topics {
topics = append(topics, t.Bytes())
}

evmEvent := &EVMEvent{
Address: ethLog.Address.Bytes(),
Topics: topics,
Data: ethLog.Data,
}
if err := evmEvent.Verify(); err != nil {
return nil, errors.Wrap(err, "verify log")
}

return evmEvent, nil
}

// SortEVMEvents sorts EVM events by Address > Topics > Data.
func SortEVMEvents(events []*EVMEvent) {
sort.Slice(events, func(i, j int) bool {
if cmp := bytes.Compare(events[i].Address, events[j].Address); cmp != 0 {
return cmp < 0
}

topicI := slices.Concat(events[i].Topics...)
topicJ := slices.Concat(events[j].Topics...)
if cmp := bytes.Compare(topicI, topicJ); cmp != 0 {
return cmp < 0
}

return bytes.Compare(events[i].Data, events[j].Data) < 0
})
}

// IsSortedEVMEvents check if the events are sorted by ascending order of address, topics, and data.
func IsSortedEVMEvents(events []*EVMEvent) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this only used in the test? If so, we should move it to the test

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@edisonz0718

applied it in 828cb40

thanks for your comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@edisonz0718
Could you review this PR when you have time? I applied your comment. Thanks in advance.

for i := 1; i < len(events); i++ {
// Compare addresses first
addressComparison := bytes.Compare(events[i-1].Address, events[i].Address)
if addressComparison > 0 {
// it is not sorted by ascending order of address
return false
}

if addressComparison == 0 {
// If addresses are equal, compare by topics
previousTopic := slices.Concat(events[i-1].Topics...)
currentTopic := slices.Concat(events[i].Topics...)
topicComparison := bytes.Compare(previousTopic, currentTopic)

if topicComparison > 0 {
// it is not sorted by ascending order of topics
return false
}

if topicComparison == 0 {
// If topics are also equal, compare by data
if bytes.Compare(events[i-1].Data, events[i].Data) > 0 {
// it is not sorted by ascending order of data
return false
}
}
}
}

return true
}
Loading
Loading