-
Notifications
You must be signed in to change notification settings - Fork 0
/
logdb.go
423 lines (394 loc) · 9.78 KB
/
logdb.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (c) 2020 The Meter.io 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 logdb
import (
"context"
"database/sql"
"fmt"
"math/big"
"github.com/saolacoincom/saolacoin/block"
"github.com/saolacoincom/saolacoin/meter"
"github.com/saolacoincom/saolacoin/tx"
sqlite3 "github.com/mattn/go-sqlite3"
)
type LogDB struct {
path string
db *sql.DB
driverVersion string
}
var (
GlobalLogDBInstance *LogDB
)
func setGlobalLogDBInstance(db *LogDB) {
GlobalLogDBInstance = db
}
func GetGlobalLogDBInstance() *LogDB {
return GlobalLogDBInstance
}
// New create or open log db at given path.
func New(path string) (logDB *LogDB, err error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
defer func() {
if logDB == nil {
err := db.Close()
if err != nil {
fmt.Println("could not close logdb error:", err)
}
}
}()
if _, err := db.Exec(eventTableSchema + transferTableSchema); err != nil {
return nil, err
}
driverVer, _, _ := sqlite3.Version()
logdbInstance := &LogDB{
path,
db,
driverVer,
}
setGlobalLogDBInstance(logdbInstance)
return logdbInstance, nil
}
// NewMem create a log db in ram.
func NewMem() (*LogDB, error) {
return New(":memory:")
}
// Close close the log db.
func (db *LogDB) Close() {
err := db.db.Close()
if err != nil {
fmt.Println("could not close logdb error:", err)
}
}
func (db *LogDB) Path() string {
return db.path
}
func (db *LogDB) Prepare(header *block.Header) *BlockBatch {
return &BlockBatch{
db: db.db,
header: header,
}
}
func (db *LogDB) FilterEvents(ctx context.Context, filter *EventFilter) ([]*Event, error) {
if filter == nil {
return db.queryEvents(ctx, "SELECT * FROM event")
}
var args []interface{}
stmt := "SELECT * FROM event WHERE 1"
condition := "blockNumber"
if filter.Range != nil {
if filter.Range.Unit == Time {
condition = "blockTime"
}
args = append(args, filter.Range.From)
stmt += " AND " + condition + " >= ? "
if filter.Range.To >= filter.Range.From {
args = append(args, filter.Range.To)
stmt += " AND " + condition + " <= ? "
}
}
for i, criteria := range filter.CriteriaSet {
if i == 0 {
stmt += " AND ( 1"
} else {
stmt += " OR ( 1"
}
if criteria.Address != nil {
args = append(args, criteria.Address.Bytes())
stmt += " AND address = ? "
}
for j, topic := range criteria.Topics {
if topic != nil {
args = append(args, topic.Bytes())
stmt += fmt.Sprintf(" AND topic%v = ?", j)
}
}
stmt += ")"
}
if filter.Order == DESC {
stmt += " ORDER BY blockNumber DESC,eventIndex DESC "
} else {
stmt += " ORDER BY blockNumber ASC,eventIndex ASC "
}
if filter.Options != nil {
stmt += " limit ?, ? "
args = append(args, filter.Options.Offset, filter.Options.Limit)
}
return db.queryEvents(ctx, stmt, args...)
}
func (db *LogDB) FilterTransfers(ctx context.Context, filter *TransferFilter) ([]*Transfer, error) {
if filter == nil {
return db.queryTransfers(ctx, "SELECT * FROM transfer")
}
var args []interface{}
stmt := "SELECT * FROM transfer WHERE 1"
condition := "blockNumber"
if filter.Range != nil {
if filter.Range.Unit == Time {
condition = "blockTime"
}
args = append(args, filter.Range.From)
stmt += " AND " + condition + " >= ? "
if filter.Range.To >= filter.Range.From {
args = append(args, filter.Range.To)
stmt += " AND " + condition + " <= ? "
}
}
if filter.TxID != nil {
args = append(args, filter.TxID.Bytes())
stmt += " AND txID = ? "
}
length := len(filter.CriteriaSet)
if length > 0 {
for i, criteria := range filter.CriteriaSet {
if i == 0 {
stmt += " AND (( 1 "
} else {
stmt += " OR ( 1 "
}
if criteria.TxOrigin != nil {
args = append(args, criteria.TxOrigin.Bytes())
stmt += " AND txOrigin = ? "
}
if criteria.Sender != nil {
args = append(args, criteria.Sender.Bytes())
stmt += " AND sender = ? "
}
if criteria.Recipient != nil {
args = append(args, criteria.Recipient.Bytes())
stmt += " AND recipient = ? "
}
if i == length-1 {
stmt += " )) "
} else {
stmt += " ) "
}
}
}
if filter.Order == DESC {
stmt += " ORDER BY blockNumber DESC,transferIndex DESC "
} else {
stmt += " ORDER BY blockNumber ASC,transferIndex ASC "
}
if filter.Options != nil {
stmt += " limit ?, ? "
args = append(args, filter.Options.Offset, filter.Options.Limit)
}
return db.queryTransfers(ctx, stmt, args...)
}
func (db *LogDB) queryEvents(ctx context.Context, stmt string, args ...interface{}) ([]*Event, error) {
rows, err := db.db.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var events []*Event
for rows.Next() {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
var (
blockID []byte
index uint32
blockNumber uint32
blockTime uint64
txID []byte
txOrigin []byte
address []byte
topics [5][]byte
data []byte
)
if err := rows.Scan(
&blockID,
&index,
&blockNumber,
&blockTime,
&txID,
&txOrigin,
&address,
&topics[0],
&topics[1],
&topics[2],
&topics[3],
&topics[4],
&data,
); err != nil {
return nil, err
}
event := &Event{
BlockID: meter.BytesToBytes32(blockID),
Index: index,
BlockNumber: blockNumber,
BlockTime: blockTime,
TxID: meter.BytesToBytes32(txID),
TxOrigin: meter.BytesToAddress(txOrigin),
Address: meter.BytesToAddress(address),
Data: data,
}
for i, topic := range topics {
if len(topic) > 0 {
h := meter.BytesToBytes32(topic)
event.Topics[i] = &h
}
}
events = append(events, event)
}
if err := rows.Err(); err != nil {
return nil, err
}
return events, nil
}
func (db *LogDB) queryTransfers(ctx context.Context, stmt string, args ...interface{}) ([]*Transfer, error) {
rows, err := db.db.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var transfers []*Transfer
for rows.Next() {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
var (
blockID []byte
index uint32
blockNumber uint32
blockTime uint64
txID []byte
txOrigin []byte
sender []byte
recipient []byte
amount []byte
token uint32
)
if err := rows.Scan(
&blockID,
&index,
&blockNumber,
&blockTime,
&txID,
&txOrigin,
&sender,
&recipient,
&amount,
&token,
); err != nil {
return nil, err
}
trans := &Transfer{
BlockID: meter.BytesToBytes32(blockID),
Index: index,
BlockNumber: blockNumber,
BlockTime: blockTime,
TxID: meter.BytesToBytes32(txID),
TxOrigin: meter.BytesToAddress(txOrigin),
Sender: meter.BytesToAddress(sender),
Recipient: meter.BytesToAddress(recipient),
Amount: new(big.Int).SetBytes(amount),
Token: token,
}
transfers = append(transfers, trans)
}
if err := rows.Err(); err != nil {
return nil, err
}
return transfers, nil
}
func topicValue(topic *meter.Bytes32) []byte {
if topic == nil {
return nil
}
return topic.Bytes()
}
type BlockBatch struct {
db *sql.DB
header *block.Header
events []*Event
transfers []*Transfer
}
func (bb *BlockBatch) execInTx(proc func(*sql.Tx) error) (err error) {
tx, err := bb.db.Begin()
if err != nil {
return err
}
if err := proc(tx); err != nil {
e := tx.Rollback()
if e != nil {
fmt.Println("could not rollback, error:", e)
}
return err
}
return tx.Commit()
}
func (bb *BlockBatch) Commit(abandonedBlocks ...meter.Bytes32) error {
return bb.execInTx(func(tx *sql.Tx) error {
for _, event := range bb.events {
if _, err := tx.Exec("INSERT OR REPLACE INTO event(blockID ,eventIndex, blockNumber ,blockTime ,txID ,txOrigin ,address ,topic0 ,topic1 ,topic2 ,topic3 ,topic4, data) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
event.BlockID.Bytes(),
event.Index,
event.BlockNumber,
event.BlockTime,
event.TxID.Bytes(),
event.TxOrigin.Bytes(),
event.Address.Bytes(),
topicValue(event.Topics[0]),
topicValue(event.Topics[1]),
topicValue(event.Topics[2]),
topicValue(event.Topics[3]),
topicValue(event.Topics[4]),
event.Data,
); err != nil {
return err
}
}
for _, transfer := range bb.transfers {
if _, err := tx.Exec("INSERT OR REPLACE INTO transfer(blockID ,transferIndex, blockNumber ,blockTime ,txID ,txOrigin ,sender ,recipient ,amount, token) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
transfer.BlockID.Bytes(),
transfer.Index,
transfer.BlockNumber,
transfer.BlockTime,
transfer.TxID.Bytes(),
transfer.TxOrigin.Bytes(),
transfer.Sender.Bytes(),
transfer.Recipient.Bytes(),
transfer.Amount.Bytes(),
transfer.Token,
); err != nil {
return err
}
}
for _, id := range abandonedBlocks {
if _, err := tx.Exec("DELETE FROM event WHERE blockID = ?;", id.Bytes()); err != nil {
return err
}
if _, err := tx.Exec("DELETE FROM transfer WHERE blockID = ?;", id.Bytes()); err != nil {
return err
}
}
return nil
})
}
func (bb *BlockBatch) ForTransaction(txID meter.Bytes32, txOrigin meter.Address) struct {
Insert func(tx.Events, tx.Transfers) *BlockBatch
} {
return struct {
Insert func(events tx.Events, transfers tx.Transfers) *BlockBatch
}{
func(events tx.Events, transfers tx.Transfers) *BlockBatch {
for _, event := range events {
bb.events = append(bb.events, newEvent(bb.header, uint32(len(bb.events)), txID, txOrigin, event))
}
for _, transfer := range transfers {
bb.transfers = append(bb.transfers, newTransfer(bb.header, uint32(len(bb.transfers)), txID, txOrigin, transfer))
}
return bb
},
}
}