Skip to content

Commit

Permalink
Add SBFT test facilities
Browse files Browse the repository at this point in the history
Change-Id: I89e203db13cb0d77f3264377604e492d8e75a107
Signed-off-by: Gabor Hosszu <gabor@digitalasset.com>
  • Loading branch information
gaborh-da committed Nov 8, 2016
1 parent f2a4bcb commit 22273c3
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 8 deletions.
140 changes: 140 additions & 0 deletions orderer/sample_clients/single_tx_client/single_tx_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
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 main

import (
"fmt"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/op/go-logging"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"time"
)

var logger = logging.MustGetLogger("sbft_test")

var UPDATE byte = 0
var SEND byte = 1

var NEEDED_UPDATES = 2
var NEEDED_SENT = 1

func main() {
logger.Info("Creating an Atomic Broadcast GRPC connection.")
timeout := 4 * time.Second
clientconn, err := grpc.Dial(":7101", grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure())
if err != nil {
logger.Errorf("Failed to connect to GRPC: %s", err)
return
}
client := ab.NewAtomicBroadcastClient(clientconn)

resultch := make(chan byte)
errorch := make(chan error)

logger.Info("Starting a goroutine waiting for ledger updates.")
go updateReceiver(resultch, errorch, client)

logger.Info("Starting a single broadcast sender goroutine.")
go broadcastSender(resultch, errorch, client)

checkResults(resultch, errorch)
}

func checkResults(resultch chan byte, errorch chan error) {
l := len(errorch)
for i := 0; i < l; i++ {
errres := <-errorch
logger.Error(errres)
}

updates := 0
sentBroadcast := 0
for i := 0; i < 3; i++ {
select {
case result := <-resultch:
switch result {
case UPDATE:
updates++
case SEND:
sentBroadcast++
}
case <-time.After(30 * time.Second):
continue
}
}
if updates != NEEDED_UPDATES {
logger.Errorf("We did not get all the ledger updates.")
} else if sentBroadcast != NEEDED_SENT {
logger.Errorf("We were unable to send all the broadcasts.")
} else {
logger.Info("Successfully sent and received everything.")
}
}

func updateReceiver(resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) {
logger.Info("{Update Receiver} Creating a ledger update delivery stream.")
dstream, err := client.Deliver(context.Background())
if err != nil {
errorch <- fmt.Errorf("Failed to get Deliver stream: %s", err)
return
}
dstream.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Seek{Seek: &ab.SeekInfo{Start: ab.SeekInfo_NEWEST, WindowSize: 10}}})
logger.Info("{Update Receiver} Listening to ledger updates.")
for i := 0; i < 2; i++ {
m, inerr := dstream.Recv()
if inerr != nil {
errorch <- fmt.Errorf("Failed to receive consensus: %s", inerr)
return
}
b := m.Type.(*ab.DeliverResponse_Block)
logger.Info("{Update Receiver} Received a ledger update.")
for i, tx := range b.Block.Data.Data {
pl := &cb.Payload{}
e := &cb.Envelope{}
merr1 := proto.Unmarshal(tx, e)
merr2 := proto.Unmarshal(e.Payload, pl)
if merr1 == nil && merr2 == nil {
logger.Infof("{Update Receiver} %d - %v", i+1, pl.Data)
}
}
resultch <- UPDATE
}
logger.Info("{Update Receiver} Exiting...")
}

func broadcastSender(resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) {
logger.Info("{Broadcast Sender} Waiting before sending.")
<-time.After(5 * time.Second)
bstream, err := client.Broadcast(context.Background())
if err != nil {
errorch <- fmt.Errorf("Failed to get broadcast stream: %s", err)
return
}
bs := []byte{0, 1, 2, 3}
pl := &cb.Payload{Data: bs}
mpl, err := proto.Marshal(pl)
if err != nil {
panic("Failed to marshal payload.")
}
bstream.Send(&cb.Envelope{Payload: mpl})
logger.Infof("{Broadcast Sender} Broadcast sent: %v", bs)
logger.Info("{Broadcast Sender} Exiting...")
resultch <- SEND
}
4 changes: 2 additions & 2 deletions orderer/sbft/backend/backendab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package backend

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

type BackendAB struct {
Expand Down Expand Up @@ -51,7 +51,7 @@ func (b *BackendAB) Broadcast(srv ab.AtomicBroadcast_BroadcastServer) error {
}
req, err := proto.Marshal(envelope)
if err != nil {
return err
panic(err)
}
b.backend.enqueueRequest(req)
err = srv.Send(&ab.BroadcastResponse{ab.Status_SUCCESS})
Expand Down
6 changes: 3 additions & 3 deletions orderer/sbft/local-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ cat > config.json <<EOF
"consensus": {
"n" : $count,
"f" : $fail,
"batch_size_bytes" : 3,
"batch_duration_nsec" : 0,
"request_timeout_nsec" : 2
"batch_size_bytes" : 1000,
"batch_duration_nsec" : 1000000000,
"request_timeout_nsec" : 1000000000
},
"peers": [${peerconf}]
}
Expand Down
174 changes: 174 additions & 0 deletions orderer/sbft/sbft_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
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 main

import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/op/go-logging"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

var logger = logging.MustGetLogger("sbft_test")

var UPDATE byte = 0
var SEND byte = 1

var NEEDED_UPDATES = 2
var NEEDED_SENT = 1

func TestSbftPeer(t *testing.T) {
tempDir, err := ioutil.TempDir("", "sbft_test")
if err != nil {
panic("Failed to create a temporary directory")
}
// We only need the path as the directory will be created
// by the peer - TODO: modify sbft to tolerate an existing
// directory
os.RemoveAll(tempDir)
defer func() {
os.RemoveAll(tempDir)
}()
c := flags{init: "testdata/config.json",
listenAddr: ":6101",
grpcAddr: ":7101",
certFile: "testdata/cert1.pem",
keyFile: "testdata/key.pem",
dataDir: tempDir}

logger.Info("Initialization of instance.")
err = initInstance(c)
if err != nil {
t.Errorf("Initialization failed: %s", err)
return
}
logging.SetLevel(logging.DEBUG, "")

logger.Info("Starting an instance in the background.")
go serve(c)
<-time.After(5 * time.Second)

logger.Info("Creating an Atomic Broadcast GRPC connection.")
timeout := 4 * time.Second
clientconn, err := grpc.Dial(":7101", grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure())
if err != nil {
t.Errorf("Failed to connect to GRPC: %s", err)
return
}
client := ab.NewAtomicBroadcastClient(clientconn)

resultch := make(chan byte)
errorch := make(chan error)

logger.Info("Starting a goroutine waiting for ledger updates.")
go updateReceiver(t, resultch, errorch, client)

logger.Info("Starting a single broadcast sender goroutine.")
go broadcastSender(t, resultch, errorch, client)

checkResults(t, resultch, errorch)
}

func checkResults(t *testing.T, resultch chan byte, errorch chan error) {
l := len(errorch)
for i := 0; i < l; i++ {
errres := <-errorch
t.Error(errres)
}

updates := 0
sentBroadcast := 0
for i := 0; i < 3; i++ {
select {
case result := <-resultch:
switch result {
case UPDATE:
updates++
case SEND:
sentBroadcast++
}
case <-time.After(30 * time.Second):
continue
}
}
if updates != NEEDED_UPDATES {
t.Errorf("We did not get all the ledger updates.")
} else if sentBroadcast != NEEDED_SENT {
t.Errorf("We were unable to send all the broadcasts.")
} else {
logger.Info("Successfully sent and received everything.")
}
}

func updateReceiver(t *testing.T, resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) {
logger.Info("{Update Receiver} Creating a ledger update delivery stream.")
dstream, err := client.Deliver(context.Background())
if err != nil {
errorch <- fmt.Errorf("Failed to get Deliver stream: %s", err)
return
}
dstream.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Seek{Seek: &ab.SeekInfo{Start: ab.SeekInfo_NEWEST, WindowSize: 10}}})
logger.Info("{Update Receiver} Listening to ledger updates.")
for i := 0; i < 2; i++ {
m, inerr := dstream.Recv()
if inerr != nil {
errorch <- fmt.Errorf("Failed to receive consensus: %s", inerr)
return
}
b := m.Type.(*ab.DeliverResponse_Block)
logger.Info("{Update Receiver} Received a ledger update.")
for i, tx := range b.Block.Data.Data {
pl := &cb.Payload{}
e := &cb.Envelope{}
merr1 := proto.Unmarshal(tx, e)
merr2 := proto.Unmarshal(e.Payload, pl)
if merr1 == nil && merr2 == nil {
logger.Infof("{Update Receiver} %d - %v", i+1, pl.Data)
}
}
resultch <- UPDATE
}
logger.Info("{Update Receiver} Exiting...")
}

func broadcastSender(t *testing.T, resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) {
logger.Info("{Broadcast Sender} Waiting before sending.")
<-time.After(5 * time.Second)
bstream, err := client.Broadcast(context.Background())
if err != nil {
errorch <- fmt.Errorf("Failed to get broadcast stream: %s", err)
return
}
bs := []byte{0, 1, 2, 3}
pl := &cb.Payload{Data: bs}
mpl, err := proto.Marshal(pl)
if err != nil {
panic("Failed to marshal payload.")
}
bstream.Send(&cb.Envelope{Payload: mpl})
logger.Infof("{Broadcast Sender} Broadcast sent: %v", bs)
logger.Info("{Broadcast Sender} Exiting...")
resultch <- SEND
}
6 changes: 3 additions & 3 deletions orderer/sbft/testdata/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"consensus": {
"n" : 1,
"f" : 0,
"batch_size_bytes" : 3,
"batch_duration_nsec" : 0,
"request_timeout_nsec" : 60
"batch_size_bytes" : 1000,
"batch_duration_nsec" : 1000000000,
"request_timeout_nsec" : 1000000000
},
"peers": [
{
Expand Down

0 comments on commit 22273c3

Please sign in to comment.