<p>You can use a wrapper to mock the feerate.</p>
<p>Let's say you are writing your tests in Python. You can set up a Python RPC wrapper for the <code>bitcoind</code> interface, and optionally turn on a mock for specific tests (instead of calling <code>bitcoind</code>'s <code>estimatesmartfee</code> it would return a preset value).</p>
<p>That is how it's done in C-lightning's functional tests. A good instance is <a href="https://github.com/ElementsProject/lightning/blob/d41b383cf7e6258b1defd5f712a0b59ce2a7c0ab/tests/test_misc.py#L102-L140" rel="nofollow noreferrer">this test</a> which simulates a crash of <code>bitcoind</code> to test <code>lightningd</code> can support spurious failures of its Bitcoin backend.<br />
See also a sample of feerate mock <a href="https://github.com/ElementsProject/lightning/blob/d41b383cf7e6258b1defd5f712a0b59ce2a7c0ab/contrib/pyln-testing/pyln/testing/utils.py#L957-L990" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2021-01-06T12:48:26.370Z
2021-01-06T12:48:26.370Z
Antoine Poinsot
<p>I have created a bash script that is less complex than the Python script that @darosior references, but is heavily influenced by it. It keeps creating sets of 10-30 transactions until <code>estimatesmartfee</code> returns a fee rate.</p>
<pre><code>#!/usr/bin/env bash

set -eo pipefail

cont=true
smartfee=$(bitcoin-cli estimatesmartfee 6)
if [[ &quot;$smartfee&quot; == *&quot;\&quot;feerate\&quot;:&quot;* ]]; then
    cont=false
fi
while $cont
do
    counterb=0
    range=$(( $RANDOM % 11 + 20 ))
    while [ $counterb -lt $range ]
    do
        power=$(( $RANDOM % 29 ))
        randfee=`echo &quot;scale=8; 0.00001 * (1.1892 ^ $power)&quot; | bc`
        newaddress=$(bitcoin-cli getnewaddress)
        rawtx=$(bitcoin-cli createrawtransaction &quot;[]&quot; &quot;[{\&quot;$newaddress\&quot;:0.005}]&quot;)
        fundedtx=$(bitcoin-cli fundrawtransaction &quot;$rawtx&quot; &quot;{\&quot;feeRate\&quot;: \&quot;0$randfee\&quot;}&quot; | jq -r &quot;.hex&quot;)
        signedtx=$(bitcoin-cli signrawtransactionwithwallet &quot;$fundedtx&quot; | jq -r &quot;.hex&quot;)
        senttx=$(bitcoin-cli sendrawtransaction &quot;$signedtx&quot;)
        ((++counterb))
        echo &quot;Created $counterb transactions this block&quot;
    done
    bitcoin-cli generatetoaddress 1 &quot;mp76nrashrCCYLy3a8cAc5HufEas11yHbh&quot;
    smartfee=$(bitcoin-cli estimatesmartfee 6)
    if [[ &quot;$smartfee&quot; == *&quot;\&quot;feerate\&quot;:&quot;* ]]; then
        cont=false
    fi
done
bitcoin-cli generatetoaddress 6 &quot;mp76nrashrCCYLy3a8cAc5HufEas11yHbh&quot;
</code></pre>

CC BY-SA 4.0
2021-06-29T07:18:39.227Z
2021-06-29T07:18:39.227Z
gijswijs
<p>I'm setting up some automated testing for a lightning wallet. At the heart of my tests is a bitcoind instance running in regtest mode. I need to be able to set the fee rate - both the fee rate reported by <code>estimatesmartfee</code> and the fee rate which transactions need to match in order to get confirmed.</p>
<p>So far, the only way I can see to do this is to mine lots of blocks filled with transactions that have the desired fee rate. I'm doing this by calling <code>sendtoaddress</code> (via bitcoin-cli) repeatedly in a loop to send small amounts of money into the void. This is painfully slow - it involves generating ~2500 transactions before bitcoind catches the drift that this should be the new reported fee rate. And I have to do this for every test, sometimes multiple times per test, so my CI is taking forever.</p>
<p>I'm wondering if there's any way to avoid doing this. Is there some way to override bitcoind's estimated fee rate and have it refuse to include transactions which pay less than that fee rate when mining blocks? If not, are there any quicker ways to pad out blocks with transactions than what I'm doing?</p>

<bitcoind><bitcoin-cli><regtest><testing><fee-estimation>
Setting the fee rate on regtest, or, quickly generating full blocks
Shum
# Setting the fee rate on regtest, or, quickly generating full blocks

(Asked by: username_0 on 2021-01-06)

I'm setting up some automated testing for a lightning wallet. At the heart of my tests is a bitcoind instance running in regtest mode. I need to be able to set the fee rate - both the fee rate reported by `estimatesmartfee` and the fee rate which transactions need to match in order to get confirmed.

So far, the only way I can see to do this is to mine lots of blocks filled with transactions that have the desired fee rate. I'm doing this by calling `sendtoaddress` (via bitcoin-cli) repeatedly in a loop to send small amounts of money into the void. This is painfully slow - it involves generating ~2500 transactions before bitcoind catches the drift that this should be the new reported fee rate. And I have to do this for every test, sometimes multiple times per test, so my CI is taking forever.

I'm wondering if there's any way to avoid doing this. Is there some way to override bitcoind's estimated fee rate and have it refuse to include transactions which pay less than that fee rate when mining blocks? If not, are there any quicker ways to pad out blocks with transactions than what I'm doing?

**Tags:** bitcoind, bitcoin-cli, regtest, testing, fee-estimation


---

## 2 answers

### Answer by username_1 on 2021-01-06. Score: 2

You can use a wrapper to mock the feerate.

Let's say you are writing your tests in Python. You can set up a Python RPC wrapper for the `bitcoind` interface, and optionally turn on a mock for specific tests (instead of calling `bitcoind`'s `estimatesmartfee` it would return a preset value).

That is how it's done in C-lightning's functional tests. A good instance is [this test](https://github.com/ElementsProject/lightning/blob/d41b383cf7e6258b1defd5f712a0b59ce2a7c0ab/tests/test_misc.py#L102-L140) which simulates a crash of `bitcoind` to test `lightningd` can support spurious failures of its Bitcoin backend.  
See also a sample of feerate mock [here](https://github.com/ElementsProject/lightning/blob/d41b383cf7e6258b1defd5f712a0b59ce2a7c0ab/contrib/pyln-testing/pyln/testing/utils.py#L957-L990).


---

### Answer by username_2 on 2021-06-29. Score: 2

I have created a bash script that is less complex than the Python script that @darosior references, but is heavily influenced by it. It keeps creating sets of 10-30 transactions until estimatesmartfee returns a fee rate.
#!/usr/bin/env bash

set -eo pipefail

cont=true
smartfee=$(bitcoin-cli estimatesmartfee 6)
if [[ "$smartfee" == *"\"feerate\":"* ]]; then
    cont=false
fi
while $cont
do
    counterb=0
    range=$(( $RANDOM % 11 + 20 ))
    while [ $counterb -lt $range ]
    do
        power=$(( $RANDOM % 29 ))
        randfee=`echo "scale=8; 0.00001 * (1.1892 ^ $power)" | bc`
        newaddress=$(bitcoin-cli getnewaddress)
        rawtx=$(bitcoin-cli createrawtransaction "[]" "[{\"$newaddress\":0.005}]")
        fundedtx=$(bitcoin-cli fundrawtransaction "$rawtx" "{\"feeRate\": \"0$randfee\"}" | jq -r ".hex")
        signedtx=$(bitcoin-cli signrawtransactionwithwallet "$fundedtx" | jq -r ".hex")
        senttx=$(bitcoin-cli sendrawtransaction "$signedtx")
        ((++counterb))
        echo "Created $counterb transactions this block"
    done
    bitcoin-cli generatetoaddress 1 "mp76nrashrCCYLy3a8cAc5HufEas11yHbh"
    smartfee=$(bitcoin-cli estimatesmartfee 6)
    if [[ "$smartfee" == *"\"feerate\":"* ]]; then
        cont=false
    fi
done
bitcoin-cli generatetoaddress 6 "mp76nrashrCCYLy3a8cAc5HufEas11yHbh"


---

