Skip to content

Commit f2a4bcb

Browse files
committed
Introduce consensus peer application [FAB-473]
This work is based on Simon Schubert's previous work from his 'separate-consensus' branch. Change-Id: Id774a7f30f4564115245bdbf3f975d179cd524a9 Signed-off-by: Gabor Hosszu <gabor@digitalasset.com> Signed-off-by: Simon Schubert <sis@zurich.ibm.com>
1 parent 216ae65 commit f2a4bcb

20 files changed

+1775
-0
lines changed

orderer/sbft/backend/backend.go

Lines changed: 406 additions & 0 deletions
Large diffs are not rendered by default.

orderer/sbft/backend/backend_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright Digital Asset Holdings, LLC 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 backend
18+
19+
import (
20+
"crypto/ecdsa"
21+
"crypto/elliptic"
22+
crand "crypto/rand"
23+
"crypto/rsa"
24+
"testing"
25+
)
26+
27+
func TestSignAndVerifyRsa(t *testing.T) {
28+
data := []byte{1, 1, 1, 1, 1}
29+
privateKey, err := rsa.GenerateKey(crand.Reader, 1024)
30+
if err != nil {
31+
panic("RSA failed to generate private key in test.")
32+
}
33+
s := Sign(privateKey, data)
34+
if s == nil {
35+
t.Error("Nil signature was generated.")
36+
}
37+
38+
publicKey := privateKey.Public()
39+
err = CheckSig(publicKey, data, s)
40+
if err != nil {
41+
t.Errorf("Signature check failed: %s", err)
42+
}
43+
}
44+
45+
func TestSignAndVerifyEcdsa(t *testing.T) {
46+
data := []byte{1, 1, 1, 1, 1}
47+
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
48+
if err != nil {
49+
panic("ECDSA failed to generate private key in test.")
50+
}
51+
s := Sign(privateKey, data)
52+
if s == nil {
53+
t.Error("Nil signature was generated.")
54+
}
55+
56+
publicKey := privateKey.Public()
57+
err = CheckSig(publicKey, data, s)
58+
if err != nil {
59+
t.Errorf("Signature check failed: %s", err)
60+
}
61+
}

orderer/sbft/backend/backendab.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright Digital Asset Holdings, LLC 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 backend
18+
19+
import (
20+
"github.com/golang/protobuf/proto"
21+
ab "github.com/hyperledger/fabric/protos/orderer"
22+
"github.com/hyperledger/fabric/orderer/solo"
23+
)
24+
25+
type BackendAB struct {
26+
backend *Backend
27+
deliverserver *solo.DeliverServer
28+
}
29+
30+
func NewBackendAB(backend *Backend) *BackendAB {
31+
bab := &BackendAB{
32+
backend: backend,
33+
deliverserver: solo.NewDeliverServer(backend.ledger, 1000),
34+
}
35+
return bab
36+
}
37+
38+
// Broadcast receives a stream of messages from a client for ordering
39+
func (b *BackendAB) Broadcast(srv ab.AtomicBroadcast_BroadcastServer) error {
40+
for {
41+
envelope, err := srv.Recv()
42+
if err != nil {
43+
return err
44+
}
45+
46+
if envelope.Payload == nil {
47+
err = srv.Send(&ab.BroadcastResponse{ab.Status_BAD_REQUEST})
48+
if err != nil {
49+
return err
50+
}
51+
}
52+
req, err := proto.Marshal(envelope)
53+
if err != nil {
54+
return err
55+
}
56+
b.backend.enqueueRequest(req)
57+
err = srv.Send(&ab.BroadcastResponse{ab.Status_SUCCESS})
58+
if err != nil {
59+
return err
60+
}
61+
}
62+
}
63+
64+
// Deliver sends a stream of blocks to a client after ordering
65+
func (b *BackendAB) Deliver(srv ab.AtomicBroadcast_DeliverServer) error {
66+
return b.deliverserver.HandleDeliver(srv)
67+
}

orderer/sbft/backend/consensus.pb.go

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orderer/sbft/backend/consensus.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
package backend;
4+
5+
import "github.com/hyperledger/fabric/consensus/simplebft/simplebft.proto";
6+
7+
service consensus {
8+
rpc consensus(handshake) returns (stream simplebft.msg) {}
9+
}
10+
11+
message handshake {
12+
}

orderer/sbft/backend/events.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright Digital Asset Holdings, LLC 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 backend
18+
19+
import (
20+
s "github.com/hyperledger/fabric/consensus/simplebft"
21+
)
22+
23+
type Timer struct {
24+
tf func()
25+
execute bool
26+
}
27+
28+
func (t *Timer) Cancel() {
29+
t.execute = false
30+
}
31+
32+
type Executable interface {
33+
Execute(*Backend)
34+
}
35+
36+
func (t *Timer) Execute(backend *Backend) {
37+
if t.execute {
38+
t.tf()
39+
}
40+
}
41+
42+
type msgEvent struct {
43+
msg *s.Msg
44+
src uint64
45+
}
46+
47+
func (m *msgEvent) Execute(backend *Backend) {
48+
backend.consensus.Receive(m.msg, m.src)
49+
}
50+
51+
type requestEvent struct {
52+
req []byte
53+
}
54+
55+
func (r *requestEvent) Execute(backend *Backend) {
56+
backend.consensus.Request(r.req)
57+
}
58+
59+
type connectionEvent struct {
60+
peerid uint64
61+
}
62+
63+
func (c *connectionEvent) Execute(backend *Backend) {
64+
backend.consensus.Connection(c.peerid)
65+
}
66+

0 commit comments

Comments
 (0)