-
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.
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>
- Loading branch information
Showing
20 changed files
with
1,775 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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) | ||
} | ||
} |
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,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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 { | ||
} |
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,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) | ||
} | ||
|
Oops, something went wrong.