亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何通過blockNumber獲得transactionHash?

我如何通過blockNumber獲得transactionHash?

Go
幕布斯6054654 2023-01-03 14:06:04
如何通過blockNumber獲取transactionHash?我輸入了 block.transactions.0.transactionHash 但沒有捕捉到它(空字符串)。我怎樣才能得到交易哈希?func GetBlockTransactions(number int) string {    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)    var hashHeaders []models.Block    defer cancel()    options := options.Find().SetProjection(bson.M{"block.transactions.0.transactionHash": 1})    options.SetLimit(1)    results, err := blocksCollections.Find(ctx, bson.M{"block.header.blockNumber": strconv.Itoa(number)}, options)    if err != nil {        fmt.Println(err)    }    //reading from the db in an optimal way    defer results.Close(ctx)    for results.Next(ctx) {        var singleHashHeader models.Block        if err = results.Decode(&singleHashHeader); err != nil {            fmt.Println(err)        }        hashHeaders = append(hashHeaders, singleHashHeader)    }    fmt.Println("%v", hashHeaders[0].Transactions.TransactionHash)    return string(hashHeaders[0].Transactions.TransactionHash)}
查看完整描述

1 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超8個贊

您的方法有 3 個錯誤。


首先,find()塊集合返回完整文檔,而不僅僅是塊文檔(即block字段)。因此,將結果解組為描述完整文檔的類型值,例如:


type Doc struct {

    ID    primitive.ObjectID `bson:"_id"`

    Block Block              `bson:"block"`

}

和解組代碼:


var doc Doc

if err = results.Decode(&doc); err != nil {

    fmt.Println(err)

}

hashHeaders = append(hashHeaders, doc.Block)

其次,transactions是一個數組,所以在 Go 中你必須用一個切片來建模它:


type Block struct {

    Transactions []BlockTransaction

}

第三,投影和過濾器中對象數組字段的屬性在沒有索引的情況下被引用,所以使用投影:


options := options.Find().

    SetProjection(bson.M{"block.transactions.transactionHash": 1})

另請注意,您可以使用如下Cursor.All()方法簡單地解碼結果:


var docs []Doc

err = results.All(ctx, &docs)

if err != nil {

    fmt.Println(err)

}

工作完整代碼:


func GetBlockTransactions(number int) string {

    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

    defer cancel()


    options := options.Find().

        SetProjection(bson.M{"block.transactions.transactionHash": 1}).

        SetLimit(1)


    results, err := blocksCollections.Find(ctx, bson.M{"block.header.blockNumber": strconv.Itoa(number)}, options)


    if err != nil {

        fmt.Println(err)

    }


    var docs []Doc

    err = results.All(ctx, &docs)

    if err != nil {

        fmt.Println(err)

    }


    fmt.Printf("hash: %v\n", docs[0].Block.Transactions[0].TransactionHash)

    return string(docs[0].Block.Transactions[0].TransactionHash)

}

還要注意,如果遇到錯誤,后續代碼很可能會失敗,所以你應該早點返回。您還應該返回帶注釋的錯誤。


像這樣:


func GetBlockTransactions(number int) (string, error) {

    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

    defer cancel()


    options := options.Find().

        SetProjection(bson.M{"block.transactions.transactionHash": 1}).

        SetLimit(1)


    results, err := blocksCollections.Find(ctx, bson.M{"block.header.blockNumber": strconv.Itoa(number)}, options)

    if err != nil {

        return "", fmt.Errorf("Find() failed: %w", err)

    }


    var docs []Doc

    err = results.All(ctx, &docs)

    if err != nil {

        return "", fmt.Errorf("Cursor.All() failed: %w", err)

    }


    fmt.Printf("hash: %v\n", docs[0].Block.Transactions[0].TransactionHash)

    return string(docs[0].Block.Transactions[0].TransactionHash), nil

}

另請注意,如果您只期望一個結果,則更簡單的解決方案是使用Collection.FindOne().


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號