-
Notifications
You must be signed in to change notification settings - Fork 2
/
configblock.go
213 lines (176 loc) · 6.82 KB
/
configblock.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
//<developer>
// <name>linapex 曹一峰</name>
// <email>linapex@163.com</email>
// <wx>superexc</wx>
// <qqgroup>128148617</qqgroup>
// <url>https://jsq.ink</url>
// <role>pku engineer</role>
// <date>2019-03-16 19:40:25</date>
//</624456078033162240>
/*
版权所有IBM公司保留所有权利。
SPDX许可证标识符:Apache-2.0
**/
package nwo
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/tools/configtxlator/update"
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
//getconfigblock检索通道的当前配置块,并
//解封它。
func GetConfigBlock(n *Network, peer *Peer, orderer *Orderer, channel string) *common.Config {
tempDir, err := ioutil.TempDir("", "getConfigBlock")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
//获取配置块
output := filepath.Join(tempDir, "config_block.pb")
sess, err := n.PeerAdminSession(peer, commands.ChannelFetch{
ChannelID: channel,
Block: "config",
Orderer: n.OrdererAddress(orderer, ListenPort),
OutputFile: output,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Received block: "))
//取消配置块字节的标记
configBlock := UnmarshalBlockFromFile(output)
//取消信封字节的标记
envelope, err := utils.GetEnvelopeFromBlock(configBlock.Data.Data[0])
Expect(err).NotTo(HaveOccurred())
//取消有效负载字节的标记
payload, err := utils.GetPayload(envelope)
Expect(err).NotTo(HaveOccurred())
//取消配置信封字节的标记
configEnv := &common.ConfigEnvelope{}
err = proto.Unmarshal(payload.Data, configEnv)
Expect(err).NotTo(HaveOccurred())
//克隆配置
return configEnv.Config
}
//updateConfig计算、签名并提交配置更新并等待
//以完成更新。
func UpdateConfig(n *Network, orderer *Orderer, channel string, current, updated *common.Config, submitter *Peer, additionalSigners ...*Peer) {
tempDir, err := ioutil.TempDir("", "updateConfig")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
//计算更新
configUpdate, err := update.Compute(current, updated)
Expect(err).NotTo(HaveOccurred())
configUpdate.ChannelId = channel
signedEnvelope, err := utils.CreateSignedEnvelope(
common.HeaderType_CONFIG_UPDATE,
channel,
nil, //本地签名者
&common.ConfigUpdateEnvelope{ConfigUpdate: utils.MarshalOrPanic(configUpdate)},
0, //消息版本
0, //时代
)
Expect(err).NotTo(HaveOccurred())
Expect(signedEnvelope).NotTo(BeNil())
updateFile := filepath.Join(tempDir, "update.pb")
err = ioutil.WriteFile(updateFile, utils.MarshalOrPanic(signedEnvelope), 0600)
Expect(err).NotTo(HaveOccurred())
for _, signer := range additionalSigners {
sess, err := n.PeerAdminSession(signer, commands.SignConfigTx{File: updateFile})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
}
//获取当前配置块编号
currentBlockNumber := CurrentConfigBlockNumber(n, submitter, orderer, channel)
sess, err := n.PeerAdminSession(submitter, commands.ChannelUpdate{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, ListenPort),
File: updateFile,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Successfully submitted channel update"))
//等待将该块提交给
//已加入频道
for _, peer := range n.PeersWithChannel(channel) {
ccb := func() uint64 { return CurrentConfigBlockNumber(n, peer, orderer, channel) }
Eventually(ccb, n.EventuallyTimeout).Should(BeNumerically(">", currentBlockNumber))
}
}
//updateOrdererConfig计算、签名并提交一个需要订购者签名和等待的配置更新
//以完成更新。
func UpdateOrdererConfig(n *Network, orderer *Orderer, channel string, current, updated *common.Config, submitter *Peer, additionalSigners ...*Orderer) {
tempDir, err := ioutil.TempDir("", "updateConfig")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
//计算更新
configUpdate, err := update.Compute(current, updated)
Expect(err).NotTo(HaveOccurred())
configUpdate.ChannelId = channel
signedEnvelope, err := utils.CreateSignedEnvelope(
common.HeaderType_CONFIG_UPDATE,
channel,
nil, //本地签名者
&common.ConfigUpdateEnvelope{ConfigUpdate: utils.MarshalOrPanic(configUpdate)},
0, //消息版本
0, //时代
)
Expect(err).NotTo(HaveOccurred())
Expect(signedEnvelope).NotTo(BeNil())
updateFile := filepath.Join(tempDir, "update.pb")
err = ioutil.WriteFile(updateFile, utils.MarshalOrPanic(signedEnvelope), 0600)
Expect(err).NotTo(HaveOccurred())
for _, signer := range additionalSigners {
sess, err := n.OrdererAdminSession(signer, submitter, commands.SignConfigTx{File: updateFile})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
}
//获取当前配置块编号
currentBlockNumber := CurrentConfigBlockNumber(n, submitter, orderer, channel)
sess, err := n.PeerAdminSession(submitter, commands.ChannelUpdate{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, ListenPort),
File: updateFile,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Successfully submitted channel update"))
//等待提交块
ccb := func() uint64 { return CurrentConfigBlockNumber(n, submitter, orderer, channel) }
Eventually(ccb, n.EventuallyTimeout).Should(BeNumerically(">", currentBlockNumber))
}
//CurrentConfigBlockNumber从
//当前配置块。这可用于检测Whena配置更改
//已经完成。
func CurrentConfigBlockNumber(n *Network, peer *Peer, orderer *Orderer, channel string) uint64 {
tempDir, err := ioutil.TempDir("", "currentConfigBlock")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
//获取配置块
output := filepath.Join(tempDir, "config_block.pb")
sess, err := n.PeerAdminSession(peer, commands.ChannelFetch{
ChannelID: channel,
Block: "config",
Orderer: n.OrdererAddress(orderer, ListenPort),
OutputFile: output,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Received block: "))
//取消配置块字节的标记
configBlock := UnmarshalBlockFromFile(output)
return configBlock.Header.Number
}
//unmashalblockfromfile从文件中取消对原型编码块的标记。
func UnmarshalBlockFromFile(blockFile string) *common.Block {
blockBytes, err := ioutil.ReadFile(blockFile)
Expect(err).NotTo(HaveOccurred())
block, err := utils.UnmarshalBlock(blockBytes)
Expect(err).NotTo(HaveOccurred())
return block
}