go-blockchain/rpc/info.go
2026-03-26 14:10:18 +00:00

61 lines
1.7 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 (
"dappco.re/go/core"
coreerr "dappco.re/go/core/log"
)
// GetInfo returns the daemon status.
// Uses flags=0 for the cheapest query (no expensive calculations).
func (c *Client) GetInfo() (*DaemonInfo, error) {
params := struct {
Flags uint64 `json:"flags"`
}{Flags: 0}
var resp struct {
DaemonInfo
Status string `json:"status"`
}
if err := c.call("getinfo", params, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, coreerr.E("Client.GetInfo", core.Sprintf("getinfo: status %q", resp.Status), nil)
}
return &resp.DaemonInfo, nil
}
// GetHeight returns the current blockchain height.
// Uses the legacy /getheight endpoint (not available via /json_rpc).
func (c *Client) GetHeight() (uint64, error) {
var resp struct {
Height uint64 `json:"height"`
Status string `json:"status"`
}
if err := c.legacyCall("/getheight", struct{}{}, &resp); err != nil {
return 0, err
}
if resp.Status != "OK" {
return 0, coreerr.E("Client.GetHeight", core.Sprintf("getheight: status %q", resp.Status), nil)
}
return resp.Height, nil
}
// GetBlockCount returns the total number of blocks (height of top block + 1).
func (c *Client) GetBlockCount() (uint64, error) {
var resp struct {
Count uint64 `json:"count"`
Status string `json:"status"`
}
if err := c.call("getblockcount", struct{}{}, &resp); err != nil {
return 0, err
}
if resp.Status != "OK" {
return 0, coreerr.E("Client.GetBlockCount", core.Sprintf("getblockcount: status %q", resp.Status), nil)
}
return resp.Count, nil
}