-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockwriter.go
228 lines (170 loc) · 6.86 KB
/
blockwriter.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
/*
Copyright IBM Corp. 2017 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package multichannel
import (
"sync"
"github.com/golang/protobuf/proto"
cb "github.com/mcc-github/blockchain-protos-go/common"
newchannelconfig "github.com/mcc-github/blockchain/common/channelconfig"
"github.com/mcc-github/blockchain/common/configtx"
"github.com/mcc-github/blockchain/common/ledger/blockledger"
"github.com/mcc-github/blockchain/common/util"
"github.com/mcc-github/blockchain/internal/pkg/identity"
"github.com/mcc-github/blockchain/protoutil"
)
type blockWriterSupport interface {
identity.SignerSerializer
blockledger.ReadWriter
configtx.Validator
Update(*newchannelconfig.Bundle)
CreateBundle(channelID string, config *cb.Config) (*newchannelconfig.Bundle, error)
SharedConfig() newchannelconfig.Orderer
}
type BlockWriter struct {
support blockWriterSupport
registrar *Registrar
lastConfigBlockNum uint64
lastConfigSeq uint64
lastBlock *cb.Block
committingBlock sync.Mutex
}
func newBlockWriter(lastBlock *cb.Block, r *Registrar, support blockWriterSupport) *BlockWriter {
bw := &BlockWriter{
support: support,
lastConfigSeq: support.Sequence(),
lastBlock: lastBlock,
registrar: r,
}
if lastBlock.Header.Number != 0 {
var err error
bw.lastConfigBlockNum, err = protoutil.GetLastConfigIndexFromBlock(lastBlock)
if err != nil {
logger.Panicf("[channel: %s] Error extracting last config block from block metadata: %s", support.ChannelID(), err)
}
}
logger.Debugf("[channel: %s] Creating block writer for tip of chain (blockNumber=%d, lastConfigBlockNum=%d, lastConfigSeq=%d)", support.ChannelID(), lastBlock.Header.Number, bw.lastConfigBlockNum, bw.lastConfigSeq)
return bw
}
func (bw *BlockWriter) CreateNextBlock(messages []*cb.Envelope) *cb.Block {
previousBlockHash := protoutil.BlockHeaderHash(bw.lastBlock.Header)
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 {
logger.Panicf("Could not marshal envelope: %s", err)
}
}
block := protoutil.NewBlock(bw.lastBlock.Header.Number+1, previousBlockHash)
block.Header.DataHash = protoutil.BlockDataHash(data)
block.Data = data
return block
}
func (bw *BlockWriter) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) {
ctx, err := protoutil.ExtractEnvelope(block, 0)
if err != nil {
logger.Panicf("Told to write a config block, but could not get configtx: %s", err)
}
payload, err := protoutil.UnmarshalPayload(ctx.Payload)
if err != nil {
logger.Panicf("Told to write a config block, but configtx payload is invalid: %s", err)
}
if payload.Header == nil {
logger.Panicf("Told to write a config block, but configtx payload header is missing")
}
chdr, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
logger.Panicf("Told to write a config block with an invalid channel header: %s", err)
}
switch chdr.Type {
case int32(cb.HeaderType_ORDERER_TRANSACTION):
newChannelConfig, err := protoutil.UnmarshalEnvelope(payload.Data)
if err != nil {
logger.Panicf("Told to write a config block with new channel, but did not have config update embedded: %s", err)
}
bw.registrar.newChain(newChannelConfig)
case int32(cb.HeaderType_CONFIG):
configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data)
if err != nil {
logger.Panicf("Told to write a config block with new channel, but did not have config envelope encoded: %s", err)
}
err = bw.support.Validate(configEnvelope)
if err != nil {
logger.Panicf("Told to write a config block with new config, but could not apply it: %s", err)
}
bundle, err := bw.support.CreateBundle(chdr.ChannelId, configEnvelope.Config)
if err != nil {
logger.Panicf("Told to write a config block with a new config, but could not convert it to a bundle: %s", err)
}
oc, ok := bundle.OrdererConfig()
if !ok {
logger.Panicf("[channel: %s] OrdererConfig missing from bundle", bw.support.ChannelID())
}
currentType := bw.support.SharedConfig().ConsensusType()
nextType := oc.ConsensusType()
if currentType != nextType {
encodedMetadataValue = nil
logger.Debugf("[channel: %s] Consensus-type migration: maintenance mode, change from %s to %s, setting metadata to nil",
bw.support.ChannelID(), currentType, nextType)
}
bw.committingBlock.Lock()
bw.committingBlock.Unlock()
bw.support.Update(bundle)
default:
logger.Panicf("Told to write a config block with unknown header type: %v", chdr.Type)
}
bw.WriteBlock(block, encodedMetadataValue)
}
func (bw *BlockWriter) WriteBlock(block *cb.Block, encodedMetadataValue []byte) {
bw.committingBlock.Lock()
bw.lastBlock = block
go func() {
defer bw.committingBlock.Unlock()
bw.commitBlock(encodedMetadataValue)
}()
}
func (bw *BlockWriter) commitBlock(encodedMetadataValue []byte) {
bw.addLastConfig(bw.lastBlock)
bw.addBlockSignature(bw.lastBlock, encodedMetadataValue)
err := bw.support.Append(bw.lastBlock)
if err != nil {
logger.Panicf("[channel: %s] Could not append block: %s", bw.support.ChannelID(), err)
}
logger.Debugf("[channel: %s] Wrote block [%d]", bw.support.ChannelID(), bw.lastBlock.GetHeader().Number)
}
func (bw *BlockWriter) addBlockSignature(block *cb.Block, consenterMetadata []byte) {
blockSignature := &cb.MetadataSignature{
SignatureHeader: protoutil.MarshalOrPanic(protoutil.NewSignatureHeaderOrPanic(bw.support)),
}
blockSignatureValue := protoutil.MarshalOrPanic(&cb.OrdererBlockMetadata{
LastConfig: &cb.LastConfig{Index: bw.lastConfigBlockNum},
ConsenterMetadata: protoutil.MarshalOrPanic(&cb.Metadata{Value: consenterMetadata}),
})
blockSignature.Signature = protoutil.SignOrPanic(
bw.support,
util.ConcatenateBytes(blockSignatureValue, blockSignature.SignatureHeader, protoutil.BlockHeaderBytes(block.Header)),
)
block.Metadata.Metadata[cb.BlockMetadataIndex_SIGNATURES] = protoutil.MarshalOrPanic(&cb.Metadata{
Value: blockSignatureValue,
Signatures: []*cb.MetadataSignature{
blockSignature,
},
})
}
func (bw *BlockWriter) addLastConfig(block *cb.Block) {
configSeq := bw.support.Sequence()
if configSeq > bw.lastConfigSeq {
logger.Debugf("[channel: %s] Detected lastConfigSeq transitioning from %d to %d, setting lastConfigBlockNum from %d to %d", bw.support.ChannelID(), bw.lastConfigSeq, configSeq, bw.lastConfigBlockNum, block.Header.Number)
bw.lastConfigBlockNum = block.Header.Number
bw.lastConfigSeq = configSeq
}
lastConfigValue := protoutil.MarshalOrPanic(&cb.LastConfig{Index: bw.lastConfigBlockNum})
logger.Debugf("[channel: %s] About to write block, setting its LAST_CONFIG to %d", bw.support.ChannelID(), bw.lastConfigBlockNum)
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = protoutil.MarshalOrPanic(&cb.Metadata{
Value: lastConfigValue,
})
}