From 81ec217899796e85cd68b57c46c34173477179aa Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Tue, 20 Sep 2022 23:39:34 -0700 Subject: [PATCH] rpcserver: fix up getblockstats --- integration/rpcserver_test.go | 17 ++++++++++------- integration/rpctest/rpc_harness.go | 13 +++++++++++++ rpcserver.go | 9 ++++++--- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/integration/rpcserver_test.go b/integration/rpcserver_test.go index 970454c7..5f59b594 100644 --- a/integration/rpcserver_test.go +++ b/integration/rpcserver_test.go @@ -21,6 +21,9 @@ import ( "github.com/lbryio/lbcd/chaincfg/chainhash" "github.com/lbryio/lbcd/integration/rpctest" "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) { @@ -143,7 +146,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) { baseFeeRate := int64(10) txValue := int64(50000000) txQuantity := 10 - txs := make([]*btcutil.Tx, txQuantity) + txs := make([]*lbcutil.Tx, txQuantity) fees := make([]int64, txQuantity) sizes := 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. 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 { t.Fatalf("Unable to generate segwit transaction: %v", err) } - txs[i] = btcutil.NewTx(tx) + txs[i] = lbcutil.NewTx(tx) sizes[i] = int64(tx.SerializeSize()) // memWallet.fundTx makes some assumptions when calculating fees. @@ -215,13 +218,13 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) { tests := []struct { name string - txs []*btcutil.Tx + txs []*lbcutil.Tx stats []string expectedResults map[string]interface{} }{ { name: "empty block", - txs: []*btcutil.Tx{}, + txs: []*lbcutil.Tx{}, stats: []string{}, expectedResults: map[string]interface{}{ "avgfee": int64(0), @@ -270,7 +273,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) { "minfeerate": minFeeRate, "mintxsize": minSize, "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. "swtxs": int64(0), "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) } - blockStats, err := r.Node.GetBlockStats(block.Hash(), &test.stats) + blockStats, err := r.GetBlockStats(block.Hash(), &test.stats) if err != nil { t.Fatalf("Call to `getblockstats` on test %s failed: %v", test.name, err) } diff --git a/integration/rpctest/rpc_harness.go b/integration/rpctest/rpc_harness.go index 17603aa7..4bd72f9c 100644 --- a/integration/rpctest/rpc_harness.go +++ b/integration/rpctest/rpc_harness.go @@ -16,6 +16,7 @@ import ( "testing" "time" + "github.com/lbryio/lbcd/btcjson" "github.com/lbryio/lbcd/chaincfg" "github.com/lbryio/lbcd/chaincfg/chainhash" "github.com/lbryio/lbcd/rpcclient" @@ -512,6 +513,18 @@ func (h *Harness) GenerateAndSubmitBlockWithCustomCoinbaseOutputs( 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 // 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 diff --git a/rpcserver.go b/rpcserver.go index 3cc71472..28b0f670 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1635,16 +1635,19 @@ func handleGetBlockStats(s *rpcServer, cmd interface{}, closeChan <-chan struct{ 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. statsSet := make(map[string]bool) - for _, value := range *selectedStats { + for _, value := range selectedStats { statsSet[value] = true } // 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"] || statsSet["medianfee"] || statsSet["totalfee"] || statsSet["feerate_percentiles"]