rpcserver: fix up getblockstats

This commit is contained in:
Roy Lee 2022-09-20 23:39:34 -07:00
parent 5acfa4c81b
commit 81ec217899
3 changed files with 29 additions and 10 deletions

View file

@ -21,6 +21,9 @@ import (
"github.com/lbryio/lbcd/chaincfg/chainhash" "github.com/lbryio/lbcd/chaincfg/chainhash"
"github.com/lbryio/lbcd/integration/rpctest" "github.com/lbryio/lbcd/integration/rpctest"
"github.com/lbryio/lbcd/rpcclient" "github.com/lbryio/lbcd/rpcclient"
"github.com/lbryio/lbcd/txscript"
"github.com/lbryio/lbcd/wire"
"github.com/lbryio/lbcutil"
) )
func testGetBestBlock(r *rpctest.Harness, t *testing.T) { func testGetBestBlock(r *rpctest.Harness, t *testing.T) {
@ -143,7 +146,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
baseFeeRate := int64(10) baseFeeRate := int64(10)
txValue := int64(50000000) txValue := int64(50000000)
txQuantity := 10 txQuantity := 10
txs := make([]*btcutil.Tx, txQuantity) txs := make([]*lbcutil.Tx, txQuantity)
fees := make([]int64, txQuantity) fees := make([]int64, txQuantity)
sizes := make([]int64, txQuantity) sizes := make([]int64, txQuantity)
feeRates := make([]int64, txQuantity) feeRates := make([]int64, txQuantity)
@ -164,12 +167,12 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
// This feerate is not the actual feerate. See comment below. // This feerate is not the actual feerate. See comment below.
feeRate := baseFeeRate * int64(i) feeRate := baseFeeRate * int64(i)
tx, err := r.CreateTransaction([]*wire.TxOut{wire.NewTxOut(txValue, pkScript)}, btcutil.Amount(feeRate), true) tx, err := r.CreateTransaction([]*wire.TxOut{wire.NewTxOut(txValue, pkScript)}, lbcutil.Amount(feeRate), true)
if err != nil { if err != nil {
t.Fatalf("Unable to generate segwit transaction: %v", err) t.Fatalf("Unable to generate segwit transaction: %v", err)
} }
txs[i] = btcutil.NewTx(tx) txs[i] = lbcutil.NewTx(tx)
sizes[i] = int64(tx.SerializeSize()) sizes[i] = int64(tx.SerializeSize())
// memWallet.fundTx makes some assumptions when calculating fees. // memWallet.fundTx makes some assumptions when calculating fees.
@ -215,13 +218,13 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
tests := []struct { tests := []struct {
name string name string
txs []*btcutil.Tx txs []*lbcutil.Tx
stats []string stats []string
expectedResults map[string]interface{} expectedResults map[string]interface{}
}{ }{
{ {
name: "empty block", name: "empty block",
txs: []*btcutil.Tx{}, txs: []*lbcutil.Tx{},
stats: []string{}, stats: []string{},
expectedResults: map[string]interface{}{ expectedResults: map[string]interface{}{
"avgfee": int64(0), "avgfee": int64(0),
@ -270,7 +273,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
"minfeerate": minFeeRate, "minfeerate": minFeeRate,
"mintxsize": minSize, "mintxsize": minSize,
"outs": int64(outputCount + 1), // Coinbase output also counts. "outs": int64(outputCount + 1), // Coinbase output also counts.
"subsidy": int64(5000000000), "subsidy": int64(100000000),
"swtotal_weight": nil, // This stat was not selected, so it should be nil. "swtotal_weight": nil, // This stat was not selected, so it should be nil.
"swtxs": int64(0), "swtxs": int64(0),
"total_size": totalSize, "total_size": totalSize,
@ -289,7 +292,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
t.Fatalf("Unable to generate block: %v from test %s", err, test.name) t.Fatalf("Unable to generate block: %v from test %s", err, test.name)
} }
blockStats, err := r.Node.GetBlockStats(block.Hash(), &test.stats) blockStats, err := r.GetBlockStats(block.Hash(), &test.stats)
if err != nil { if err != nil {
t.Fatalf("Call to `getblockstats` on test %s failed: %v", test.name, err) t.Fatalf("Call to `getblockstats` on test %s failed: %v", test.name, err)
} }

View file

@ -16,6 +16,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/lbryio/lbcd/btcjson"
"github.com/lbryio/lbcd/chaincfg" "github.com/lbryio/lbcd/chaincfg"
"github.com/lbryio/lbcd/chaincfg/chainhash" "github.com/lbryio/lbcd/chaincfg/chainhash"
"github.com/lbryio/lbcd/rpcclient" "github.com/lbryio/lbcd/rpcclient"
@ -512,6 +513,18 @@ func (h *Harness) GenerateAndSubmitBlockWithCustomCoinbaseOutputs(
return newBlock, nil return newBlock, nil
} }
// GetBlockStats returns block statistics. First argument specifies height or
// hash of the target block. Second argument allows to select certain stats to
// return. If second argument is empty, all stats are returned.
func (h *Harness) GetBlockStats(hashOrHeight interface{}, stats *[]string) (
*btcjson.GetBlockStatsResult, error) {
h.Lock()
defer h.Unlock()
return h.Client.GetBlockStats(hashOrHeight, stats)
}
// generateListeningAddresses returns two strings representing listening // generateListeningAddresses returns two strings representing listening
// addresses designated for the current rpc test. If there haven't been any // addresses designated for the current rpc test. If there haven't been any
// test instances created, the default ports are used. Otherwise, in order to // test instances created, the default ports are used. Otherwise, in order to

View file

@ -1635,16 +1635,19 @@ func handleGetBlockStats(s *rpcServer, cmd interface{}, closeChan <-chan struct{
return nil, internalRPCError(err.Error(), context) return nil, internalRPCError(err.Error(), context)
} }
selectedStats := c.Stats var selectedStats []string
if c.Stats != nil {
selectedStats = *c.Stats
}
// Create a set of selected stats to facilitate queries. // Create a set of selected stats to facilitate queries.
statsSet := make(map[string]bool) statsSet := make(map[string]bool)
for _, value := range *selectedStats { for _, value := range selectedStats {
statsSet[value] = true statsSet[value] = true
} }
// Return all stats if an empty array was provided. // Return all stats if an empty array was provided.
allStats := len(*selectedStats) == 0 allStats := len(selectedStats) == 0
calcFees := statsSet["avgfee"] || statsSet["avgfeerate"] || statsSet["maxfee"] || statsSet["maxfeerate"] || calcFees := statsSet["avgfee"] || statsSet["avgfeerate"] || statsSet["maxfee"] || statsSet["maxfeerate"] ||
statsSet["medianfee"] || statsSet["totalfee"] || statsSet["feerate_percentiles"] statsSet["medianfee"] || statsSet["totalfee"] || statsSet["feerate_percentiles"]