-
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.
[FAB-1288]: Expose gossip API for cscc.
Add gossip service entity to encapsulate gossip + state instance. Make service instance a singelton and add JoinChannel method so cscc will be able to leverage it. Change-Id: I38233781276d538f861e472a427a1df12887c887 Signed-off-by: Artem Barger <bartem@il.ibm.com> Signed-off-by: Yacov Manevich <yacovm@il.ibm.com>
- Loading branch information
Showing
5 changed files
with
241 additions
and
49 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,145 @@ | ||
/* | ||
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 service | ||
|
||
import ( | ||
"sync" | ||
"fmt" | ||
|
||
pb "github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric/core/committer" | ||
"github.com/hyperledger/fabric/gossip/comm" | ||
gossipCommon "github.com/hyperledger/fabric/gossip/common" | ||
"github.com/hyperledger/fabric/gossip/discovery" | ||
"github.com/hyperledger/fabric/gossip/gossip" | ||
"github.com/hyperledger/fabric/gossip/integration" | ||
"github.com/hyperledger/fabric/gossip/proto" | ||
"github.com/hyperledger/fabric/gossip/state" | ||
"github.com/hyperledger/fabric/protos/common" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
var ( | ||
gossipServiceInstance *gossipServiceImpl | ||
once sync.Once | ||
) | ||
|
||
// GossipService encapsulates gossip and state capabilities into single interface | ||
type GossipService interface { | ||
gossip.Gossip | ||
|
||
// JoinChannel joins new chain given the configuration block and initialized committer service | ||
JoinChannel(committer committer.Committer, block *common.Block) error | ||
// GetBlock returns block for given chain | ||
GetBlock(chainID string, index uint64) *common.Block | ||
// AddPayload appends message payload to for given chain | ||
AddPayload(chainID string, payload *proto.Payload) error | ||
} | ||
|
||
type gossipServiceImpl struct { | ||
comm comm.Comm | ||
gossip gossip.Gossip | ||
chains map[string]state.GossipStateProvider | ||
lock sync.RWMutex | ||
} | ||
|
||
// InitGossipService initialize gossip service | ||
func InitGossipService(endpoint string, s *grpc.Server, bootPeers ...string) { | ||
once.Do(func() { | ||
gossip, communication := integration.NewGossipComponent(endpoint, s, bootPeers...) | ||
gossipServiceInstance = &gossipServiceImpl{ | ||
gossip: gossip, | ||
comm: communication, | ||
chains: make(map[string]state.GossipStateProvider), | ||
} | ||
}) | ||
} | ||
|
||
// GetGossipService returns an instance of gossip service | ||
func GetGossipService() GossipService { | ||
return gossipServiceInstance | ||
} | ||
|
||
// JoinChannel joins the channel and initialize gossip state with given committer | ||
func (g *gossipServiceImpl) JoinChannel(commiter committer.Committer, block *common.Block) error { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
|
||
if block.Data == nil || block.Data.Data == nil || len(block.Data.Data) == 0 { | ||
return fmt.Errorf("Cannot join channel, configuration block is empty") | ||
} | ||
|
||
envelope := &common.Envelope{} | ||
if err := pb.Unmarshal(block.Data.Data[0], envelope); err != nil { | ||
return err | ||
} | ||
|
||
payload := &common.Payload{} | ||
if err := pb.Unmarshal(envelope.Payload, payload); err != nil { | ||
return err | ||
} | ||
|
||
chainID := payload.Header.ChainHeader.ChainID | ||
if len(chainID) == 0 { | ||
return fmt.Errorf("Cannot join channel, with empty chainID") | ||
} | ||
// Initialize new state provider for given committer | ||
g.chains[chainID] = state.NewGossipStateProvider(g.gossip, g.comm, commiter) | ||
return nil | ||
} | ||
|
||
// GetPeers returns a mapping of endpoint --> []discovery.NetworkMember | ||
func (g *gossipServiceImpl) GetPeers() []discovery.NetworkMember { | ||
return g.gossip.GetPeers() | ||
} | ||
|
||
// UpdateMetadata updates the self metadata of the discovery layer | ||
func (g *gossipServiceImpl) UpdateMetadata(data []byte) { | ||
g.gossip.UpdateMetadata(data) | ||
} | ||
|
||
// Gossip sends a message to other peers to the network | ||
func (g *gossipServiceImpl) Gossip(msg *proto.GossipMessage) { | ||
g.gossip.Gossip(msg) | ||
} | ||
|
||
// Accept returns a channel that outputs messages from other peers | ||
func (g *gossipServiceImpl) Accept(acceptor gossipCommon.MessageAcceptor) <-chan *proto.GossipMessage { | ||
return g.gossip.Accept(acceptor) | ||
} | ||
|
||
// GetBlock returns block for given chain | ||
func (g *gossipServiceImpl) GetBlock(chainID string, index uint64) *common.Block { | ||
g.lock.RLock() | ||
defer g.lock.RUnlock() | ||
return g.chains[chainID].GetBlock(index) | ||
} | ||
|
||
// AddPayload appends message payload to for given chain | ||
func (g *gossipServiceImpl) AddPayload(chainID string, payload *proto.Payload) error { | ||
g.lock.RLock() | ||
defer g.lock.RUnlock() | ||
return g.chains[chainID].AddPayload(payload) | ||
} | ||
|
||
// Stop stops the gossip component | ||
func (g *gossipServiceImpl) Stop() { | ||
for _, ch := range g.chains { | ||
ch.Stop() | ||
} | ||
g.gossip.Stop() | ||
} |
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,56 @@ | ||
/* | ||
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 service | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestInitGossipService(t *testing.T) { | ||
// Test whenever gossip service is indeed singleton | ||
grpcServer := grpc.NewServer() | ||
socket, error := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5611)) | ||
assert.NoError(t, error) | ||
|
||
go grpcServer.Serve(socket) | ||
defer grpcServer.Stop() | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(10) | ||
for i := 0; i < 10; i++ { | ||
go func() { | ||
InitGossipService("localhost:5611", grpcServer) | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
defer GetGossipService().Stop() | ||
gossip := GetGossipService() | ||
|
||
for i := 0; i < 10; i++ { | ||
go func(gossipInstance GossipService) { | ||
assert.Equal(t, gossip, GetGossipService()) | ||
}(gossip) | ||
} | ||
} |
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
Oops, something went wrong.