Skip to content

Commit 5f08c25

Browse files
committed
Gossip integration auxilary
This commits provides code that assists integration of the gossip component to the peer by providing naive method implementations of needed methods Change-Id: Ic44d02b236912b83282530121f7d5448f68461c5 Signed-off-by: Yacov Manevich <[email protected]>
1 parent 9d3abd1 commit 5f08c25

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

gossip/integration/integration.go

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"strconv"
23+
"strings"
24+
"time"
25+
26+
"github.com/hyperledger/fabric/gossip/comm"
27+
"github.com/hyperledger/fabric/gossip/gossip"
28+
"github.com/hyperledger/fabric/gossip/proto"
29+
"google.golang.org/grpc"
30+
)
31+
32+
// This file is used to bootstrap a gossip instance for integration/demo purposes ONLY
33+
34+
func newConfig(selfEndpoint string, bootPeers ...string) *gossip.Config {
35+
port, err := strconv.ParseInt(strings.Split(selfEndpoint, ":")[1], 10, 64)
36+
if err != nil {
37+
panic(err)
38+
}
39+
return &gossip.Config{
40+
BindPort: int(port),
41+
BootstrapPeers: bootPeers,
42+
ID: selfEndpoint,
43+
MaxMessageCountToStore: 100,
44+
MaxPropagationBurstLatency: time.Millisecond * 50,
45+
MaxPropagationBurstSize: 3,
46+
PropagateIterations: 1,
47+
PropagatePeerNum: 3,
48+
PullInterval: time.Second * 5,
49+
PullPeerNum: 3,
50+
SelfEndpoint: selfEndpoint,
51+
}
52+
}
53+
54+
func newComm(selfEndpoint string, s *grpc.Server, dialOpts ...grpc.DialOption) comm.Comm {
55+
comm, err := comm.NewCommInstance(s, NewGossipCryptoService(), []byte(selfEndpoint), dialOpts...)
56+
if err != nil {
57+
panic(err)
58+
}
59+
return comm
60+
}
61+
62+
// NewGossipComponent creates a gossip component that attaches itself to the given gRPC server
63+
func NewGossipComponent(endpoint string, s *grpc.Server, bootPeers ...string) (gossip.Gossip, comm.Comm) {
64+
conf := newConfig(endpoint, bootPeers...)
65+
comm := newComm(endpoint, s, grpc.WithInsecure())
66+
return gossip.NewGossipService(conf, comm, NewGossipCryptoService()), comm
67+
}
68+
69+
// GossipCryptoService is an interface that conforms to both
70+
// the comm.SecurityProvider and to discovery.CryptoService
71+
type GossipCryptoService interface {
72+
73+
// isEnabled returns whether authentication is enabled
74+
IsEnabled() bool
75+
76+
// Sign signs msg with this peers signing key and outputs
77+
// the signature if no error occurred.
78+
Sign(msg []byte) ([]byte, error)
79+
80+
// Verify checks that signature if a valid signature of message under vkID's verification key.
81+
// If the verification succeeded, Verify returns nil meaning no error occurred.
82+
// If vkID is nil, then the signature is verified against this validator's verification key.
83+
Verify(vkID, signature, message []byte) error
84+
85+
// validateAliveMsg validates that an Alive message is authentic
86+
ValidateAliveMsg(*proto.AliveMessage) bool
87+
88+
// SignMessage signs an AliveMessage and updates its signature field
89+
SignMessage(*proto.AliveMessage) *proto.AliveMessage
90+
}
91+
92+
// NewGossipCryptoService returns an instance that implements naively every security
93+
// interface that the gossip layer needs
94+
func NewGossipCryptoService() GossipCryptoService {
95+
return &naiveCryptoServiceImpl{}
96+
}
97+
98+
type naiveCryptoServiceImpl struct {
99+
}
100+
101+
func (cs *naiveCryptoServiceImpl) ValidateAliveMsg(*proto.AliveMessage) bool {
102+
return true
103+
}
104+
105+
// SignMessage signs an AliveMessage and updates its signature field
106+
func (cs *naiveCryptoServiceImpl) SignMessage(msg *proto.AliveMessage) *proto.AliveMessage {
107+
return msg
108+
}
109+
110+
// IsEnabled returns true whether authentication is enabled
111+
func (cs *naiveCryptoServiceImpl) IsEnabled() bool {
112+
return false
113+
}
114+
115+
// Sign signs a message with the local peer's private key
116+
func (cs *naiveCryptoServiceImpl) Sign(msg []byte) ([]byte, error) {
117+
return msg, nil
118+
}
119+
120+
// Verify verifies a signature on a message that came from a peer with a certain vkID
121+
func (cs *naiveCryptoServiceImpl) Verify(vkID, signature, message []byte) error {
122+
if !bytes.Equal(signature, message) {
123+
return fmt.Errorf("Invalid signature!")
124+
}
125+
return nil
126+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"fmt"
21+
"net"
22+
"testing"
23+
"time"
24+
25+
"google.golang.org/grpc"
26+
)
27+
28+
// This is just a test that shows how to instantiate a gossip component
29+
func TestNewGossipCryptoService(t *testing.T) {
30+
s1 := grpc.NewServer()
31+
s2 := grpc.NewServer()
32+
s3 := grpc.NewServer()
33+
34+
ll1, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5611))
35+
ll2, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5612))
36+
ll3, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5613))
37+
38+
endpoint1 := "localhost:5611"
39+
endpoint2 := "localhost:5612"
40+
endpoint3 := "localhost:5613"
41+
42+
g1, _ := NewGossipComponent(endpoint1, s1)
43+
g2, _ := NewGossipComponent(endpoint2, s2, "localhost:5611")
44+
g3, _ := NewGossipComponent(endpoint3, s3, "localhost:5611")
45+
go s1.Serve(ll1)
46+
go s2.Serve(ll2)
47+
go s2.Serve(ll3)
48+
49+
time.Sleep(time.Second * 5)
50+
fmt.Println(g1.GetPeers())
51+
fmt.Println(g2.GetPeers())
52+
fmt.Println(g3.GetPeers())
53+
time.Sleep(time.Second)
54+
}

0 commit comments

Comments
 (0)