Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 6265e33

Browse files
committed
[FAB-5201] Refactor SendTransactionProposal
- SendTransactionProposal and QueryByChaincode take struct parameters - SendTransactionProposal creates and sends in one step - TransactionID struct used (and returned) Change-Id: I41bc22d207c0b15d0ad449edf3f28bbba489965e Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent ab834b8 commit 6265e33

35 files changed

+764
-459
lines changed

api/apifabclient/channel.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@ type Channel interface {
3030
Name() string
3131
Initialize(data []byte) error
3232
IsInitialized() bool
33-
IsSecurityEnabled() bool
34-
QueryExtensionInterface() ChannelExtension
3533
LoadConfigUpdateEnvelope(data []byte) error
36-
SendInstantiateProposal(chaincodeName string, args []string, chaincodePath string, chaincodeVersion string, targets []txn.ProposalProcessor) ([]*txn.TransactionProposalResponse, string, error)
37-
38-
// TCerts
39-
TCertBatchSize() int
40-
SetTCertBatchSize(batchSize int)
34+
SendInstantiateProposal(chaincodeName string, args []string, chaincodePath string, chaincodeVersion string, targets []txn.ProposalProcessor) ([]*txn.TransactionProposalResponse, txn.TransactionID, error)
4135

4236
// Network
4337
// TODO: Use PeerEndorser
@@ -66,18 +60,8 @@ type Channel interface {
6660
QueryBlockByHash(blockHash []byte) (*common.Block, error)
6761
QueryTransaction(transactionID string) (*pb.ProcessedTransaction, error)
6862
QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, error)
69-
QueryByChaincode(chaincodeName string, args []string, targets []txn.ProposalProcessor) ([][]byte, error)
70-
}
71-
72-
// The ChannelExtension interface allows extensions of the SDK to add functionality to Channel overloads.
73-
type ChannelExtension interface {
74-
ClientContext() FabricClient
75-
76-
SignPayload(payload []byte) (*SignedEnvelope, error)
77-
BroadcastEnvelope(envelope *SignedEnvelope) ([]*txn.TransactionResponse, error)
78-
79-
// TODO: This should go somewhere else - see TransactionProposal.GetBytes(). - deprecated
80-
ProposalBytes(tp *txn.TransactionProposal) ([]byte, error)
63+
QueryByChaincode(txn.ChaincodeInvokeRequest) ([][]byte, error)
64+
QueryBySystemChaincode(request txn.ChaincodeInvokeRequest) ([][]byte, error)
8165
}
8266

8367
// OrgAnchorPeer contains information about an anchor peer on this channel
@@ -89,14 +73,12 @@ type OrgAnchorPeer struct {
8973

9074
// GenesisBlockRequest ...
9175
type GenesisBlockRequest struct {
92-
TxID string
93-
Nonce []byte
76+
TxnID txn.TransactionID
9477
}
9578

9679
// JoinChannelRequest allows a set of peers to transact on a channel on the network
9780
type JoinChannelRequest struct {
9881
Targets []Peer
9982
GenesisBlock *common.Block
100-
TxID string
101-
Nonce []byte
83+
TxnID txn.TransactionID
10284
}

api/apifabclient/event.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package apifabclient
88

99
import (
10+
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1011
common "github.com/hyperledger/fabric/protos/common"
1112
ehpb "github.com/hyperledger/fabric/protos/peer"
1213
pb "github.com/hyperledger/fabric/protos/peer"
@@ -20,8 +21,8 @@ type EventHub interface {
2021
Disconnect()
2122
RegisterChaincodeEvent(ccid string, eventname string, callback func(*ChaincodeEvent)) *ChainCodeCBE
2223
UnregisterChaincodeEvent(cbe *ChainCodeCBE)
23-
RegisterTxEvent(txID string, callback func(string, pb.TxValidationCode, error))
24-
UnregisterTxEvent(txID string)
24+
RegisterTxEvent(txnID apitxn.TransactionID, callback func(string, pb.TxValidationCode, error))
25+
UnregisterTxEvent(txnID apitxn.TransactionID)
2526
RegisterBlockEvent(callback func(*common.Block))
2627
UnregisterBlockEvent(callback func(*common.Block))
2728
}

api/apifabclient/fabricclient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type FabricClient interface {
5151
GetUserContext() User
5252
SetUserContext(user User)
5353
GetConfig() config.Config // TODO: refactor to a fab client config interface
54+
NewTxnID() (txn.TransactionID, error)
5455
}
5556

5657
// CreateChannelRequest requests channel creation on the network

api/apitxn/proposer.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,30 @@ type ProposalProcessor interface {
1616
ProcessTransactionProposal(proposal TransactionProposal) (TransactionProposalResult, error)
1717
}
1818

19+
// ProposalSender provides the ability for a transaction proposal to be created and sent.
20+
type ProposalSender interface {
21+
SendTransactionProposal(ChaincodeInvokeRequest) ([]*TransactionProposalResponse, TransactionID, error)
22+
}
23+
24+
// TransactionID contains the ID of a Fabric Transaction Proposal
25+
type TransactionID struct {
26+
ID string
27+
Nonce []byte
28+
}
29+
30+
// ChaincodeInvokeRequest contains the parameters for sending a transaction proposal.
31+
type ChaincodeInvokeRequest struct {
32+
Targets []ProposalProcessor
33+
ChaincodeID string
34+
TxnID TransactionID // TODO: does it make sense to include the TxnID in the request?
35+
TransientMap map[string][]byte
36+
Fcn string
37+
Args []string
38+
}
39+
1940
// TransactionProposal requests simulation of a proposed transaction from transaction processors.
2041
type TransactionProposal struct {
21-
TransactionID string
42+
TxnID TransactionID
2243

2344
SignedProposal *pb.SignedProposal
2445
Proposal *pb.Proposal

api/apitxn/sender.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ type Sender interface {
1717
SendTransaction(tx *Transaction) ([]*TransactionResponse, error)
1818
}
1919

20-
// ProposalSender provides the ability for a transaction proposal to be created and sent.
21-
type ProposalSender interface {
22-
CreateTransactionProposal(chaincodeName string, args []string, sign bool, transientData map[string][]byte) (*TransactionProposal, error)
23-
SendTransactionProposal(proposal *TransactionProposal, retry int, targets []ProposalProcessor) ([]*TransactionProposalResponse, error)
24-
}
25-
2620
// The Transaction object created from an endorsed proposal.
2721
type Transaction struct {
2822
Proposal *TransactionProposal

pkg/fabric-client/channel/block.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func (c *Channel) GenesisBlock(request *fab.GenesisBlockRequest) (*common.Block,
3434
return nil, fmt.Errorf("GenesisBlock - error: Missing orderer assigned to this channel for the GenesisBlock request")
3535
}
3636
// verify that we have transaction id
37-
if request.TxID == "" {
37+
if request.TxnID.ID == "" {
3838
return nil, fmt.Errorf("GenesisBlock - error: Missing txId input parameter with the required transaction identifier")
3939
}
4040
// verify that we have the nonce
41-
if request.Nonce == nil {
41+
if request.TxnID.Nonce == nil {
4242
return nil, fmt.Errorf("GenesisBlock - error: Missing nonce input parameter with the required single use number")
4343
}
4444

@@ -57,11 +57,11 @@ func (c *Channel) GenesisBlock(request *fab.GenesisBlockRequest) (*common.Block,
5757
Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
5858
}
5959
protos_utils.MakeChannelHeader(common.HeaderType_DELIVER_SEEK_INFO, 1, c.Name(), 0)
60-
seekInfoHeader, err := BuildChannelHeader(common.HeaderType_DELIVER_SEEK_INFO, c.Name(), request.TxID, 0, "", time.Now())
60+
seekInfoHeader, err := BuildChannelHeader(common.HeaderType_DELIVER_SEEK_INFO, c.Name(), request.TxnID.ID, 0, "", time.Now())
6161
if err != nil {
6262
return nil, fmt.Errorf("Error building channel header: %v", err)
6363
}
64-
seekHeader, err := fc.BuildHeader(creator, seekInfoHeader, request.Nonce)
64+
seekHeader, err := fc.BuildHeader(creator, seekInfoHeader, request.TxnID.Nonce)
6565
if err != nil {
6666
return nil, fmt.Errorf("Error building header: %v", err)
6767
}

pkg/fabric-client/channel/block_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111

1212
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
13-
fc "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/internal"
13+
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1414
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
1515
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/peer"
1616
)
@@ -22,12 +22,13 @@ func TestGenesisBlock(t *testing.T) {
2222
peers = append(peers, peer)
2323
orderer := mocks.NewMockOrderer("", nil)
2424
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(mocks.NewSimpleMockError())
25-
nonce, _ := fc.GenerateRandomNonce()
26-
txID, _ := fc.ComputeTxID(nonce, []byte("testID"))
25+
txid, _ := channel.ClientContext().NewTxnID()
26+
badtxid := apitxn.TransactionID{
27+
ID: txid.ID,
28+
}
2729

2830
genesisBlockReq := &fab.GenesisBlockRequest{
29-
TxID: txID,
30-
Nonce: nonce,
31+
TxnID: txid,
3132
}
3233

3334
channel.AddOrderer(orderer)
@@ -65,7 +66,7 @@ func TestGenesisBlock(t *testing.T) {
6566
}
6667

6768
genesisBlockReq = &fab.GenesisBlockRequest{
68-
TxID: txID,
69+
TxnID: badtxid,
6970
}
7071
_, err = channel.GenesisBlock(genesisBlockReq)
7172

@@ -76,8 +77,7 @@ func TestGenesisBlock(t *testing.T) {
7677
channel.RemoveOrderer(orderer)
7778

7879
genesisBlockReq = &fab.GenesisBlockRequest{
79-
TxID: txID,
80-
Nonce: nonce,
80+
TxnID: txid,
8181
}
8282

8383
_, err = channel.GenesisBlock(genesisBlockReq)

pkg/fabric-client/channel/channel.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"fmt"
1111

1212
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
13+
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
14+
"github.com/hyperledger/fabric/bccsp"
1315
"github.com/hyperledger/fabric/msp"
1416
"github.com/op/go-logging"
1517
)
@@ -22,14 +24,21 @@ type Channel struct {
2224
name string // aka channel ID
2325
securityEnabled bool // Security enabled flag
2426
peers map[string]fab.Peer
25-
// TODO proposalProcessors map[string]txn.ProposalProcessor
26-
tcertBatchSize int // The number of tcerts to get in each batch
27-
orderers map[string]fab.Orderer
28-
clientContext fab.FabricClient
29-
primaryPeer fab.Peer
30-
mspManager msp.MSPManager
31-
anchorPeers []*fab.OrgAnchorPeer
32-
initialized bool
27+
orderers map[string]fab.Orderer
28+
clientContext ClientContext
29+
primaryPeer fab.Peer
30+
mspManager msp.MSPManager
31+
anchorPeers []*fab.OrgAnchorPeer
32+
initialized bool
33+
}
34+
35+
// ClientContext ...
36+
type ClientContext interface {
37+
GetUserContext() fab.User
38+
GetIdentity() ([]byte, error)
39+
GetCryptoSuite() bccsp.BCCSP
40+
NewTxnID() (apitxn.TransactionID, error)
41+
// TODO: ClientContext.IsSecurityEnabled()
3342
}
3443

3544
// NewChannel represents a channel in a Fabric network.
@@ -46,19 +55,14 @@ func NewChannel(name string, client fab.FabricClient) (*Channel, error) {
4655
p := make(map[string]fab.Peer)
4756
o := make(map[string]fab.Orderer)
4857
c := Channel{name: name, securityEnabled: client.GetConfig().IsSecurityEnabled(), peers: p,
49-
tcertBatchSize: client.GetConfig().TcertBatchSize(), orderers: o, clientContext: client, mspManager: msp.NewMSPManager()}
58+
orderers: o, clientContext: client, mspManager: msp.NewMSPManager()}
5059
logger.Infof("Constructed channel instance: %v", c)
5160

5261
return &c, nil
5362
}
5463

55-
// QueryExtensionInterface ... TODO.
56-
func (c *Channel) QueryExtensionInterface() fab.ChannelExtension {
57-
return c
58-
}
59-
6064
// ClientContext returns the Client that was passed in to NewChannel
61-
func (c *Channel) ClientContext() fab.FabricClient {
65+
func (c *Channel) ClientContext() ClientContext {
6266
return c.clientContext
6367
}
6468

@@ -67,21 +71,6 @@ func (c *Channel) Name() string {
6771
return c.name
6872
}
6973

70-
// IsSecurityEnabled determine if security is enabled.
71-
func (c *Channel) IsSecurityEnabled() bool {
72-
return c.securityEnabled
73-
}
74-
75-
// TCertBatchSize gets the tcert batch size.
76-
func (c *Channel) TCertBatchSize() int {
77-
return c.tcertBatchSize
78-
}
79-
80-
// SetTCertBatchSize sets the tcert batch size.
81-
func (c *Channel) SetTCertBatchSize(batchSize int) {
82-
c.tcertBatchSize = batchSize
83-
}
84-
8574
// AddPeer adds a peer endpoint to channel.
8675
// It returns error if the peer with that url already exists.
8776
func (c *Channel) AddPeer(peer fab.Peer) error {

pkg/fabric-client/channel/channel_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,12 @@ func setupTestChannel() (*Channel, error) {
298298
user := mocks.NewMockUser("test")
299299
cryptoSuite := &mocks.MockCryptoSuite{}
300300
client.SaveUserToStateStore(user, true)
301+
client.SetUserContext(user)
301302
client.SetCryptoSuite(cryptoSuite)
302303
return NewChannel("testChannel", client)
303304
}
304305

305-
func setupMassiveTestChannel(numberOfPeers int, numberOfOrderers int) (fab.Channel, error) {
306+
func setupMassiveTestChannel(numberOfPeers int, numberOfOrderers int) (*Channel, error) {
306307
channel, error := setupTestChannel()
307308
if error != nil {
308309
return channel, error

pkg/fabric-client/channel/config_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ func TestChannelConfigs(t *testing.T) {
2222

2323
channel, _ := NewChannel("testChannel", client)
2424

25-
if client.GetConfig().IsSecurityEnabled() != channel.IsSecurityEnabled() {
26-
t.Fatal("Is Security Enabled flag is incorrect in channel")
27-
}
28-
29-
if client.GetConfig().TcertBatchSize() != channel.TCertBatchSize() {
30-
t.Fatal("Tcert batch size is incorrect")
31-
}
32-
33-
channel.SetTCertBatchSize(22)
34-
35-
if channel.TCertBatchSize() != 22 {
36-
t.Fatal("TCert batch size update on channel is not working")
37-
}
38-
3925
if channel.IsReadonly() {
4026
//TODO: Rightnow it is returning false always, need to revisit test once actual implementation is provided
4127
t.Fatal("Is Readonly test failed")
@@ -46,10 +32,6 @@ func TestChannelConfigs(t *testing.T) {
4632
t.Fatal("UpdateChannel test failed")
4733
}
4834

49-
if channel.QueryExtensionInterface().ClientContext() != client {
50-
t.Fatal("Client context not matching with client")
51-
}
52-
5335
channel.SetMSPManager(nil)
5436

5537
}

0 commit comments

Comments
 (0)