forked from hyperledger-archives/fabric
/
helper.go
359 lines (314 loc) · 12.6 KB
/
helper.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package helper
import (
"fmt"
"github.com/spf13/viper"
"golang.org/x/net/context"
"github.com/openblockchain/obc-peer/openchain/chaincode"
"github.com/openblockchain/obc-peer/openchain/consensus"
crypto "github.com/openblockchain/obc-peer/openchain/crypto"
"github.com/openblockchain/obc-peer/openchain/ledger"
"github.com/openblockchain/obc-peer/openchain/ledger/statemgmt"
"github.com/openblockchain/obc-peer/openchain/peer"
pb "github.com/openblockchain/obc-peer/protos"
)
// Helper contains the reference to the peer's MessageHandlerCoordinator
type Helper struct {
coordinator peer.MessageHandlerCoordinator
secOn bool
secHelper crypto.Peer
curBatch []*pb.Transaction // TODO, remove after issue 579
}
// NewHelper constructs the consensus helper object
func NewHelper(mhc peer.MessageHandlerCoordinator) consensus.Stack {
return &Helper{coordinator: mhc,
secOn: viper.GetBool("security.enabled"),
secHelper: mhc.GetSecHelper()}
}
// GetNetworkInfo returns the PeerEndpoints of the current validator and the entire validating network
func (h *Helper) GetNetworkInfo() (self *pb.PeerEndpoint, network []*pb.PeerEndpoint, err error) {
ep, err := h.coordinator.GetPeerEndpoint()
if err != nil {
return self, network, fmt.Errorf("Couldn't retrieve own endpoint: %v", err)
}
self = ep
peersMsg, err := h.coordinator.GetPeers()
if err != nil {
return self, network, fmt.Errorf("Couldn't retrieve list of peers: %v", err)
}
peers := peersMsg.GetPeers()
for _, endpoint := range peers {
if endpoint.Type == pb.PeerEndpoint_VALIDATOR {
network = append(network, endpoint)
}
}
network = append(network, self)
return
}
// GetNetworkHandles returns the PeerIDs of the current validator and the entire validating network
func (h *Helper) GetNetworkHandles() (self *pb.PeerID, network []*pb.PeerID, err error) {
selfEP, networkEP, err := h.GetNetworkInfo()
if err != nil {
return self, network, fmt.Errorf("Couldn't retrieve validating network's endpoints: %v", err)
}
self = selfEP.ID
for _, endpoint := range networkEP {
network = append(network, endpoint.ID)
}
network = append(network, self)
return
}
// Broadcast sends a message to all validating peers
func (h *Helper) Broadcast(msg *pb.OpenchainMessage, peerType pb.PeerEndpoint_Type) error {
errors := h.coordinator.Broadcast(msg, peerType)
if len(errors) > 0 {
return fmt.Errorf("Couldn't broadcast successfully")
}
return nil
}
// Unicast sends a message to a specified receiver
func (h *Helper) Unicast(msg *pb.OpenchainMessage, receiverHandle *pb.PeerID) error {
return h.coordinator.Unicast(msg, receiverHandle)
}
// Sign a message with this validator's signing key
func (h *Helper) Sign(msg []byte) ([]byte, error) {
if h.secOn {
return h.secHelper.Sign(msg)
}
logger.Debug("Security is disabled")
return msg, nil
}
// Verify that the given signature is valid under the given replicaID's verification key
// If replicaID is nil, use this validator's verification key
// If the signature is valid, the function should return nil
func (h *Helper) Verify(replicaID *pb.PeerID, signature []byte, message []byte) error {
if !h.secOn {
logger.Debug("Security is disabled")
return nil
}
logger.Debug("Verify message from: %v", replicaID.Name)
_, network, err := h.GetNetworkInfo()
if err != nil {
return fmt.Errorf("Couldn't retrieve validating network's endpoints: %v", err)
}
// check that the sender is a valid replica
// if so, call crypto verify() with that endpoint's pkiID
for _, endpoint := range network {
logger.Debug("Endpoint name: %v", endpoint.ID.Name)
if *replicaID == *endpoint.ID {
cryptoID := endpoint.PkiID
return h.secHelper.Verify(cryptoID, signature, message)
}
}
return fmt.Errorf("Could not verify message from %s (unknown peer)", replicaID.Name)
}
// BeginTxBatch gets invoked when the next round
// of transaction-batch execution begins
func (h *Helper) BeginTxBatch(id interface{}) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger: %v", err)
}
if err := ledger.BeginTxBatch(id); err != nil {
return fmt.Errorf("Failed to begin transaction with the ledger: %v", err)
}
h.curBatch = nil // TODO, remove after issue 579
return nil
}
// ExecTxs executes all the transactions listed in the txs array
// one-by-one. If all the executions are successful, it returns
// the candidate global state hash, and nil error array.
func (h *Helper) ExecTxs(id interface{}, txs []*pb.Transaction) ([]byte, error) {
// TODO id is currently ignored, fix once the underlying implementation accepts id
// The secHelper is set during creat ChaincodeSupport, so we don't need this step
// cxt := context.WithValue(context.Background(), "security", h.coordinator.GetSecHelper())
// TODO return directly once underlying implementation no longer returns []error
res, _ := chaincode.ExecuteTransactions(context.Background(), chaincode.DefaultChain, txs)
h.curBatch = append(h.curBatch, txs...) // TODO, remove after issue 579
return res, nil
}
// CommitTxBatch gets invoked when the current transaction-batch needs
// to be committed. This function returns successfully iff the
// transactions details and state changes (that may have happened
// during execution of this transaction-batch) have been committed to
// permanent storage.
func (h *Helper) CommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger: %v", err)
}
// TODO fix this one the ledger has been fixed to implement
if err := ledger.CommitTxBatch(id, h.curBatch, nil, metadata); err != nil {
return nil, fmt.Errorf("Failed to commit transaction to the ledger: %v", err)
}
size := ledger.GetBlockchainSize()
h.curBatch = nil // TODO, remove after issue 579
block, err := ledger.GetBlockByNumber(size - 1)
if err != nil {
return nil, fmt.Errorf("Failed to get the block at the head of the chain: %v", err)
}
return block, nil
}
// RollbackTxBatch discards all the state changes that may have taken
// place during the execution of current transaction-batch
func (h *Helper) RollbackTxBatch(id interface{}) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger: %v", err)
}
if err := ledger.RollbackTxBatch(id); err != nil {
return fmt.Errorf("Failed to rollback transaction with the ledger: %v", err)
}
h.curBatch = nil // TODO, remove after issue 579
return nil
}
// PreviewCommitTxBatch retrieves a preview copy of the block that would be inserted into the ledger if CommitTxBatch were invoked.
// As a preview copy, it only guarantees that the hashable portions of the block will match the committed block. Consequently,
// this preview block should only be used for hash computations and never distributed, passed into PutBlock, etc..
// The guarantee of hashable equality will be violated if additional ExecTXs calls are invoked.
func (h *Helper) PreviewCommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger: %v", err)
}
// TODO fix this once the underlying API is fixed
block, err := ledger.GetTXBatchPreviewBlock(id, h.curBatch, metadata)
if err != nil {
return nil, fmt.Errorf("Failed to commit transaction to the ledger: %v", err)
}
return block, err
}
// GetBlock returns a block from the chain
func (h *Helper) GetBlock(blockNumber uint64) (block *pb.Block, err error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetBlockByNumber(blockNumber)
}
// GetCurrentStateHash returns the current/temporary state hash
func (h *Helper) GetCurrentStateHash() (stateHash []byte, err error) {
ledger, err := ledger.GetLedger()
if err != nil {
return nil, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetTempStateHash()
}
// GetBlockchainSize returns the current size of the blockchain
func (h *Helper) GetBlockchainSize() (uint64, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return 0, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.GetBlockchainSize(), nil
}
// HashBlock returns the hash of the included block, useful for mocking
func (h *Helper) HashBlock(block *pb.Block) ([]byte, error) {
return block.GetHash()
}
// PutBlock inserts a raw block into the blockchain at the specified index, nearly no error checking is performed
func (h *Helper) PutBlock(blockNumber uint64, block *pb.Block) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.PutRawBlock(block, blockNumber)
}
// ApplyStateDelta applies a state delta to the current state
// The result of this function can be retrieved using GetCurrentStateDelta
// To commit the result, call CommitStateDelta, or to roll it back
// call RollbackStateDelta
func (h *Helper) ApplyStateDelta(id interface{}, delta *statemgmt.StateDelta) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.ApplyStateDelta(id, delta)
}
// CommitStateDelta makes the result of ApplyStateDelta permanent
// and releases the resources necessary to rollback the delta
func (h *Helper) CommitStateDelta(id interface{}) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.CommitStateDelta(id)
}
// RollbackStateDelta undoes the results of ApplyStateDelta to revert
// the current state back to the state before ApplyStateDelta was invoked
func (h *Helper) RollbackStateDelta(id interface{}) error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.RollbackStateDelta(id)
}
// EmptyState completely empties the state and prepares it to restore a snapshot
func (h *Helper) EmptyState() error {
ledger, err := ledger.GetLedger()
if err != nil {
return fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.DeleteALLStateKeysAndValues()
}
// VerifyBlockchain checks the integrity of the blockchain between indices start and finish,
// returning the first block who's PreviousBlockHash field does not match the hash of the previous block
func (h *Helper) VerifyBlockchain(start, finish uint64) (uint64, error) {
ledger, err := ledger.GetLedger()
if err != nil {
return finish, fmt.Errorf("Failed to get the ledger :%v", err)
}
return ledger.VerifyChain(start, finish)
}
func (h *Helper) getRemoteLedger(peerID *pb.PeerID) (peer.RemoteLedger, error) {
remoteLedger, err := h.coordinator.GetRemoteLedger(peerID)
if nil != err {
return nil, fmt.Errorf("Error retrieving the remote ledger for the given handle '%s' : %s", peerID, err)
}
return remoteLedger, nil
}
// GetRemoteBlocks will return a channel to stream blocks from the desired replicaID
func (h *Helper) GetRemoteBlocks(replicaID *pb.PeerID, start, finish uint64) (<-chan *pb.SyncBlocks, error) {
remoteLedger, err := h.getRemoteLedger(replicaID)
if nil != err {
return nil, err
}
return remoteLedger.RequestBlocks(&pb.SyncBlockRange{
Start: start,
End: finish,
})
}
// GetRemoteStateSnapshot will return a channel to stream a state snapshot from the desired replicaID
func (h *Helper) GetRemoteStateSnapshot(replicaID *pb.PeerID) (<-chan *pb.SyncStateSnapshot, error) {
remoteLedger, err := h.getRemoteLedger(replicaID)
if nil != err {
return nil, err
}
return remoteLedger.RequestStateSnapshot()
}
// GetRemoteStateDeltas will return a channel to stream a state snapshot deltas from the desired replicaID
func (h *Helper) GetRemoteStateDeltas(replicaID *pb.PeerID, start, finish uint64) (<-chan *pb.SyncStateDeltas, error) {
remoteLedger, err := h.getRemoteLedger(replicaID)
if nil != err {
return nil, err
}
return remoteLedger.RequestStateDeltas(&pb.SyncBlockRange{
Start: start,
End: finish,
})
}