Skip to content

Commit

Permalink
Introduce consensus peer application [FAB-473]
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
gaborh-da committed Nov 8, 2016
1 parent 216ae65 commit f2a4bcb
Show file tree
Hide file tree
Showing 20 changed files with 1,775 additions and 0 deletions.
406 changes: 406 additions & 0 deletions orderer/sbft/backend/backend.go

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions orderer/sbft/backend/backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright Digital Asset Holdings, LLC 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 backend

import (
"crypto/ecdsa"
"crypto/elliptic"
crand "crypto/rand"
"crypto/rsa"
"testing"
)

func TestSignAndVerifyRsa(t *testing.T) {
data := []byte{1, 1, 1, 1, 1}
privateKey, err := rsa.GenerateKey(crand.Reader, 1024)
if err != nil {
panic("RSA failed to generate private key in test.")
}
s := Sign(privateKey, data)
if s == nil {
t.Error("Nil signature was generated.")
}

publicKey := privateKey.Public()
err = CheckSig(publicKey, data, s)
if err != nil {
t.Errorf("Signature check failed: %s", err)
}
}

func TestSignAndVerifyEcdsa(t *testing.T) {
data := []byte{1, 1, 1, 1, 1}
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
if err != nil {
panic("ECDSA failed to generate private key in test.")
}
s := Sign(privateKey, data)
if s == nil {
t.Error("Nil signature was generated.")
}

publicKey := privateKey.Public()
err = CheckSig(publicKey, data, s)
if err != nil {
t.Errorf("Signature check failed: %s", err)
}
}
67 changes: 67 additions & 0 deletions orderer/sbft/backend/backendab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright Digital Asset Holdings, LLC 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 backend

import (
"github.com/golang/protobuf/proto"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/orderer/solo"
)

type BackendAB struct {
backend *Backend
deliverserver *solo.DeliverServer
}

func NewBackendAB(backend *Backend) *BackendAB {
bab := &BackendAB{
backend: backend,
deliverserver: solo.NewDeliverServer(backend.ledger, 1000),
}
return bab
}

// Broadcast receives a stream of messages from a client for ordering
func (b *BackendAB) Broadcast(srv ab.AtomicBroadcast_BroadcastServer) error {
for {
envelope, err := srv.Recv()
if err != nil {
return err
}

if envelope.Payload == nil {
err = srv.Send(&ab.BroadcastResponse{ab.Status_BAD_REQUEST})
if err != nil {
return err
}
}
req, err := proto.Marshal(envelope)
if err != nil {
return err
}
b.backend.enqueueRequest(req)
err = srv.Send(&ab.BroadcastResponse{ab.Status_SUCCESS})
if err != nil {
return err
}
}
}

// Deliver sends a stream of blocks to a client after ordering
func (b *BackendAB) Deliver(srv ab.AtomicBroadcast_DeliverServer) error {
return b.deliverserver.HandleDeliver(srv)
}
130 changes: 130 additions & 0 deletions orderer/sbft/backend/consensus.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions orderer/sbft/backend/consensus.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package backend;

import "github.com/hyperledger/fabric/consensus/simplebft/simplebft.proto";

service consensus {
rpc consensus(handshake) returns (stream simplebft.msg) {}
}

message handshake {
}
66 changes: 66 additions & 0 deletions orderer/sbft/backend/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright Digital Asset Holdings, LLC 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 backend

import (
s "github.com/hyperledger/fabric/consensus/simplebft"
)

type Timer struct {
tf func()
execute bool
}

func (t *Timer) Cancel() {
t.execute = false
}

type Executable interface {
Execute(*Backend)
}

func (t *Timer) Execute(backend *Backend) {
if t.execute {
t.tf()
}
}

type msgEvent struct {
msg *s.Msg
src uint64
}

func (m *msgEvent) Execute(backend *Backend) {
backend.consensus.Receive(m.msg, m.src)
}

type requestEvent struct {
req []byte
}

func (r *requestEvent) Execute(backend *Backend) {
backend.consensus.Request(r.req)
}

type connectionEvent struct {
peerid uint64
}

func (c *connectionEvent) Execute(backend *Backend) {
backend.consensus.Connection(c.peerid)
}

Loading

0 comments on commit f2a4bcb

Please sign in to comment.