Enrich found-block records with real coinbase reward
Adds RPC.GetBlock(hash, verbosity=2) and a CoinbaseReward helper that sums the first tx's outputs. IngestBlockEvents now does getblockhash -> getblock -> sum(vout) so BlockRecord.RewardBT carries the actual BTC paid out on solve instead of always being zero. Both RPC calls share a single 5s deadline and are best-effort — bitcoind being down just leaves the reward at zero.
This commit is contained in:
@@ -122,6 +122,50 @@ func (c *RPC) GetBlockHash(ctx context.Context, height int64) (string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// BlockVerbose2 is the subset of `getblock <hash> 2` we care about.
|
||||
// Verbosity 2 expands each tx into its full object so we can read the
|
||||
// coinbase output values without a second getrawtransaction call.
|
||||
type BlockVerbose2 struct {
|
||||
Hash string `json:"hash"`
|
||||
Height int64 `json:"height"`
|
||||
Confirmations int64 `json:"confirmations"`
|
||||
Time int64 `json:"time"`
|
||||
Tx []BlockTx `json:"tx"`
|
||||
}
|
||||
|
||||
type BlockTx struct {
|
||||
Txid string `json:"txid"`
|
||||
Vout []BlockVout `json:"vout"`
|
||||
}
|
||||
|
||||
type BlockVout struct {
|
||||
Value float64 `json:"value"`
|
||||
N int `json:"n"`
|
||||
}
|
||||
|
||||
// GetBlock returns a verbose-level-2 block decode for the given hash.
|
||||
func (c *RPC) GetBlock(ctx context.Context, hash string) (*BlockVerbose2, error) {
|
||||
var out BlockVerbose2
|
||||
if err := c.Call(ctx, "getblock", []any{hash, 2}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// CoinbaseReward sums all outputs of the first transaction in the block
|
||||
// (the coinbase). In solo mining this is the full subsidy + fees the
|
||||
// solving worker receives.
|
||||
func (b *BlockVerbose2) CoinbaseReward() float64 {
|
||||
if len(b.Tx) == 0 {
|
||||
return 0
|
||||
}
|
||||
var total float64
|
||||
for _, vout := range b.Tx[0].Vout {
|
||||
total += vout.Value
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// NetworkHashPS returns the network hashrate at the given block height.
|
||||
// `blocks` is a window (default 120). Pass -1 to use the default.
|
||||
func (c *RPC) GetNetworkHashPS(ctx context.Context, blocks, height int) (float64, error) {
|
||||
|
||||
Reference in New Issue
Block a user