Skip to content

Commit

Permalink
[FAB-8390] Refactor proposal creation to inject txn ID
Browse files Browse the repository at this point in the history
Change-Id: I7eb7335e1be31b73b7481408dec3bd7306884c1d
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Feb 20, 2018
1 parent 75c84d2 commit 830f8ca
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 43 deletions.
7 changes: 5 additions & 2 deletions api/apifabclient/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ type ProposalProcessor interface {
//
// TODO: CreateChaincodeInvokeProposal should be refactored as it is mostly a factory method.
type ProposalSender interface {
CreateTransactionID() (TransactionID, error)
CreateChaincodeInvokeProposal(ChaincodeInvokeRequest) (*TransactionProposal, error)
SendTransactionProposal(*TransactionProposal, []ProposalProcessor) ([]*TransactionProposalResponse, error)
}

// TransactionID contains the ID of a Fabric Transaction Proposal
// TODO: change to interface?
type TransactionID struct {
ID string
Nonce []byte
ID string
Creator []byte
Nonce []byte
}

// ChaincodeInvokeRequest contains the parameters for sending a transaction proposal.
Expand Down
14 changes: 12 additions & 2 deletions pkg/fabric-client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,12 @@ func (c *Channel) SendInstantiateProposal(chaincodeName string,
CollConfig: collConfig,
}

tp, err := CreateChaincodeDeployProposal(c.clientContext, InstantiateChaincode, c.name, cp)
txid, err := txn.NewID(c.clientContext)
if err != nil {
return nil, fab.TransactionID{}, errors.WithMessage(err, "create transaction ID failed")
}

tp, err := CreateChaincodeDeployProposal(txid, InstantiateChaincode, c.name, cp)
if err != nil {
return nil, fab.TransactionID{}, errors.WithMessage(err, "creation of chaincode proposal failed")
}
Expand Down Expand Up @@ -517,7 +522,12 @@ func (c *Channel) SendUpgradeProposal(chaincodeName string,
Policy: chaincodePolicy,
}

tp, err := CreateChaincodeDeployProposal(c.clientContext, UpgradeChaincode, c.name, cp)
txid, err := txn.NewID(c.clientContext)
if err != nil {
return nil, fab.TransactionID{}, errors.WithMessage(err, "create transaction ID failed")
}

tp, err := CreateChaincodeDeployProposal(txid, UpgradeChaincode, c.name, cp)
if err != nil {
return nil, fab.TransactionID{}, errors.WithMessage(err, "creation of chaincode proposal failed")
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/fabric-client/channel/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ func collectProposalResponses(tprs []*fab.TransactionProposalResponse) [][]byte
}

func queryChaincode(ctx fab.Context, channel string, request fab.ChaincodeInvokeRequest, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error) {
tp, err := txn.CreateChaincodeInvokeProposal(ctx, channel, request)
txid, err := txn.NewID(ctx)
if err != nil {
return nil, errors.WithMessage(err, "creation of transaction ID failed")
}

tp, err := txn.CreateChaincodeInvokeProposal(txid, channel, request)
if err != nil {
return nil, errors.WithMessage(err, "NewProposal failed")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/fabric-client/channel/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ChaincodeDeployRequest struct {
}

// CreateChaincodeDeployProposal creates an instantiate or upgrade chaincode proposal.
func CreateChaincodeDeployProposal(ctx fab.Context, deploy ChaincodeProposalType, channelID string, chaincode ChaincodeDeployRequest) (*fab.TransactionProposal, error) {
func CreateChaincodeDeployProposal(txid fab.TransactionID, deploy ChaincodeProposalType, channelID string, chaincode ChaincodeDeployRequest) (*fab.TransactionProposal, error) {

// Generate arguments for deploy (channel, marshaled CCDS, marshaled chaincode policy, marshaled collection policy)
args := [][]byte{}
Expand Down Expand Up @@ -87,5 +87,6 @@ func CreateChaincodeDeployProposal(ctx fab.Context, deploy ChaincodeProposalType
Fcn: fcn,
Args: args,
}
return txn.CreateChaincodeInvokeProposal(ctx, channelID, cir)

return txn.CreateChaincodeInvokeProposal(txid, channelID, cir)
}
22 changes: 19 additions & 3 deletions pkg/fabric-client/channel/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,38 @@ func orderersByTarget(ctx fab.Context) (map[string]apiconfig.OrdererConfig, erro
return ordererDict, nil
}

// CreateChaincodeInvokeProposal creates a Transaction Proposal based on the current context and channel config.
// CreateTransactionID creates a Transaction ID based on the current context.
func (t *Transactor) CreateTransactionID() (fab.TransactionID, error) {
txid, err := txn.NewID(t.ctx)
if err != nil {
return fab.TransactionID{}, errors.WithMessage(err, "new transaction ID failed")
}

return txid, nil
}

// CreateChaincodeInvokeProposal creates a Transaction Proposal based on the current context and channel ID.
func (t *Transactor) CreateChaincodeInvokeProposal(request fab.ChaincodeInvokeRequest) (*fab.TransactionProposal, error) {
tp, err := txn.CreateChaincodeInvokeProposal(t.ctx, t.ChannelID, request)
txid, err := t.CreateTransactionID()
if err != nil {
return nil, errors.WithMessage(err, "create transaction ID failed")
}

tp, err := txn.CreateChaincodeInvokeProposal(txid, t.ChannelID, request)
if err != nil {
return nil, errors.WithMessage(err, "new transaction proposal failed")
}

return tp, nil
}

// SendTransactionProposal ...
// SendTransactionProposal sends a TransactionProposal to the target peers.
func (t *Transactor) SendTransactionProposal(proposal *fab.TransactionProposal, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error) {
return txn.SendProposal(t.ctx, proposal, targets)
}

// CreateTransaction create a transaction with proposal response.
// TODO: should this be removed as it is purely a wrapper?
func (t *Transactor) CreateTransaction(request fab.TransactionRequest) (*fab.Transaction, error) {
return txn.New(request)
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/fabric-client/channel/transactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"

"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/txn"
"github.com/stretchr/testify/assert"
)

func TestCreateTxnID(t *testing.T) {
transactor := createTransactor(t)
createTxnID(t, transactor)
}

func TestTransactionProposal(t *testing.T) {
transactor := createTransactor(t)
tp := createTransactionProposal(t, transactor)
Expand All @@ -30,7 +36,7 @@ func TestTransaction(t *testing.T) {
Proposal: tp,
ProposalResponses: tpr,
}
tx, err := transactor.CreateTransaction(request)
tx, err := txn.New(request)
assert.Nil(t, err)

_, err = transactor.SendTransaction(tx)
Expand All @@ -46,7 +52,7 @@ func TestTransactionBadStatus(t *testing.T) {
Proposal: tp,
ProposalResponses: tpr,
}
_, err := transactor.CreateTransaction(request)
_, err := txn.New(request)
assert.NotNil(t, err)
}

Expand All @@ -63,12 +69,21 @@ func createTransactor(t *testing.T) apifabclient.Transactor {
return transactor
}

func createTxnID(t *testing.T, transactor apifabclient.Transactor) apifabclient.TransactionID {
txid, err := transactor.CreateTransactionID()
assert.Nil(t, err, "creation of transaction ID failed")

return txid
}

func createTransactionProposal(t *testing.T, transactor apifabclient.Transactor) *apifabclient.TransactionProposal {
request := apifabclient.ChaincodeInvokeRequest{
ChaincodeID: "example",
Fcn: "fcn",
}
tp, err := transactor.CreateChaincodeInvokeProposal(request)

txid := createTxnID(t, transactor)
tp, err := txn.CreateChaincodeInvokeProposal(txid, "testChannel", request)
assert.Nil(t, err)

assert.NotEmpty(t, tp.TxnID.ID)
Expand Down
5 changes: 3 additions & 2 deletions pkg/fabric-client/resource/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ChaincodePackage struct {
}

// CreateChaincodeInstallProposal creates an install chaincode proposal.
func CreateChaincodeInstallProposal(ctx fab.Context, request ChaincodeInstallRequest) (*fab.TransactionProposal, error) {
func CreateChaincodeInstallProposal(txid fab.TransactionID, request ChaincodeInstallRequest) (*fab.TransactionProposal, error) {

// Generate arguments for install
args := [][]byte{}
Expand All @@ -61,7 +61,8 @@ func CreateChaincodeInstallProposal(ctx fab.Context, request ChaincodeInstallReq
Fcn: "install",
Args: args,
}
return txn.CreateChaincodeInvokeProposal(ctx, "", cir)

return txn.CreateChaincodeInvokeProposal(txid, "", cir)
}

// CreateConfigSignature creates a ConfigSignature for the current context.
Expand Down
5 changes: 4 additions & 1 deletion pkg/fabric-client/resource/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func TestCreateChaincodeInstallProposal(t *testing.T) {
Package: &ChaincodePackage{},
}

prop, err := CreateChaincodeInstallProposal(c.clientContext, request)
txid, err := txn.NewID(c.clientContext)
assert.Nil(t, err, "create transaction ID failed")

prop, err := CreateChaincodeInstallProposal(txid, request)
assert.Nil(t, err, "CreateChaincodeInstallProposal failed")

_, err = txn.SendProposal(c.clientContext, prop, []fab.ProposalProcessor{&peer})
Expand Down
16 changes: 13 additions & 3 deletions pkg/fabric-client/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,14 @@ func (c *Resource) InstallChaincode(req fab.InstallChaincodeRequest) ([]*fab.Tra
},
}

prop, err := CreateChaincodeInstallProposal(c.clientContext, propReq)
txid, err := txn.NewID(c.clientContext)
if err != nil {
return nil, "", errors.Wrap(err, "creation of install chaincode proposal failed")
return nil, "", errors.WithMessage(err, "create transaction ID failed")
}

prop, err := CreateChaincodeInstallProposal(txid, propReq)
if err != nil {
return nil, "", errors.WithMessage(err, "creation of install chaincode proposal failed")
}

transactionProposalResponse, err := txn.SendProposal(c.clientContext, prop, req.Targets)
Expand Down Expand Up @@ -372,7 +377,12 @@ func (c *Resource) queryChaincodeWithTarget(request fab.ChaincodeInvokeRequest,

targets := []fab.ProposalProcessor{target}

tp, err := txn.CreateChaincodeInvokeProposal(c.clientContext, systemChannel, request)
txid, err := txn.NewID(c.clientContext)
if err != nil {
return nil, errors.WithMessage(err, "create transaction ID failed")
}

tp, err := txn.CreateChaincodeInvokeProposal(txid, systemChannel, request)
if err != nil {
return nil, errors.WithMessage(err, "NewProposal failed")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/fabric-client/txn/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func NewID(ctx fab.Context) (fab.TransactionID, error) {
}

txnID := fab.TransactionID{
ID: id,
Nonce: nonce,
ID: id,
Creator: creator,
Nonce: nonce,
}

return txnID, nil
Expand Down
15 changes: 2 additions & 13 deletions pkg/fabric-client/txn/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// CreateChaincodeInvokeProposal creates a proposal for transaction.
func CreateChaincodeInvokeProposal(ctx fab.Context, channelID string, request fab.ChaincodeInvokeRequest) (*fab.TransactionProposal, error) {
func CreateChaincodeInvokeProposal(txid fab.TransactionID, channelID string, request fab.ChaincodeInvokeRequest) (*fab.TransactionProposal, error) {
if request.ChaincodeID == "" {
return nil, errors.New("ChaincodeID is required")
}
Expand All @@ -30,11 +30,6 @@ func CreateChaincodeInvokeProposal(ctx fab.Context, channelID string, request fa
return nil, errors.New("Fcn is required")
}

txid, err := NewID(ctx)
if err != nil {
return nil, errors.WithMessage(err, "unable to create a transaction ID")
}

// Add function name to arguments
argsArray := make([][]byte, len(request.Args)+1)
argsArray[0] = []byte(request.Fcn)
Expand All @@ -47,13 +42,7 @@ func CreateChaincodeInvokeProposal(ctx fab.Context, channelID string, request fa
Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: request.ChaincodeID},
Input: &pb.ChaincodeInput{Args: argsArray}}}

// create a proposal from a ChaincodeInvocationSpec
creator, err := ctx.Identity()
if err != nil {
return nil, errors.WithMessage(err, "Failed to get user context identity")
}

proposal, _, err := protos_utils.CreateChaincodeProposalWithTxIDNonceAndTransient(txid.ID, common.HeaderType_ENDORSER_TRANSACTION, channelID, ccis, txid.Nonce, creator, request.TransientMap)
proposal, _, err := protos_utils.CreateChaincodeProposalWithTxIDNonceAndTransient(txid.ID, common.HeaderType_ENDORSER_TRANSACTION, channelID, ccis, txid.Nonce, txid.Creator, request.TransientMap)
if err != nil {
return nil, errors.Wrap(err, "failed to create chaincode proposal")
}
Expand Down
27 changes: 21 additions & 6 deletions pkg/fabric-client/txn/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func TestNewTransactionProposal(t *testing.T) {
Fcn: "Hello",
}

tp, err := CreateChaincodeInvokeProposal(ctx, testChannel, request)
txid, err := NewID(ctx)
if err != nil {
t.Fatalf("create transaction ID failed: %s", err)
}

tp, err := CreateChaincodeInvokeProposal(txid, testChannel, request)
if err != nil {
t.Fatalf("Create Transaction Proposal Failed: %s", err)
}
Expand Down Expand Up @@ -67,7 +72,12 @@ func TestSendTransactionProposal(t *testing.T) {
Args: [][]byte{[]byte{1, 2, 3}},
}

tp, err := CreateChaincodeInvokeProposal(ctx, testChannel, request)
txid, err := NewID(ctx)
if err != nil {
t.Fatalf("create transaction ID failed: %s", err)
}

tp, err := CreateChaincodeInvokeProposal(txid, testChannel, request)
if err != nil {
t.Fatalf("new transaction proposal failed: %s", err)
}
Expand All @@ -93,7 +103,12 @@ func TestNewTransactionProposalParams(t *testing.T) {
Fcn: "Hello",
}

tp, err := CreateChaincodeInvokeProposal(ctx, testChannel, request)
txid, err := NewID(ctx)
if err != nil {
t.Fatalf("create transaction ID failed: %s", err)
}

tp, err := CreateChaincodeInvokeProposal(txid, testChannel, request)
if err != nil {
t.Fatalf("new transaction proposal failed: %s", err)
}
Expand All @@ -107,7 +122,7 @@ func TestNewTransactionProposalParams(t *testing.T) {
Fcn: "Hello",
}

tp, err = CreateChaincodeInvokeProposal(ctx, testChannel, request)
tp, err = CreateChaincodeInvokeProposal(txid, testChannel, request)
if err == nil {
t.Fatalf("Expected error")
}
Expand All @@ -116,7 +131,7 @@ func TestNewTransactionProposalParams(t *testing.T) {
ChaincodeID: "cc",
}

tp, err = CreateChaincodeInvokeProposal(ctx, testChannel, request)
tp, err = CreateChaincodeInvokeProposal(txid, testChannel, request)
if err == nil {
t.Fatalf("Expected error")
}
Expand All @@ -125,7 +140,7 @@ func TestNewTransactionProposalParams(t *testing.T) {
ChaincodeID: "cc",
Fcn: "Hello",
}
tp, err = CreateChaincodeInvokeProposal(ctx, testChannel, request)
tp, err = CreateChaincodeInvokeProposal(txid, testChannel, request)
if err != nil {
t.Fatalf("new transaction proposal failed: %s", err)
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/fabric-txn/mocks/mocktransactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@ type MockTransactor struct {
Orderers []fab.Orderer
}

// CreateTransactionID creates a Transaction ID based on the current context.
func (t *MockTransactor) CreateTransactionID() (fab.TransactionID, error) {
txid, err := txn.NewID(t.Ctx)
if err != nil {
return fab.TransactionID{}, errors.WithMessage(err, "new transaction ID failed")
}

return txid, nil
}

// CreateChaincodeInvokeProposal creates a Transaction Proposal based on the current context and channel config.
func (t *MockTransactor) CreateChaincodeInvokeProposal(request fab.ChaincodeInvokeRequest) (*fab.TransactionProposal, error) {
tp, err := txn.CreateChaincodeInvokeProposal(t.Ctx, t.ChannelID, request)
txid, err := t.CreateTransactionID()
if err != nil {
return nil, errors.WithMessage(err, "create transaction ID failed")
}

tp, err := txn.CreateChaincodeInvokeProposal(txid, t.ChannelID, request)
if err != nil {
return nil, errors.WithMessage(err, "new transaction proposal failed")
}

return tp, nil
}

// SendTransactionProposal ...
// SendTransactionProposal sends a TransactionProposal to the target peers.
func (t *MockTransactor) SendTransactionProposal(proposal *fab.TransactionProposal, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error) {
return txn.SendProposal(t.Ctx, proposal, targets)
}
Expand Down
Loading

0 comments on commit 830f8ca

Please sign in to comment.