-
Notifications
You must be signed in to change notification settings - Fork 0
/
multichannel.go
175 lines (137 loc) · 5.79 KB
/
multichannel.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package multichannel
import (
"github.com/hyperledger/fabric/common/channelconfig"
mockconfig "github.com/hyperledger/fabric/common/mocks/config"
"github.com/hyperledger/fabric/orderer/common/blockcutter"
"github.com/hyperledger/fabric/orderer/common/msgprocessor"
mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/common/blockcutter"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
// ConsenterSupport is used to mock the multichannel.ConsenterSupport interface
// Whenever a block is written, it writes to the Batches channel to allow for synchronization
type ConsenterSupport struct {
// SharedConfigVal is the value returned by SharedConfig()
SharedConfigVal *mockconfig.Orderer
// SharedConfigVal is the value returned by ChannelConfig()
ChannelConfigVal *mockconfig.Channel
// BlockCutterVal is the value returned by BlockCutter()
BlockCutterVal *mockblockcutter.Receiver
// BlockByIndex maps block numbers to retrieved values of these blocks
BlockByIndex map[uint64]*cb.Block
// Blocks is the channel where WriteBlock writes the most recently created block,
Blocks chan *cb.Block
// ChainIDVal is the value returned by ChainID()
ChainIDVal string
// HeightVal is the value returned by Height()
HeightVal uint64
// NextBlockVal stores the block created by the most recent CreateNextBlock() call
NextBlockVal *cb.Block
// ClassifyMsgVal is returned by ClassifyMsg
ClassifyMsgVal msgprocessor.Classification
// ConfigSeqVal is returned as the configSeq for Process*Msg
ConfigSeqVal uint64
// ProcessNormalMsgErr is returned as the error for ProcessNormalMsg
ProcessNormalMsgErr error
// ProcessConfigUpdateMsgVal is returned as the error for ProcessConfigUpdateMsg
ProcessConfigUpdateMsgVal *cb.Envelope
// ProcessConfigUpdateMsgErr is returned as the error for ProcessConfigUpdateMsg
ProcessConfigUpdateMsgErr error
// ProcessConfigMsgVal is returned as the error for ProcessConfigMsg
ProcessConfigMsgVal *cb.Envelope
// ProcessConfigMsgErr is returned by ProcessConfigMsg
ProcessConfigMsgErr error
// SequenceVal is returned by Sequence
SequenceVal uint64
// BlockVerificationErr is returned by VerifyBlockSignature
BlockVerificationErr error
}
// Block returns the block with the given number or nil if not found
func (mcs *ConsenterSupport) Block(number uint64) *cb.Block {
return mcs.BlockByIndex[number]
}
// BlockCutter returns BlockCutterVal
func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver {
return mcs.BlockCutterVal
}
// SharedConfig returns SharedConfigVal
func (mcs *ConsenterSupport) SharedConfig() channelconfig.Orderer {
return mcs.SharedConfigVal
}
// ChannelConfig returns ChannelConfigVal
func (mcs *ConsenterSupport) ChannelConfig() channelconfig.Channel {
return mcs.ChannelConfigVal
}
// CreateNextBlock creates a simple block structure with the given data
func (mcs *ConsenterSupport) CreateNextBlock(data []*cb.Envelope) *cb.Block {
block := cb.NewBlock(0, nil)
mtxs := make([][]byte, len(data))
for i := range data {
mtxs[i] = utils.MarshalOrPanic(data[i])
}
block.Data = &cb.BlockData{Data: mtxs}
mcs.NextBlockVal = block
return block
}
// WriteBlock writes data to the Blocks channel
func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, encodedMetadataValue []byte) {
if encodedMetadataValue != nil {
block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue})
}
mcs.Append(block)
}
// WriteConfigBlock calls WriteBlock
func (mcs *ConsenterSupport) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) {
mcs.WriteBlock(block, encodedMetadataValue)
}
// ChainID returns the chain ID this specific consenter instance is associated with
func (mcs *ConsenterSupport) ChainID() string {
return mcs.ChainIDVal
}
// Height returns the number of blocks of the chain this specific consenter instance is associated with
func (mcs *ConsenterSupport) Height() uint64 {
return mcs.HeightVal
}
// Sign returns the bytes passed in
func (mcs *ConsenterSupport) Sign(message []byte) ([]byte, error) {
return message, nil
}
// NewSignatureHeader returns an empty signature header
func (mcs *ConsenterSupport) NewSignatureHeader() (*cb.SignatureHeader, error) {
return &cb.SignatureHeader{}, nil
}
// ClassifyMsg returns ClassifyMsgVal, ClassifyMsgErr
func (mcs *ConsenterSupport) ClassifyMsg(chdr *cb.ChannelHeader) msgprocessor.Classification {
return mcs.ClassifyMsgVal
}
// ProcessNormalMsg returns ConfigSeqVal, ProcessNormalMsgErr
func (mcs *ConsenterSupport) ProcessNormalMsg(env *cb.Envelope) (configSeq uint64, err error) {
return mcs.ConfigSeqVal, mcs.ProcessNormalMsgErr
}
// ProcessConfigUpdateMsg returns ProcessConfigUpdateMsgVal, ConfigSeqVal, ProcessConfigUpdateMsgErr
func (mcs *ConsenterSupport) ProcessConfigUpdateMsg(env *cb.Envelope) (config *cb.Envelope, configSeq uint64, err error) {
return mcs.ProcessConfigUpdateMsgVal, mcs.ConfigSeqVal, mcs.ProcessConfigUpdateMsgErr
}
// ProcessConfigMsg returns ProcessConfigMsgVal, ConfigSeqVal, ProcessConfigMsgErr
func (mcs *ConsenterSupport) ProcessConfigMsg(env *cb.Envelope) (*cb.Envelope, uint64, error) {
return mcs.ProcessConfigMsgVal, mcs.ConfigSeqVal, mcs.ProcessConfigMsgErr
}
// Sequence returns SequenceVal
func (mcs *ConsenterSupport) Sequence() uint64 {
return mcs.SequenceVal
}
// VerifyBlockSignature verifies a signature of a block
func (mcs *ConsenterSupport) VerifyBlockSignature(_ []*cb.SignedData, _ *cb.ConfigEnvelope) error {
return mcs.BlockVerificationErr
}
// Append appends a new block to the ledger in its raw form,
// unlike WriteBlock that also mutates its metadata.
func (mcs *ConsenterSupport) Append(block *cb.Block) error {
mcs.HeightVal++
mcs.Blocks <- block
return nil
}