Skip to content

Commit

Permalink
[FAB-3091] TX ID missing from TX in genesis block
Browse files Browse the repository at this point in the history
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 <kostas@christidis.io>
  • Loading branch information
kchristidis committed Apr 21, 2017
1 parent 441b308 commit ae277cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion common/configtx/tool/provisional/provisional.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
13 changes: 9 additions & 4 deletions common/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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}
Expand Down
19 changes: 14 additions & 5 deletions common/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit ae277cd

Please sign in to comment.