Commit 21e02f31 authored by Yacov Manevich's avatar Yacov Manevich Committed by Gerrit Code Review
Browse files

Merge "[FAB-14697] Piggyback orderer metadata signature" into release-1.4

parents 42f0e647 1a7b3262
......@@ -167,8 +167,9 @@ func (bw *BlockWriter) commitBlock(encodedMetadataValue []byte) {
if encodedMetadataValue != nil {
bw.lastBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = utils.MarshalOrPanic(&cb.Metadata{Value: encodedMetadataValue})
}
bw.addBlockSignature(bw.lastBlock)
bw.addLastConfigSignature(bw.lastBlock)
bw.addBlockSignature(bw.lastBlock)
err := bw.support.Append(bw.lastBlock)
if err != nil {
......@@ -182,9 +183,10 @@ func (bw *BlockWriter) addBlockSignature(block *cb.Block) {
SignatureHeader: utils.MarshalOrPanic(utils.NewSignatureHeaderOrPanic(bw.support)),
}
// Note, this value is intentionally nil, as this metadata is only about the signature, there is no additional metadata
// information required beyond the fact that the metadata item is signed.
blockSignatureValue := []byte(nil)
blockSignatureValue := utils.MarshalOrPanic(&cb.OrdererBlockMetadata{
LastConfig: &cb.LastConfig{Index: bw.lastConfigBlockNum},
ConsenterMetadata: bw.lastBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER],
})
blockSignature.Signature = utils.SignOrPanic(bw.support, util.ConcatenateBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes()))
......@@ -204,19 +206,10 @@ func (bw *BlockWriter) addLastConfigSignature(block *cb.Block) {
bw.lastConfigSeq = configSeq
}
lastConfigSignature := &cb.MetadataSignature{
SignatureHeader: utils.MarshalOrPanic(utils.NewSignatureHeaderOrPanic(bw.support)),
}
lastConfigValue := utils.MarshalOrPanic(&cb.LastConfig{Index: bw.lastConfigBlockNum})
logger.Debugf("[channel: %s] About to write block, setting its LAST_CONFIG to %d", bw.support.ChainID(), bw.lastConfigBlockNum)
lastConfigSignature.Signature = utils.SignOrPanic(bw.support, util.ConcatenateBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes()))
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&cb.Metadata{
Value: lastConfigValue,
Signatures: []*cb.MetadataSignature{
lastConfigSignature,
},
})
}
......@@ -12,11 +12,13 @@ import (
newchannelconfig "github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/crypto"
"github.com/hyperledger/fabric/common/ledger/blockledger"
ramledger "github.com/hyperledger/fabric/common/ledger/blockledger/ram"
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
"github.com/hyperledger/fabric/common/tools/configtxgen/configtxgentest"
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
......@@ -50,17 +52,38 @@ func TestCreateBlock(t *testing.T) {
}
func TestBlockSignature(t *testing.T) {
rlf := ramledger.New(2)
l, err := rlf.GetOrCreate("mychannel")
assert.NoError(t, err)
lastBlock := cb.NewBlock(0, nil)
l.Append(lastBlock)
bw := &BlockWriter{
lastConfigBlockNum: 42,
support: &mockBlockWriterSupport{
LocalSigner: mockCrypto(),
Validator: &mockconfigtx.Validator{},
ReadWriter: l,
},
lastBlock: cb.NewBlock(1, lastBlock.Header.Hash()),
}
block := cb.NewBlock(7, []byte("foo"))
bw.addBlockSignature(block)
consensusMetadata := []byte("bar")
bw.commitBlock(consensusMetadata)
it, seq := l.Iterator(&orderer.SeekPosition{Type: &orderer.SeekPosition_Newest{}})
assert.Equal(t, uint64(1), seq)
committedBlock, status := it.Next()
assert.Equal(t, cb.Status_SUCCESS, status)
md := utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_SIGNATURES)
assert.Nil(t, md.Value, "Value is empty in this case")
md := utils.GetMetadataFromBlockOrPanic(committedBlock, cb.BlockMetadataIndex_SIGNATURES)
expectedMetadataValue := utils.MarshalOrPanic(&cb.OrdererBlockMetadata{
LastConfig: &cb.LastConfig{Index: 42},
ConsenterMetadata: utils.MarshalOrPanic(&cb.Metadata{Value: consensusMetadata}),
})
assert.Equal(t, expectedMetadataValue, md.Value, "Value contains the consensus metadata and the last config")
assert.NotNil(t, md.Signatures, "Should have signature")
}
......@@ -87,7 +110,7 @@ func TestBlockLastConfig(t *testing.T) {
md := utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_LAST_CONFIG)
assert.NotNil(t, md.Value, "Value not be empty in this case")
assert.NotNil(t, md.Signatures, "Should have signature")
assert.Nil(t, md.Signatures, "Should have no signatures")
lc := utils.GetLastConfigIndexFromBlockOrPanic(block)
assert.Equal(t, newBlockNum, lc)
......
This diff is collapsed.
......@@ -175,3 +175,9 @@ message BlockData {
message BlockMetadata {
repeated bytes metadata = 1;
}
// OrdererBlockMetadata defines metadata that is set by the ordering service.
message OrdererBlockMetadata {
LastConfig last_config = 1;
bytes consenter_metadata = 2;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment