-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
pool.go
198 lines (168 loc) · 6.01 KB
/
pool.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
192
193
194
195
196
197
198
package log
import (
"math"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
heaps "github.com/theodesp/go-heaps"
pairingHeap "github.com/theodesp/go-heaps/pairing"
"github.com/smartcontractkit/chainlink/v2/core/logger"
)
// The Log Pool interface.
type iLogPool interface {
// AddLog adds log to the pool and returns true if its block number is a new minimum.
addLog(log types.Log) bool
// GetAndDeleteAll purges the pool completely, returns all logs, and also the minimum and
// maximum block numbers retrieved.
getAndDeleteAll() ([]logsOnBlock, int64, int64)
// GetLogsToSend returns all logs upto the block number specified in latestBlockNum.
// Also returns the minimum block number in the result.
// In case the pool is empty, returns empty results, and min block number=0
getLogsToSend(latestBlockNum int64) ([]logsOnBlock, int64)
// DeleteOlderLogs deletes all logs in blocks that are less than specific block number keptDepth.
// Also returns the remaining minimum block number in pool after these deletions.
// Returns nil if this ends up emptying the pool.
deleteOlderLogs(keptDepth int64) *int64
// RemoveBlock removes all logs for the block identified by provided Block hash and number.
removeBlock(hash common.Hash, number uint64)
// TestOnly_getNumLogsForBlock FOR TESTING USE ONLY.
// Returns all logs for the provided block hash.
testOnly_getNumLogsForBlock(bh common.Hash) int
}
type logPool struct {
// A mapping of block numbers to a set of block hashes for all
// the logs in the pool.
hashesByBlockNumbers map[uint64]map[common.Hash]struct{}
// A mapping of block hashes, to tx index within block, to log index, to logs
logsByBlockHash map[common.Hash]map[uint]map[uint]types.Log
// This min-heap maintains block numbers of logs in the pool.
// it helps us easily determine the minimum log block number
// in the pool (while the set of log block numbers is dynamically changing).
heap *pairingHeap.PairHeap
logger logger.Logger
}
func newLogPool(lggr logger.Logger) *logPool {
return &logPool{
hashesByBlockNumbers: make(map[uint64]map[common.Hash]struct{}),
logsByBlockHash: make(map[common.Hash]map[uint]map[uint]types.Log),
heap: pairingHeap.New(),
logger: lggr.Named("LogPool"),
}
}
func (pool *logPool) addLog(log types.Log) bool {
_, exists := pool.hashesByBlockNumbers[log.BlockNumber]
if !exists {
pool.hashesByBlockNumbers[log.BlockNumber] = make(map[common.Hash]struct{})
}
pool.hashesByBlockNumbers[log.BlockNumber][log.BlockHash] = struct{}{}
if _, exists := pool.logsByBlockHash[log.BlockHash]; !exists {
pool.logsByBlockHash[log.BlockHash] = make(map[uint]map[uint]types.Log)
}
if _, exists := pool.logsByBlockHash[log.BlockHash][log.TxIndex]; !exists {
pool.logsByBlockHash[log.BlockHash][log.TxIndex] = make(map[uint]types.Log)
}
pool.logsByBlockHash[log.BlockHash][log.TxIndex][log.Index] = log
min := pool.heap.FindMin()
pool.heap.Insert(Uint64(log.BlockNumber))
pool.logger.Debugw("Inserted block to log pool", "blockNumber", log.BlockNumber, "blockHash", log.BlockHash, "index", log.Index, "prevMinBlockNumber", min)
// first or new min
return min == nil || log.BlockNumber < uint64(min.(Uint64))
}
func (pool *logPool) getAndDeleteAll() ([]logsOnBlock, int64, int64) {
logsToReturn := make([]logsOnBlock, 0)
lowest := int64(math.MaxInt64)
highest := int64(0)
for {
item := pool.heap.DeleteMin()
if item == nil {
break
}
blockNum := uint64(item.(Uint64))
hashes, exists := pool.hashesByBlockNumbers[blockNum]
if exists {
if int64(blockNum) < lowest {
lowest = int64(blockNum)
}
if int64(blockNum) > highest {
highest = int64(blockNum)
}
for hash := range hashes {
logsToReturn = append(logsToReturn, newLogsOnBlock(blockNum, pool.logsByBlockHash[hash]))
delete(pool.hashesByBlockNumbers[blockNum], hash)
delete(pool.logsByBlockHash, hash)
}
}
delete(pool.hashesByBlockNumbers, blockNum)
}
return logsToReturn, lowest, highest
}
func (pool *logPool) getLogsToSend(latestBlockNum int64) ([]logsOnBlock, int64) {
logsToReturn := make([]logsOnBlock, 0)
// gathering logs to return - from min block number kept, to latestBlockNum
minBlockNumToSendItem := pool.heap.FindMin()
if minBlockNumToSendItem == nil {
return logsToReturn, 0
}
minBlockNumToSend := int64(minBlockNumToSendItem.(Uint64))
for num := minBlockNumToSend; num <= latestBlockNum; num++ {
for hash := range pool.hashesByBlockNumbers[uint64(num)] {
logsToReturn = append(logsToReturn, newLogsOnBlock(uint64(num), pool.logsByBlockHash[hash]))
}
}
return logsToReturn, minBlockNumToSend
}
func (pool *logPool) deleteOlderLogs(keptDepth int64) *int64 {
min := pool.heap.FindMin
for item := min(); item != nil; item = min() {
blockNum := uint64(item.(Uint64))
if i := int64(blockNum); i >= keptDepth {
return &i
}
pool.heap.DeleteMin()
for hash := range pool.hashesByBlockNumbers[blockNum] {
delete(pool.logsByBlockHash, hash)
}
delete(pool.hashesByBlockNumbers, blockNum)
}
return nil
}
func (pool *logPool) removeBlock(hash common.Hash, number uint64) {
// deleting all logs for this log's block hash
delete(pool.logsByBlockHash, hash)
delete(pool.hashesByBlockNumbers[number], hash)
if len(pool.hashesByBlockNumbers[number]) == 0 {
delete(pool.hashesByBlockNumbers, number)
}
}
func (pool *logPool) testOnly_getNumLogsForBlock(bh common.Hash) int {
var numLogs int
for _, txLogs := range pool.logsByBlockHash[bh] {
numLogs += len(txLogs)
}
return numLogs
}
type Uint64 uint64
func (a Uint64) Compare(b heaps.Item) int {
a1 := a
a2 := b.(Uint64)
switch {
case a1 > a2:
return 1
case a1 < a2:
return -1
default:
return 0
}
}
type logsOnBlock struct {
BlockNumber uint64
Logs []types.Log
}
func newLogsOnBlock(num uint64, logsMap map[uint]map[uint]types.Log) logsOnBlock {
logs := make([]types.Log, 0, len(logsMap))
for _, txLogs := range logsMap {
for _, l := range txLogs {
logs = append(logs, l)
}
}
return logsOnBlock{num, logs}
}