eth-dump-genblock/main.go
Roy Crihfield c0655063ab Update geth to 1.14 (#2)
Updates Geth and dependencies, adds a basic unit test.

Reviewed-on: #2
2024-06-21 07:48:16 +00:00

57 lines
1.2 KiB
Go

// This script reads a geth genesis config and outputs the corresponding genesis block as
// RPC-compatible JSON.
//
// E.g.: eth-dump-genblock genesis.json > genesis_block.json
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/triedb"
)
func main() {
blockJSON := Main(os.Args)
fmt.Println(string(blockJSON))
}
func Main(args []string) []byte {
if len(args) != 2 {
fatal(fmt.Sprintf("Usage: %s genesis.json", args[0]))
}
configPath := args[1]
file, err := os.Open(configPath)
if err != nil {
fatal("Failed to read genesis config file:", err)
}
genesis := new(core.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
fatal("Failed to parse genesis config:", err)
}
db := rawdb.NewMemoryDatabase()
block, err := genesis.Commit(db, triedb.NewDatabase(db, nil))
if err != nil {
fatal("Failed to create genesis block:", err)
}
rpcBlock := RPCMarshalBlock(block)
blockJSON, err := json.MarshalIndent(rpcBlock, "", " ")
if err != nil {
fatal("Failed to convert block to JSON:", err)
}
return blockJSON
}
func fatal(args ...any) {
fmt.Fprintln(os.Stderr, args...)
os.Exit(1)
}