|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/golang/protobuf/proto" |
| 27 | + cb "github.com/hyperledger/fabric/protos/common" |
| 28 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 29 | + "github.com/op/go-logging" |
| 30 | + "golang.org/x/net/context" |
| 31 | + "google.golang.org/grpc" |
| 32 | +) |
| 33 | + |
| 34 | +var logger = logging.MustGetLogger("sbft_test") |
| 35 | + |
| 36 | +var UPDATE byte = 0 |
| 37 | +var SEND byte = 1 |
| 38 | + |
| 39 | +var NEEDED_UPDATES = 2 |
| 40 | +var NEEDED_SENT = 1 |
| 41 | + |
| 42 | +func TestSbftPeer(t *testing.T) { |
| 43 | + tempDir, err := ioutil.TempDir("", "sbft_test") |
| 44 | + if err != nil { |
| 45 | + panic("Failed to create a temporary directory") |
| 46 | + } |
| 47 | + // We only need the path as the directory will be created |
| 48 | + // by the peer - TODO: modify sbft to tolerate an existing |
| 49 | + // directory |
| 50 | + os.RemoveAll(tempDir) |
| 51 | + defer func() { |
| 52 | + os.RemoveAll(tempDir) |
| 53 | + }() |
| 54 | + c := flags{init: "testdata/config.json", |
| 55 | + listenAddr: ":6101", |
| 56 | + grpcAddr: ":7101", |
| 57 | + certFile: "testdata/cert1.pem", |
| 58 | + keyFile: "testdata/key.pem", |
| 59 | + dataDir: tempDir} |
| 60 | + |
| 61 | + logger.Info("Initialization of instance.") |
| 62 | + err = initInstance(c) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("Initialization failed: %s", err) |
| 65 | + return |
| 66 | + } |
| 67 | + logging.SetLevel(logging.DEBUG, "") |
| 68 | + |
| 69 | + logger.Info("Starting an instance in the background.") |
| 70 | + go serve(c) |
| 71 | + <-time.After(5 * time.Second) |
| 72 | + |
| 73 | + logger.Info("Creating an Atomic Broadcast GRPC connection.") |
| 74 | + timeout := 4 * time.Second |
| 75 | + clientconn, err := grpc.Dial(":7101", grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure()) |
| 76 | + if err != nil { |
| 77 | + t.Errorf("Failed to connect to GRPC: %s", err) |
| 78 | + return |
| 79 | + } |
| 80 | + client := ab.NewAtomicBroadcastClient(clientconn) |
| 81 | + |
| 82 | + resultch := make(chan byte) |
| 83 | + errorch := make(chan error) |
| 84 | + |
| 85 | + logger.Info("Starting a goroutine waiting for ledger updates.") |
| 86 | + go updateReceiver(t, resultch, errorch, client) |
| 87 | + |
| 88 | + logger.Info("Starting a single broadcast sender goroutine.") |
| 89 | + go broadcastSender(t, resultch, errorch, client) |
| 90 | + |
| 91 | + checkResults(t, resultch, errorch) |
| 92 | +} |
| 93 | + |
| 94 | +func checkResults(t *testing.T, resultch chan byte, errorch chan error) { |
| 95 | + l := len(errorch) |
| 96 | + for i := 0; i < l; i++ { |
| 97 | + errres := <-errorch |
| 98 | + t.Error(errres) |
| 99 | + } |
| 100 | + |
| 101 | + updates := 0 |
| 102 | + sentBroadcast := 0 |
| 103 | + for i := 0; i < 3; i++ { |
| 104 | + select { |
| 105 | + case result := <-resultch: |
| 106 | + switch result { |
| 107 | + case UPDATE: |
| 108 | + updates++ |
| 109 | + case SEND: |
| 110 | + sentBroadcast++ |
| 111 | + } |
| 112 | + case <-time.After(30 * time.Second): |
| 113 | + continue |
| 114 | + } |
| 115 | + } |
| 116 | + if updates != NEEDED_UPDATES { |
| 117 | + t.Errorf("We did not get all the ledger updates.") |
| 118 | + } else if sentBroadcast != NEEDED_SENT { |
| 119 | + t.Errorf("We were unable to send all the broadcasts.") |
| 120 | + } else { |
| 121 | + logger.Info("Successfully sent and received everything.") |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func updateReceiver(t *testing.T, resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) { |
| 126 | + logger.Info("{Update Receiver} Creating a ledger update delivery stream.") |
| 127 | + dstream, err := client.Deliver(context.Background()) |
| 128 | + if err != nil { |
| 129 | + errorch <- fmt.Errorf("Failed to get Deliver stream: %s", err) |
| 130 | + return |
| 131 | + } |
| 132 | + dstream.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Seek{Seek: &ab.SeekInfo{Start: ab.SeekInfo_NEWEST, WindowSize: 10}}}) |
| 133 | + logger.Info("{Update Receiver} Listening to ledger updates.") |
| 134 | + for i := 0; i < 2; i++ { |
| 135 | + m, inerr := dstream.Recv() |
| 136 | + if inerr != nil { |
| 137 | + errorch <- fmt.Errorf("Failed to receive consensus: %s", inerr) |
| 138 | + return |
| 139 | + } |
| 140 | + b := m.Type.(*ab.DeliverResponse_Block) |
| 141 | + logger.Info("{Update Receiver} Received a ledger update.") |
| 142 | + for i, tx := range b.Block.Data.Data { |
| 143 | + pl := &cb.Payload{} |
| 144 | + e := &cb.Envelope{} |
| 145 | + merr1 := proto.Unmarshal(tx, e) |
| 146 | + merr2 := proto.Unmarshal(e.Payload, pl) |
| 147 | + if merr1 == nil && merr2 == nil { |
| 148 | + logger.Infof("{Update Receiver} %d - %v", i+1, pl.Data) |
| 149 | + } |
| 150 | + } |
| 151 | + resultch <- UPDATE |
| 152 | + } |
| 153 | + logger.Info("{Update Receiver} Exiting...") |
| 154 | +} |
| 155 | + |
| 156 | +func broadcastSender(t *testing.T, resultch chan byte, errorch chan error, client ab.AtomicBroadcastClient) { |
| 157 | + logger.Info("{Broadcast Sender} Waiting before sending.") |
| 158 | + <-time.After(5 * time.Second) |
| 159 | + bstream, err := client.Broadcast(context.Background()) |
| 160 | + if err != nil { |
| 161 | + errorch <- fmt.Errorf("Failed to get broadcast stream: %s", err) |
| 162 | + return |
| 163 | + } |
| 164 | + bs := []byte{0, 1, 2, 3} |
| 165 | + pl := &cb.Payload{Data: bs} |
| 166 | + mpl, err := proto.Marshal(pl) |
| 167 | + if err != nil { |
| 168 | + panic("Failed to marshal payload.") |
| 169 | + } |
| 170 | + bstream.Send(&cb.Envelope{Payload: mpl}) |
| 171 | + logger.Infof("{Broadcast Sender} Broadcast sent: %v", bs) |
| 172 | + logger.Info("{Broadcast Sender} Exiting...") |
| 173 | + resultch <- SEND |
| 174 | +} |
0 commit comments