forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainStore.go
867 lines (705 loc) · 19.4 KB
/
ChainStore.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
package ChainStore
import (
"fmt"
"sync"
"time"
"bytes"
"errors"
"container/list"
"Elastos.ELA/events"
. "Elastos.ELA/common"
"Elastos.ELA/common/log"
. "Elastos.ELA/core/asset"
. "Elastos.ELA/core/store"
. "Elastos.ELA/core/ledger"
tx "Elastos.ELA/core/transaction"
"Elastos.ELA/common/serialization"
"Elastos.ELA/core/contract/program"
. "Elastos.ELA/core/store/LevelDBStore"
)
const TaskChanCap = 4
var (
ErrDBNotFound = errors.New("leveldb: not found")
zeroHash = Uint256{}
)
type persistTask interface{}
type rollbackBlockTask struct {
blockHash Uint256
reply chan bool
}
type persistBlockTask struct {
block *Block
ledger *Ledger
reply chan bool
}
type ChainStore struct {
IStore
taskCh chan persistTask
quit chan chan bool
mu sync.RWMutex // guard the following var
headerIndex map[uint32]Uint256
headerCache map[Uint256]*Header
//blockCache map[Uint256]*Block
headerIdx *list.List
currentBlockHeight uint32
storedHeaderCount uint32
ledger *Ledger
}
func NewLedgerStore() (ILedgerStore, error) {
// TODO: read config file decide which db to use.
st, err := NewLevelDBStore("Chain")
if err != nil {
return nil, err
}
chain := &ChainStore{
IStore: st,
headerIndex: map[uint32]Uint256{},
//blockCache: map[Uint256]*Block{},
headerCache: map[Uint256]*Header{},
headerIdx: list.New(),
currentBlockHeight: 0,
storedHeaderCount: 0,
taskCh: make(chan persistTask, TaskChanCap),
quit: make(chan chan bool, 1),
}
go chain.loop()
return chain, nil
}
func (self *ChainStore) Close() {
closed := make(chan bool)
self.quit <- closed
<-closed
self.Close()
}
func (self *ChainStore) loop() {
for {
select {
case t := <-self.taskCh:
now := time.Now()
switch task := t.(type) {
case *persistBlockTask:
self.handlePersistBlockTask(task.block, task.ledger)
task.reply <- true
tcall := float64(time.Now().Sub(now)) / float64(time.Second)
log.Debugf("handle block exetime: %g num transactions:%d \n", tcall, len(task.block.Transactions))
case *rollbackBlockTask:
self.handleRollbackBlockTask(task.blockHash)
task.reply <- true
tcall := float64(time.Now().Sub(now)) / float64(time.Second)
log.Debugf("handle block rollback exetime: %g \n", tcall)
}
case closed := <-self.quit:
closed <- true
return
}
}
}
// can only be invoked by backend write goroutine
func (self *ChainStore) clearCache(b *Block) {
self.mu.Lock()
defer self.mu.Unlock()
for e := self.headerIdx.Front(); e != nil; e = e.Next() {
n := e.Value.(Header)
h := n.Blockdata.Hash()
if h.CompareTo(b.Hash()) == 0 {
self.headerIdx.Remove(e)
}
}
}
func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block) (uint32, error) {
prefix := []byte{byte(CFG_Version)}
version, err := bd.Get(prefix)
if err != nil {
version = []byte{0x00}
}
if version[0] == 0x00 {
// batch delete old data
bd.NewBatch()
iter := bd.NewIterator(nil)
for iter.Next() {
bd.BatchDelete(iter.Key())
}
iter.Release()
err := bd.BatchCommit()
if err != nil {
return 0, err
}
// persist genesis block
bd.persist(genesisBlock)
// put version to db
err = bd.Put(prefix, []byte{0x01})
if err != nil {
return 0, err
}
}
// GenesisBlock should exist in chain
// Or the bookkeepers are not consistent with the chain
hash := genesisBlock.Hash()
if !bd.IsBlockInStore(hash) {
return 0, errors.New("genesis block is not consistent with the chain")
}
bd.ledger.Blockchain.GenesisHash = hash
//bd.headerIndex[0] = hash
// Get Current Block
currentBlockPrefix := []byte{byte(SYS_CurrentBlock)}
data, err := bd.Get(currentBlockPrefix)
if err != nil {
return 0, err
}
r := bytes.NewReader(data)
var blockHash Uint256
blockHash.Deserialize(r)
bd.currentBlockHeight, err = serialization.ReadUint32(r)
endHeight := bd.currentBlockHeight
startHeight := uint32(0)
if endHeight > MinMemoryNodes {
startHeight = endHeight - MinMemoryNodes
}
for start := startHeight; start <= endHeight; start++ {
hash, err := bd.GetBlockHash(start)
if err != nil {
return 0, err
}
header, err := bd.GetHeader(hash)
if err != nil {
return 0, err
}
node, err := bd.ledger.Blockchain.LoadBlockNode(header.Blockdata, &hash)
if err != nil {
return 0, err
}
// This node is now the end of the best chain.
bd.ledger.Blockchain.BestChain = node
}
//bd.ledger.Blockchain.DumpState()
return bd.currentBlockHeight, nil
}
func (bd *ChainStore) InitLedgerStore(l *Ledger) error {
// TODO: InitLedgerStore
bd.ledger = l
return nil
}
func (bd *ChainStore) IsTxHashDuplicate(txhash Uint256) bool {
prefix := []byte{byte(DATA_Transaction)}
_, err_get := bd.Get(append(prefix, txhash.ToArray()...))
if err_get != nil {
return false
} else {
return true
}
}
func (bd *ChainStore) IsDoubleSpend(txn *tx.Transaction) bool {
if len(txn.UTXOInputs) == 0 {
return false
}
unspentPrefix := []byte{byte(IX_Unspent)}
for i := 0; i < len(txn.UTXOInputs); i++ {
txhash := txn.UTXOInputs[i].ReferTxID
unspentValue, err_get := bd.Get(append(unspentPrefix, txhash.ToArray()...))
if err_get != nil {
return true
}
unspents, _ := GetUint16Array(unspentValue)
findFlag := false
for k := 0; k < len(unspents); k++ {
if unspents[k] == txn.UTXOInputs[i].ReferTxOutputIndex {
findFlag = true
break
}
}
if !findFlag {
return true
}
}
return false
}
func (bd *ChainStore) GetBlockHash(height uint32) (Uint256, error) {
queryKey := bytes.NewBuffer(nil)
queryKey.WriteByte(byte(DATA_BlockHash))
err := serialization.WriteUint32(queryKey, height)
if err != nil {
return Uint256{}, err
}
blockHash, err := bd.Get(queryKey.Bytes())
if err != nil {
//TODO: implement error process
return Uint256{}, err
}
blockHash256, err := Uint256ParseFromBytes(blockHash)
if err != nil {
return Uint256{}, err
}
return blockHash256, nil
}
func (bd *ChainStore) getHeaderWithCache(hash Uint256) *Header {
for e := bd.headerIdx.Front(); e != nil; e = e.Next() {
n := e.Value.(Header)
eh := n.Blockdata.Hash()
if eh.CompareTo(hash) == 0 {
return &n
}
}
h, _ := bd.GetHeader(hash)
return h
}
func (bd *ChainStore) GetCurrentBlockHash() Uint256 {
hash, err := bd.GetBlockHash(bd.currentBlockHeight)
if err != nil {
return Uint256{}
}
return hash
}
func (db *ChainStore) RollbackBlock(blockHash Uint256) error {
reply := make(chan bool)
db.taskCh <- &rollbackBlockTask{blockHash: blockHash, reply: reply}
<-reply
return nil
}
func (bd *ChainStore) GetHeader(hash Uint256) (*Header, error) {
var h *Header = new(Header)
h.Blockdata = new(Blockdata)
h.Blockdata.Program = new(program.Program)
prefix := []byte{byte(DATA_Header)}
data, err_get := bd.Get(append(prefix, hash.ToArray()...))
//log.Debug( "Get Header Data: %x\n", data )
if err_get != nil {
//TODO: implement error process
return nil, err_get
}
r := bytes.NewReader(data)
// first 8 bytes is sys_fee
sysfee, err := serialization.ReadUint64(r)
if err != nil {
return nil, err
}
_ = sysfee
// Deserialize block data
err = h.Deserialize(r)
if err != nil {
return nil, err
}
return h, err
}
func (bd *ChainStore) PersistAsset(assetId Uint256, asset *Asset) error {
w := bytes.NewBuffer(nil)
asset.Serialize(w)
// generate key
assetKey := bytes.NewBuffer(nil)
// add asset prefix.
assetKey.WriteByte(byte(ST_Info))
// contact asset id
assetId.Serialize(assetKey)
log.Debug(fmt.Sprintf("asset key: %x\n", assetKey))
// PUT VALUE
err := bd.BatchPut(assetKey.Bytes(), w.Bytes())
if err != nil {
return err
}
return nil
}
func (bd *ChainStore) GetAsset(hash Uint256) (*Asset, error) {
log.Debug(fmt.Sprintf("GetAsset Hash: %x\n", hash))
asset := new(Asset)
prefix := []byte{byte(ST_Info)}
data, err_get := bd.Get(append(prefix, hash.ToArray()...))
log.Debug(fmt.Sprintf("GetAsset Data: %x\n", data))
if err_get != nil {
//TODO: implement error process
return nil, err_get
}
r := bytes.NewReader(data)
asset.Deserialize(r)
return asset, nil
}
func (bd *ChainStore) GetTransaction(hash Uint256) (*tx.Transaction, uint32, error) {
key := append([]byte{byte(DATA_Transaction)}, hash.ToArray()...)
value, err := bd.Get(key)
if err != nil {
return nil, 0, err
}
r := bytes.NewReader(value)
height, err := serialization.ReadUint32(r)
if err != nil {
return nil, 0, err
}
var txn tx.Transaction
if err := txn.Deserialize(r); err != nil {
return nil, height, err
}
return &txn, height, nil
}
func (bd *ChainStore) PersistTransaction(tx *tx.Transaction, height uint32) error {
//////////////////////////////////////////////////////////////
// generate key with DATA_Transaction prefix
txhash := bytes.NewBuffer(nil)
// add transaction header prefix.
txhash.WriteByte(byte(DATA_Transaction))
// get transaction hash
txHashValue := tx.Hash()
txHashValue.Serialize(txhash)
log.Debug(fmt.Sprintf("transaction header + hash: %x\n", txhash))
// generate value
w := bytes.NewBuffer(nil)
serialization.WriteUint32(w, height)
tx.Serialize(w)
log.Debug(fmt.Sprintf("transaction tx data: %x\n", w))
// put value
err := bd.BatchPut(txhash.Bytes(), w.Bytes())
if err != nil {
return err
}
return nil
}
func (bd *ChainStore) GetBlock(hash Uint256) (*Block, error) {
var b *Block = new(Block)
b.Blockdata = new(Blockdata)
b.Blockdata.Program = new(program.Program)
prefix := []byte{byte(DATA_Header)}
bHash, err := bd.Get(append(prefix, hash.ToArray()...))
if err != nil {
return nil, err
}
r := bytes.NewReader(bHash)
// first 8 bytes is sys_fee
_, err = serialization.ReadUint64(r)
if err != nil {
return nil, err
}
// Deserialize block data
if err := b.FromTrimmedData(r); err != nil {
return nil, err
}
// Deserialize transaction
for i, txn := range b.Transactions {
tmp, _, err := bd.GetTransaction(txn.Hash())
if err != nil {
return nil, err
}
b.Transactions[i] = tmp
}
return b, nil
}
func (db *ChainStore) rollback(b *Block) error {
db.BatchInit()
db.RollbackTrimemedBlock(b)
db.RollbackBlockHash(b)
db.RollbackTransactions(b)
db.RollbackUnspendUTXOs(b)
db.RollbackUnspend(b)
db.RollbackCurrentBlock(b)
db.BatchFinish()
db.ledger.Blockchain.UpdateBestHeight(b.Blockdata.Height - 1)
db.mu.Lock()
db.currentBlockHeight = b.Blockdata.Height - 1
db.mu.Unlock()
db.ledger.Blockchain.BCEvents.Notify(events.EventRollbackTransaction, b)
return nil
}
func (db *ChainStore) persist(b *Block) error {
//unspents := make(map[Uint256][]uint16)
db.BatchInit()
db.PersistTrimmedBlock(b)
db.PersistBlockHash(b)
db.PersistTransactions(b)
db.PersistUnspendUTXOs(b)
db.PersistUnspend(b)
db.PersistCurrentBlock(b)
db.BatchFinish()
return nil
}
// can only be invoked by backend write goroutine
func (bd *ChainStore) addHeader(header *Header) {
log.Debugf("addHeader(), Height=%d\n", header.Blockdata.Height)
hash := header.Blockdata.Hash()
bd.mu.Lock()
bd.headerCache[header.Blockdata.Hash()] = header
bd.headerIndex[header.Blockdata.Height] = hash
bd.headerIdx.PushBack(*header)
bd.mu.Unlock()
log.Debug("[addHeader]: finish, header height:", header.Blockdata.Height)
}
func (self *ChainStore) SaveBlock(b *Block, ledger *Ledger) error {
log.Debug("SaveBlock()")
//log.Trace("validation.PowVerifyBlock(b, ledger, false)")
//err := validation.PowVerifyBlock(b, ledger, false)
//if err != nil {
// log.Error("PowVerifyBlock error!")
// return err
//}
//log.Trace("validation.PowVerifyBlock(b, ledger, false)222222")
reply := make(chan bool)
self.taskCh <- &persistBlockTask{block: b, ledger: ledger, reply: reply}
<-reply
return nil
}
func (db *ChainStore) handleRollbackBlockTask(blockHash Uint256) {
block, err := db.GetBlock(blockHash)
if err != nil {
log.Errorf("block %x can't be found", BytesToHexString(blockHash.ToArray()))
return
}
db.rollback(block)
}
func (self *ChainStore) handlePersistBlockTask(b *Block, ledger *Ledger) {
if b.Blockdata.Height <= self.currentBlockHeight {
return
}
// self.mu.Lock()
//self.blockCache[b.Hash()] = b
//self.mu.Unlock()
//log.Trace(b.Blockdata.Height)
//log.Trace(b.Blockdata)
//log.Trace(b.Transactions[0])
//if b.Blockdata.Height < uint32(len(self.headerIndex)) {
self.persistBlocks(b, ledger)
//self.NewBatch()
//storedHeaderCount := self.storedHeaderCount
//for self.currentBlockHeight-storedHeaderCount >= HeaderHashListCount {
// hashBuffer := new(bytes.Buffer)
// serialization.WriteVarUint(hashBuffer, uint64(HeaderHashListCount))
// var hashArray []byte
// for i := 0; i < HeaderHashListCount; i++ {
// index := storedHeaderCount + uint32(i)
// thash := self.headerIndex[index]
// thehash := thash.ToArray()
// hashArray = append(hashArray, thehash...)
// }
// hashBuffer.Write(hashArray)
// hhlPrefix := bytes.NewBuffer(nil)
// hhlPrefix.WriteByte(byte(IX_HeaderHashList))
// serialization.WriteUint32(hhlPrefix, storedHeaderCount)
// self.BatchPut(hhlPrefix.Bytes(), hashBuffer.Bytes())
// storedHeaderCount += HeaderHashListCount
//}
//err := self.BatchCommit()
//if err != nil {
// log.Error("failed to persist header hash list:", err)
// return
//}
//self.mu.Lock()
//self.storedHeaderCount = storedHeaderCount
//self.mu.Unlock()
self.clearCache(b)
//}
}
func (bd *ChainStore) persistBlocks(block *Block, ledger *Ledger) {
//stopHeight := uint32(len(bd.headerIndex))
//for h := bd.currentBlockHeight + 1; h <= stopHeight; h++ {
//hash := bd.headerIndex[h]
//block, ok := bd.blockCache[hash]
//if !ok {
// break
//}
//log.Trace(block.Blockdata)
//log.Trace(block.Transactions[0])
err := bd.persist(block)
if err != nil {
log.Fatal("[persistBlocks]: error to persist block:", err.Error())
return
}
// PersistCompleted event
//ledger.Blockchain.BlockHeight = block.Blockdata.Height
ledger.Blockchain.UpdateBestHeight(block.Blockdata.Height)
bd.mu.Lock()
bd.currentBlockHeight = block.Blockdata.Height
bd.mu.Unlock()
ledger.Blockchain.BCEvents.Notify(events.EventBlockPersistCompleted, block)
//log.Tracef("The latest block height:%d, block hash: %x", block.Blockdata.Height, hash)
//}
}
func (bd *ChainStore) BlockInCache(hash Uint256) bool {
//TODO mutex
//_, ok := bd.ledger.Blockchain.Index[hash]
//return ok
return false
}
func (bd *ChainStore) GetUnspent(txid Uint256, index uint16) (*tx.TxOutput, error) {
if ok, _ := bd.ContainsUnspent(txid, index); ok {
Tx, _, err := bd.GetTransaction(txid)
if err != nil {
return nil, err
}
return Tx.Outputs[index], nil
}
return nil, errors.New("[GetUnspent] NOT ContainsUnspent.")
}
func (bd *ChainStore) ContainsUnspent(txid Uint256, index uint16) (bool, error) {
unspentPrefix := []byte{byte(IX_Unspent)}
unspentValue, err_get := bd.Get(append(unspentPrefix, txid.ToArray()...))
if err_get != nil {
return false, err_get
}
unspentArray, err_get := GetUint16Array(unspentValue)
if err_get != nil {
return false, err_get
}
for i := 0; i < len(unspentArray); i++ {
if unspentArray[i] == index {
return true, nil
}
}
return false, nil
}
func (bd *ChainStore) RemoveHeaderListElement(hash Uint256) {
for e := bd.headerIdx.Front(); e != nil; e = e.Next() {
n := e.Value.(Header)
h := n.Blockdata.Hash()
if h.CompareTo(hash) == 0 {
bd.headerIdx.Remove(e)
}
}
}
func (bd *ChainStore) GetHeight() uint32 {
bd.mu.RLock()
defer bd.mu.RUnlock()
return bd.currentBlockHeight
}
func (bd *ChainStore) IsBlockInStore(hash Uint256) bool {
var b *Block = new(Block)
b.Blockdata = new(Blockdata)
prefix := []byte{byte(DATA_Header)}
blockData, err_get := bd.Get(append(prefix, hash.ToArray()...))
if err_get != nil {
return false
}
r := bytes.NewReader(blockData)
// first 8 bytes is sys_fee
_, err := serialization.ReadUint64(r)
if err != nil {
return false
}
// Deserialize block data
err = b.FromTrimmedData(r)
if err != nil {
return false
}
if b.Blockdata.Height > bd.currentBlockHeight {
return false
}
return true
}
func (bd *ChainStore) GetUnspentElementFromProgramHash(programHash Uint168, assetid Uint256, height uint32) ([]*tx.UTXOUnspent, error) {
prefix := []byte{byte(IX_Unspent_UTXO)}
prefix = append(prefix, programHash.ToArray()...)
prefix = append(prefix, assetid.ToArray()...)
key := bytes.NewBuffer(prefix)
if err := serialization.WriteUint32(key, height); err != nil {
return nil, err
}
unspentsData, err := bd.Get(key.Bytes())
if err != nil {
return nil, err
}
r := bytes.NewReader(unspentsData)
listNum, err := serialization.ReadVarUint(r, 0)
if err != nil {
return nil, err
}
// read unspent list in store
unspents := make([]*tx.UTXOUnspent, listNum)
for i := 0; i < int(listNum); i++ {
uu := new(tx.UTXOUnspent)
err := uu.Deserialize(r)
if err != nil {
return nil, err
}
unspents[i] = uu
}
return unspents, nil
}
func (bd *ChainStore) GetUnspentFromProgramHash(programHash Uint168, assetid Uint256) ([]*tx.UTXOUnspent, error) {
unspents := make([]*tx.UTXOUnspent, 0)
key := []byte{byte(IX_Unspent_UTXO)}
key = append(key, programHash.ToArray()...)
key = append(key, assetid.ToArray()...)
iter := bd.NewIterator(key)
for iter.Next() {
r := bytes.NewReader(iter.Value())
listNum, err := serialization.ReadVarUint(r, 0)
if err != nil {
return nil, err
}
for i := 0; i < int(listNum); i++ {
uu := new(tx.UTXOUnspent)
err := uu.Deserialize(r)
if err != nil {
return nil, err
}
unspents = append(unspents, uu)
}
}
return unspents, nil
}
func (bd *ChainStore) GetUnspentsFromProgramHash(programHash Uint168) (map[Uint256][]*tx.UTXOUnspent, error) {
uxtoUnspents := make(map[Uint256][]*tx.UTXOUnspent)
prefix := []byte{byte(IX_Unspent_UTXO)}
key := append(prefix, programHash.ToArray()...)
iter := bd.NewIterator(key)
for iter.Next() {
rk := bytes.NewReader(iter.Key())
// read prefix
_, _ = serialization.ReadBytes(rk, 1)
var ph Uint168
ph.Deserialize(rk)
var assetid Uint256
assetid.Deserialize(rk)
r := bytes.NewReader(iter.Value())
listNum, err := serialization.ReadVarUint(r, 0)
if err != nil {
return nil, err
}
// read unspent list in store
unspents := make([]*tx.UTXOUnspent, listNum)
for i := 0; i < int(listNum); i++ {
uu := new(tx.UTXOUnspent)
err := uu.Deserialize(r)
if err != nil {
return nil, err
}
unspents[i] = uu
}
uxtoUnspents[assetid] = append(uxtoUnspents[assetid], unspents[:]...)
}
return uxtoUnspents, nil
}
func (bd *ChainStore) PersistUnspentWithProgramHash(programHash Uint168, assetid Uint256, height uint32, unspents []*tx.UTXOUnspent) error {
prefix := []byte{byte(IX_Unspent_UTXO)}
prefix = append(prefix, programHash.ToArray()...)
prefix = append(prefix, assetid.ToArray()...)
key := bytes.NewBuffer(prefix)
if err := serialization.WriteUint32(key, height); err != nil {
return err
}
if len(unspents) == 0 {
bd.BatchDelete(key.Bytes())
return nil
}
listnum := len(unspents)
w := bytes.NewBuffer(nil)
serialization.WriteVarUint(w, uint64(listnum))
for i := 0; i < listnum; i++ {
unspents[i].Serialize(w)
}
// BATCH PUT VALUE
if err := bd.BatchPut(key.Bytes(), w.Bytes()); err != nil {
return err
}
return nil
}
func (bd *ChainStore) GetAssets() map[Uint256]*Asset {
assets := make(map[Uint256]*Asset)
iter := bd.NewIterator([]byte{byte(ST_Info)})
for iter.Next() {
rk := bytes.NewReader(iter.Key())
// read prefix
_, _ = serialization.ReadBytes(rk, 1)
var assetid Uint256
assetid.Deserialize(rk)
log.Tracef("[GetAssets] assetid: %x\n", assetid.ToArray())
asset := new(Asset)
r := bytes.NewReader(iter.Value())
asset.Deserialize(r)
assets[assetid] = asset
}
return assets
}