-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathethEventBuffer.go
165 lines (144 loc) · 4.43 KB
/
ethEventBuffer.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
package types
import (
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
)
// EventsInBlock store all events in a block, parent hash used to determine the best chain
type EventsInBlock struct {
ParentHash common.Hash
Events []EthereumEvent
}
// NewEventsInBlock create new instance with parent hash
func NewEventsInBlock() EventsInBlock {
return EventsInBlock{
ParentHash: common.Hash{},
Events: []EthereumEvent{},
}
}
// AddEvent append new event to list
func (e *EventsInBlock) AddEvent(event EthereumEvent) {
// avoid add the same event twice
for _, n := range e.Events {
if n.Equal(event) {
log.Println("EventsInBlock event already in list")
return
}
}
e.Events = append(e.Events, event)
}
// EventsInHeight store all events at the same height
type EventsInHeight struct {
// map the block hash to its parent hash and event list
EventsMap map[common.Hash]*EventsInBlock
}
// NewEventsInHeight create a new instance
func NewEventsInHeight() EventsInHeight {
return EventsInHeight{
EventsMap: make(map[common.Hash]*EventsInBlock),
}
}
// AddEvent append event
func (e *EventsInHeight) AddEvent(blockHash common.Hash, event EthereumEvent) {
events, ok := e.EventsMap[blockHash]
if ok {
events.AddEvent(event)
} else {
newEventsInBlock := NewEventsInBlock()
newEventsInBlock.AddEvent(event)
e.EventsMap[blockHash] = &newEventsInBlock
}
}
// AddHeader add a new block hash into map
func (e *EventsInHeight) AddHeader(blockHash common.Hash, parentHash common.Hash) {
events, ok := e.EventsMap[blockHash]
// the events list the block hash already existed, then update the parent hash
if ok {
events.ParentHash = parentHash
} else {
newEventsInBlock := NewEventsInBlock()
newEventsInBlock.ParentHash = parentHash
e.EventsMap[blockHash] = &newEventsInBlock
}
}
// EthEventBuffer store all events from Ethereum smart contract
type EthEventBuffer struct {
Buffer map[string]EventsInHeight
MinHeight *big.Int
}
// NewEthEventBuffer create a new instance of EthEventBuffer
func NewEthEventBuffer() EthEventBuffer {
return EthEventBuffer{
Buffer: make(map[string]EventsInHeight),
MinHeight: big.NewInt(0),
}
}
// AddEvent insert a new event to queue
// func (buff *EthEventBuffer) AddEvent(blockNumber *big.Int, blockHash common.Hash, event EthereumEvent) {
func (buff *EthEventBuffer) AddEvent(blockNumber fmt.Stringer, blockHash common.Hash, event EthereumEvent) {
// Check if block number already in the map
events, ok := buff.Buffer[blockNumber.String()]
if ok {
events.AddEvent(blockHash, event)
} else {
newEvents := NewEventsInHeight()
newEvents.AddEvent(blockHash, event)
buff.Buffer[blockNumber.String()] = newEvents
}
}
// AddHeader create new entry for new header
func (buff *EthEventBuffer) AddHeader(blockNumber *big.Int, blockHash common.Hash, parentHash common.Hash) {
if buff.MinHeight.Cmp(big.NewInt(0)) == 0 {
buff.MinHeight = blockNumber
}
// Check if block number already in the map
eventsInHeight, ok := buff.Buffer[blockNumber.String()]
if ok {
eventsInHeight.AddHeader(blockHash, parentHash)
} else {
newEventsInHeight := NewEventsInHeight()
newEventsInHeight.AddHeader(blockHash, parentHash)
buff.Buffer[blockNumber.String()] = newEventsInHeight
}
}
// GetDepth get the depth of a block
func (buff *EthEventBuffer) GetDepth(blockNumber *big.Int, blockHash common.Hash) uint64 {
eventsInHeight, ok := buff.Buffer[blockNumber.String()]
if ok {
// if there is block's parent is the block hash
for key, eventsInBlock := range eventsInHeight.EventsMap {
if eventsInBlock.ParentHash == blockHash {
one := big.NewInt(1)
one.Add(one, blockNumber)
// recursive to its child block
return buff.GetDepth(one, key) + 1
}
}
}
return 0
}
// RemoveHeight remove an entry
func (buff *EthEventBuffer) RemoveHeight() {
delete(buff.Buffer, buff.MinHeight.String())
buff.MinHeight.Add(buff.MinHeight, big.NewInt(1))
}
// GetHeaderEvents get the events in block of best chain
func (buff *EthEventBuffer) GetHeaderEvents() []EthereumEvent {
eventsInHeight, ok := buff.Buffer[buff.MinHeight.String()]
if ok {
maxDepth := uint64(0)
result := []EthereumEvent{}
one := big.NewInt(1)
one.Add(one, buff.MinHeight)
for blockHash, eventsInBlock := range eventsInHeight.EventsMap {
depth := buff.GetDepth(one, blockHash)
if depth >= maxDepth {
maxDepth = depth
result = eventsInBlock.Events
}
}
return result
}
return []EthereumEvent{}
}