go-blockchain/rpc/mining.go
Claude 8735e535d5
feat(rpc): add GetBlockTemplate endpoint
Co-Authored-By: Charon <charon@lethean.io>
2026-02-21 02:01:12 +00:00

53 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 "fmt"
// SubmitBlock submits a mined block to the daemon.
// The hexBlob is the hex-encoded serialised block.
// Note: submitblock takes a JSON array as params, not an object.
func (c *Client) SubmitBlock(hexBlob string) error {
// submitblock expects params as an array: ["hexblob"]
params := []string{hexBlob}
var resp struct {
Status string `json:"status"`
}
if err := c.call("submitblock", params, &resp); err != nil {
return err
}
if resp.Status != "OK" {
return fmt.Errorf("submitblock: status %q", resp.Status)
}
return nil
}
// BlockTemplateResponse is the daemon's response to getblocktemplate.
type BlockTemplateResponse struct {
Difficulty string `json:"difficulty"`
Height uint64 `json:"height"`
BlockTemplateBlob string `json:"blocktemplate_blob"`
PrevHash string `json:"prev_hash"`
BlockRewardWithoutFee uint64 `json:"block_reward_without_fee"`
BlockReward uint64 `json:"block_reward"`
TxsFee uint64 `json:"txs_fee"`
Status string `json:"status"`
}
// GetBlockTemplate requests a block template from the daemon for mining.
func (c *Client) GetBlockTemplate(walletAddr string) (*BlockTemplateResponse, error) {
params := struct {
WalletAddress string `json:"wallet_address"`
}{WalletAddress: walletAddr}
var resp BlockTemplateResponse
if err := c.call("getblocktemplate", params, &resp); err != nil {
return nil, err
}
if resp.Status != "OK" {
return nil, fmt.Errorf("getblocktemplate: status %q", resp.Status)
}
return &resp, nil
}