/
util.go
105 lines (87 loc) · 2.2 KB
/
util.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
//此源码被清华学神尹成大魔王专业翻译分析并修改
//尹成QQ77025077
//尹成微信18510341407
//尹成所在QQ群721929980
//尹成邮箱 yinc13@mails.tsinghua.edu.cn
//尹成毕业于清华大学,微软区块链领域全球最有价值专家
//https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
/*
版权所有IBM公司。保留所有权利。
SPDX许可证标识符:Apache-2.0
**/
package blockledger
import (
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
)
var closedChan chan struct{}
func init() {
closedChan = make(chan struct{})
close(closedChan)
}
//NotFoundErrorIterator只返回cb.status的错误“未找到”,
//
type NotFoundErrorIterator struct{}
//
func (nfei *NotFoundErrorIterator) Next() (*cb.Block, cb.Status) {
return nil, cb.Status_NOT_FOUND
}
//
func (nfei *NotFoundErrorIterator) ReadyChan() <-chan struct{} {
return closedChan
}
//
func (nfei *NotFoundErrorIterator) Close() {}
//
//
//
//适应非确定性编组
func CreateNextBlock(rl Reader, messages []*cb.Envelope) *cb.Block {
var nextBlockNumber uint64
var previousBlockHash []byte
if rl.Height() > 0 {
it, _ := rl.Iterator(&ab.SeekPosition{
Type: &ab.SeekPosition_Newest{
Newest: &ab.SeekNewest{},
},
})
block, status := it.Next()
if status != cb.Status_SUCCESS {
panic("Error seeking to newest block for chain with non-zero height")
}
nextBlockNumber = block.Header.Number + 1
previousBlockHash = block.Header.Hash()
}
data := &cb.BlockData{
Data: make([][]byte, len(messages)),
}
var err error
for i, msg := range messages {
data.Data[i], err = proto.Marshal(msg)
if err != nil {
panic(err)
}
}
block := cb.NewBlock(nextBlockNumber, previousBlockHash)
block.Header.DataHash = data.Hash()
block.Data = data
return block
}
//
func GetBlock(rl Reader, index uint64) *cb.Block {
iterator, _ := rl.Iterator(&ab.SeekPosition{
Type: &ab.SeekPosition_Specified{
Specified: &ab.SeekSpecified{Number: index},
},
})
if iterator == nil {
return nil
}
defer iterator.Close()
block, status := iterator.Next()
if status != cb.Status_SUCCESS {
return nil
}
return block
}