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

Commit 031b888

Browse files
committed
[FAB-8211] Expose ledger from channel svc
This patch exposes the ledger interface from a client's channel service. The integration tests related to ledger queries are adjusted to use this interface. Change-Id: I6ccf8455583f5f62e8fdb1982482853aab315efd Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent ac89b89 commit 031b888

25 files changed

+532
-351
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ You're good to go, happy coding! Check out the examples for usage demonstrations
2323
### Examples
2424

2525
- [E2E Test](test/integration/e2e/end_to_end.go): Basic example that uses SDK to query and execute transaction
26+
- [Ledger Query Test](test/integration/fab/channel_ledger_test.go): Basic example that uses SDK to query a channel's underlying ledger
2627
- [Multi Org Test](test/integration/orgs/multiple_orgs_test.go): An example that has multiple organisations involved in transaction
2728
- [Dynamic Endorser Selection](test/integration/sdk/sdk_provider_test.go): An example that uses dynamic endorser selection (based on chaincode policy)
2829
- [E2E PKCS11 Test](test/integration/pkcs11/e2e_test.go): E2E Test using a PKCS11 crypto suite and configuration

api/apifabclient/channel.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ type Channel interface {
6161

6262
// ChannelLedger provides access to the underlying ledger for a channel.
6363
type ChannelLedger interface {
64-
QueryInfo(targets []ProposalProcessor) (*common.BlockchainInfo, error)
65-
QueryBlock(blockNumber int, targets []ProposalProcessor) (*common.Block, error)
66-
QueryBlockByHash(blockHash []byte, targets []ProposalProcessor) (*common.Block, error)
67-
QueryTransaction(transactionID string, targets []ProposalProcessor) (*pb.ProcessedTransaction, error)
68-
QueryInstantiatedChaincodes(targets []ProposalProcessor) (*pb.ChaincodeQueryResponse, error)
64+
QueryInfo(targets []ProposalProcessor) ([]*common.BlockchainInfo, error)
65+
QueryBlock(blockNumber int, targets []ProposalProcessor) ([]*common.Block, error)
66+
QueryBlockByHash(blockHash []byte, targets []ProposalProcessor) ([]*common.Block, error)
67+
QueryTransaction(transactionID string, targets []ProposalProcessor) ([]*pb.ProcessedTransaction, error)
68+
QueryInstantiatedChaincodes(targets []ProposalProcessor) ([]*pb.ChaincodeQueryResponse, error)
6969
}
7070

7171
// OrgAnchorPeer contains information about an anchor peer on this channel

api/apifabclient/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ChannelProvider interface {
3232
// ChannelService supplies services related to a channel.
3333
type ChannelService interface {
3434
ChannelConfig() (ChannelConfig, error)
35+
Ledger() (ChannelLedger, error)
3536
Channel() (Channel, error)
3637
EventHub() (EventHub, error) // TODO support new event delivery
3738
}

api/apifabclient/resource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
type Resource interface {
1616
CreateChannel(request CreateChannelRequest) (TransactionID, error)
1717
InstallChaincode(request InstallChaincodeRequest) ([]*TransactionProposalResponse, string, error)
18-
QueryInstalledChaincodes(peer Peer) (*pb.ChaincodeQueryResponse, error)
19-
QueryChannels(peer Peer) (*pb.ChannelQueryResponse, error)
18+
QueryInstalledChaincodes(peer ProposalProcessor) (*pb.ChaincodeQueryResponse, error)
19+
QueryChannels(peer ProposalProcessor) (*pb.ChannelQueryResponse, error)
2020

2121
GenesisBlockFromOrderer(channelName string, orderer Orderer) (*common.Block, error)
2222
JoinChannel(request JoinChannelRequest) error

pkg/fabric-client/channel/channel.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ func resolveOrdererURL(ordererURL string) string {
331331
// (height, known peers).
332332
// This query will be made to the primary peer.
333333
func (c *Channel) QueryInfo() (*common.BlockchainInfo, error) {
334-
l := NewLedger(c.clientContext, c.name)
334+
l, err := NewLedger(c.clientContext, c.name)
335+
if err != nil {
336+
return nil, errors.WithMessage(err, "ledger client creation failed")
337+
}
338+
335339
resps, err := l.QueryInfo([]fab.ProposalProcessor{c.PrimaryPeer()})
336340
if err != nil {
337341
return nil, err
@@ -343,7 +347,11 @@ func (c *Channel) QueryInfo() (*common.BlockchainInfo, error) {
343347
// This query will be made to the primary peer.
344348
// Returns the block.
345349
func (c *Channel) QueryBlockByHash(blockHash []byte) (*common.Block, error) {
346-
l := NewLedger(c.clientContext, c.name)
350+
l, err := NewLedger(c.clientContext, c.name)
351+
if err != nil {
352+
return nil, errors.WithMessage(err, "ledger client creation failed")
353+
}
354+
347355
resps, err := l.QueryBlockByHash(blockHash, []fab.ProposalProcessor{c.PrimaryPeer()})
348356
if err != nil {
349357
return nil, err
@@ -356,7 +364,11 @@ func (c *Channel) QueryBlockByHash(blockHash []byte) (*common.Block, error) {
356364
// blockNumber: The number which is the ID of the Block.
357365
// It returns the block.
358366
func (c *Channel) QueryBlock(blockNumber int) (*common.Block, error) {
359-
l := NewLedger(c.clientContext, c.name)
367+
l, err := NewLedger(c.clientContext, c.name)
368+
if err != nil {
369+
return nil, errors.WithMessage(err, "ledger client creation failed")
370+
}
371+
360372
resps, err := l.QueryBlock(blockNumber, []fab.ProposalProcessor{c.PrimaryPeer()})
361373
if err != nil {
362374
return nil, err
@@ -369,7 +381,11 @@ func (c *Channel) QueryBlock(blockNumber int) (*common.Block, error) {
369381
// Returns the ProcessedTransaction information containing the transaction.
370382
// TODO: add optional target
371383
func (c *Channel) QueryTransaction(transactionID string) (*pb.ProcessedTransaction, error) {
372-
l := NewLedger(c.clientContext, c.name)
384+
l, err := NewLedger(c.clientContext, c.name)
385+
if err != nil {
386+
return nil, errors.WithMessage(err, "ledger client creation failed")
387+
}
388+
373389
resps, err := l.QueryTransaction(transactionID, []fab.ProposalProcessor{c.PrimaryPeer()})
374390
if err != nil {
375391
return nil, err
@@ -380,7 +396,11 @@ func (c *Channel) QueryTransaction(transactionID string) (*pb.ProcessedTransacti
380396
// QueryInstantiatedChaincodes queries the instantiated chaincodes on this channel.
381397
// This query will be made to the primary peer.
382398
func (c *Channel) QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, error) {
383-
l := NewLedger(c.clientContext, c.name)
399+
l, err := NewLedger(c.clientContext, c.name)
400+
if err != nil {
401+
return nil, errors.WithMessage(err, "ledger client creation failed")
402+
}
403+
384404
resps, err := l.QueryInstantiatedChaincodes([]fab.ProposalProcessor{c.PrimaryPeer()})
385405
if err != nil {
386406
return nil, err
@@ -392,7 +412,11 @@ func (c *Channel) QueryInstantiatedChaincodes() (*pb.ChaincodeQueryResponse, err
392412
// QueryConfigBlock returns the current configuration block for the specified channel. If the
393413
// peer doesn't belong to the channel, return error
394414
func (c *Channel) QueryConfigBlock(peers []fab.Peer, minResponses int) (*common.ConfigEnvelope, error) {
395-
l := NewLedger(c.clientContext, c.name)
415+
l, err := NewLedger(c.clientContext, c.name)
416+
if err != nil {
417+
return nil, errors.WithMessage(err, "ledger client creation failed")
418+
}
419+
396420
return l.QueryConfigBlock(peers, minResponses)
397421
}
398422

pkg/fabric-client/channel/ledger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ type Ledger struct {
3232
}
3333

3434
// NewLedger constructs a Ledger client for the current context and named channel.
35-
func NewLedger(ctx fab.Context, chName string) *Ledger {
35+
func NewLedger(ctx fab.Context, chName string) (*Ledger, error) {
3636
l := Ledger{
3737
ctx: ctx,
3838
chName: chName,
3939
}
40-
return &l
40+
return &l, nil
4141
}
4242

4343
// QueryInfo queries for various useful information on the state of the channel

pkg/fabric-client/channel/ledger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,5 @@ func setupTestLedger() (*Ledger, error) {
181181
func setupLedger(channelID string) (*Ledger, error) {
182182
user := mocks.NewMockUser("test")
183183
ctx := mocks.NewMockContext(user)
184-
return NewLedger(ctx, channelID), nil
184+
return NewLedger(ctx, channelID)
185185
}

pkg/fabric-client/mocks/mockchprovider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ func (cs *MockChannelService) Channel() (fab.Channel, error) {
6868
func (cs *MockChannelService) ChannelConfig() (fab.ChannelConfig, error) {
6969
return nil, nil
7070
}
71+
72+
// Ledger ...
73+
func (cs *MockChannelService) Ledger() (fab.ChannelLedger, error) {
74+
return nil, nil
75+
}

pkg/fabric-client/mocks/mockresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *MockResource) CreateChannel(request fab.CreateChannelRequest) (fab.Tran
5858
}
5959

6060
//QueryChannels ...
61-
func (c *MockResource) QueryChannels(peer fab.Peer) (*pb.ChannelQueryResponse, error) {
61+
func (c *MockResource) QueryChannels(peer fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
6262
return nil, errors.New("Not implemented yet")
6363
}
6464

@@ -77,7 +77,7 @@ func (c *MockResource) JoinChannel(request fab.JoinChannelRequest) error {
7777
}
7878

7979
//QueryInstalledChaincodes mocks query installed chaincodes
80-
func (c *MockResource) QueryInstalledChaincodes(peer fab.Peer) (*pb.ChaincodeQueryResponse, error) {
80+
func (c *MockResource) QueryInstalledChaincodes(peer fab.ProposalProcessor) (*pb.ChaincodeQueryResponse, error) {
8181
if peer == nil {
8282
return nil, errors.New("Generate Error")
8383
}

pkg/fabric-client/resource/resource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (c *Resource) createOrUpdateChannel(request fab.CreateChannelRequest, haveE
321321
}
322322

323323
// QueryChannels queries the names of all the channels that a peer has joined.
324-
func (c *Resource) QueryChannels(peer fab.Peer) (*pb.ChannelQueryResponse, error) {
324+
func (c *Resource) QueryChannels(peer fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
325325

326326
if peer == nil {
327327
return nil, errors.New("peer required")
@@ -346,7 +346,7 @@ func (c *Resource) QueryChannels(peer fab.Peer) (*pb.ChannelQueryResponse, error
346346

347347
// QueryInstalledChaincodes queries the installed chaincodes on a peer.
348348
// Returns the details of all chaincodes installed on a peer.
349-
func (c *Resource) QueryInstalledChaincodes(peer fab.Peer) (*pb.ChaincodeQueryResponse, error) {
349+
func (c *Resource) QueryInstalledChaincodes(peer fab.ProposalProcessor) (*pb.ChaincodeQueryResponse, error) {
350350

351351
if peer == nil {
352352
return nil, errors.New("peer required")

0 commit comments

Comments
 (0)