Skip to content

Commit

Permalink
rpcserver: fix up getblockstats
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 21, 2022
1 parent 5acfa4c commit 81ec217
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
17 changes: 10 additions & 7 deletions integration/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
13 changes: 13 additions & 0 deletions integration/rpctest/rpc_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down

0 comments on commit 81ec217

Please sign in to comment.