go-blockchain/rpc/blocks.go
Snider 71f0a5c1d5
Some checks failed
Security Scan / security (push) Successful in 11s
Test / Test (push) Failing after 23s
refactor: replace fmt.Errorf/os.* with go-io/go-log conventions
Replace all fmt.Errorf and errors.New in production code with
coreerr.E("Caller.Method", "message", err) from go-log. Replace
os.MkdirAll with coreio.Local.EnsureDir from go-io. Sentinel errors
(consensus/errors.go, wire/varint.go) intentionally kept as errors.New
for errors.Is compatibility.

270 error call sites converted across 38 files. Test files untouched.
crypto/ directory (CGO) untouched.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-16 21:17:49 +00:00

83 lines
2.6 KiB
Go

// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2
package rpc
import (
"fmt"
coreerr "forge.lthn.ai/core/go-log"
)
// GetLastBlockHeader returns the header of the most recent block.
func (c *Client) GetLastBlockHeader() (*BlockHeader, error) {
var resp struct {
BlockHeader BlockHeader `json:"block_header"`
Status string `json:"status"`
}
if err := c.call("getlastblockheader", struct{}{}, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, coreerr.E("Client.GetLastBlockHeader", fmt.Sprintf("getlastblockheader: status %q", resp.Status), nil)
}
return &resp.BlockHeader, nil
}
// GetBlockHeaderByHeight returns the block header at the given height.
func (c *Client) GetBlockHeaderByHeight(height uint64) (*BlockHeader, error) {
params := struct {
Height uint64 `json:"height"`
}{Height: height}
var resp struct {
BlockHeader BlockHeader `json:"block_header"`
Status string `json:"status"`
}
if err := c.call("getblockheaderbyheight", params, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, coreerr.E("Client.GetBlockHeaderByHeight", fmt.Sprintf("getblockheaderbyheight: status %q", resp.Status), nil)
}
return &resp.BlockHeader, nil
}
// GetBlockHeaderByHash returns the block header with the given hash.
func (c *Client) GetBlockHeaderByHash(hash string) (*BlockHeader, error) {
params := struct {
Hash string `json:"hash"`
}{Hash: hash}
var resp struct {
BlockHeader BlockHeader `json:"block_header"`
Status string `json:"status"`
}
if err := c.call("getblockheaderbyhash", params, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, coreerr.E("Client.GetBlockHeaderByHash", fmt.Sprintf("getblockheaderbyhash: status %q", resp.Status), nil)
}
return &resp.BlockHeader, nil
}
// GetBlocksDetails returns full block details starting at the given height.
func (c *Client) GetBlocksDetails(heightStart, count uint64) ([]BlockDetails, error) {
params := struct {
HeightStart uint64 `json:"height_start"`
Count uint64 `json:"count"`
IgnoreTransactions bool `json:"ignore_transactions"`
}{HeightStart: heightStart, Count: count}
var resp struct {
Blocks []BlockDetails `json:"blocks"`
Status string `json:"status"`
}
if err := c.call("get_blocks_details", params, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, coreerr.E("Client.GetBlocksDetails", fmt.Sprintf("get_blocks_details: status %q", resp.Status), nil)
}
return resp.Blocks, nil
}