2 # Copyright (c) 2014-2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test fee estimation code."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
9 from test_framework
.script
import CScript
, OP_1
, OP_DROP
, OP_2
, OP_HASH160
, OP_EQUAL
, hash160
, OP_TRUE
10 from test_framework
.mininode
import CTransaction
, CTxIn
, CTxOut
, COutPoint
, ToHex
, COIN
12 # Construct 2 trivial P2SH's and the ScriptSigs that spend them
13 # So we can create many transactions without needing to spend
15 redeem_script_1
= CScript([OP_1
, OP_DROP
])
16 redeem_script_2
= CScript([OP_2
, OP_DROP
])
17 P2SH_1
= CScript([OP_HASH160
, hash160(redeem_script_1
), OP_EQUAL
])
18 P2SH_2
= CScript([OP_HASH160
, hash160(redeem_script_2
), OP_EQUAL
])
20 # Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
21 SCRIPT_SIG
= [CScript([OP_TRUE
, redeem_script_1
]), CScript([OP_TRUE
, redeem_script_2
])]
25 def small_txpuzzle_randfee(from_node
, conflist
, unconflist
, amount
, min_fee
, fee_increment
):
27 Create and send a transaction with a random fee.
28 The transaction pays to a trivial P2SH script, and assumes that its inputs
30 The function takes a list of confirmed outputs and unconfirmed outputs
31 and attempts to use the confirmed list first for its inputs.
32 It adds the newly created outputs to the unconfirmed list.
33 Returns (raw transaction, fee)
35 # It's best to exponentially distribute our random fees
36 # because the buckets are exponentially spaced.
37 # Exponentially distributed from 1-128 * fee_increment
38 rand_fee
= float(fee_increment
)*(1.1892**random
.randint(0,28))
39 # Total fee ranges from min_fee to min_fee + 127*fee_increment
40 fee
= min_fee
- fee_increment
+ satoshi_round(rand_fee
)
42 total_in
= Decimal("0.00000000")
43 while total_in
<= (amount
+ fee
) and len(conflist
) > 0:
45 total_in
+= t
["amount"]
46 tx
.vin
.append(CTxIn(COutPoint(int(t
["txid"], 16), t
["vout"]), b
""))
47 if total_in
<= amount
+ fee
:
48 while total_in
<= (amount
+ fee
) and len(unconflist
) > 0:
50 total_in
+= t
["amount"]
51 tx
.vin
.append(CTxIn(COutPoint(int(t
["txid"], 16), t
["vout"]), b
""))
52 if total_in
<= amount
+ fee
:
53 raise RuntimeError("Insufficient funds: need %d, have %d"%(amount
+fee
, total_in
))
54 tx
.vout
.append(CTxOut(int((total_in
- amount
- fee
)*COIN
), P2SH_1
))
55 tx
.vout
.append(CTxOut(int(amount
*COIN
), P2SH_2
))
56 # These transactions don't need to be signed, but we still have to insert
57 # the ScriptSig that will satisfy the ScriptPubKey.
59 inp
.scriptSig
= SCRIPT_SIG
[inp
.prevout
.n
]
60 txid
= from_node
.sendrawtransaction(ToHex(tx
), True)
61 unconflist
.append({ "txid" : txid
, "vout" : 0 , "amount" : total_in
- amount
- fee
})
62 unconflist
.append({ "txid" : txid
, "vout" : 1 , "amount" : amount
})
64 return (ToHex(tx
), fee
)
66 def split_inputs(from_node
, txins
, txouts
, initial_split
= False):
68 We need to generate a lot of inputs so we can generate a ton of transactions.
69 This function takes an input from txins, and creates and sends a transaction
70 which splits the value into 2 outputs which are appended to txouts.
71 Previously this was designed to be small inputs so they wouldn't have
72 a high coin age when the notion of priority still existed.
74 prevtxout
= txins
.pop()
76 tx
.vin
.append(CTxIn(COutPoint(int(prevtxout
["txid"], 16), prevtxout
["vout"]), b
""))
78 half_change
= satoshi_round(prevtxout
["amount"]/2)
79 rem_change
= prevtxout
["amount"] - half_change
- Decimal("0.00001000")
80 tx
.vout
.append(CTxOut(int(half_change
*COIN
), P2SH_1
))
81 tx
.vout
.append(CTxOut(int(rem_change
*COIN
), P2SH_2
))
83 # If this is the initial split we actually need to sign the transaction
84 # Otherwise we just need to insert the proper ScriptSig
86 completetx
= from_node
.signrawtransaction(ToHex(tx
))["hex"]
88 tx
.vin
[0].scriptSig
= SCRIPT_SIG
[prevtxout
["vout"]]
89 completetx
= ToHex(tx
)
90 txid
= from_node
.sendrawtransaction(completetx
, True)
91 txouts
.append({ "txid" : txid
, "vout" : 0 , "amount" : half_change
})
92 txouts
.append({ "txid" : txid
, "vout" : 1 , "amount" : rem_change
})
94 def check_estimates(node
, fees_seen
, max_invalid
, print_estimates
= True):
96 This function calls estimatefee and verifies that the estimates
97 meet certain invariants.
99 all_estimates
= [ node
.estimatefee(i
) for i
in range(1,26) ]
101 log
.info([str(all_estimates
[e
-1]) for e
in [1,2,3,6,15,25]])
102 delta
= 1.0e-6 # account for rounding error
103 last_e
= max(fees_seen
)
104 for e
in [x
for x
in all_estimates
if x
>= 0]:
105 # Estimates should be within the bounds of what transactions fees actually were:
106 if float(e
)+delta
< min(fees_seen
) or float(e
)-delta
> max(fees_seen
):
107 raise AssertionError("Estimated fee (%f) out of range (%f,%f)"
108 %(float(e
), min(fees_seen
), max(fees_seen
)))
109 # Estimates should be monotonically decreasing
110 if float(e
)-delta
> last_e
:
111 raise AssertionError("Estimated fee (%f) larger than last fee (%f) for lower number of confirms"
112 %(float(e
),float(last_e
)))
114 valid_estimate
= False
115 invalid_estimates
= 0
116 for i
,e
in enumerate(all_estimates
): # estimate is for i+1
118 valid_estimate
= True
119 if i
>= 13: # for n>=14 estimatesmartfee(n/2) should be at least as high as estimatefee(n)
120 assert(node
.estimatesmartfee((i
+1)//2)["feerate"] > float(e
) - delta
)
123 invalid_estimates
+= 1
125 # estimatesmartfee should still be valid
126 approx_estimate
= node
.estimatesmartfee(i
+1)["feerate"]
127 answer_found
= node
.estimatesmartfee(i
+1)["blocks"]
128 assert(approx_estimate
> 0)
129 assert(answer_found
> i
+1)
131 # Once we're at a high enough confirmation count that we can give an estimate
132 # We should have estimates for all higher confirmation counts
134 raise AssertionError("Invalid estimate appears at higher confirm count than valid estimate")
136 # Check on the expected number of different confirmation counts
137 # that we might not have valid estimates for
138 if invalid_estimates
> max_invalid
:
139 raise AssertionError("More than (%d) invalid estimates"%(max_invalid))
143 class EstimateFeeTest(BitcoinTestFramework
):
144 def set_test_params(self
):
147 def setup_network(self
):
149 We'll setup the network to have 3 nodes that all mine with different parameters.
150 But first we need to use one node to create a lot of outputs
151 which we will use to generate our transactions.
153 self
.add_nodes(3, extra_args
=[["-maxorphantx=1000", "-whitelist=127.0.0.1"],
154 ["-blockmaxsize=17000", "-maxorphantx=1000", "-deprecatedrpc=estimatefee"],
155 ["-blockmaxsize=8000", "-maxorphantx=1000"]])
156 # Use node0 to mine blocks for input splitting
157 # Node1 mines small blocks but that are bigger than the expected transaction rate.
158 # NOTE: the CreateNewBlock code starts counting block size at 1,000 bytes,
159 # (17k is room enough for 110 or so transactions)
160 # Node2 is a stingy miner, that
161 # produces too small blocks (room for only 55 or so transactions)
164 def transact_and_mine(self
, numblocks
, mining_node
):
165 min_fee
= Decimal("0.00001")
166 # We will now mine numblocks blocks generating on average 100 transactions between each block
167 # We shuffle our confirmed txout set before each set of transactions
168 # small_txpuzzle_randfee will use the transactions that have inputs already in the chain when possible
169 # resorting to tx's that depend on the mempool when those run out
170 for i
in range(numblocks
):
171 random
.shuffle(self
.confutxo
)
172 for j
in range(random
.randrange(100-50,100+50)):
173 from_index
= random
.randint(1,2)
174 (txhex
, fee
) = small_txpuzzle_randfee(self
.nodes
[from_index
], self
.confutxo
,
175 self
.memutxo
, Decimal("0.005"), min_fee
, min_fee
)
176 tx_kbytes
= (len(txhex
) // 2) / 1000.0
177 self
.fees_per_kb
.append(float(fee
)/tx_kbytes
)
178 sync_mempools(self
.nodes
[0:3], wait
=.1)
179 mined
= mining_node
.getblock(mining_node
.generate(1)[0],True)["tx"]
180 sync_blocks(self
.nodes
[0:3], wait
=.1)
181 # update which txouts are confirmed
183 for utx
in self
.memutxo
:
184 if utx
["txid"] in mined
:
185 self
.confutxo
.append(utx
)
188 self
.memutxo
= newmem
191 self
.log
.info("This test is time consuming, please be patient")
192 self
.log
.info("Splitting inputs so we can generate tx's")
194 # Make log handler available to helper functions
202 # Split a coinbase into two transaction puzzle outputs
203 split_inputs(self
.nodes
[0], self
.nodes
[0].listunspent(0), self
.txouts
, True)
206 while (len(self
.nodes
[0].getrawmempool()) > 0):
207 self
.nodes
[0].generate(1)
209 # Repeatedly split those 2 outputs, doubling twice for each rep
210 # Use txouts to monitor the available utxo, since these won't be tracked in wallet
213 #Double txouts to txouts2
214 while (len(self
.txouts
)>0):
215 split_inputs(self
.nodes
[0], self
.txouts
, self
.txouts2
)
216 while (len(self
.nodes
[0].getrawmempool()) > 0):
217 self
.nodes
[0].generate(1)
218 #Double txouts2 to txouts
219 while (len(self
.txouts2
)>0):
220 split_inputs(self
.nodes
[0], self
.txouts2
, self
.txouts
)
221 while (len(self
.nodes
[0].getrawmempool()) > 0):
222 self
.nodes
[0].generate(1)
224 self
.log
.info("Finished splitting")
226 # Now we can connect the other nodes, didn't want to connect them earlier
227 # so the estimates would not be affected by the splitting transactions
230 connect_nodes(self
.nodes
[1], 0)
231 connect_nodes(self
.nodes
[0], 2)
232 connect_nodes(self
.nodes
[2], 1)
236 self
.fees_per_kb
= []
238 self
.confutxo
= self
.txouts
# Start with the set of confirmed txouts after splitting
239 self
.log
.info("Will output estimates for 1/2/3/6/15/25 blocks")
242 self
.log
.info("Creating transactions and mining them with a block size that can't keep up")
243 # Create transactions and mine 10 small blocks with node 2, but create txs faster than we can mine
244 self
.transact_and_mine(10, self
.nodes
[2])
245 check_estimates(self
.nodes
[1], self
.fees_per_kb
, 14)
247 self
.log
.info("Creating transactions and mining them at a block size that is just big enough")
248 # Generate transactions while mining 10 more blocks, this time with node1
249 # which mines blocks with capacity just above the rate that transactions are being created
250 self
.transact_and_mine(10, self
.nodes
[1])
251 check_estimates(self
.nodes
[1], self
.fees_per_kb
, 2)
253 # Finish by mining a normal-sized block:
254 while len(self
.nodes
[1].getrawmempool()) > 0:
255 self
.nodes
[1].generate(1)
257 sync_blocks(self
.nodes
[0:3], wait
=.1)
258 self
.log
.info("Final estimates after emptying mempools")
259 check_estimates(self
.nodes
[1], self
.fees_per_kb
, 2)
261 if __name__
== '__main__':
262 EstimateFeeTest().main()