ledger包支持在Fabric網(wǎng)絡(luò)上的指定通道上啟用賬本查詢(xún)。如果應(yīng)用程序需要對(duì)多個(gè)通道進(jìn)行賬本查詢(xún),需要為每個(gè)通道的賬本客戶端創(chuàng)建一個(gè)單獨(dú)實(shí)例。賬本客戶端支持以下查詢(xún):QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction和QueryConfig。
官方文檔:
https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/ledger
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到漠河網(wǎng)站設(shè)計(jì)與漠河網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋漠河地區(qū)。
ledger使用的基本流程如下:
A、準(zhǔn)備通道上下文
B、創(chuàng)建分類(lèi)帳客戶端
C、查詢(xún)分類(lèi)帳
使用示例:
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
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返回賬本客戶端實(shí)例。賬本客戶端實(shí)例提供處理程序以查詢(xún)指定通道上的各種信息。如果應(yīng)用程序需要對(duì)多個(gè)通道進(jìn)行賬本查詢(xún),需要為每個(gè)通道的賬本客戶端創(chuàng)建一個(gè)單獨(dú)實(shí)例。賬本客戶端僅支持指定查詢(xún)。
使用示例:
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 (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error)
QueryBlock根據(jù)區(qū)塊編號(hào)查詢(xún)區(qū)塊的賬本。
參數(shù):
blockNumber是必需的區(qū)塊號(hào)
options包含可選的請(qǐng)求選項(xiàng)
返回區(qū)塊信息
使用示例:
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根據(jù)區(qū)塊hash查詢(xún)區(qū)塊賬本。
參數(shù):
blockHash是區(qū)塊哈希
options包含可選的請(qǐng)求選項(xiàng)
返回區(qū)塊信息
使用示例:
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查詢(xún)包含交易的區(qū)塊
參數(shù):
txID是交易ID
options包含可選的請(qǐng)求選項(xiàng)
返回區(qū)塊信息
使用示例:
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 (c *Client) QueryConfig(options ...RequestOption) (fab.ChannelCfg, error)
QueryConfig查詢(xún)通道配置。
參數(shù):
options包含可選的請(qǐng)求選項(xiàng)
返回通道配置信息
使用示例:
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 (c *Client) QueryInfo(options ...RequestOption) (*fab.BlockchainInfoResponse, error)
QueryInfo查詢(xún)此通道上的各種區(qū)塊鏈信息,例如區(qū)塊高度和當(dāng)前區(qū)塊哈希。
參數(shù):
options是可選的請(qǐng)求選項(xiàng)
返回區(qū)塊鏈信息
使用示例:
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 (c *Client) QueryTransaction(transactionID fab.TransactionID, options ...RequestOption) (*pb.ProcessedTransaction, error)
QueryTransaction通過(guò)交易ID查詢(xún)賬本上的已處理交易。
參數(shù):
txID是必需的交易ID
options包含可選的請(qǐng)求選項(xiàng)
返回已經(jīng)處理交易的信息
使用示例:
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 func(*Client) error
// WithDefaultTargetFilter option to configure new
func WithDefaultTargetFilter(filter fab.TargetFilter) ClientOption
使用示例:
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 func(ctx context.Client, opts *requestOptions) error
//requestOptions contains options for operations performed by LedgerClient
type 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指定每個(gè)請(qǐng)求選擇的最大目標(biāo)數(shù)。最大目標(biāo)數(shù)的默認(rèn)值為1.func WithMinTargets(minTargets int) RequestOption
WithMinTargets指定必須響應(yīng)且沒(méi)有錯(cuò)誤(或同意結(jié)果)的最小目標(biāo)數(shù)。 最小目標(biāo)數(shù)的默認(rèn)值為1。func WithParentContext(parentContext reqContext.Context) RequestOption
WithParentContext封裝了grpc父上下文
使用示例:
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(keys ...string) RequestOption
withTargetEndpoints允許每個(gè)請(qǐng)求覆蓋目標(biāo)Peer節(jié)點(diǎn)。目標(biāo)由名稱(chēng)或URL指定,SDK將創(chuàng)建底層Peer節(jié)點(diǎn)對(duì)象。func WithTargetFilter(targetFilter fab.TargetFilter) RequestOption
WithTargetFilter指定每個(gè)請(qǐng)求的目標(biāo)Peer節(jié)點(diǎn)過(guò)濾器。
使用示例:
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允許每個(gè)請(qǐng)求覆蓋目標(biāo)Peer節(jié)點(diǎn)。
使用示例:
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封裝了超時(shí)類(lèi)型、超時(shí)時(shí)間的鍵值對(duì)到Options選項(xiàng),次選項(xiàng)用于QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction,QueryConfig等函數(shù)。
var (
sdk *fabsdk.FabricSDK
channelName = "assetchannel"
org = "org1"
user = "Admin"
)
// 賬本查詢(xún)
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 <= resp.BCI.Height; i++ {
cli.QueryBlock(i)
}
}