From ae277cd8c5a9f2278203e1a6c3a5fa10858b2a35 Mon Sep 17 00:00:00 2001 From: Kostas Christidis Date: Fri, 14 Apr 2017 13:31:24 -0400 Subject: [PATCH] [FAB-3091] TX ID missing from TX in genesis block This changeset: 1. Modifies the `Factory`'s `Block` method so that a TX ID is set. 2. Adds a test for the issue in question. The test fails w/o this changeset, and passes otherwise. It also: 1. Add comments to the exported identifiers of the genesis package. 2. Renames all instances of `chainID` to `channelID` (we have to start this transition eventually). 3. Modifies the tests so that they use the `assert` package. Change-Id: I13db93326cbf43544812c1de4a515940163760e8 Signed-off-by: Kostas Christidis --- .../configtx/tool/provisional/provisional.go | 2 +- common/genesis/genesis.go | 13 +++++++++---- common/genesis/genesis_test.go | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/common/configtx/tool/provisional/provisional.go b/common/configtx/tool/provisional/provisional.go index fee181e1668..c5f6fb4c416 100644 --- a/common/configtx/tool/provisional/provisional.go +++ b/common/configtx/tool/provisional/provisional.go @@ -59,7 +59,7 @@ const ( // TestChainID is the default value of ChainID. It is used by all testing // networks. It it necessary to set and export this variable so that test - // clients can connect without being rejected for targetting a chain which + // clients can connect without being rejected for targeting a chain which // does not exist. TestChainID = "testchainid" diff --git a/common/genesis/genesis.go b/common/genesis/genesis.go index d443b65fad7..daa166e24d4 100644 --- a/common/genesis/genesis.go +++ b/common/genesis/genesis.go @@ -31,20 +31,24 @@ const ( epoch = 0 ) +// Factory facilitates the creation of genesis blocks. type Factory interface { - Block(chainID string) (*cb.Block, error) + // Block returns a genesis block for a given channel ID. + Block(channelID string) (*cb.Block, error) } type factory struct { template configtx.Template } +// NewFactoryImpl creates a new Factory. func NewFactoryImpl(template configtx.Template) Factory { return &factory{template: template} } -func (f *factory) Block(chainID string) (*cb.Block, error) { - configEnv, err := f.template.Envelope(chainID) +// Block constructs and returns a genesis block for a given channel ID. +func (f *factory) Block(channelID string) (*cb.Block, error) { + configEnv, err := f.template.Envelope(channelID) if err != nil { return nil, err } @@ -55,8 +59,9 @@ func (f *factory) Block(chainID string) (*cb.Block, error) { return nil, err } - payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, chainID, epoch) + payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, channelID, epoch) payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic()) + utils.SetTxID(payloadChannelHeader, payloadSignatureHeader) payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader) payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: configUpdate.WriteSet}})} envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil} diff --git a/common/genesis/genesis_test.go b/common/genesis/genesis_test.go index 0bcd40245ac..8afcf23b4cf 100644 --- a/common/genesis/genesis_test.go +++ b/common/genesis/genesis_test.go @@ -20,12 +20,21 @@ import ( "testing" "github.com/hyperledger/fabric/common/configtx" + "github.com/hyperledger/fabric/protos/utils" + "github.com/stretchr/testify/assert" ) -func TestSanity(t *testing.T) { +func TestBasicSanity(t *testing.T) { impl := NewFactoryImpl(configtx.NewSimpleTemplate()) - _, err := impl.Block("TestChainID") - if err != nil { - t.Fatalf("Basic sanity fails") - } + _, err := impl.Block("testchainid") + assert.NoError(t, err, "Basic sanity fails") +} + +func TestForTransactionID(t *testing.T) { + impl := NewFactoryImpl(configtx.NewSimpleTemplate()) + block, _ := impl.Block("testchainid") + configEnv, _ := utils.ExtractEnvelope(block, 0) + configEnvPayload, _ := utils.ExtractPayload(configEnv) + configEnvPayloadChannelHeader, _ := utils.UnmarshalChannelHeader(configEnvPayload.GetHeader().ChannelHeader) + assert.NotEmpty(t, configEnvPayloadChannelHeader.TxId, "tx_id of configuration transaction should not be empty") }