-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.go
More file actions
43 lines (35 loc) · 806 Bytes
/
iterator.go
File metadata and controls
43 lines (35 loc) · 806 Bytes
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
package blc
import (
"fmt"
"github.com/treeforest/easyblc/dao"
"math/big"
)
//BlockIterator block iterator
type BlockIterator struct {
dao *dao.DAO
cur []byte // 当前迭代区块的哈希
}
func NewBlockIterator(d *dao.DAO) *BlockIterator {
return &BlockIterator{dao: d, cur: d.GetLatestBlockHash()}
}
func (it *BlockIterator) Next() (*Block, error) {
if it.cur == nil {
return nil, nil
}
var hash big.Int
hash.SetBytes(it.cur)
if big.NewInt(0).Cmp(&hash) == 0 {
return nil, nil
}
var block Block
data, err := it.dao.GetBlock(it.cur)
if err != nil {
return nil, fmt.Errorf("get block from database failed:%v", err)
}
err = block.Unmarshal(data)
if err != nil {
return nil, fmt.Errorf("block unmarshal failed:%v", err)
}
it.cur = block.PreHash[:]
return &block, nil
}