|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package multichannel |
| 8 | + |
| 9 | +import ( |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/hyperledger/fabric/common/configtx" |
| 13 | + configtxapi "github.com/hyperledger/fabric/common/configtx/api" |
| 14 | + "github.com/hyperledger/fabric/common/crypto" |
| 15 | + "github.com/hyperledger/fabric/common/util" |
| 16 | + "github.com/hyperledger/fabric/orderer/common/ledger" |
| 17 | + cb "github.com/hyperledger/fabric/protos/common" |
| 18 | + "github.com/hyperledger/fabric/protos/utils" |
| 19 | + |
| 20 | + "github.com/golang/protobuf/proto" |
| 21 | +) |
| 22 | + |
| 23 | +type blockWriterSupport interface { |
| 24 | + crypto.LocalSigner |
| 25 | + ledger.ReadWriter |
| 26 | + configtxapi.Manager |
| 27 | +} |
| 28 | + |
| 29 | +// BlockWriter efficiently writes the blockchain to disk. |
| 30 | +// To safely use BlockWriter, only one thread should interact with it. |
| 31 | +// BlockWriter will spawn additional committing go routines and handle locking |
| 32 | +// so that these other go routines safely interact with the calling one. |
| 33 | +type BlockWriter struct { |
| 34 | + support blockWriterSupport |
| 35 | + registrar *Registrar |
| 36 | + lastConfigBlockNum uint64 |
| 37 | + lastConfigSeq uint64 |
| 38 | + lastBlock *cb.Block |
| 39 | + committingBlock sync.Mutex |
| 40 | +} |
| 41 | + |
| 42 | +func newBlockWriter(lastBlock *cb.Block, r *Registrar, support blockWriterSupport) *BlockWriter { |
| 43 | + bw := &BlockWriter{ |
| 44 | + support: support, |
| 45 | + lastConfigSeq: support.Sequence(), |
| 46 | + lastBlock: lastBlock, |
| 47 | + registrar: r, |
| 48 | + } |
| 49 | + |
| 50 | + // If this is the genesis block, the lastconfig field may be empty, and, the last config block is necessarily block 0 |
| 51 | + // so no need to initialize lastConfig |
| 52 | + if lastBlock.Header.Number != 0 { |
| 53 | + var err error |
| 54 | + bw.lastConfigBlockNum, err = utils.GetLastConfigIndexFromBlock(lastBlock) |
| 55 | + if err != nil { |
| 56 | + logger.Panicf("[channel: %s] Error extracting last config block from block metadata: %s", support.ChainID(), err) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + logger.Debugf("[channel: %s] Creating block writer for tip of chain (blockNumber=%d, lastConfigBlockNum=%d, lastConfigSeq=%d)", support.ChainID(), lastBlock.Header.Number, bw.lastConfigBlockNum, bw.lastConfigSeq) |
| 61 | + return bw |
| 62 | +} |
| 63 | + |
| 64 | +// CreateNextBlock creates a new block with the next block number, and the given contents. |
| 65 | +func (bw *BlockWriter) CreateNextBlock(messages []*cb.Envelope) *cb.Block { |
| 66 | + previousBlockHash := bw.lastBlock.Header.Hash() |
| 67 | + |
| 68 | + data := &cb.BlockData{ |
| 69 | + Data: make([][]byte, len(messages)), |
| 70 | + } |
| 71 | + |
| 72 | + var err error |
| 73 | + for i, msg := range messages { |
| 74 | + data.Data[i], err = proto.Marshal(msg) |
| 75 | + if err != nil { |
| 76 | + logger.Panicf("Could not marshal envelope: %s", err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + block := cb.NewBlock(bw.lastBlock.Header.Number+1, previousBlockHash) |
| 81 | + block.Header.DataHash = data.Hash() |
| 82 | + block.Data = data |
| 83 | + |
| 84 | + return block |
| 85 | +} |
| 86 | + |
| 87 | +// WriteConfigBlock should be invoked for blocks which contain a config transaction. |
| 88 | +// This call will block until the new config has taken effect, then will return |
| 89 | +// while the block is written asynchronously to disk. |
| 90 | +func (bw *BlockWriter) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) { |
| 91 | + ctx, err := utils.ExtractEnvelope(block, 0) |
| 92 | + if err != nil { |
| 93 | + logger.Panicf("Told to write a config block, but could not get configtx: %s", err) |
| 94 | + } |
| 95 | + |
| 96 | + payload, err := utils.UnmarshalPayload(ctx.Payload) |
| 97 | + if err != nil { |
| 98 | + logger.Panicf("Told to write a config block, but configtx payload is invalid: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + if payload.Header == nil { |
| 102 | + logger.Panicf("Told to write a config block, but configtx payload header is missing") |
| 103 | + } |
| 104 | + |
| 105 | + chdr, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader) |
| 106 | + if err != nil { |
| 107 | + logger.Panicf("Told to write a config block with an invalid channel header: %s", err) |
| 108 | + } |
| 109 | + |
| 110 | + switch chdr.Type { |
| 111 | + case int32(cb.HeaderType_ORDERER_TRANSACTION): |
| 112 | + newChannelConfig, err := utils.UnmarshalEnvelope(payload.Data) |
| 113 | + if err != nil { |
| 114 | + logger.Panicf("Told to write a config block with new channel, but did not have config update embedded: %s", err) |
| 115 | + } |
| 116 | + bw.registrar.newChain(newChannelConfig) |
| 117 | + case int32(cb.HeaderType_CONFIG): |
| 118 | + configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data) |
| 119 | + if err != nil { |
| 120 | + logger.Panicf("Told to write a config block with new channel, but did not have config envelope encoded: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + err = bw.support.Apply(configEnvelope) |
| 124 | + if err != nil { |
| 125 | + logger.Panicf("Told to write a config block with new config, but could not apply it: %s", err) |
| 126 | + } |
| 127 | + default: |
| 128 | + logger.Panicf("Told to write a config block with unknown header type: %v", chdr.Type) |
| 129 | + } |
| 130 | + |
| 131 | + bw.WriteBlock(block, encodedMetadataValue) |
| 132 | +} |
| 133 | + |
| 134 | +// WriteBlock should be invoked for blocks which contain normal transactions. |
| 135 | +// It sets the target block as the pending next block, and returns before it is committed. |
| 136 | +// Before returning, it acquires the committing lock, and spawns a go routine which will |
| 137 | +// annotate the block with metadata and signatures, and write the block to the ledger |
| 138 | +// then release the lock. This allows the calling thread to begin assembling the next block |
| 139 | +// before the commit phase is complete. |
| 140 | +func (bw *BlockWriter) WriteBlock(block *cb.Block, encodedMetadataValue []byte) { |
| 141 | + bw.committingBlock.Lock() |
| 142 | + bw.lastBlock = block |
| 143 | + |
| 144 | + go func() { |
| 145 | + defer bw.committingBlock.Unlock() |
| 146 | + bw.commitBlock(encodedMetadataValue) |
| 147 | + }() |
| 148 | +} |
| 149 | + |
| 150 | +// commitBlock should only ever be invoked with the bw.committingBlock held |
| 151 | +// this ensures that the encoded config sequence numbers stay in sync |
| 152 | +func (bw *BlockWriter) commitBlock(encodedMetadataValue []byte) { |
| 153 | + // Set the orderer-related metadata field |
| 154 | + if encodedMetadataValue != nil { |
| 155 | + bw.lastBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue}) |
| 156 | + } |
| 157 | + bw.addBlockSignature(bw.lastBlock) |
| 158 | + bw.addLastConfigSignature(bw.lastBlock) |
| 159 | + |
| 160 | + err := bw.support.Append(bw.lastBlock) |
| 161 | + if err != nil { |
| 162 | + logger.Panicf("[channel: %s] Could not append block: %s", bw.support.ChainID(), err) |
| 163 | + } |
| 164 | + logger.Debugf("[channel: %s] Wrote block %d", bw.support.ChainID(), bw.lastBlock.GetHeader().Number) |
| 165 | +} |
| 166 | + |
| 167 | +func (bw *BlockWriter) addBlockSignature(block *cb.Block) { |
| 168 | + blockSignature := &cb.MetadataSignature{ |
| 169 | + SignatureHeader: utils.MarshalOrPanic(utils.NewSignatureHeaderOrPanic(bw.support)), |
| 170 | + } |
| 171 | + |
| 172 | + // Note, this value is intentionally nil, as this metadata is only about the signature, there is no additional metadata |
| 173 | + // information required beyond the fact that the metadata item is signed. |
| 174 | + blockSignatureValue := []byte(nil) |
| 175 | + |
| 176 | + blockSignature.Signature = utils.SignOrPanic(bw.support, util.ConcatenateBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes())) |
| 177 | + |
| 178 | + block.Metadata.Metadata[cb.BlockMetadataIndex_SIGNATURES] = utils.MarshalOrPanic(&cb.Metadata{ |
| 179 | + Value: blockSignatureValue, |
| 180 | + Signatures: []*cb.MetadataSignature{ |
| 181 | + blockSignature, |
| 182 | + }, |
| 183 | + }) |
| 184 | +} |
| 185 | + |
| 186 | +func (bw *BlockWriter) addLastConfigSignature(block *cb.Block) { |
| 187 | + configSeq := bw.support.Sequence() |
| 188 | + if configSeq > bw.lastConfigSeq { |
| 189 | + logger.Debugf("[channel: %s] Detected lastConfigSeq transitioning from %d to %d, setting lastConfigBlockNum from %d to %d", bw.support.ChainID(), bw.lastConfigSeq, configSeq, bw.lastConfigBlockNum, block.Header.Number) |
| 190 | + bw.lastConfigBlockNum = block.Header.Number |
| 191 | + bw.lastConfigSeq = configSeq |
| 192 | + } |
| 193 | + |
| 194 | + lastConfigSignature := &cb.MetadataSignature{ |
| 195 | + SignatureHeader: utils.MarshalOrPanic(utils.NewSignatureHeaderOrPanic(bw.support)), |
| 196 | + } |
| 197 | + |
| 198 | + lastConfigValue := utils.MarshalOrPanic(&cb.LastConfig{Index: bw.lastConfigBlockNum}) |
| 199 | + logger.Debugf("[channel: %s] About to write block, setting its LAST_CONFIG to %d", bw.support.ChainID(), bw.lastConfigBlockNum) |
| 200 | + |
| 201 | + lastConfigSignature.Signature = utils.SignOrPanic(bw.support, util.ConcatenateBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes())) |
| 202 | + |
| 203 | + block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&cb.Metadata{ |
| 204 | + Value: lastConfigValue, |
| 205 | + Signatures: []*cb.MetadataSignature{ |
| 206 | + lastConfigSignature, |
| 207 | + }, |
| 208 | + }) |
| 209 | +} |
0 commit comments