This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain_iterator.go
78 lines (57 loc) · 1.4 KB
/
blockchain_iterator.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
package main
import (
"log"
"github.com/boltdb/bolt"
)
// BlockchainIterator is used to iterate over blockchain blocks
type BlockchainIterator struct {
currentHash1 []byte
currentHash2 []byte
currentHash3 []byte
db *bolt.DB
}
// Next1 returns next block starting from the tip1
func (i *BlockchainIterator) Next1() *Block {
var block *Block
err := i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(transactionsBucket))
encodedBlock := b.Get(i.currentHash1)
block = DeserializeBlock(encodedBlock)
return nil
})
if err != nil {
log.Panic(err)
}
i.currentHash1 = block.PrevBlockHash
return block
}
// Next2 returns next block starting from the tip2
func (i *BlockchainIterator) Next2() *Block {
var block *Block
err := i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(productsBucket))
encodedBlock := b.Get(i.currentHash2)
block = DeserializeBlock(encodedBlock)
return nil
})
if err != nil {
log.Panic(err)
}
i.currentHash2 = block.PrevBlockHash
return block
}
// Next3 returns next block starting from the tip3
func (i *BlockchainIterator) Next3() *Block {
var block *Block
err := i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(organisationBucket))
encodedBlock := b.Get(i.currentHash3)
block = DeserializeBlock(encodedBlock)
return nil
})
if err != nil {
log.Panic(err)
}
i.currentHash3 = block.PrevBlockHash
return block
}