forked from dreamerjackson/BuildingBlockChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_iterator.go
69 lines (56 loc) · 1.68 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
package main
import (
"github.com/boltdb/bolt"
"log"
"fmt"
"strconv"
)
// BlockchainIterator is used to iterate over blockchain blocks
//迭代器,用于循环区块
type BlockchainIterator struct {
currentHash []byte
db *bolt.DB
}
// 迭代器,
func (bc *Blockchain) Iterator() *BlockchainIterator {
bci := &BlockchainIterator{bc.tip, bc.db}
return bci
}
// Next returns next block starting from the tip
//反序列化并获取下一个区块的hash
func (i *BlockchainIterator) Next() *Block {
var block *Block
err := i.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
encodedBlock := b.Get(i.currentHash)
block = DeserializeBlock(encodedBlock)
//pow := NewProofOfWork(block)
//fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
return nil
})
if err != nil {
log.Panic(err)
}
i.currentHash = block.PrevBlockHash
return block
}
//循环打印出区块链中的所有数据并进行了验证,一旦成功就说明,不管是序列化反序列化,都是成功的。
func (bc *Blockchain) printChain() {
bci := bc.Iterator()
for {
block := bci.Next()
fmt.Printf("Prev. version: %s\n", strconv.FormatInt(block.Version,10))
fmt.Printf("Prev. hash: %x\n",block.PrevBlockHash)
fmt.Printf("merkleroot: %s\n", block.MerkleRoot)
fmt.Printf("time: %s\n", strconv.FormatInt(block.Timestamp,10))
fmt.Printf("nbits: %s\n", strconv.FormatInt(block.Nbits,10))
fmt.Printf("nonce: %s\n", strconv.FormatInt(block.Nonce,10))
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
fmt.Println()
if len(block.PrevBlockHash) == 0 {
break
}
}
}