Skip to content

Commit

Permalink
Add AnchorPeers to ConfigurationBlock
Browse files Browse the repository at this point in the history
This commit:
1) Adds anchor peers upon channel creation
2) Extracts them at the peer side in the gossip layer
   and uses them.
3) Changes an internal API of a method in gossip
   from GetTimestamp to SequenceNumber, because
   using sequence numbers is better than using
   timestamps, that are machine-specific in
   contrast to sequence numbers which are global.

Signed-off-by: Yacov Manevich <yacovm@il.ibm.com>
Change-Id: Id080585a56a5083d9cd4911ce790e5be389cfa52
  • Loading branch information
yacovm committed Jan 20, 2017
1 parent 076923e commit 282ed86
Show file tree
Hide file tree
Showing 12 changed files with 380 additions and 45 deletions.
3 changes: 2 additions & 1 deletion common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func init() {
}

template = configtx.NewSimpleTemplate(templateProto.Items...)
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, template))
gossTemplate := configtx.NewSimpleTemplate(utils.EncodeAnchorPeers())
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, template, gossTemplate))
}

func MakeGenesisBlock(chainID string) (*cb.Block, error) {
Expand Down
3 changes: 3 additions & 0 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
return err
}

// TODO This should be called from a configtx.Manager but it's not
// implemented yet. When it will be, this needs to move there,
// and the inner fields (AnchorPeers) only should be passed to this.
if err := service.GetGossipService().JoinChannel(c, cb); err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions gossip/api/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package api

import (
"time"

"github.com/hyperledger/fabric/gossip/common"
)

Expand Down Expand Up @@ -47,8 +45,9 @@ type ChannelNotifier interface {
// among the peers
type JoinChannelMessage interface {

// GetTimestamp returns the timestamp of the message's creation
GetTimestamp() time.Time
// SequenceNumber returns the sequence number of the configuration block
// the JoinChannelMessage originated from
SequenceNumber() uint64

// AnchorPeers returns all the anchor peers that are in the channel
AnchorPeers() []AnchorPeer
Expand Down
4 changes: 2 additions & 2 deletions gossip/gossip/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ func (gc *gossipChannel) ConfigureChannel(joinMsg api.JoinChannelMessage) {
gc.joinMsg = joinMsg
}

if gc.joinMsg.GetTimestamp().After(joinMsg.GetTimestamp()) {
gc.logger.Warning("Already have a more updated JoinChannel message(", gc.joinMsg.GetTimestamp(), ") than", gc.joinMsg.GetTimestamp())
if gc.joinMsg.SequenceNumber() > (joinMsg.SequenceNumber()) {
gc.logger.Warning("Already have a more updated JoinChannel message(", gc.joinMsg.SequenceNumber(), ") than", gc.joinMsg.SequenceNumber())
return
}
orgs := []api.OrgIdentityType{}
Expand Down
10 changes: 6 additions & 4 deletions gossip/gossip/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ type joinChanMsg struct {
anchorPeers func() []api.AnchorPeer
}

// GetTimestamp returns the timestamp of the message's creation
func (jcm *joinChanMsg) GetTimestamp() time.Time {
// SequenceNumber returns the sequence number of the block
// this joinChanMsg was derived from.
// I use timestamps here just for the test.
func (jcm *joinChanMsg) SequenceNumber() uint64 {
if jcm.getTS != nil {
return jcm.getTS()
return uint64(jcm.getTS().UnixNano())
}
return time.Now()
return uint64(time.Now().UnixNano())
}

// AnchorPeers returns all the anchor peers that are in the channel
Expand Down
7 changes: 4 additions & 3 deletions gossip/gossip/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ func acceptData(m interface{}) bool {
type joinChanMsg struct {
}

// GetTimestamp returns the timestamp of the message's creation
func (*joinChanMsg) GetTimestamp() time.Time {
return time.Now()
// SequenceNumber returns the sequence number of the block this joinChanMsg
// is derived from
func (*joinChanMsg) SequenceNumber() uint64 {
return uint64(time.Now().UnixNano())
}

// AnchorPeers returns all the anchor peers that are in the channel
Expand Down
147 changes: 147 additions & 0 deletions gossip/service/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Copyright IBM Corp. 2017 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 service

import (
"fmt"

"github.com/hyperledger/fabric/gossip/api"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/utils"
)

// unMarshal is used to un-marshall proto-buffer types.
// In tests, I override this variable with a function
var unMarshal func (buf []byte, pb proto.Message) error

func init() {
unMarshal = proto.Unmarshal
}

// This is an implementation of api.JoinChannelMessage.
// This object is created from a *common.Block
type joinChannelMessage struct {
seqNum uint64
anchorPeers []api.AnchorPeer
}

// JoinChannelMessageFromBlock returns an api.JoinChannelMessage, nil
// or nil, error if the block is not a valid channel configuration block
func JoinChannelMessageFromBlock(block *common.Block) (api.JoinChannelMessage, error) {
anchorPeers, err := parseBlock(block)
if err != nil {
return nil, err
}
jcm := &joinChannelMessage{seqNum: block.Header.Number, anchorPeers: []api.AnchorPeer{}}
for _, ap := range anchorPeers.AnchorPees {
anchorPeer := api.AnchorPeer{
Host: ap.Host,
Port: int(ap.Port),
Cert: api.PeerIdentityType(ap.Cert),
}
jcm.anchorPeers = append(jcm.anchorPeers, anchorPeer)
}
return jcm, nil
}

// parseBlock parses a configuration block, and returns error
// if it's not a valid channel configuration block
func parseBlock(block *common.Block) (*peer.AnchorPeers, error) {
confEnvelope, err := extractConfigurationEnvelope(block)
if err != nil {
return nil, err
}
// Find anchor peer configuration
found := false
var anchorPeerConfig *common.ConfigurationItem
for _, item := range confEnvelope.Items {
rawConfItem := item.ConfigurationItem
confItem := &common.ConfigurationItem{}
if err := unMarshal(rawConfItem, confItem); err != nil {
return nil, fmt.Errorf("Failed unmarshalling configuration item")
}
if confItem.Header.Type != int32(common.HeaderType_CONFIGURATION_ITEM) {
continue
}
if confItem.Type != common.ConfigurationItem_Peer {
continue
}
if confItem.Key != utils.AnchorPeerConfItemKey {
continue
}
if found {
return nil, fmt.Errorf("Found multiple definition of AnchorPeers instead of a single one")
}
found = true
anchorPeerConfig = confItem
}
if ! found {
return nil, fmt.Errorf("Didn't find AnchorPeer definition in configuration block")
}
rawAnchorPeersBytes := anchorPeerConfig.Value
anchorPeers := &peer.AnchorPeers{}
if err := unMarshal(rawAnchorPeersBytes, anchorPeers); err != nil {
return nil, fmt.Errorf("Failed deserializing anchor peers from configuration item")
}
if len(anchorPeers.AnchorPees) == 0 {
return nil, fmt.Errorf("AnchorPeers field in configuration block was found, but is empty")
}
return anchorPeers, nil
}

// extractConfigurationEnvelope extracts the configuration envelope from a block,
// or returns nil and an error if extraction fails
func extractConfigurationEnvelope(block *common.Block) (*common.ConfigurationEnvelope, error) {
if block.Header == nil {
return nil, fmt.Errorf("Block header is empty")
}
if block.Data == nil {
return nil, fmt.Errorf("Block data is empty")
}
env := &common.Envelope{}
if len(block.Data.Data) == 0 {
return nil, fmt.Errorf("Empty data in block")
}
if len(block.Data.Data) != 1 {
return nil, fmt.Errorf("More than 1 transaction in a block")
}
if err := unMarshal(block.Data.Data[0], env); err != nil {
return nil, fmt.Errorf("Failed unmarshalling envelope from block: %v", err)
}
payload := &common.Payload{}
if err := unMarshal(env.Payload, payload); err != nil {
return nil, fmt.Errorf("Failed unmarshalling payload from envelope: %v", err)
}
confEnvelope := &common.ConfigurationEnvelope{}
if err := unMarshal(payload.Data, confEnvelope); err != nil {
return nil, fmt.Errorf("Failed unmarshalling configuration envelope from payload: %v", err)
}
if len(confEnvelope.Items) == 0 {
return nil, fmt.Errorf("Empty configuration envelope, no items detected")
}
return confEnvelope, nil
}

func (jcm *joinChannelMessage) SequenceNumber() uint64 {
return jcm.seqNum
}

func (jcm *joinChannelMessage) AnchorPeers() []api.AnchorPeer {
return jcm.anchorPeers
}
Loading

0 comments on commit 282ed86

Please sign in to comment.