fabric-sdk-go: github.com/hyperledger/fabric-sdk-go/pkg/client/ledger Index | Examples | Files

package ledger

import "github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"

Package ledger enables ledger queries on specified channel on a Fabric network. An application that requires ledger queries from multiple channels should create a separate instance of the ledger client for each channel. Ledger client supports the following queries: QueryInfo, QueryBlock, QueryBlockByHash, QueryBlockByTxID, QueryTransaction and QueryConfig.

Basic Flow:
1) Prepare channel context
2) Create ledger client
3) Query ledger

Code:

ctx := mockChannelProvider("mychannel")

c, err := New(ctx)
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlock(1)
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1")
}

Output:

Retrieved block #1

Index

Examples

Package Files

ledger.go opts.go

type Client Uses

type Client struct {
    // contains filtered or unexported fields
}

Client enables ledger queries on a Fabric network.

func New Uses

func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)

New returns a ledger client instance. A ledger client instance provides a handler to query various info on specified channel. An application that requires interaction with multiple channels should create a separate instance of the ledger client for each channel. Ledger client supports specific queries only.

Code:

ctx := mockChannelProvider("mychannel")

c, err := New(ctx)
if err != nil {
    fmt.Println(err)
}

if c != nil {
    fmt.Println("ledger client created")
}

Output:

ledger client created

func (*Client) QueryBlock Uses

func (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error)

QueryBlock queries the ledger for Block by block number.

Parameters:
blockNumber is required block number(ID)
options hold optional request options

Returns:
block information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlock(1)
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1")
}

Output:

Retrieved block #1

func (*Client) QueryBlockByHash Uses

func (c *Client) QueryBlockByHash(blockHash []byte, options ...RequestOption) (*common.Block, error)

QueryBlockByHash queries the ledger for block by block hash.

Parameters:
blockHash is required block hash
options hold optional request options

Returns:
block information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlockByHash([]byte("hash"))
if err != nil {
    fmt.Printf("failed to query block by hash: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block by hash")
}

Output:

Retrieved block by hash

func (*Client) QueryBlockByTxID Uses

func (c *Client) QueryBlockByTxID(txID fab.TransactionID, options ...RequestOption) (*common.Block, error)

QueryBlockByTxID queries for block which contains a transaction.

Parameters:
txID is required transaction ID
options hold optional request options

Returns:
block information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlockByTxID("123")
if err != nil {
    fmt.Printf("failed to query block by transaction ID: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block by transaction ID")
}

Output:

Retrieved block by transaction ID

func (*Client) QueryConfig Uses

func (c *Client) QueryConfig(options ...RequestOption) (fab.ChannelCfg, error)

QueryConfig queries for channel configuration.

Parameters:
options hold optional request options

Returns:
channel configuration information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
if err != nil {
    fmt.Printf("failed to query config: %s\n", err)
}

if cfg != nil {
    fmt.Println("Retrieved channel configuration")
}

Output:

Retrieved channel configuration

func (*Client) QueryConfigBlock Uses

func (c *Client) QueryConfigBlock(options ...RequestOption) (*common.Block, error)

QueryConfigBlock returns the current configuration block for the specified channel.

func (*Client) QueryInfo Uses

func (c *Client) QueryInfo(options ...RequestOption) (*fab.BlockchainInfoResponse, error)

QueryInfo queries for various useful blockchain information on this channel such as block height and current block hash.

Parameters:
options are optional request options

Returns:
blockchain information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

bci, err := c.QueryInfo()
if err != nil {
    fmt.Printf("failed to query for blockchain info: %s\n", err)
}

if bci != nil {
    fmt.Println("Retrieved ledger info")
}

Output:

Retrieved ledger info

func (*Client) QueryTransaction Uses

func (c *Client) QueryTransaction(transactionID fab.TransactionID, options ...RequestOption) (*pb.ProcessedTransaction, error)

QueryTransaction queries the ledger for processed transaction by transaction ID.

Parameters:
txID is required transaction ID
options hold optional request options

Returns:
processed transaction information

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

t, err := c.QueryTransaction("123")
if err != nil {
    fmt.Printf("failed to query transaction: %s\n", err)
}

if t != nil {
    fmt.Println("Retrieved transaction")
}

Output:

Retrieved transaction

type ClientOption Uses

type ClientOption func(*Client) error

ClientOption describes a functional parameter for the New constructor

func WithDefaultTargetFilter Uses

func WithDefaultTargetFilter(filter fab.TargetFilter) ClientOption

WithDefaultTargetFilter option to configure new

Code:

ctx := mockChannelProvider("mychannel")

c, err := New(ctx, WithDefaultTargetFilter(&urlTargetFilter{url: "example.com"}))
if err != nil {
    fmt.Println(err)
}

if c != nil {
    fmt.Println("ledger client created with url target filter")
}

Output:

ledger client created with url target filter

type RequestOption Uses

type RequestOption func(ctx context.Client, opts *requestOptions) error

RequestOption func for each requestOptions argument

func WithMaxTargets Uses

func WithMaxTargets(maxTargets int) RequestOption

WithMaxTargets specifies maximum number of targets to select per request. Default value for maximum number of targets is 1.

func WithMinTargets Uses

func WithMinTargets(minTargets int) RequestOption

WithMinTargets specifies minimum number of targets that have to respond with no error (or agree on result). Default value for minimum number of targets is 1.

func WithParentContext Uses

func WithParentContext(parentContext reqContext.Context) RequestOption

WithParentContext encapsulates grpc parent context

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println(err)
}

channelContext, err := mockChannelProvider("mychannel")()
if err != nil {
    fmt.Println("failed to return channel context")
    return
}

// get parent context and cancel
parentContext, cancel := sdkCtx.NewRequest(channelContext, sdkCtx.WithTimeout(20*time.Second))
defer cancel()

bci, err := c.QueryInfo(WithParentContext(parentContext))
if err != nil {
    fmt.Printf("failed to query for blockchain info: %s\n", err)
}

if bci != nil {
    fmt.Println("Retrieved blockchain info")
}

Output:

Retrieved blockchain info

func WithTargetEndpoints Uses

func WithTargetEndpoints(keys ...string) RequestOption

WithTargetEndpoints allows overriding of the target peers per request. Targets are specified by name or URL, and the SDK will create the underlying peer objects.

func WithTargetFilter Uses

func WithTargetFilter(targetFilter fab.TargetFilter) RequestOption

WithTargetFilter specifies a per-request target peer-filter.

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println(err)
}

block, err := c.QueryBlock(1, WithTargetFilter(&urlTargetFilter{url: "example.com"}))
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1 from example.com")
}

Output:

Retrieved block #1 from example.com

func WithTargets Uses

func WithTargets(targets ...fab.Peer) RequestOption

WithTargets allows for overriding of the target peers per request.

Code:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
if err != nil {
    fmt.Printf("failed to query config with target peer: %s\n", err)
}

if cfg != nil {
    fmt.Println("Retrieved config from target peer")
}

Output:

Retrieved config from target peer

func WithTimeout Uses

func WithTimeout(timeoutType fab.TimeoutType, timeout time.Duration) RequestOption

WithTimeout encapsulates key value pairs of timeout type, timeout duration to Options for QueryInfo, QueryBlock, QueryBlockByHash, QueryBlockByTxID, QueryTransaction, QueryConfig functions

Package ledger imports 17 packages (graph) and is imported by 3 packages. Updated 2019-08-25. Refresh now. Tools for package owners.