Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

HyperLeger Fabric SDK Development (7)-- ledger

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

HyperLeger Fabric SDK development (7)-- ledger 1, ledger introduction 1, ledger introduction

The ledger package supports enabling ledger queries on specified channels on the Fabric network. If the application needs to query the ledger for multiple channels, you need to create a separate instance of the ledger client for each channel. The account client supports the following queries: QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction and QueryConfig.

Official documents:

Https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/ledger

2. Ledger usage process

The basic process used by ledger is as follows:

A. prepare the channel context

B. Create a ledger client

C. Inquiry ledger

Examples of use:

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 II, ledger Common API 1, get account client instance type Client struct {ctx context.Channel filter fab.TargetFilter ledger * channel.Ledger verifier channel.ResponseVerifier discovery fab.DiscoveryService} func New (channelProvider context.ChannelProvider, opts... ClientOption) (* Client, error)

New returns the ledger client instance. The ledger client instance provides a handler to query various information on the specified channel. If the application needs to query the ledger for multiple channels, you need to create a separate instance of the ledger client for each channel. The account client only supports specified queries.

Examples of use:

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 2, query account books

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

QueryBlock queries the ledger of the block based on the block number.

Parameters:

BlockNumber is a required block number

Options contains optional request options

Returns block information

Examples of use:

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 (c * Client) QueryBlockByHash (blockHash [] byte, options... RequestOption) (* common.Block, error)

QueryBlockByHash queries block account books according to block hash.

Parameters:

BlockHash is a block hash

Options contains optional request options

Returns block information

Examples of use:

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 (c * Client) QueryBlockByTxID (txID fab.TransactionID, options... RequestOption) (* common.Block, error)

QueryBlockByTxID query blocks containing transactions

Parameters:

TxID is a transaction ID.

Options contains optional request options

Returns block information

Examples of use:

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 3, query channel configuration

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

QueryConfig queries the channel configuration.

Parameters:

Options contains optional request options

Return channel configuration information

Examples of use:

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 4, query channel information

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

QueryInfo queries various block chain information on this channel, such as block height and current block hash.

Parameters:

Options is an optional request option

Returns block chain information

Examples of use:

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 5, query transaction information

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

QueryTransaction queries the books for processed transactions through the transaction ID.

Parameters:

TxID is a required transaction ID

Options contains optional request options

Returns information about transactions that have been processed

Examples of use:

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 6, set filter for client type ClientOption func (* Client) error// WithDefaultTargetFilter option to configure newfunc WithDefaultTargetFilter (filter fab.TargetFilter) ClientOption example: 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 7, RequestOption parameter to build type RequestOption func (ctx context.Client) Opts * requestOptions) error//requestOptions contains options for operations performed by LedgerClienttype requestOptions struct {Targets [] fab.Peer / / target peers TargetFilter fab.TargetFilter / / target filter MaxTargets int / / maximum number of targets to select MinTargets int / / min number of targets that have to respond with no Error (or agree on result) Timeouts map [fab.TimeoutType] time.Duration / / timeout options for ledger query operations ParentContext reqContext.Context / / parent grpc context for ledger operations} func WithMaxTargets (maxTargets int) RequestOption

WithMaxTargets specifies the maximum number of targets selected per request. The default value for the maximum number of targets is 1.

Func WithMinTargets (minTargets int) RequestOption

WithMinTargets specifies the minimum number of goals that must respond without errors (or agree to the result). The default value for the minimum number of targets is 1.

Func WithParentContext (parentContext reqContext.Context) RequestOption

WithParentContext encapsulates the grpc parent context

Examples of use:

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 cancelparentContext, 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 (keys... string) RequestOption

WithTargetEndpoints allows each request to override the target Peer node. The target is specified by the name or URL, and SDK creates the underlying Peer node object.

Func WithTargetFilter (targetFilter fab.TargetFilter) RequestOption

WithTargetFilter specifies the target Peer node filter for each request.

Examples of use:

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 (targets... fab.Peer) RequestOption

WithTargets allows each request to override the target Peer node.

Examples of use:

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 (timeoutType fab.TimeoutType, timeout time.Duration) RequestOption

WithTimeout encapsulates the timeout type, the key-value pair of the timeout time to the Options option, and the secondary option is used for functions such as QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction,QueryConfig.

3. Ledger sample var (sdk * fabsdk.FabricSDK channelName = "assetchannel" org = "org1" user = "Admin") / / account query func queryBlockchain () {ctx: = sdk.ChannelContext (channelName, fabsdk.WithOrg (org), fabsdk.WithUser (user)) cli, err: = ledger.New (ctx) if err! = nil {panic (err)} resp Err: = cli.QueryInfo (ledger.WithTargetEndpoints ("peer0.org1.example.com")) if err! = nil {panic (err)} fmt.Println (resp) / / 1 cli.QueryBlockByHash (resp.BCI.CurrentBlockHash) / / 2 for I: = uint64 (0) I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report