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
|
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.
|
// NetworkHashPS returns the network hashrate at the given block height.
|
||||||
// `blocks` is a window (default 120). Pass -1 to use the default.
|
// `blocks` is a window (default 120). Pass -1 to use the default.
|
||||||
func (c *RPC) GetNetworkHashPS(ctx context.Context, blocks, height int) (float64, error) {
|
func (c *RPC) GetNetworkHashPS(ctx context.Context, blocks, height int) (float64, error) {
|
||||||
|
|||||||
@@ -35,11 +35,20 @@ func (a *Aggregator) IngestBlockEvents(ctx context.Context, events <-chan logmon
|
|||||||
FoundAt: ev.SeenAt,
|
FoundAt: ev.SeenAt,
|
||||||
Source: "logmon",
|
Source: "logmon",
|
||||||
}
|
}
|
||||||
// Best-effort enrich with hash via bitcoind.
|
// Best-effort enrich with hash + coinbase reward via bitcoind.
|
||||||
|
// We look up the hash from height, then fetch the full block
|
||||||
|
// (verbosity 2) to sum the coinbase outputs. Both are fire-
|
||||||
|
// and-forget — if bitcoind is down we still record the block
|
||||||
|
// with whatever we have.
|
||||||
if a.RPC != nil {
|
if a.RPC != nil {
|
||||||
lookupCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
lookupCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
if hash, err := a.RPC.GetBlockHash(lookupCtx, ev.Height); err == nil {
|
if hash, err := a.RPC.GetBlockHash(lookupCtx, ev.Height); err == nil {
|
||||||
rec.Hash = hash
|
rec.Hash = hash
|
||||||
|
if blk, err := a.RPC.GetBlock(lookupCtx, hash); err == nil {
|
||||||
|
rec.RewardBT = blk.CoinbaseReward()
|
||||||
|
} else {
|
||||||
|
a.Log.Warn("bitcoind getblock failed", "hash", hash, "err", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
a.Log.Warn("bitcoind getblockhash failed", "height", ev.Height, "err", err)
|
a.Log.Warn("bitcoind getblockhash failed", "height", ev.Height, "err", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user