Skip to content

Commit 81ec217

Browse files
committed
rpcserver: fix up getblockstats
1 parent 5acfa4c commit 81ec217

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

integration/rpcserver_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"github.com/lbryio/lbcd/chaincfg/chainhash"
2222
"github.com/lbryio/lbcd/integration/rpctest"
2323
"github.com/lbryio/lbcd/rpcclient"
24+
"github.com/lbryio/lbcd/txscript"
25+
"github.com/lbryio/lbcd/wire"
26+
"github.com/lbryio/lbcutil"
2427
)
2528

2629
func testGetBestBlock(r *rpctest.Harness, t *testing.T) {
@@ -143,7 +146,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
143146
baseFeeRate := int64(10)
144147
txValue := int64(50000000)
145148
txQuantity := 10
146-
txs := make([]*btcutil.Tx, txQuantity)
149+
txs := make([]*lbcutil.Tx, txQuantity)
147150
fees := make([]int64, txQuantity)
148151
sizes := make([]int64, txQuantity)
149152
feeRates := make([]int64, txQuantity)
@@ -164,12 +167,12 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
164167
// This feerate is not the actual feerate. See comment below.
165168
feeRate := baseFeeRate * int64(i)
166169

167-
tx, err := r.CreateTransaction([]*wire.TxOut{wire.NewTxOut(txValue, pkScript)}, btcutil.Amount(feeRate), true)
170+
tx, err := r.CreateTransaction([]*wire.TxOut{wire.NewTxOut(txValue, pkScript)}, lbcutil.Amount(feeRate), true)
168171
if err != nil {
169172
t.Fatalf("Unable to generate segwit transaction: %v", err)
170173
}
171174

172-
txs[i] = btcutil.NewTx(tx)
175+
txs[i] = lbcutil.NewTx(tx)
173176
sizes[i] = int64(tx.SerializeSize())
174177

175178
// memWallet.fundTx makes some assumptions when calculating fees.
@@ -215,13 +218,13 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
215218

216219
tests := []struct {
217220
name string
218-
txs []*btcutil.Tx
221+
txs []*lbcutil.Tx
219222
stats []string
220223
expectedResults map[string]interface{}
221224
}{
222225
{
223226
name: "empty block",
224-
txs: []*btcutil.Tx{},
227+
txs: []*lbcutil.Tx{},
225228
stats: []string{},
226229
expectedResults: map[string]interface{}{
227230
"avgfee": int64(0),
@@ -270,7 +273,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
270273
"minfeerate": minFeeRate,
271274
"mintxsize": minSize,
272275
"outs": int64(outputCount + 1), // Coinbase output also counts.
273-
"subsidy": int64(5000000000),
276+
"subsidy": int64(100000000),
274277
"swtotal_weight": nil, // This stat was not selected, so it should be nil.
275278
"swtxs": int64(0),
276279
"total_size": totalSize,
@@ -289,7 +292,7 @@ func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
289292
t.Fatalf("Unable to generate block: %v from test %s", err, test.name)
290293
}
291294

292-
blockStats, err := r.Node.GetBlockStats(block.Hash(), &test.stats)
295+
blockStats, err := r.GetBlockStats(block.Hash(), &test.stats)
293296
if err != nil {
294297
t.Fatalf("Call to `getblockstats` on test %s failed: %v", test.name, err)
295298
}

integration/rpctest/rpc_harness.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"testing"
1717
"time"
1818

19+
"github.com/lbryio/lbcd/btcjson"
1920
"github.com/lbryio/lbcd/chaincfg"
2021
"github.com/lbryio/lbcd/chaincfg/chainhash"
2122
"github.com/lbryio/lbcd/rpcclient"
@@ -512,6 +513,18 @@ func (h *Harness) GenerateAndSubmitBlockWithCustomCoinbaseOutputs(
512513
return newBlock, nil
513514
}
514515

516+
// GetBlockStats returns block statistics. First argument specifies height or
517+
// hash of the target block. Second argument allows to select certain stats to
518+
// return. If second argument is empty, all stats are returned.
519+
func (h *Harness) GetBlockStats(hashOrHeight interface{}, stats *[]string) (
520+
*btcjson.GetBlockStatsResult, error) {
521+
522+
h.Lock()
523+
defer h.Unlock()
524+
525+
return h.Client.GetBlockStats(hashOrHeight, stats)
526+
}
527+
515528
// generateListeningAddresses returns two strings representing listening
516529
// addresses designated for the current rpc test. If there haven't been any
517530
// test instances created, the default ports are used. Otherwise, in order to

rpcserver.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,16 +1635,19 @@ func handleGetBlockStats(s *rpcServer, cmd interface{}, closeChan <-chan struct{
16351635
return nil, internalRPCError(err.Error(), context)
16361636
}
16371637

1638-
selectedStats := c.Stats
1638+
var selectedStats []string
1639+
if c.Stats != nil {
1640+
selectedStats = *c.Stats
1641+
}
16391642

16401643
// Create a set of selected stats to facilitate queries.
16411644
statsSet := make(map[string]bool)
1642-
for _, value := range *selectedStats {
1645+
for _, value := range selectedStats {
16431646
statsSet[value] = true
16441647
}
16451648

16461649
// Return all stats if an empty array was provided.
1647-
allStats := len(*selectedStats) == 0
1650+
allStats := len(selectedStats) == 0
16481651
calcFees := statsSet["avgfee"] || statsSet["avgfeerate"] || statsSet["maxfee"] || statsSet["maxfeerate"] ||
16491652
statsSet["medianfee"] || statsSet["totalfee"] || statsSet["feerate_percentiles"]
16501653

0 commit comments

Comments
 (0)