-
Notifications
You must be signed in to change notification settings - Fork 49
/
repository.go
652 lines (570 loc) · 16.9 KB
/
repository.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package database
import (
"encoding/binary"
"github.com/golang/protobuf/proto"
"github.com/idena-network/idena-go/blockchain/types"
"github.com/idena-network/idena-go/common"
"github.com/idena-network/idena-go/common/math"
"github.com/idena-network/idena-go/log"
models "github.com/idena-network/idena-go/protobuf"
dbm "github.com/tendermint/tm-db"
math2 "math"
"math/big"
"sort"
)
const (
MaxWeakCertificatesCount = 100
maxEventName = "\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F\u007F"
)
type Repo struct {
db dbm.DB
}
func NewRepo(db dbm.DB) *Repo {
return &Repo{
db: db,
}
}
func encodeUint32Number(number uint32) []byte {
enc := make([]byte, 4)
binary.BigEndian.PutUint32(enc, number)
return enc
}
func encodeUint64Number(number uint64) []byte {
enc := make([]byte, 8)
binary.BigEndian.PutUint64(enc, number)
return enc
}
// headerKey = headerPrefix + hash
func headerKey(hash common.Hash) []byte {
return append(headerPrefix, hash.Bytes()...)
}
func certKey(hash common.Hash) []byte {
return append(certPrefix, hash.Bytes()...)
}
func headerHashKey(number uint64) []byte {
return append(append(headerPrefix, encodeUint64Number(number)...), headerHashSuffix...)
}
func finalConsensusKey(hash common.Hash) []byte {
return append(finalConsensusPrefix, hash.Bytes()...)
}
func txIndexKey(hash common.Hash) []byte {
return append(transactionIndexPrefix, hash.Bytes()...)
}
func receiptIndexKey(hash common.Hash) []byte {
return append(receiptIndexPrefix, hash.Bytes()...)
}
func savedTxKey(sender common.Address, timestamp int64, nonce uint32, hash common.Hash) []byte {
key := append(ownTransactionIndexPrefix, sender[:]...)
key = append(key, encodeUint64Number(uint64(timestamp))...)
key = append(key, encodeUint32Number(nonce)...)
return append(key, hash[:]...)
}
func savedEventKey(contact common.Address, txHash []byte, idx uint32, event string) []byte {
key := append(eventPrefix, contact.Bytes()...)
key = append(key, txHash...)
key = append(key, common.ToBytes(idx)...)
key = append(key, []byte(event)...)
return key
}
func burntCoinsKey(height uint64, hash common.Hash) []byte {
key := append(burntCoinsPrefix, encodeUint64Number(height)...)
return append(key, hash[:]...)
}
func burntCoinsMinKey() []byte {
return burntCoinsKey(0, common.BytesToHash(common.MinHash[:]))
}
func identityStateDiffKey(height uint64) []byte {
return append(identityStateDiffPrefix, encodeUint64Number(height)...)
}
func applyTxLogKey(hash common.Hash) []byte {
return append(applyTxLogPrefix, hash.Bytes()...)
}
func blackListTxKey(hash common.Hash) []byte {
return append(blackListedTxPrefix, hash.Bytes()...)
}
func (r *Repo) ReadBlockHeader(hash common.Hash) *types.Header {
data, err := r.db.Get(headerKey(hash))
assertNoError(err)
if data == nil {
return nil
}
header := new(types.Header)
if err := header.FromBytes(data); err != nil {
log.Error("Invalid block header proto", "hash", hash, "err", err)
return nil
}
return header
}
func (r *Repo) ReadHead() *types.Header {
data, err := r.db.Get(headBlockKey)
assertNoError(err)
if data == nil {
return nil
}
header := new(types.Header)
if err := header.FromBytes(data); err != nil {
log.Error("Invalid block header proto", "err", err)
return nil
}
return header
}
func (r *Repo) WriteHead(batch dbm.Batch, header *types.Header) {
// Write the encoded header
data, err := header.ToBytes()
if err != nil {
log.Crit("Failed to proto encode header", "err", err)
return
}
if batch != nil {
batch.Set(headBlockKey, data)
} else {
r.db.Set(headBlockKey, data)
}
}
func (r *Repo) WriteBlockHeader(header *types.Header) {
data, err := header.ToBytes()
if err != nil {
log.Crit("Failed to proto encode header", "err", err)
}
r.db.Set(headerKey(header.Hash()), data)
}
func (r *Repo) RemoveHeader(hash common.Hash) {
r.db.Delete(headerKey(hash))
}
func (r *Repo) WriteCertificate(hash common.Hash, cert *types.BlockCert) {
data, err := cert.ToBytes()
if err != nil {
log.Crit("failed to proto encode block cert", "err", err)
}
r.db.Set(certKey(hash), data)
}
func (r *Repo) WriteCanonicalHash(height uint64, hash common.Hash) {
key := headerHashKey(height)
r.db.Set(key, hash.Bytes())
}
func (r *Repo) RemoveCanonicalHash(height uint64) {
key := headerHashKey(height)
r.db.Delete(key)
}
func (r *Repo) ReadCanonicalHash(height uint64) common.Hash {
key := headerHashKey(height)
data, err := r.db.Get(key)
assertNoError(err)
if len(data) == 0 {
return common.Hash{}
}
return common.BytesToHash(data)
}
func (r *Repo) WriteFinalConsensus(hash common.Hash) {
key := finalConsensusKey(hash)
r.db.Set(key, []byte{0x1})
}
func (r *Repo) SetHead(batch dbm.Batch, height uint64) {
hash := r.ReadCanonicalHash(height)
if hash != (common.Hash{}) {
header := r.ReadBlockHeader(hash)
if header != nil {
r.WriteHead(batch, header)
}
}
}
func (r *Repo) WriteTxIndex(txHash common.Hash, index *types.TransactionIndex) {
data, err := index.ToBytes()
if err != nil {
log.Crit("failed to proto encode transaction index", "err", err)
return
}
r.db.Set(txIndexKey(txHash), data)
}
func (r *Repo) ReadTxIndex(hash common.Hash) *types.TransactionIndex {
key := txIndexKey(hash)
data, err := r.db.Get(key)
assertNoError(err)
if data == nil {
return nil
}
index := new(types.TransactionIndex)
if err := index.FromBytes(data); err != nil {
log.Error("invalid transaction index proto", "err", err)
return nil
}
return index
}
func (r *Repo) WriteReceiptIndex(hash common.Hash, idx *types.TxReceiptIndex) {
data, err := idx.ToBytes()
if err != nil {
log.Crit("failed to proto encode receipt index", "err", err)
return
}
r.db.Set(receiptIndexKey(hash), data)
}
func (r *Repo) ReadReceiptIndex(hash common.Hash) *types.TxReceiptIndex {
key := receiptIndexKey(hash)
data, err := r.db.Get(key)
assertNoError(err)
if data == nil {
return nil
}
index := new(types.TxReceiptIndex)
if err := index.FromBytes(data); err != nil {
log.Error("invalid receipt index proto", "err", err)
return nil
}
return index
}
func (r *Repo) ReadCertificate(hash common.Hash) *types.BlockCert {
data, err := r.db.Get(certKey(hash))
assertNoError(err)
if data == nil {
return nil
}
cert := new(types.BlockCert)
if err := cert.FromBytes(data); err != nil {
log.Error("Invalid block cert proto", "err", err)
return nil
}
return cert
}
func (r *Repo) readWeakCertificates() *models.ProtoWeakCertificates {
data, err := r.db.Get(weakCertificatesKey)
assertNoError(err)
if data == nil {
return nil
}
w := new(models.ProtoWeakCertificates)
if err := proto.Unmarshal(data, w); err != nil {
log.Error("invalid weak certificates proto", "err", err)
return nil
}
return w
}
func (r *Repo) writeWeakCertificate(w *models.ProtoWeakCertificates) {
data, err := proto.Marshal(w)
if err != nil {
log.Crit("failed to proto encode weak certificates", "err", err)
return
}
r.db.Set(weakCertificatesKey, data)
}
func (r *Repo) removeCertificate(hash common.Hash) {
r.db.Delete(certKey(hash))
}
func (r *Repo) WriteWeakCertificate(hash common.Hash) {
weakCerts := r.readWeakCertificates()
if weakCerts == nil {
weakCerts = &models.ProtoWeakCertificates{}
}
weakCerts.Hashes = append(weakCerts.Hashes, hash[:])
if len(weakCerts.Hashes) > MaxWeakCertificatesCount {
r.removeCertificate(common.BytesToHash(weakCerts.Hashes[0]))
weakCerts.Hashes = weakCerts.Hashes[1:]
}
r.writeWeakCertificate(weakCerts)
}
func (r *Repo) LastSnapshotManifest() (cidV2 []byte, root common.Hash, height uint64, fileName string) {
data, err := r.db.Get(lastSnapshotKey)
assertNoError(err)
if data == nil {
return nil, common.Hash{}, 0, ""
}
manifest := new(models.ProtoSnapshotManifestDb)
if err := proto.Unmarshal(data, manifest); err != nil {
log.Error("invalid snapshot manifest proto", "err", err)
return nil, common.Hash{}, 0, ""
}
return manifest.CidV2, common.BytesToHash(manifest.Root), manifest.Height, manifest.FileNameV2
}
func (r *Repo) WriteLastSnapshotManifest(cidV2 []byte, root common.Hash, height uint64, fileNameV2 string) error {
manifest := &models.ProtoSnapshotManifestDb{
CidV2: cidV2,
Height: height,
FileNameV2: fileNameV2,
Root: root[:],
}
data, err := proto.Marshal(manifest)
if err != nil {
log.Crit("failed to proto encode snapshot manifest", "err", err)
return err
}
r.db.Set(lastSnapshotKey, data)
return nil
}
func (r *Repo) WriteIdentityStateDiff(height uint64, diff []byte) {
r.db.Set(identityStateDiffKey(height), diff)
}
func (r *Repo) ReadIdentityStateDiff(height uint64) []byte {
data, err := r.db.Get(identityStateDiffKey(height))
assertNoError(err)
return data
}
func (r *Repo) WritePreliminaryHead(header *types.Header) {
data, err := header.ToBytes()
if err != nil {
log.Crit("Failed to proto encode header", "err", err)
return
}
assertNoError(r.db.Set(preliminaryHeadKey, data))
}
func (r *Repo) ReadPreliminaryHead() *types.Header {
data, err := r.db.Get(preliminaryHeadKey)
assertNoError(err)
if data == nil {
return nil
}
header := new(types.Header)
if err := header.FromBytes(data); err != nil {
log.Error("Invalid block header proto", "err", err)
return nil
}
return header
}
func (r *Repo) RemovePreliminaryHead(batch dbm.Batch) {
if batch != nil {
batch.Delete(preliminaryHeadKey)
} else {
assertNoError(r.db.Delete(preliminaryHeadKey))
}
}
func (r *Repo) ReadActivity() *types.ActivityMonitor {
data, err := r.db.Get(activityMonitorKey)
assertNoError(err)
if data == nil {
return nil
}
monitor := new(types.ActivityMonitor)
if err := monitor.FromBytes(data); err != nil {
log.Error("invalid activity monitor proto", "err", err)
return nil
}
return monitor
}
func (r *Repo) WriteActivity(monitor *types.ActivityMonitor) {
data, err := monitor.ToBytes()
if err != nil {
log.Crit("failed to proto encode activity monitor", "err", err)
return
}
r.db.Set(activityMonitorKey, data)
}
func (r *Repo) SaveTx(address common.Address, blockHash common.Hash, timestamp int64, feePerGas *big.Int, transaction *types.Transaction) {
s := &types.SavedTransaction{
Tx: transaction,
FeePerGas: feePerGas,
BlockHash: blockHash,
Timestamp: timestamp,
}
data, err := s.ToBytes()
if err != nil {
log.Crit("failed to proto encode saved transaction", "err", err)
return
}
r.db.Set(savedTxKey(address, timestamp, transaction.AccountNonce, transaction.Hash()), data)
}
func (r *Repo) GetSavedTxs(address common.Address, count int, token []byte) (txs []*types.SavedTransaction, nextToken []byte) {
if token == nil {
token = savedTxKey(address, math.MaxInt64, uint32(math.MaxUint32), common.BytesToHash(common.MaxHash))
} else {
token = append(token, common.MaxHash...)
}
it, err := r.db.ReverseIterator(savedTxKey(address, 0, 0, common.BytesToHash(common.MinHash[:])), token)
assertNoError(err)
defer it.Close()
for ; it.Valid(); it.Next() {
key, value := it.Key(), it.Value()
if len(txs) == count {
continuationToken := make([]byte, len(key)-common.HashLength)
copy(continuationToken, key[:len(key)-common.HashLength])
return txs, continuationToken
}
tx := new(types.SavedTransaction)
if err := tx.FromBytes(value); err != nil {
log.Error("cannot parse tx", "key", key)
continue
}
txs = append(txs, tx)
}
return txs, nil
}
func (r *Repo) DeleteOutdatedBurntCoins(blockHeight uint64, blockRange uint64) {
if blockHeight <= blockRange {
return
}
it, err := r.db.Iterator(burntCoinsMinKey(), burntCoinsKey(blockHeight-blockRange,
common.BytesToHash(common.MaxHash[:])))
assertNoError(err)
defer it.Close()
for ; it.Valid(); it.Next() {
r.db.Delete(it.Key())
}
}
func (r *Repo) SaveBurntCoins(blockHeight uint64, txHash common.Hash, address common.Address, key string, amount *big.Int) {
s := &types.BurntCoins{
Address: address,
Key: key,
Amount: amount,
}
data, err := s.ToBytes()
if err != nil {
log.Crit("failed to proto encode saved burnt coins", "err", err)
return
}
r.db.Set(burntCoinsKey(blockHeight, txHash), data)
}
func (r *Repo) GetTotalBurntCoins() []*types.BurntCoins {
it, err := r.db.Iterator(burntCoinsMinKey(), burntCoinsKey(math.MaxUint64, common.BytesToHash(common.MaxHash[:])))
assertNoError(err)
defer it.Close()
var coinsByAddressAndKey map[common.Address]map[string]*types.BurntCoins
var res []*types.BurntCoins
for ; it.Valid(); it.Next() {
key, value := it.Key(), it.Value()
burntCoins := new(types.BurntCoins)
if err := burntCoins.FromBytes(value); err != nil {
log.Error("cannot parse burnt coins", "key", key)
continue
}
if common.ZeroOrNil(burntCoins.Amount) {
continue
}
if coinsByAddressAndKey == nil {
coinsByAddressAndKey = make(map[common.Address]map[string]*types.BurntCoins)
}
if addressCoinsByKey, ok := coinsByAddressAndKey[burntCoins.Address]; ok {
if total, ok := addressCoinsByKey[burntCoins.Key]; ok {
total.Amount = new(big.Int).Add(total.Amount, burntCoins.Amount)
} else {
addressCoinsByKey[burntCoins.Key] = burntCoins
res = append(res, burntCoins)
}
} else {
addressCoinsByKey = make(map[string]*types.BurntCoins)
addressCoinsByKey[burntCoins.Key] = burntCoins
coinsByAddressAndKey[burntCoins.Address] = addressCoinsByKey
res = append(res, burntCoins)
}
}
sort.Slice(res, func(i, j int) bool {
return res[i].Amount.Cmp(res[j].Amount) == 1
})
return res
}
func (r *Repo) WriteEvent(contract common.Address, txHash common.Hash, idx uint32, event *types.TxEvent) {
e := types.SavedEvent{
Contract: contract,
Event: event.EventName,
Args: event.Data,
}
data, err := e.ToBytes()
if err != nil {
log.Crit("failed to proto encode saved event", "err", err)
return
}
r.db.Set(savedEventKey(contract, txHash.Bytes(), idx, event.EventName), data)
}
func (r *Repo) GetSavedEvents(contract common.Address) (events []*types.SavedEvent) {
it, err := r.db.ReverseIterator(savedEventKey(contract, common.MinHash[:], 0, "\x00"), savedEventKey(contract, common.MaxHash, math2.MaxUint32, maxEventName))
assertNoError(err)
defer it.Close()
for ; it.Valid(); it.Next() {
key, value := it.Key(), it.Value()
e := new(types.SavedEvent)
if err := e.FromBytes(value); err != nil {
log.Error("cannot parse tx", "key", key)
continue
}
events = append(events, e)
}
return events
}
func (r *Repo) WriteIntermediateGenesis(batch dbm.Batch, height uint64) {
if batch != nil {
batch.Set(intermediateGenesisKey, common.ToBytes(height))
} else {
r.db.Set(intermediateGenesisKey, common.ToBytes(height))
}
}
func (r *Repo) ReadIntermediateGenesis() uint64 {
data, err := r.db.Get(intermediateGenesisKey)
if err != nil || len(data) == 0 {
return 0
}
return binary.LittleEndian.Uint64(data)
}
func (r *Repo) WriteUpgradeVotes(votes *types.UpgradeVotes) {
data, _ := votes.ToBytes()
r.db.Set(upgradeVotesKey, data)
}
func (r *Repo) ReadUpgradeVotes() *types.UpgradeVotes {
data, _ := r.db.Get(upgradeVotesKey)
if len(data) > 0 {
v := types.NewUpgradeVotes()
v.FromBytes(data)
return v
}
return nil
}
func (r *Repo) WriteConsensusVersion(batch dbm.Batch, v uint32) {
if batch != nil {
batch.Set(consensusVersionKey, common.ToBytes(v))
} else {
r.db.Set(consensusVersionKey, common.ToBytes(v))
}
}
func (r *Repo) ReadConsensusVersion() uint32 {
data, err := r.db.Get(consensusVersionKey)
if err != nil || len(data) == 0 {
return 0
}
return binary.LittleEndian.Uint32(data)
}
func (r *Repo) WritePreliminaryConsensusVersion(v uint32) {
r.db.Set(preliminaryConsVersionKey, common.ToBytes(v))
}
func (r *Repo) ReadPreliminaryConsensusVersion() uint32 {
data, err := r.db.Get(preliminaryConsVersionKey)
if err != nil || len(data) == 0 {
return 0
}
return binary.LittleEndian.Uint32(data)
}
func (r *Repo) RemovePreliminaryConsensusVersion(batch dbm.Batch) {
if batch != nil {
batch.Delete(preliminaryConsVersionKey)
} else {
r.db.Delete(preliminaryConsVersionKey)
}
}
func (r *Repo) WritePreliminaryIntermediateGenesis(height uint64) {
r.db.Set(preliminaryIntermediateGenesisKey, common.ToBytes(height))
}
func (r *Repo) ReadPreliminaryIntermediateGenesis() uint64 {
data, err := r.db.Get(preliminaryIntermediateGenesisKey)
if err != nil || len(data) == 0 {
return 0
}
return binary.LittleEndian.Uint64(data)
}
func (r *Repo) RemovePreliminaryIntermediateGenesis(batch dbm.Batch) {
if batch != nil {
batch.Delete(preliminaryIntermediateGenesisKey)
} else {
r.db.Delete(preliminaryIntermediateGenesisKey)
}
}
func (r *Repo) StartApplyingTx(txHash common.Hash) {
r.db.Set(applyTxLogKey(txHash), txHash.Bytes())
}
func (r *Repo) FinishApplyingTx(txHash common.Hash) {
r.db.Delete(applyTxLogKey(txHash))
}
func (r *Repo) HasApplyingTxLog(txHash common.Hash) bool {
has, _ := r.db.Has(applyTxLogKey(txHash))
return has
}
func (r *Repo) AddToBlackList(txHash common.Hash) {
r.db.Set(blackListTxKey(txHash), txHash.Bytes())
}
func (r *Repo) IsInBlackList(txHash common.Hash) bool {
has, _ := r.db.Has(blackListTxKey(txHash))
return has
}