-
Notifications
You must be signed in to change notification settings - Fork 247
/
types.go
191 lines (171 loc) · 4.31 KB
/
types.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2018 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package events
import (
"fmt"
"math"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/vechain/thor/block"
"github.com/vechain/thor/chain"
"github.com/vechain/thor/logdb"
"github.com/vechain/thor/thor"
)
type LogMeta struct {
BlockID thor.Bytes32 `json:"blockID"`
BlockNumber uint32 `json:"blockNumber"`
BlockTimestamp uint64 `json:"blockTimestamp"`
TxID thor.Bytes32 `json:"txID"`
TxOrigin thor.Address `json:"txOrigin"`
ClauseIndex uint32 `json:"clauseIndex"`
}
type TopicSet struct {
Topic0 *thor.Bytes32 `json:"topic0"`
Topic1 *thor.Bytes32 `json:"topic1"`
Topic2 *thor.Bytes32 `json:"topic2"`
Topic3 *thor.Bytes32 `json:"topic3"`
Topic4 *thor.Bytes32 `json:"topic4"`
}
// FilteredEvent only comes from one contract
type FilteredEvent struct {
Address thor.Address `json:"address"`
Topics []*thor.Bytes32 `json:"topics"`
Data string `json:"data"`
Meta LogMeta `json:"meta"`
}
//convert a logdb.Event into a json format Event
func convertEvent(event *logdb.Event) *FilteredEvent {
fe := FilteredEvent{
Address: event.Address,
Data: hexutil.Encode(event.Data),
Meta: LogMeta{
BlockID: event.BlockID,
BlockNumber: event.BlockNumber,
BlockTimestamp: event.BlockTime,
TxID: event.TxID,
TxOrigin: event.TxOrigin,
ClauseIndex: event.ClauseIndex,
},
}
fe.Topics = make([]*thor.Bytes32, 0)
for i := 0; i < 5; i++ {
if event.Topics[i] != nil {
fe.Topics = append(fe.Topics, event.Topics[i])
}
}
return &fe
}
func (e *FilteredEvent) String() string {
return fmt.Sprintf(`
Event(
address: %v,
topics: %v,
data: %v,
meta: (blockID %v,
blockNumber %v,
blockTimestamp %v),
txID %v,
txOrigin %v,
clauseIndex %v)
)`,
e.Address,
e.Topics,
e.Data,
e.Meta.BlockID,
e.Meta.BlockNumber,
e.Meta.BlockTimestamp,
e.Meta.TxID,
e.Meta.TxOrigin,
e.Meta.ClauseIndex,
)
}
type EventCriteria struct {
Address *thor.Address `json:"address"`
TopicSet
}
type EventFilter struct {
CriteriaSet []*EventCriteria `json:"criteriaSet"`
Range *Range `json:"range"`
Options *logdb.Options `json:"options"`
Order logdb.Order `json:"order"`
}
func convertEventFilter(chain *chain.Chain, filter *EventFilter) (*logdb.EventFilter, error) {
rng, err := ConvertRange(chain, filter.Range)
if err != nil {
return nil, err
}
f := &logdb.EventFilter{
Range: rng,
Options: filter.Options,
Order: filter.Order,
}
if len(filter.CriteriaSet) > 0 {
criterias := make([]*logdb.EventCriteria, len(filter.CriteriaSet))
for i, criteria := range filter.CriteriaSet {
var topics [5]*thor.Bytes32
topics[0] = criteria.Topic0
topics[1] = criteria.Topic1
topics[2] = criteria.Topic2
topics[3] = criteria.Topic3
topics[4] = criteria.Topic4
criteria := &logdb.EventCriteria{
Address: criteria.Address,
Topics: topics,
}
criterias[i] = criteria
}
f.CriteriaSet = criterias
}
return f, nil
}
type RangeType string
const (
BlockRangeType RangeType = "block"
TimeRangeType RangeType = "time"
)
type Range struct {
Unit RangeType
From uint64
To uint64
}
func ConvertRange(chain *chain.Chain, r *Range) (*logdb.Range, error) {
if r == nil {
return nil, nil
}
if r.Unit == TimeRangeType {
emptyRange := logdb.Range{
From: math.MaxUint32,
To: math.MaxUint32,
}
genesis, err := chain.GetBlockHeader(0)
if err != nil {
return nil, err
}
if r.To < genesis.Timestamp() {
return &emptyRange, nil
}
head, err := chain.GetBlockHeader(block.Number(chain.HeadID()))
if err != nil {
return nil, err
}
if r.From > head.Timestamp() {
return &emptyRange, nil
}
fromHeader, err := chain.FindBlockHeaderByTimestamp(r.From, 1)
if err != nil {
return nil, err
}
toHeader, err := chain.FindBlockHeaderByTimestamp(r.To, -1)
if err != nil {
return nil, err
}
return &logdb.Range{
From: fromHeader.Number(),
To: toHeader.Number(),
}, nil
}
return &logdb.Range{
From: uint32(r.From),
To: uint32(r.To),
}, nil
}