|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 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 | + "flag" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 25 | + logging "github.com/op/go-logging" |
| 26 | + "google.golang.org/grpc" |
| 27 | +) |
| 28 | + |
| 29 | +const pkgName = "orderer/broadcast_config" |
| 30 | + |
| 31 | +var logger *logging.Logger |
| 32 | + |
| 33 | +// Include here all the possible arguments for a command |
| 34 | +type argsImpl struct { |
| 35 | + creationPolicy string |
| 36 | + chainID string |
| 37 | +} |
| 38 | + |
| 39 | +// This holds the command and its arguments |
| 40 | +type cmdImpl struct { |
| 41 | + cmd string |
| 42 | + args argsImpl |
| 43 | +} |
| 44 | + |
| 45 | +type configImpl struct { |
| 46 | + logLevel logging.Level |
| 47 | + server string |
| 48 | + cmd cmdImpl |
| 49 | +} |
| 50 | + |
| 51 | +type clientImpl struct { |
| 52 | + config configImpl |
| 53 | + rpc ab.AtomicBroadcastClient |
| 54 | + doneChan chan struct{} |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + var loglevel string |
| 59 | + |
| 60 | + client := &clientImpl{doneChan: make(chan struct{})} |
| 61 | + |
| 62 | + backend := logging.NewLogBackend(os.Stderr, "", 0) |
| 63 | + logging.SetBackend(backend) |
| 64 | + formatter := logging.MustStringFormatter("[%{time:15:04:05}] %{shortfile:18s}: %{color}[%{level:-5s}]%{color:reset} %{message}") |
| 65 | + logging.SetFormatter(formatter) |
| 66 | + logger = logging.MustGetLogger(pkgName) |
| 67 | + |
| 68 | + flag.StringVar(&loglevel, "loglevel", "info", |
| 69 | + "The logging level. (Suggested values: info, debug)") |
| 70 | + flag.StringVar(&client.config.server, "server", |
| 71 | + "127.0.0.1:7050", "The RPC server to connect to.") |
| 72 | + flag.StringVar(&client.config.cmd.cmd, "cmd", "new-chain", |
| 73 | + "The action that this client is requesting via the config transaction.") |
| 74 | + flag.StringVar(&client.config.cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", |
| 75 | + "In case of a new-chain command, the chain createion policy this request should be validated against.") |
| 76 | + flag.StringVar(&client.config.cmd.args.chainID, "chainID", "NewChainID", |
| 77 | + "In case of a new-chain command, the chain ID to create.") |
| 78 | + flag.Parse() |
| 79 | + |
| 80 | + client.config.logLevel, _ = logging.LogLevel(strings.ToUpper(loglevel)) |
| 81 | + logging.SetLevel(client.config.logLevel, logger.Module) |
| 82 | + |
| 83 | + conn, err := grpc.Dial(client.config.server, grpc.WithInsecure()) |
| 84 | + if err != nil { |
| 85 | + logger.Fatalf("Client did not connect to %s: %v", client.config.server, err) |
| 86 | + } |
| 87 | + defer conn.Close() |
| 88 | + client.rpc = ab.NewAtomicBroadcastClient(conn) |
| 89 | + |
| 90 | + switch client.config.cmd.cmd { |
| 91 | + case "new-chain": |
| 92 | + envelope := newChainRequest(client.config.cmd.args.creationPolicy, client.config.cmd.args.chainID) |
| 93 | + logger.Infof("Requesting the creation of chain \"%s\"", client.config.cmd.args.chainID) |
| 94 | + client.broadcast(envelope) |
| 95 | + default: |
| 96 | + panic("Invalid cmd given") |
| 97 | + } |
| 98 | +} |
0 commit comments