-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "[FAB-1364] Switch to provisional bootstrapper"
- Loading branch information
Showing
31 changed files
with
412 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.