-
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.
Change-Id: I1bf2c44e5dc77a13b4749ea82c1df7fed618fd2f Signed-off-by: jyellick <jyellick@us.ibm.com>
- Loading branch information
jyellick
committed
Aug 30, 2016
1 parent
16ca7b0
commit 53fd500
Showing
11 changed files
with
1,159 additions
and
0 deletions.
There are no files selected for viewing
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,31 @@ | ||
/* | ||
Copyright IBM Corp. 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 atomicbroadcast | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric/core/util" | ||
) | ||
|
||
func (b *Block) Hash() []byte { | ||
data, err := proto.Marshal(b) // XXX this is wrong, protobuf is not the right mechanism to serialize for a hash | ||
if err != nil { | ||
panic("This should never fail and is generally irrecoverable") | ||
} | ||
|
||
return util.ComputeCryptoHash(data) | ||
} |
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,52 @@ | ||
/* | ||
Copyright IBM Corp. 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" | ||
"net" | ||
"os" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric/orderer/solo" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
func main() { | ||
|
||
address := os.Getenv("ORDERER_LISTEN_ADDRESS") | ||
if address == "" { | ||
address = "127.0.0.1" | ||
} | ||
|
||
port := os.Getenv("ORDERER_LISTEN_PORT") | ||
if port == "" { | ||
port = "5005" | ||
} | ||
|
||
lis, err := net.Listen("tcp", address+":"+port) | ||
if err != nil { | ||
fmt.Println("Failed to listen:", err) | ||
return | ||
} | ||
|
||
grpcServer := grpc.NewServer() | ||
|
||
solo.New(100, 10, 10, 10*time.Second, grpcServer) | ||
grpcServer.Serve(lis) | ||
} |
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,57 @@ | ||
/* | ||
Copyright IBM Corp. 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" | ||
"time" | ||
|
||
ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type broadcastClient struct { | ||
client ab.AtomicBroadcast_BroadcastClient | ||
} | ||
|
||
// newBroadcastClient creates a simple instance of the broadcastClient interface | ||
func newBroadcastClient(client ab.AtomicBroadcast_BroadcastClient) *broadcastClient { | ||
return &broadcastClient{client: client} | ||
} | ||
|
||
func (s *broadcastClient) broadcast(transaction []byte) error { | ||
return s.client.Send(&ab.BroadcastMessage{transaction}) | ||
} | ||
|
||
func main() { | ||
serverAddr := "127.0.0.1:5005" | ||
conn, err := grpc.Dial(serverAddr, grpc.WithInsecure()) | ||
defer conn.Close() | ||
if err != nil { | ||
fmt.Println("Error connecting:", err) | ||
return | ||
} | ||
client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO()) | ||
if err != nil { | ||
fmt.Println("Error connecting:", err) | ||
return | ||
} | ||
|
||
s := newBroadcastClient(client) | ||
s.broadcast([]byte(fmt.Sprintf("Testing %v", time.Now()))) | ||
} |
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,120 @@ | ||
/* | ||
Copyright IBM Corp. 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" | ||
|
||
ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type deliverClient struct { | ||
client ab.AtomicBroadcast_DeliverClient | ||
windowSize uint64 | ||
unAcknowledged uint64 | ||
} | ||
|
||
func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, windowSize uint64) *deliverClient { | ||
return &deliverClient{client: client, windowSize: windowSize} | ||
} | ||
|
||
func (r *deliverClient) seekOldest() error { | ||
return r.client.Send(&ab.DeliverUpdate{ | ||
Type: &ab.DeliverUpdate_Seek{ | ||
Seek: &ab.SeekInfo{ | ||
Start: ab.SeekInfo_OLDEST, | ||
WindowSize: r.windowSize, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func (r *deliverClient) seekNewest() error { | ||
return r.client.Send(&ab.DeliverUpdate{ | ||
Type: &ab.DeliverUpdate_Seek{ | ||
Seek: &ab.SeekInfo{ | ||
Start: ab.SeekInfo_NEWEST, | ||
WindowSize: r.windowSize, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func (r *deliverClient) seek(blockNumber uint64) error { | ||
return r.client.Send(&ab.DeliverUpdate{ | ||
Type: &ab.DeliverUpdate_Seek{ | ||
Seek: &ab.SeekInfo{ | ||
Start: ab.SeekInfo_SPECIFIED, | ||
SpecifiedNumber: blockNumber, | ||
WindowSize: r.windowSize, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func (r *deliverClient) readUntilClose() { | ||
for { | ||
msg, err := r.client.Recv() | ||
if err != nil { | ||
return | ||
} | ||
|
||
switch t := msg.Type.(type) { | ||
case *ab.DeliverResponse_Error: | ||
if t.Error == ab.Status_SUCCESS { | ||
fmt.Println("ERROR! Received success in error field") | ||
return | ||
} | ||
fmt.Println("Got error ", t) | ||
case *ab.DeliverResponse_Block: | ||
fmt.Println("Received block: ", t.Block) | ||
r.unAcknowledged++ | ||
if r.unAcknowledged >= r.windowSize/2 { | ||
fmt.Println("Sending acknowledgement") | ||
err = r.client.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Acknowledgement{Acknowledgement: &ab.Acknowledgement{Number: t.Block.Number}}}) | ||
if err != nil { | ||
return | ||
} | ||
r.unAcknowledged = 0 | ||
} | ||
default: | ||
fmt.Println("Received unknock: ", t) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
serverAddr := "127.0.0.1:5005" | ||
conn, err := grpc.Dial(serverAddr, grpc.WithInsecure()) | ||
if err != nil { | ||
fmt.Println("Error connecting:", err) | ||
return | ||
} | ||
client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO()) | ||
if err != nil { | ||
fmt.Println("Error connecting:", err) | ||
return | ||
} | ||
|
||
s := newDeliverClient(client, 10) | ||
s.seekOldest() | ||
s.readUntilClose() | ||
|
||
} |
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,112 @@ | ||
/* | ||
Copyright IBM Corp. 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 solo | ||
|
||
import ( | ||
"time" | ||
|
||
ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" | ||
) | ||
|
||
type broadcastServer struct { | ||
queue chan *ab.BroadcastMessage | ||
batchSize int | ||
batchTimeout time.Duration | ||
rl *ramLedger | ||
exitChan chan struct{} | ||
} | ||
|
||
func newBroadcastServer(queueSize, batchSize int, batchTimeout time.Duration, rs *ramLedger) *broadcastServer { | ||
bs := newPlainBroadcastServer(queueSize, batchSize, batchTimeout, rs) | ||
bs.exitChan = make(chan struct{}) | ||
go bs.main() | ||
return bs | ||
} | ||
|
||
func newPlainBroadcastServer(queueSize, batchSize int, batchTimeout time.Duration, rl *ramLedger) *broadcastServer { | ||
bs := &broadcastServer{ | ||
queue: make(chan *ab.BroadcastMessage, queueSize), | ||
batchSize: batchSize, | ||
batchTimeout: batchTimeout, | ||
rl: rl, | ||
} | ||
return bs | ||
} | ||
|
||
func (bs *broadcastServer) halt() { | ||
close(bs.exitChan) | ||
} | ||
|
||
func (bs *broadcastServer) main() { | ||
var curBatch []*ab.BroadcastMessage | ||
outer: | ||
for { | ||
timer := time.After(bs.batchTimeout) | ||
select { | ||
case msg := <-bs.queue: | ||
curBatch = append(curBatch, msg) | ||
if len(curBatch) < bs.batchSize { | ||
continue | ||
} | ||
logger.Debugf("Batch size met, creating block") | ||
case <-timer: | ||
if len(curBatch) == 0 { | ||
continue outer | ||
} | ||
logger.Debugf("Batch timer expired, creating block") | ||
case <-bs.exitChan: | ||
logger.Debugf("Exiting") | ||
return | ||
} | ||
|
||
block := &ab.Block{ | ||
Number: bs.rl.newest.block.Number + 1, | ||
PrevHash: bs.rl.newest.block.Hash(), | ||
Messages: curBatch, | ||
} | ||
curBatch = nil | ||
|
||
bs.rl.appendBlock(block) | ||
} | ||
} | ||
|
||
func (bs *broadcastServer) handleBroadcast(srv ab.AtomicBroadcast_BroadcastServer) error { | ||
for { | ||
msg, err := srv.Recv() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if msg.Data == nil { | ||
err = srv.Send(&ab.BroadcastResponse{ab.Status_BAD_REQUEST}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
select { | ||
case bs.queue <- msg: | ||
err = srv.Send(&ab.BroadcastResponse{ab.Status_SUCCESS}) | ||
default: | ||
err = srv.Send(&ab.BroadcastResponse{ab.Status_SERVICE_UNAVAILABLE}) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
} |
Oops, something went wrong.