Skip to content

Commit

Permalink
[FAB-1351] New chain config client for Kafka
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1351

As things stand right now, the configuration transaction posted by the
broadcast_config sample client only works for solo. This is because the
genesis block for the Kafka orderer contains extra keys.

This changeset:

1. Introduces an extra flag that allows us to specify the consenter
type, and then, by using the provisional bootstrapper, it creates the
appropriate configuration transaction.

2. Refactors/simplifies the client to bring it more in line with the rest
of the sample_clients in the `orderer` package and make future
maintenance easier.

Change-Id: I61ce5dfb36a11ab58d41a65267768cf0948263dc
Signed-off-by: Kostas Christidis <kostas@christidis.io>
  • Loading branch information
kchristidis committed Dec 16, 2016
1 parent 89f9a10 commit 6b1b603
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 167 deletions.
61 changes: 0 additions & 61 deletions orderer/sample_clients/broadcast_config/broadcast.go

This file was deleted.

108 changes: 108 additions & 0 deletions orderer/sample_clients/broadcast_config/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
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 (
"context"
"flag"
"fmt"

"google.golang.org/grpc"

"github.com/hyperledger/fabric/orderer/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
)

var conf *config.TopLevel

type broadcastClient struct {
ab.AtomicBroadcast_BroadcastClient
}

func (bc *broadcastClient) broadcast(env *cb.Envelope) error {
var err error
var resp *ab.BroadcastResponse

err = bc.Send(env)
if err != nil {
return err
}

resp, err = bc.Recv()
if err != nil {
return err
}

fmt.Println("Status:", resp)
return nil
}

// cmdImpl holds the command and its arguments.
type cmdImpl struct {
name string
args argsImpl
}

// argsImpl holds all the possible arguments for all possible commands.
type argsImpl struct {
consensusType string
creationPolicy string
chainID string
}

func init() {
conf = config.Load()
}

func main() {
cmd := new(cmdImpl)
var srv string

flag.StringVar(&srv, "server", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort), "The RPC server to connect to.")
flag.StringVar(&cmd.name, "cmd", "newChain", "The action that this client is requesting via the config transaction.")
flag.StringVar(&cmd.args.consensusType, "consensusType", conf.General.OrdererType, "In case of a newChain command, the type of consensus the ordering service is running on.")
flag.StringVar(&cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", "In case of a newChain command, the chain creation policy this request should be validated against.")
flag.StringVar(&cmd.args.chainID, "chainID", "NewChainID", "In case of a newChain command, the chain ID to create.")
flag.Parse()

conn, err := grpc.Dial(srv, grpc.WithInsecure())
defer func() {
_ = 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
}

bc := &broadcastClient{client}

switch cmd.name {
case "newChain":
env := newChainRequest(cmd.args.consensusType, cmd.args.creationPolicy, cmd.args.chainID)
fmt.Println("Requesting the creation of chain", cmd.args.chainID)
fmt.Println(bc.broadcast(env))
default:
panic("Invalid command given")
}
}
98 changes: 0 additions & 98 deletions orderer/sample_clients/broadcast_config/main.go

This file was deleted.

11 changes: 3 additions & 8 deletions orderer/sample_clients/broadcast_config/newchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@ package main

import (
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
"github.com/hyperledger/fabric/orderer/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)

var genesisBlock *cb.Block

func init() {
genesisBlock = provisional.New(config.Load()).GenesisBlock()
}

func newChainRequest(creationPolicy, newChainID string) *cb.Envelope {
func newChainRequest(consensusType, creationPolicy, newChainID string) *cb.Envelope {
conf.General.OrdererType = consensusType
genesisBlock := provisional.New(conf).GenesisBlock()
oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)
Expand Down

0 comments on commit 6b1b603

Please sign in to comment.