Skip to content

Commit

Permalink
[FAB-1364] Switch to provisional bootstrapper
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1364

All consenters read several of their config settings (think sharedconfig)
from the genesis block that is generated by a bootstrapper. The only
bootstrapper available so far is the static one. However, when testing we
need to be able to modify several of these config values on the fly.

Therefore the bootstrapper should be able to read a config object (which
is itself created by reading the orderer.yaml file and -if set- its
associated ENV vars).

An example of that would be the KafkaBrokers value. For unit tests the
"right" value is "127.0.0.1:9092", whereas for the current Docker
Compose-based BDD tests the right value is "kafka0:9092".

Since this bootstrapper is no longer static, renaming the package seemed
appropriate. For production we will need to introduce file-based
bootstrapper that reads the genesis block created by the genesis block
tool.

This changeset allows the bootstrapper to generate the appropriate
genesis blocks (with their own custom keys) for all know consenters
types: solo, kafka, sbft (though sbft for now just short-circuits to the
solo case).

The new bootstrapper is built in a way that makes further extensions
easier, and minimizes code duplication by creating the appropriate
structs and doing the necessary embeddings.

The test that inspected the block's Data field was removed. It was a
tautological test, serving no good purpose, and was also hard to
maintain.

Finally, this changeset updates the bootstrap helper signature by
removing the error return; while the error return can be useful if the
expectation is that the caller will be able to resort to a different
bootstrapper if the first one fails, etc. one could that this amount of
flexibility is unnecessary, and complicates the code needlessly. The
bootstrapper should simply panic if the wrong settings have been passed
to it, and the user should read the fine manual.

Change-Id: I6aef9b19dbf9a39652d2d6b3ccbefc794e3001df
Signed-off-by: Kostas Christidis <kostas@christidis.io>
  • Loading branch information
kchristidis committed Dec 15, 2016
1 parent b504af9 commit 71a3389
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 445 deletions.
7 changes: 4 additions & 3 deletions orderer/common/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
ab "github.com/hyperledger/fabric/protos/common"
)

// Helper defines the functions a bootstrapping implementation to provide
// Helper defines the functions a bootstrapping implementation should provide.
type Helper interface {
// GenesisBlock should return the genesis block required to bootstrap the ledger (be it reading from the filesystem, generating it, etc.)
GenesisBlock() (*ab.Block, error)
// GenesisBlock should return the genesis block required to bootstrap
// the ledger (be it reading from the filesystem, generating it, etc.)
GenesisBlock() *ab.Block
}
43 changes: 43 additions & 0 deletions orderer/common/bootstrap/provisional/envelope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provisional

import (
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)

func (cbs *commonBootstrapper) makeGenesisConfigEnvelope() *cb.ConfigurationEnvelope {
return utils.MakeConfigurationEnvelope(
cbs.encodeConsensusType(),
cbs.encodeBatchSize(),
cbs.encodeChainCreators(),
cbs.encodeAcceptAllPolicy(),
cbs.lockDefaultModificationPolicy(),
)
}

func (kbs *kafkaBootstrapper) makeGenesisConfigEnvelope() *cb.ConfigurationEnvelope {
return utils.MakeConfigurationEnvelope(
kbs.encodeConsensusType(),
kbs.encodeBatchSize(),
kbs.encodeKafkaBrokers(),
kbs.encodeChainCreators(),
kbs.encodeAcceptAllPolicy(),
kbs.lockDefaultModificationPolicy(),
)
}
87 changes: 87 additions & 0 deletions orderer/common/bootstrap/provisional/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provisional

import (
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"
)

func (cbs *commonBootstrapper) encodeConsensusType() *cb.SignedConfigurationItem {
configItemKey := sharedconfig.ConsensusTypeKey
configItemValue := utils.MarshalOrPanic(&ab.ConsensusType{Type: cbs.consensusType})
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}

func (cbs *commonBootstrapper) encodeBatchSize() *cb.SignedConfigurationItem {
configItemKey := sharedconfig.BatchSizeKey
configItemValue := utils.MarshalOrPanic(&ab.BatchSize{Messages: cbs.batchSize})
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}

func (cbs *commonBootstrapper) encodeChainCreators() *cb.SignedConfigurationItem {
configItemKey := sharedconfig.ChainCreatorsKey
configItemValue := utils.MarshalOrPanic(&ab.ChainCreators{Policies: DefaultChainCreators})
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}

func (cbs *commonBootstrapper) encodeAcceptAllPolicy() *cb.SignedConfigurationItem {
configItemKey := AcceptAllPolicyKey
configItemValue := utils.MarshalOrPanic(utils.MakePolicyOrPanic(cauthdsl.AcceptAllPolicy))
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Policy, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}

func (cbs *commonBootstrapper) lockDefaultModificationPolicy() *cb.SignedConfigurationItem {
// Lock down the default modification policy to prevent any further policy modifications
configItemKey := configtx.DefaultModificationPolicyID
configItemValue := utils.MarshalOrPanic(utils.MakePolicyOrPanic(cauthdsl.RejectAllPolicy))
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Policy, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}

func (kbs *kafkaBootstrapper) encodeKafkaBrokers() *cb.SignedConfigurationItem {
configItemKey := sharedconfig.KafkaBrokersKey
configItemValue := utils.MarshalOrPanic(&ab.KafkaBrokers{Brokers: kbs.kafkaBrokers})
modPolicy := configtx.DefaultModificationPolicyID

configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, kbs.chainID, epoch)
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, lastModified, modPolicy, configItemKey, configItemValue)
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
}
122 changes: 122 additions & 0 deletions orderer/common/bootstrap/provisional/provisional.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provisional

import (
"fmt"

"github.com/hyperledger/fabric/orderer/common/bootstrap"
"github.com/hyperledger/fabric/orderer/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)

const (
msgVersion = int32(1)

// ConsensusTypeSolo identifies the solo consensus implementation.
ConsensusTypeSolo = "solo"
// ConsensusTypeKafka identifies the Kafka-based consensus implementation.
ConsensusTypeKafka = "kafka"
// ConsensusTypeSbft identifies the SBFT consensus implementation.
ConsensusTypeSbft = "sbft"

// 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
// does not exist.
TestChainID = "**TEST_CHAINID**"

// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
AcceptAllPolicyKey = "AcceptAllPolicy"

// These values are fixed for the genesis block.
lastModified = 0
epoch = 0
)

// DefaultChainCreators is the default value of ChainCreatorsKey.
var DefaultChainCreators = []string{AcceptAllPolicyKey}

type commonBootstrapper struct {
chainID string
consensusType string
batchSize uint32
}

type soloBootstrapper struct {
commonBootstrapper
}

type kafkaBootstrapper struct {
commonBootstrapper
kafkaBrokers []string
}

// New returns a new provisional bootstrap helper.
func New(conf *config.TopLevel) bootstrap.Helper {
cbs := &commonBootstrapper{
chainID: TestChainID,
consensusType: conf.General.OrdererType,
batchSize: conf.General.BatchSize,
}

switch conf.General.OrdererType {
case ConsensusTypeSolo, ConsensusTypeSbft:
return &soloBootstrapper{
commonBootstrapper: *cbs,
}
case ConsensusTypeKafka:
return &kafkaBootstrapper{
commonBootstrapper: *cbs,
kafkaBrokers: conf.Kafka.Brokers,
}
default:
panic(fmt.Errorf("Wrong consenter type value given: %s", conf.General.OrdererType))
}
}

// GenesisBlock returns the genesis block to be used for bootstrapping.
func (cbs *commonBootstrapper) GenesisBlock() *cb.Block {
return cbs.makeGenesisBlock(cbs.makeGenesisConfigEnvelope())
}

// GenesisBlock returns the genesis block to be used for bootstrapping.
func (kbs *kafkaBootstrapper) GenesisBlock() *cb.Block {
return kbs.makeGenesisBlock(kbs.makeGenesisConfigEnvelope())
}

func (cbs *commonBootstrapper) makeGenesisBlock(configEnvelope *cb.ConfigurationEnvelope) *cb.Block {
configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, cbs.chainID, epoch)
payloadChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, configItemChainHeader.Version, cbs.chainID, epoch)
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
payloadHeader := utils.MakePayloadHeader(payloadChainHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(configEnvelope)}
envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}

blockData := &cb.BlockData{Data: [][]byte{utils.MarshalOrPanic(envelope)}}

return &cb.Block{
Header: &cb.BlockHeader{
Number: 0,
PreviousHash: nil,
DataHash: blockData.Hash(),
},
Data: blockData,
Metadata: nil,
}
}
57 changes: 57 additions & 0 deletions orderer/common/bootstrap/provisional/provisional_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provisional

import (
"bytes"
"testing"

"github.com/hyperledger/fabric/orderer/localconfig"
)

var confSolo, confKafka *config.TopLevel
var testCases []*config.TopLevel

func init() {
confSolo = config.Load()
confKafka = config.Load()
confKafka.General.OrdererType = ConsensusTypeKafka
testCases = []*config.TopLevel{confSolo, confKafka}
}

func TestGenesisBlockHeader(t *testing.T) {
expectedHeaderNumber := uint64(0)

for _, tc := range testCases {
genesisBlock := New(tc).GenesisBlock()
if genesisBlock.Header.Number != expectedHeaderNumber {
t.Fatalf("Case %s: Expected header number %d, got %d", tc.General.OrdererType, expectedHeaderNumber, genesisBlock.Header.Number)
}
if !bytes.Equal(genesisBlock.Header.PreviousHash, nil) {
t.Fatalf("Case %s: Expected header previousHash to be nil, got %x", tc.General.OrdererType, genesisBlock.Header.PreviousHash)
}
}
}

func TestGenesisMetadata(t *testing.T) {
for _, tc := range testCases {
genesisBlock := New(tc).GenesisBlock()
if genesisBlock.Metadata != nil {
t.Fatalf("Expected metadata nil, got %x", genesisBlock.Metadata)
}
}
}

0 comments on commit 71a3389

Please sign in to comment.