diff --git a/orderer/sample_clients/bd_counter/main.go b/orderer/sample_clients/bd_counter/main.go index 444c8c4bf21..379c55d4bb9 100644 --- a/orderer/sample_clients/bd_counter/main.go +++ b/orderer/sample_clients/bd_counter/main.go @@ -27,6 +27,8 @@ import ( "google.golang.org/grpc" ) +const pkgName = "orderer/bd_counter" + var logger *logging.Logger type configImpl struct { @@ -49,7 +51,7 @@ func main() { logging.SetBackend(backend) formatter := logging.MustStringFormatter("[%{time:15:04:05}] %{shortfile:18s}: %{color}[%{level:-5s}]%{color:reset} %{message}") logging.SetFormatter(formatter) - logger = logging.MustGetLogger("orderer/bd_counter") + logger = logging.MustGetLogger(pkgName) flag.StringVar(&client.config.rpc, "rpc", "broadcast", "The RPC that this client is requesting.") @@ -78,7 +80,7 @@ func main() { conn, err := grpc.Dial(client.config.server, grpc.WithInsecure()) if err != nil { - logger.Fatalf("Client did not connect to %s: %v\n", client.config.server, err) + logger.Fatalf("Client did not connect to %s: %v", client.config.server, err) } defer conn.Close() client.rpc = ab.NewAtomicBroadcastClient(conn) diff --git a/orderer/sample_clients/broadcast_config/broadcast.go b/orderer/sample_clients/broadcast_config/broadcast.go new file mode 100644 index 00000000000..9513c34f073 --- /dev/null +++ b/orderer/sample_clients/broadcast_config/broadcast.go @@ -0,0 +1,61 @@ +/* +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" + "io" + + cb "github.com/hyperledger/fabric/protos/common" + ab "github.com/hyperledger/fabric/protos/orderer" + context "golang.org/x/net/context" +) + +func (c *clientImpl) broadcast(envelope *cb.Envelope) { + stream, err := c.rpc.Broadcast(context.Background()) + if err != nil { + panic(fmt.Errorf("Failed to invoke broadcast RPC: %s", err)) + } + go c.recvBroadcastReplies(stream) + + if err := stream.Send(envelope); err != nil { + panic(fmt.Errorf("Failed to send broadcast message to ordering service: %s", err)) + } + logger.Debugf("Sent broadcast message \"%v\" to ordering service\n", envelope) + + if err := stream.CloseSend(); err != nil { + panic(fmt.Errorf("Failed to close the send direction of the broadcast stream: %v", err)) + } + + <-c.doneChan // Wait till we've had a chance to get back a reply (or an error) + logger.Info("Client shutting down") +} + +func (c *clientImpl) recvBroadcastReplies(stream ab.AtomicBroadcast_BroadcastClient) { + defer close(c.doneChan) + for { + reply, err := stream.Recv() + if err == io.EOF { + return + } + if err != nil { + panic(fmt.Errorf("Failed to receive a broadcast reply from orderer: %v", err)) + } + logger.Info("Broadcast reply from orderer:", reply.Status.String()) + break + } +} diff --git a/orderer/sample_clients/broadcast_config/main.go b/orderer/sample_clients/broadcast_config/main.go new file mode 100644 index 00000000000..ea18f2ce152 --- /dev/null +++ b/orderer/sample_clients/broadcast_config/main.go @@ -0,0 +1,98 @@ +/* +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 ( + "flag" + "os" + "strings" + + ab "github.com/hyperledger/fabric/protos/orderer" + logging "github.com/op/go-logging" + "google.golang.org/grpc" +) + +const pkgName = "orderer/broadcast_config" + +var logger *logging.Logger + +// Include here all the possible arguments for a command +type argsImpl struct { + creationPolicy string + chainID string +} + +// This holds the command and its arguments +type cmdImpl struct { + cmd string + args argsImpl +} + +type configImpl struct { + logLevel logging.Level + server string + cmd cmdImpl +} + +type clientImpl struct { + config configImpl + rpc ab.AtomicBroadcastClient + doneChan chan struct{} +} + +func main() { + var loglevel string + + client := &clientImpl{doneChan: make(chan struct{})} + + backend := logging.NewLogBackend(os.Stderr, "", 0) + logging.SetBackend(backend) + formatter := logging.MustStringFormatter("[%{time:15:04:05}] %{shortfile:18s}: %{color}[%{level:-5s}]%{color:reset} %{message}") + logging.SetFormatter(formatter) + logger = logging.MustGetLogger(pkgName) + + flag.StringVar(&loglevel, "loglevel", "info", + "The logging level. (Suggested values: info, debug)") + flag.StringVar(&client.config.server, "server", + "127.0.0.1:7050", "The RPC server to connect to.") + flag.StringVar(&client.config.cmd.cmd, "cmd", "new-chain", + "The action that this client is requesting via the config transaction.") + flag.StringVar(&client.config.cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", + "In case of a new-chain command, the chain createion policy this request should be validated against.") + flag.StringVar(&client.config.cmd.args.chainID, "chainID", "NewChainID", + "In case of a new-chain command, the chain ID to create.") + flag.Parse() + + client.config.logLevel, _ = logging.LogLevel(strings.ToUpper(loglevel)) + logging.SetLevel(client.config.logLevel, logger.Module) + + conn, err := grpc.Dial(client.config.server, grpc.WithInsecure()) + if err != nil { + logger.Fatalf("Client did not connect to %s: %v", client.config.server, err) + } + defer conn.Close() + client.rpc = ab.NewAtomicBroadcastClient(conn) + + switch client.config.cmd.cmd { + case "new-chain": + envelope := newChainRequest(client.config.cmd.args.creationPolicy, client.config.cmd.args.chainID) + logger.Infof("Requesting the creation of chain \"%s\"", client.config.cmd.args.chainID) + client.broadcast(envelope) + default: + panic("Invalid cmd given") + } +} diff --git a/orderer/sample_clients/broadcast_config/newchain.go b/orderer/sample_clients/broadcast_config/newchain.go new file mode 100644 index 00000000000..d6069b1879d --- /dev/null +++ b/orderer/sample_clients/broadcast_config/newchain.go @@ -0,0 +1,43 @@ +/* +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 ( + "github.com/hyperledger/fabric/orderer/common/bootstrap/static" + cb "github.com/hyperledger/fabric/protos/common" + ab "github.com/hyperledger/fabric/protos/orderer" + "github.com/hyperledger/fabric/protos/utils" +) + +var genesisBlock *cb.Block + +func init() { + helper := static.New() + var err error + genesisBlock, err = helper.GenesisBlock() + if err != nil { + panic("Error retrieving static genesis block") + } +} + +func newChainRequest(creationPolicy, newChainID string) *cb.Envelope { + oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0) + oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx) + oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data) + + return ab.ChainCreationConfigurationTransaction(static.AcceptAllPolicyKey, newChainID, oldConfigEnv) +} diff --git a/orderer/sample_clients/broadcast_timestamp/client.go b/orderer/sample_clients/broadcast_timestamp/client.go index 7430b7e5360..98b5ce8bc80 100644 --- a/orderer/sample_clients/broadcast_timestamp/client.go +++ b/orderer/sample_clients/broadcast_timestamp/client.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "flag" "fmt" "time" @@ -30,19 +31,20 @@ import ( ) type broadcastClient struct { - client ab.AtomicBroadcast_BroadcastClient + client ab.AtomicBroadcast_BroadcastClient + chainID string } // newBroadcastClient creates a simple instance of the broadcastClient interface -func newBroadcastClient(client ab.AtomicBroadcast_BroadcastClient) *broadcastClient { - return &broadcastClient{client: client} +func newBroadcastClient(client ab.AtomicBroadcast_BroadcastClient, chainID string) *broadcastClient { + return &broadcastClient{client: client, chainID: chainID} } func (s *broadcastClient) broadcast(transaction []byte) error { payload, err := proto.Marshal(&cb.Payload{ Header: &cb.Header{ ChainHeader: &cb.ChainHeader{ - ChainID: static.TestChainID, + ChainID: s.chainID, }, }, Data: transaction, @@ -66,7 +68,16 @@ func (s *broadcastClient) getAck() error { func main() { config := config.Load() - serverAddr := fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort) + + var chainID string + var serverAddr string + var messages uint64 + + flag.StringVar(&serverAddr, "server", fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort), "The RPC server to connect to.") + flag.StringVar(&chainID, "chainID", static.TestChainID, "The chain ID to broadcast to.") + flag.Uint64Var(&messages, "messages", 1, "The number of messages to braodcast.") + flag.Parse() + conn, err := grpc.Dial(serverAddr, grpc.WithInsecure()) defer conn.Close() if err != nil { @@ -79,9 +90,11 @@ func main() { return } - s := newBroadcastClient(client) - s.broadcast([]byte(fmt.Sprintf("Testing %v", time.Now()))) - err = s.getAck() + s := newBroadcastClient(client, chainID) + for i := uint64(0); i < messages; i++ { + s.broadcast([]byte(fmt.Sprintf("Testing %v", time.Now()))) + err = s.getAck() + } if err != nil { fmt.Printf("\nError: %v\n", err) } diff --git a/orderer/sample_clients/deliver_stdout/client.go b/orderer/sample_clients/deliver_stdout/client.go index 0aed2690022..b82c4c91076 100644 --- a/orderer/sample_clients/deliver_stdout/client.go +++ b/orderer/sample_clients/deliver_stdout/client.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "flag" "fmt" "github.com/hyperledger/fabric/orderer/common/bootstrap/static" @@ -29,12 +30,13 @@ import ( type deliverClient struct { client ab.AtomicBroadcast_DeliverClient + chainID string windowSize uint64 unAcknowledged uint64 } -func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, windowSize uint64) *deliverClient { - return &deliverClient{client: client, windowSize: windowSize} +func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, chainID string, windowSize uint64) *deliverClient { + return &deliverClient{client: client, chainID: chainID, windowSize: windowSize} } func (r *deliverClient) seekOldest() error { @@ -43,7 +45,7 @@ func (r *deliverClient) seekOldest() error { Seek: &ab.SeekInfo{ Start: ab.SeekInfo_OLDEST, WindowSize: r.windowSize, - ChainID: static.TestChainID, + ChainID: r.chainID, }, }, }) @@ -55,7 +57,7 @@ func (r *deliverClient) seekNewest() error { Seek: &ab.SeekInfo{ Start: ab.SeekInfo_NEWEST, WindowSize: r.windowSize, - ChainID: static.TestChainID, + ChainID: r.chainID, }, }, }) @@ -68,7 +70,7 @@ func (r *deliverClient) seek(blockNumber uint64) error { Start: ab.SeekInfo_SPECIFIED, SpecifiedNumber: blockNumber, WindowSize: r.windowSize, - ChainID: static.TestChainID, + ChainID: r.chainID, }, }, }) @@ -109,7 +111,16 @@ func (r *deliverClient) readUntilClose() { func main() { config := config.Load() - serverAddr := fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort) + + var chainID string + var serverAddr string + var windowSize uint64 + + flag.StringVar(&serverAddr, "server", fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort), "The RPC server to connect to.") + flag.StringVar(&chainID, "chainID", static.TestChainID, "The chain ID to deliver from.") + flag.Uint64Var(&windowSize, "windowSize", 10, "The window size for the deliver.") + flag.Parse() + conn, err := grpc.Dial(serverAddr, grpc.WithInsecure()) if err != nil { fmt.Println("Error connecting:", err) @@ -121,7 +132,7 @@ func main() { return } - s := newDeliverClient(client, 10) + s := newDeliverClient(client, chainID, windowSize) s.seekOldest() s.readUntilClose()