Skip to content

Commit

Permalink
FAB-1230 - use **TEST_CHAINID** for tests and skeleton
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1230

Orderer sets the stage for multichain by forcing brodcast
and deliver clients to specify ChainID. The solo orderer
provides a default chain called **TEST_CHAINID** to continue
development with.

It would have been easy to hard code **TEST_CHAINID** in
the lower most utility calls to continue work - basically
inside protos/utils/ functions.

However, this changeset takes the next step fo moving to
using multichain by exposing chainID in core APIs thus
forcing higher layers to deal with chains. Currently these
high layers are unit tests, CLI and SDK.

CLI accepts chain ID via the "-C" param which when not provided
defaults to **TEST_CHAINID**.

Change-Id: I0d7894c8f17ce8fae6fe075c9865afae58499005
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Nov 30, 2016
1 parent af0cd3e commit f7b3336
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bddtests/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func createProposalForChaincode(ccChaincodeDeploymentSpec *pb.ChaincodeDeploymen
uuid := createPropsalID()

// make proposal
return putils.CreateChaincodeProposal(uuid, lcChaincodeInvocationSpec, creator)
return putils.CreateChaincodeProposal(uuid, util.GetTestChainID(), lcChaincodeInvocationSpec, creator)
}
4 changes: 2 additions & 2 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func endTxSimulationCDS(txid string, txsim ledger.TxSimulator, payload []byte, c
return err
}
// get a proposal - we need it to get a transaction
prop, err := putils.CreateProposalFromCDS(txid, cds, ss)
prop, err := putils.CreateProposalFromCDS(txid, util.GetTestChainID(), cds, ss)
if err != nil {
return err
}
Expand All @@ -143,7 +143,7 @@ func endTxSimulationCIS(txid string, txsim ledger.TxSimulator, payload []byte, c
return err
}
// get a proposal - we need it to get a transaction
prop, err := putils.CreateProposalFromCIS(txid, cis, ss)
prop, err := putils.CreateProposalFromCIS(txid, util.GetTestChainID(), cis, ss)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions core/committer/noopssinglechain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode"
"github.com/hyperledger/fabric/core/committer"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/orderer"
putils "github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -194,6 +195,7 @@ func (d *DeliverService) seekOldest() error {
Seek: &orderer.SeekInfo{
Start: orderer.SeekInfo_OLDEST,
WindowSize: d.windowSize,
ChainID: []byte(util.GetTestChainID()),
},
},
})
Expand All @@ -206,6 +208,7 @@ func (d *DeliverService) seekLatestFromCommitter(height uint64) error {
Start: orderer.SeekInfo_SPECIFIED,
WindowSize: d.windowSize,
SpecifiedNumber: height,
ChainID: []byte(util.GetTestChainID()),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion core/endorser/endorser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func closeListenerAndSleep(l net.Listener) {
//Currently supported only for Invokes (Queries still go through devops client)
func getProposal(cis *pb.ChaincodeInvocationSpec, creator []byte) (*pb.Proposal, error) {
uuid := util.GenerateUUID()
return pbutils.CreateChaincodeProposal(uuid, cis, creator)
return pbutils.CreateChaincodeProposal(uuid, util.GetTestChainID(), cis, creator)
}

//getDeployProposal gets the proposal for the chaincode deployment
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/kvledger/example/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (app *App) QueryBalances(accounts []string) ([]int, error) {
}

func constructTransaction(simulationResults []byte) *common.Envelope {
txEnv, _ := ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GenerateUUID(), "foo", simulationResults, nil, nil)
txEnv, _ := ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GenerateUUID(), util.GetTestChainID(), "foo", simulationResults, nil, nil)
return txEnv
}

Expand Down
4 changes: 2 additions & 2 deletions core/ledger/testutil/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func ConstructTestTransaction(t *testing.T, simulationResults []byte, sign bool)
ccName := "foo"
txID := util.GenerateUUID()
if sign {
return ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, ccName, simulationResults, nil, nil)
return ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, util.GetTestChainID(), ccName, simulationResults, nil, nil)
}
return ptestutils.ConstructUnsingedTxEnv(txID, ccName, simulationResults, nil, nil)
return ptestutils.ConstructUnsingedTxEnv(txID, util.GetTestChainID(), ccName, simulationResults, nil, nil)
}

// ComputeBlockHash computes the crypto-hash of a block
Expand Down
2 changes: 1 addition & 1 deletion core/peer/fullflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func getProposal() (*peer.Proposal, error) {

uuid := util.GenerateUUID()

return utils.CreateProposalFromCIS(uuid, cis, signerSerialized)
return utils.CreateProposalFromCIS(uuid, util.GetTestChainID(), cis, signerSerialized)
}

func TestGoodPath(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestInvoke(t *testing.T) {

uuid := util.GenerateUUID()

proposal, err := putils.CreateChaincodeProposal(uuid, cis, sIdBytes)
proposal, err := putils.CreateChaincodeProposal(uuid, util.GetTestChainID(), cis, sIdBytes)
if err != nil {
t.Fail()
t.Fatalf("couldn't generate chaincode proposal: err %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func createTx() (*common.Envelope, error) {

uuid := util.GenerateUUID()

prop, err := utils.CreateProposalFromCIS(uuid, cis, sid)
prop, err := utils.CreateProposalFromCIS(uuid, util.GetTestChainID(), cis, sid)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions core/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ func ArrayToChaincodeArgs(args []string) [][]byte {
}
return bargs
}

const testchainid = "**TEST_CHAINID**"

//GetTestChainID returns the CHAINID constant in use by orderer
func GetTestChainID() string {
return testchainid
}
4 changes: 4 additions & 0 deletions peer/chaincode/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package chaincode
import (
"fmt"

"github.com/hyperledger/fabric/core/util"
"github.com/hyperledger/fabric/peer/common"
"github.com/op/go-logging"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,6 +49,8 @@ func Cmd() *cobra.Command {
fmt.Sprint("Username for chaincode operations when security is enabled"))
flags.StringVarP(&customIDGenAlg, "tid", "t", common.UndefinedParamValue,
fmt.Sprint("Name of a custom ID generation algorithm (hashing and decoding) e.g. sha256base64"))
flags.StringVarP(&chainID, "chainID", "C", util.GetTestChainID(),
fmt.Sprint("The chain on which this command should be executed"))

chaincodeCmd.AddCommand(deployCmd())
chaincodeCmd.AddCommand(invokeCmd())
Expand All @@ -67,6 +70,7 @@ var (
chaincodeQueryHex bool
chaincodeAttributesJSON string
customIDGenAlg string
chainID string
)

var chaincodeCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
uuid := cutil.GenerateUUID()

var prop *pb.Proposal
prop, err = putils.CreateProposalFromCIS(uuid, invocation, creator)
prop, err = putils.CreateProposalFromCIS(uuid, chainID, invocation, creator)
if err != nil {
return fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
}
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func deploy(cmd *cobra.Command) (*protcommon.Envelope, error) {

uuid := util.GenerateUUID()

prop, err := utils.CreateProposalFromCDS(uuid, cds, creator)
prop, err := utils.CreateProposalFromCDS(uuid, chainID, cds, creator)
if err != nil {
return nil, fmt.Errorf("Error creating proposal %s: %s\n", chainFuncName, err)
}
Expand Down
12 changes: 6 additions & 6 deletions protos/testutils/txtestutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ func getMSPMgrConfigFile() (string, error) {

// ConstructSingedTxEnvWithDefaultSigner constructs a transaction envelop for tests with a default signer.
// This method helps other modules to construct a transaction with supplied parameters
func ConstructSingedTxEnvWithDefaultSigner(txid string, ccName string, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, error) {
return ConstructSingedTxEnv(txid, ccName, simulationResults, events, visibility, signer)
func ConstructSingedTxEnvWithDefaultSigner(txid string, chainID, ccName string, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, error) {
return ConstructSingedTxEnv(txid, chainID, ccName, simulationResults, events, visibility, signer)
}

// ConstructSingedTxEnv constructs a transaction envelop for tests
func ConstructSingedTxEnv(txid string, ccName string, simulationResults []byte, events []byte, visibility []byte, signer msp.SigningIdentity) (*common.Envelope, error) {
func ConstructSingedTxEnv(txid string, chainID string, ccName string, simulationResults []byte, events []byte, visibility []byte, signer msp.SigningIdentity) (*common.Envelope, error) {
ss, err := signer.Serialize()
if err != nil {
return nil, err
}

prop, err := putils.CreateChaincodeProposal(txid, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: &pb.ChaincodeID{Name: ccName}}}, ss)
prop, err := putils.CreateChaincodeProposal(txid, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: &pb.ChaincodeID{Name: ccName}}}, ss)
if err != nil {
return nil, err
}
Expand All @@ -108,8 +108,8 @@ func ConstructSingedTxEnv(txid string, ccName string, simulationResults []byte,
}

// ConstructUnsingedTxEnv creates a Transaction envelope from given inputs
func ConstructUnsingedTxEnv(txid string, ccName string, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, error) {
prop, err := putils.CreateChaincodeProposal(txid, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: &pb.ChaincodeID{Name: ccName}}}, nil)
func ConstructUnsingedTxEnv(txid string, chainID string, ccName string, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, error) {
prop, err := putils.CreateChaincodeProposal(txid, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: &pb.ChaincodeID{Name: ccName}}}, nil)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions protos/utils/proputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func GetEnvelope(bytes []byte) (*common.Envelope, error) {
}

// CreateChaincodeProposal creates a proposal from given input
func CreateChaincodeProposal(txid string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, error) {
func CreateChaincodeProposal(txid string, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, error) {
ccHdrExt := &peer.ChaincodeHeaderExtension{ChaincodeID: cis.ChaincodeSpec.ChaincodeID}
ccHdrExtBytes, err := proto.Marshal(ccHdrExt)
if err != nil {
Expand All @@ -222,6 +222,7 @@ func CreateChaincodeProposal(txid string, cis *peer.ChaincodeInvocationSpec, cre

hdr := &common.Header{ChainHeader: &common.ChainHeader{Type: int32(common.HeaderType_ENDORSER_TRANSACTION),
TxID: txid,
ChainID: []byte(chainID),
Extension: ccHdrExtBytes},
SignatureHeader: &common.SignatureHeader{Nonce: nonce, Creator: creator}}

Expand Down Expand Up @@ -372,12 +373,12 @@ func GetActionFromEnvelope(envBytes []byte) (*peer.ChaincodeAction, error) {
}

// CreateProposalFromCIS returns a proposal given a serialized identity and a ChaincodeInvocationSpec
func CreateProposalFromCIS(txid string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, error) {
return CreateChaincodeProposal(txid, cis, creator)
func CreateProposalFromCIS(txid string, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, error) {
return CreateChaincodeProposal(txid, chainID, cis, creator)
}

// CreateProposalFromCDS returns a proposal given a serialized identity and a ChaincodeDeploymentSpec
func CreateProposalFromCDS(txid string, cds *peer.ChaincodeDeploymentSpec, creator []byte) (*peer.Proposal, error) {
func CreateProposalFromCDS(txid string, chainID string, cds *peer.ChaincodeDeploymentSpec, creator []byte) (*peer.Proposal, error) {
b, err := proto.Marshal(cds)
if err != nil {
return nil, err
Expand All @@ -391,5 +392,5 @@ func CreateProposalFromCDS(txid string, cds *peer.ChaincodeDeploymentSpec, creat
CtorMsg: &peer.ChaincodeInput{Args: [][]byte{[]byte("deploy"), []byte("default"), b}}}}

//...and get the proposal for it
return CreateProposalFromCIS(txid, lcccSpec, creator)
return CreateProposalFromCIS(txid, chainID, lcccSpec, creator)
}
4 changes: 2 additions & 2 deletions protos/utils/proputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func createCIS() *pb.ChaincodeInvocationSpec {
func TestProposal(t *testing.T) {
uuid := util.GenerateUUID()
// create a proposal from a ChaincodeInvocationSpec
prop, err := CreateChaincodeProposal(uuid, createCIS(), []byte("creator"))
prop, err := CreateChaincodeProposal(uuid, util.GetTestChainID(), createCIS(), []byte("creator"))
if err != nil {
t.Fatalf("Could not create chaincode proposal, err %s\n", err)
return
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestProposalResponse(t *testing.T) {
func TestEnvelope(t *testing.T) {
// create a proposal from a ChaincodeInvocationSpec
uuid := util.GenerateUUID()
prop, err := CreateChaincodeProposal(uuid, createCIS(), signerSerialized)
prop, err := CreateChaincodeProposal(uuid, util.GetTestChainID(), createCIS(), signerSerialized)
if err != nil {
t.Fatalf("Could not create chaincode proposal, err %s\n", err)
return
Expand Down

0 comments on commit f7b3336

Please sign in to comment.