import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" ) func main() { client, err := ethclient.Dial("https://mainnet.infura.io") if err != nil { log.Fatal(err) } block, err := client.BlockByNumber(context.Background(), nil) if err != nil { log.Fatal(err) } blockRoot := block.Root() }
import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" ) func main() { header := &types.Header{ ParentHash: crypto.Keccak256Hash([]byte("parent block hash")), UncleHash: crypto.Keccak256Hash(nil), Coinbase: common.HexToAddress("0x1234567890123456789012345678901234567890"), Root: crypto.Keccak256Hash([]byte("transaction root")), TxHash: crypto.Keccak256Hash([]byte("transaction hash")), ReceiptHash: crypto.Keccak256Hash([]byte("receipt hash")), Bloom: types.Bloom{}, Difficulty: big.NewInt(10), GasLimit: uint64(100000), GasUsed: uint64(50000), Time: uint64(1620000000), Extra: []byte{}, MixDigest: crypto.Keccak256Hash(nil), Nonce: types.BlockNonce{}, } blockRoot := header.Root.Hex() fmt.Println(blockRoot) // Output: 0xacd3bc9a5a8108c7fdb492cd7125ecf3b32d6b8f7465a5bc593d4267ac9dd6b2 }This example shows how to manually create a block header and hash it using Keccak256 to get its Block Root. Overall, Block Root is an important component of Ethereum Go library that helps ensure the overall security and integrity of the network. It's part of the `core.types` package library.