diff --git a/api/internal/bitcoind/rpc.go b/api/internal/bitcoind/rpc.go index b911484..30763b6 100644 --- a/api/internal/bitcoind/rpc.go +++ b/api/internal/bitcoind/rpc.go @@ -122,6 +122,50 @@ func (c *RPC) GetBlockHash(ctx context.Context, height int64) (string, error) { return out, nil } +// BlockVerbose2 is the subset of `getblock 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) { diff --git a/api/internal/state/blocks.go b/api/internal/state/blocks.go index 61976eb..feeed32 100644 --- a/api/internal/state/blocks.go +++ b/api/internal/state/blocks.go @@ -35,11 +35,20 @@ func (a *Aggregator) IngestBlockEvents(ctx context.Context, events <-chan logmon FoundAt: ev.SeenAt, 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 { lookupCtx, cancel := context.WithTimeout(ctx, 5*time.Second) if hash, err := a.RPC.GetBlockHash(lookupCtx, ev.Height); err == nil { 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 { a.Log.Warn("bitcoind getblockhash failed", "height", ev.Height, "err", err) }