-
Notifications
You must be signed in to change notification settings - Fork 242
/
block_ranges_sequential_dao.go
279 lines (230 loc) · 9.62 KB
/
block_ranges_sequential_dao.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package transfer
import (
"database/sql"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/services/wallet/bigint"
)
type BlockRangeDAOer interface {
getBlockRange(chainID uint64, address common.Address) (blockRange *ethTokensBlockRanges, exists bool, err error)
getBlockRanges(chainID uint64, addresses []common.Address) (blockRanges map[common.Address]*ethTokensBlockRanges, err error)
upsertRange(chainID uint64, account common.Address, newBlockRange *ethTokensBlockRanges) (err error)
updateTokenRange(chainID uint64, account common.Address, newBlockRange *BlockRange) (err error)
upsertEthRange(chainID uint64, account common.Address, newBlockRange *BlockRange) (err error)
}
type BlockRangeSequentialDAO struct {
db *sql.DB
}
type BlockRange struct {
Start *big.Int // Block of first transfer
FirstKnown *big.Int // Oldest scanned block
LastKnown *big.Int // Last scanned block
}
func NewBlockRange() *BlockRange {
return &BlockRange{Start: nil, FirstKnown: nil, LastKnown: nil}
}
type ethTokensBlockRanges struct {
eth *BlockRange
tokens *BlockRange
balanceCheckHash string
}
func newEthTokensBlockRanges() *ethTokensBlockRanges {
return ðTokensBlockRanges{eth: NewBlockRange(), tokens: NewBlockRange()}
}
func scanRanges(rows *sql.Rows) (map[common.Address]*ethTokensBlockRanges, error) {
blockRanges := make(map[common.Address]*ethTokensBlockRanges)
for rows.Next() {
efk := &bigint.NilableSQLBigInt{}
elk := &bigint.NilableSQLBigInt{}
es := &bigint.NilableSQLBigInt{}
tfk := &bigint.NilableSQLBigInt{}
tlk := &bigint.NilableSQLBigInt{}
ts := &bigint.NilableSQLBigInt{}
addressB := []byte{}
blockRange := newEthTokensBlockRanges()
err := rows.Scan(&addressB, es, efk, elk, ts, tfk, tlk, &blockRange.balanceCheckHash)
if err != nil {
return nil, err
}
address := common.BytesToAddress(addressB)
blockRanges[address] = blockRange
if !es.IsNil() {
blockRanges[address].eth.Start = big.NewInt(es.Int64())
}
if !efk.IsNil() {
blockRanges[address].eth.FirstKnown = big.NewInt(efk.Int64())
}
if !elk.IsNil() {
blockRanges[address].eth.LastKnown = big.NewInt(elk.Int64())
}
if !ts.IsNil() {
blockRanges[address].tokens.Start = big.NewInt(ts.Int64())
}
if !tfk.IsNil() {
blockRanges[address].tokens.FirstKnown = big.NewInt(tfk.Int64())
}
if !tlk.IsNil() {
blockRanges[address].tokens.LastKnown = big.NewInt(tlk.Int64())
}
}
return blockRanges, nil
}
func (b *BlockRangeSequentialDAO) getBlockRange(chainID uint64, address common.Address) (blockRange *ethTokensBlockRanges, exists bool, err error) {
query := `SELECT address, blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash FROM blocks_ranges_sequential
WHERE address = ?
AND network_id = ?`
rows, err := b.db.Query(query, address, chainID)
if err != nil {
return
}
defer rows.Close()
ranges, err := scanRanges(rows)
if err != nil {
return nil, false, err
}
blockRange, exists = ranges[address]
if !exists {
blockRange = newEthTokensBlockRanges()
}
return blockRange, exists, nil
}
func (b *BlockRangeSequentialDAO) getBlockRanges(chainID uint64, addresses []common.Address) (blockRanges map[common.Address]*ethTokensBlockRanges, err error) {
blockRanges = make(map[common.Address]*ethTokensBlockRanges)
addressesPlaceholder := ""
for i := 0; i < len(addresses); i++ {
addressesPlaceholder += "?"
if i < len(addresses)-1 {
addressesPlaceholder += ","
}
}
query := "SELECT address, blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash FROM blocks_ranges_sequential WHERE address IN (" + //nolint: gosec
addressesPlaceholder + ") AND network_id = ?"
params := []interface{}{}
for _, address := range addresses {
params = append(params, address)
}
params = append(params, chainID)
rows, err := b.db.Query(query, params...)
if err != nil {
return
}
defer rows.Close()
return scanRanges(rows)
}
func (b *BlockRangeSequentialDAO) deleteRange(account common.Address) error {
log.Debug("delete blocks range", "account", account)
delete, err := b.db.Prepare(`DELETE FROM blocks_ranges_sequential WHERE address = ?`)
if err != nil {
log.Error("Failed to prepare deletion of sequential block range", "error", err)
return err
}
_, err = delete.Exec(account)
return err
}
func (b *BlockRangeSequentialDAO) upsertRange(chainID uint64, account common.Address, newBlockRange *ethTokensBlockRanges) (err error) {
ethTokensBlockRange, exists, err := b.getBlockRange(chainID, account)
if err != nil {
return err
}
ethBlockRange := prepareUpdatedBlockRange(ethTokensBlockRange.eth, newBlockRange.eth)
tokensBlockRange := prepareUpdatedBlockRange(ethTokensBlockRange.tokens, newBlockRange.tokens)
log.Debug("upsert eth and tokens blocks range",
"account", account, "chainID", chainID,
"eth.start", ethBlockRange.Start,
"eth.first", ethBlockRange.FirstKnown,
"eth.last", ethBlockRange.LastKnown,
"tokens.first", tokensBlockRange.FirstKnown,
"tokens.last", tokensBlockRange.LastKnown,
"hash", newBlockRange.balanceCheckHash)
var query *sql.Stmt
if exists {
query, err = b.db.Prepare(`UPDATE blocks_ranges_sequential SET
blk_start = ?,
blk_first = ?,
blk_last = ?,
token_blk_start = ?,
token_blk_first = ?,
token_blk_last = ?,
balance_check_hash = ?
WHERE network_id = ? AND address = ?`)
} else {
query, err = b.db.Prepare(`INSERT INTO blocks_ranges_sequential
(blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash, network_id, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
}
if err != nil {
return err
}
_, err = query.Exec((*bigint.SQLBigInt)(ethBlockRange.Start), (*bigint.SQLBigInt)(ethBlockRange.FirstKnown), (*bigint.SQLBigInt)(ethBlockRange.LastKnown),
(*bigint.SQLBigInt)(tokensBlockRange.Start), (*bigint.SQLBigInt)(tokensBlockRange.FirstKnown), (*bigint.SQLBigInt)(tokensBlockRange.LastKnown), newBlockRange.balanceCheckHash, chainID, account)
return err
}
func (b *BlockRangeSequentialDAO) upsertEthRange(chainID uint64, account common.Address,
newBlockRange *BlockRange) (err error) {
ethTokensBlockRange, exists, err := b.getBlockRange(chainID, account)
if err != nil {
return err
}
blockRange := prepareUpdatedBlockRange(ethTokensBlockRange.eth, newBlockRange)
log.Debug("upsert eth blocks range", "account", account, "chainID", chainID,
"start", blockRange.Start,
"first", blockRange.FirstKnown,
"last", blockRange.LastKnown,
"old hash", ethTokensBlockRange.balanceCheckHash)
var query *sql.Stmt
if exists {
query, err = b.db.Prepare(`UPDATE blocks_ranges_sequential SET
blk_start = ?,
blk_first = ?,
blk_last = ?
WHERE network_id = ? AND address = ?`)
} else {
query, err = b.db.Prepare(`INSERT INTO blocks_ranges_sequential
(blk_start, blk_first, blk_last, network_id, address) VALUES (?, ?, ?, ?, ?)`)
}
if err != nil {
return err
}
_, err = query.Exec((*bigint.SQLBigInt)(blockRange.Start), (*bigint.SQLBigInt)(blockRange.FirstKnown), (*bigint.SQLBigInt)(blockRange.LastKnown), chainID, account)
return err
}
func (b *BlockRangeSequentialDAO) updateTokenRange(chainID uint64, account common.Address,
newBlockRange *BlockRange) (err error) {
ethTokensBlockRange, _, err := b.getBlockRange(chainID, account)
if err != nil {
return err
}
blockRange := prepareUpdatedBlockRange(ethTokensBlockRange.tokens, newBlockRange)
log.Debug("update tokens blocks range",
"first", blockRange.FirstKnown,
"last", blockRange.LastKnown)
update, err := b.db.Prepare(`UPDATE blocks_ranges_sequential SET token_blk_start = ?, token_blk_first = ?, token_blk_last = ? WHERE network_id = ? AND address = ?`)
if err != nil {
return err
}
_, err = update.Exec((*bigint.SQLBigInt)(blockRange.Start), (*bigint.SQLBigInt)(blockRange.FirstKnown),
(*bigint.SQLBigInt)(blockRange.LastKnown), chainID, account)
return err
}
func prepareUpdatedBlockRange(blockRange, newBlockRange *BlockRange) *BlockRange {
if newBlockRange != nil {
// Ovewrite start block if there was not any or if new one is older, because it can be precised only
// to a greater value, because no history can be before some block that is considered
// as a start of history, but due to concurrent block range checks, a newer greater block
// can be found that matches criteria of a start block (nonce is zero, balances are equal)
if newBlockRange.Start != nil && (blockRange.Start == nil || blockRange.Start.Cmp(newBlockRange.Start) < 0) {
blockRange.Start = newBlockRange.Start
}
// Overwrite first known block if there was not any or if new one is older
if (blockRange.FirstKnown == nil && newBlockRange.FirstKnown != nil) ||
(blockRange.FirstKnown != nil && newBlockRange.FirstKnown != nil && blockRange.FirstKnown.Cmp(newBlockRange.FirstKnown) > 0) {
blockRange.FirstKnown = newBlockRange.FirstKnown
}
// Overwrite last known block if there was not any or if new one is newer
if (blockRange.LastKnown == nil && newBlockRange.LastKnown != nil) ||
(blockRange.LastKnown != nil && newBlockRange.LastKnown != nil && blockRange.LastKnown.Cmp(newBlockRange.LastKnown) < 0) {
blockRange.LastKnown = newBlockRange.LastKnown
}
}
return blockRange
}