`go-ethereum` client.BlockByHash() gives error 'not found'

PHPz
Release: 2024-02-09 08:00:22
forward
910 people have browsed it

`go-ethereum` client.BlockByHash() 给出错误“未找到”

php Editor Banana recently received a question from a reader. He encountered an error message when using the `client.BlockByHash()` function of `go-ethereum`: " not found". This problem has been bothering him for a long time, so he hopes to get some solution. In this article, we will explore the possible causes of this error and provide some possible solutions.

Question content

I have the following code for subscribing to new blocks as they appear:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.dial("wss://mainnet.infura.io/ws/v3/apikey")
    if err != nil {
        log.fatal(err)
    }

    headers := make(chan *types.header)
    sub, err := client.subscribenewhead(context.background(), headers)
    if err != nil {
        log.fatal(err)
    }

    for {
        select {
        case err := <-sub.err():
            log.fatal(err)
        case header := <-headers:
            fmt.println(header.hash().hex()) // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f

            block, err := client.blockbyhash(context.background(), header.hash())
            if err != nil {
                log.fatal(err)
            }

            fmt.println(block.hash().hex())        // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f
            fmt.println(block.number().uint64())   // 3477413
            fmt.println(block.time())              // 1529525947
            fmt.println(block.nonce())             // 130524141876765836
            fmt.println(len(block.transactions())) // 7
        }
    }
}
Copy after login

But in the team

block, err := client.blockbyhash(context.background(), header.hash())
Copy after login

I get the error:

2023/04/19 17:31:14 not found
exit status 1
Copy after login

It still prints the hash in fmt.println(header.hash().hex()) so I know the infura connection is working.

Solution

Use block number instead of hash value.

block, err := client.BlockByNumber(context.Background(), header.Number)
Copy after login

Function header.hash() does not return the block hash, but returns the hash of the header.

The above is the detailed content of `go-ethereum` client.BlockByHash() gives error 'not found'. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!