-
Notifications
You must be signed in to change notification settings - Fork 14
/
evmmsgs.go
71 lines (60 loc) · 1.77 KB
/
evmmsgs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package keeper
import (
"bytes"
"context"
"slices"
"sort"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/piplabs/story/client/genutil/evm/predeploys"
"github.com/piplabs/story/client/x/evmengine/types"
"github.com/piplabs/story/lib/errors"
)
// evmEvents returns selected EVM log events from the provided block hash.
func (k *Keeper) evmEvents(ctx context.Context, blockHash common.Hash) ([]*types.EVMEvent, error) {
var events []*types.EVMEvent
logs, err := k.engineCl.FilterLogs(ctx, ethereum.FilterQuery{
BlockHash: &blockHash,
// only IPTokenStaking contract
Addresses: []common.Address{
common.HexToAddress(predeploys.IPTokenStaking),
common.HexToAddress(predeploys.UBIPool),
common.HexToAddress(predeploys.UpgradeEntrypoint),
},
})
if err != nil {
return nil, errors.Wrap(err, "filter logs")
}
ll := 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())
}
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, 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
})
return events, nil
}