Skip to content

Commit

Permalink
Merge "[FAB-1364] Switch to provisional bootstrapper"
Browse files Browse the repository at this point in the history
  • Loading branch information
Srinivasan Muralidharan authored and Gerrit Code Review committed Dec 15, 2016
2 parents 0d5af27 + 71a3389 commit e343403
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)
}
}
}
Loading

0 comments on commit e343403

Please sign in to comment.