Skip to content

Commit 5744d56

Browse files
committed
Make relayFee an integer everywhere.
Because an atom cannot be divided, and we convert it to an integer already in a few places, it should be an integer and not a float from the beginning. Also fix docstrings.
1 parent a06b98b commit 5744d56

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

decred/decred/dcr/dcrdata.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -852,13 +852,25 @@ def saveBlockHeader(self, header):
852852
self.heightMap[header.height] = bHash
853853
self.headerDB[bHash] = header
854854

855-
def sendToAddress(self, value, address, keysource, utxosource, relayFee):
855+
def checkFeeRate(self, relayFee):
856+
"""
857+
Check that the relay fee is lower than the allowed max of
858+
txscript.HighFeeRate.
859+
"""
860+
if relayFee > txscript.HighFeeRate:
861+
raise DecredError(
862+
f"relay fee of {relayFee} is above the allowed max of {txscript.HighFeeRate}"
863+
)
864+
865+
def sendToAddress(
866+
self, value, address, keysource, utxosource, relayFee, allowHighFees=False
867+
):
856868
"""
857869
Send the amount in atoms to the specified address.
858870
859871
Args:
860-
value int: The amount to send, in atoms.
861-
address str: The base-58 encoded address.
872+
value (int): The amount to send, in atoms.
873+
address (str): The base-58 encoded address.
862874
keysource func(str) -> PrivateKey: A function that returns the
863875
private key for an address.
864876
utxosource func(int, func(UTXO) -> bool) -> list(UTXO): A function
@@ -867,11 +879,18 @@ def sendToAddress(self, value, address, keysource, utxosource, relayFee):
867879
amount. If the filtering function is provided, UTXOs for which
868880
the function return a falsey value will not be included in the
869881
returned UTXO list.
870-
MsgTx: The newly created transaction on success, `False` on failure.
882+
relayFee (int): Transaction fees in atoms per kb.
883+
allowHighFees (bool): Optional. Default is False. Whether to allow
884+
fees higher than txscript.HighFeeRate.
885+
886+
Returns:
887+
MsgTx: The newly created transaction. Raises an exception on error.
871888
"""
889+
if not allowHighFees:
890+
self.checkFeeRate(relayFee)
872891
self.updateTip()
873892
outputs = makeOutputs([(address, value)], self.netParams)
874-
return self.sendOutputs(outputs, keysource, utxosource, relayFee)
893+
return self.sendOutputs(outputs, keysource, utxosource, relayFee, allowHighFees)
875894

876895
def broadcast(self, txHex):
877896
"""
@@ -953,7 +972,9 @@ def confirmUTXO(self, utxo, block=None, tx=None):
953972
pass
954973
return False
955974

956-
def sendOutputs(self, outputs, keysource, utxosource, relayFee):
975+
def sendOutputs(
976+
self, outputs, keysource, utxosource, relayFee, allowHighFees=False
977+
):
957978
"""
958979
Send the `TxOut`s to the address.
959980
@@ -973,12 +994,17 @@ def sendOutputs(self, outputs, keysource, utxosource, relayFee):
973994
sufficient to complete the transaction. If the filtering
974995
function is provided, UTXOs for which the function return a
975996
falsey value will not be included in the returned UTXO list.
997+
relayFee (int): Transaction fees in atoms per kb.
998+
allowHighFees (bool): Optional. Default is False. Whether to allow
999+
fees higher than txscript.HighFeeRate.
9761000
9771001
Returns:
9781002
newTx MsgTx: The sent transaction.
9791003
utxos list(UTXO): The spent UTXOs.
9801004
newUTXOs list(UTXO): Length 1 array containing the new change UTXO.
9811005
"""
1006+
if not allowHighFees:
1007+
self.checkFeeRate(relayFee)
9821008
total = 0
9831009
inputs = []
9841010
scripts = []
@@ -1100,7 +1126,9 @@ def sendOutputs(self, outputs, keysource, utxosource, relayFee):
11001126

11011127
return newTx, utxos, newUTXOs
11021128

1103-
def purchaseTickets(self, keysource, utxosource, req, relayFee):
1129+
def purchaseTickets(
1130+
self, keysource, utxosource, req, relayFee, allowHighFees=False
1131+
):
11041132
"""
11051133
Based on dcrwallet (*Wallet).purchaseTickets.
11061134
purchaseTickets indicates to the wallet that a ticket should be
@@ -1115,6 +1143,9 @@ def purchaseTickets(self, keysource, utxosource, req, relayFee):
11151143
UTXOs. The filterFunc is an optional function to filter UTXOs,
11161144
and is of the form func(UTXO) -> bool.
11171145
req account.TicketRequest: the ticket data.
1146+
relayFee (int): Transaction fees in atoms per kb.
1147+
allowHighFees (bool): Optional. Default is False. Whether to allow
1148+
fees higher than txscript.HighFeeRate.
11181149
11191150
Returns:
11201151
(splitTx, tickets) tuple: first element is the split transaction.
@@ -1125,6 +1156,8 @@ def purchaseTickets(self, keysource, utxosource, req, relayFee):
11251156
addresses.
11261157
11271158
"""
1159+
if not allowHighFees:
1160+
self.checkFeeRate(relayFee)
11281161
self.updateTip()
11291162
# account minConf is zero for regular outputs for now. Need to make that
11301163
# adjustable.
@@ -1267,7 +1300,7 @@ def purchaseTickets(self, keysource, utxosource, req, relayFee):
12671300
# Send the split transaction.
12681301
# sendOutputs takes the fee rate in atoms/byte
12691302
splitTx, splitSpent, internalOutputs = self.sendOutputs(
1270-
splitOuts, keysource, utxosource, int(txFeeIncrement / 1000)
1303+
splitOuts, keysource, utxosource, txFeeIncrement // 1000, allowHighFees
12711304
)
12721305

12731306
# Generate the tickets individually.
@@ -1363,7 +1396,7 @@ def purchaseTickets(self, keysource, utxosource, req, relayFee):
13631396
)
13641397
return (splitTx, tickets), splitSpent, internalOutputs
13651398

1366-
def revokeTicket(self, tx, keysource, redeemScript, relayFee):
1399+
def revokeTicket(self, tx, keysource, redeemScript, relayFee, allowHighFees=False):
13671400
"""
13681401
Revoke a ticket by signing the supplied redeem script and broadcasting
13691402
the raw transaction.
@@ -1374,10 +1407,15 @@ def revokeTicket(self, tx, keysource, redeemScript, relayFee):
13741407
the private key used for signing.
13751408
redeemScript (byte-like): the 1-of-2 multisig script that delegates
13761409
voting rights for the ticket.
1410+
relayFee (int): Transaction fees in atoms per kb.
1411+
allowHighFees (bool): Optional. Default is False. Whether to allow
1412+
fees higher than txscript.HighFeeRate.
13771413
13781414
Returns:
13791415
MsgTx: the signed revocation.
13801416
"""
1417+
if not allowHighFees:
1418+
self.checkFeeRate(relayFee)
13811419

13821420
revocation = txscript.makeRevocation(tx, relayFee)
13831421

decred/decred/dcr/txscript.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@
154154
# to maintain reference.
155155

156156
# DefaultRelayFeePerKb is the default minimum relay fee policy for a mempool.
157-
DefaultRelayFeePerKb = 1e4
157+
DefaultRelayFeePerKb = int(1e4)
158+
159+
# HighFeeRate is the atoms/kb rate that is considered too high.
160+
HighFeeRate = DefaultRelayFeePerKb * 1000
158161

159162
# MaxStandardTxSize is the maximum size allowed for transactions that are
160163
# considered standard and will therefore be relayed and considered for mining.
@@ -2605,7 +2608,7 @@ def paysHighFees(totalInput, tx):
26052608
# Impossible to determine
26062609
return False
26072610

2608-
maxFee = calcMinRequiredTxRelayFee(1000 * DefaultRelayFeePerKb, tx.serializeSize())
2611+
maxFee = calcMinRequiredTxRelayFee(HighFeeRate, tx.serializeSize())
26092612
return fee > maxFee
26102613

26112614

@@ -3406,7 +3409,7 @@ def calcMinRequiredTxRelayFee(relayFeePerKb, txSerializeSize):
34063409
pool and relayed.
34073410
34083411
Args:
3409-
relayFeePerKb (float): The fee per kilobyte.
3412+
relayFeePerKb (int): The fee in atoms per kilobyte.
34103413
txSerializeSize int: (Size) of the byte-encoded transaction.
34113414
34123415
Returns:
@@ -3436,7 +3439,7 @@ def isDustAmount(amount, scriptSize, relayFeePerKb):
34363439
Args:
34373440
amount (int): Atoms.
34383441
scriptSize (int): Byte-size of the script.
3439-
relayFeePerKb (float): Fees paid per kilobyte.
3442+
relayFeePerKb (int): Fees paid in atoms per kilobyte.
34403443
34413444
Returns:
34423445
bool: True if the amount is considered dust.
@@ -3495,7 +3498,7 @@ def isDustOutput(output, relayFeePerKb):
34953498
34963499
Args:
34973500
output (wire.TxOut): The transaction output.
3498-
relayFeePerKb: Minimum transaction fee allowable.
3501+
relayFeePerKb (int): The transaction fee in atoms per kb.
34993502
35003503
Returns:
35013504
bool: True if output is a dust output.
@@ -3770,7 +3773,7 @@ def stakePoolTicketFee(stakeDiff, relayFee, height, poolFee, subsidyCache, netPa
37703773
37713774
Args:
37723775
stakeDiff (int): The ticket price.
3773-
relayFee (int): Transaction fees.
3776+
relayFee (int): Transaction fees in atoms per kb.
37743777
height (int): Current block height.
37753778
poolFee (int): The pools fee, as percent.
37763779
subsidyCache (calc.SubsidyCache): A subsidy cache.
@@ -3805,8 +3808,8 @@ def stakePoolTicketFee(stakeDiff, relayFee, height, poolFee, subsidyCache, netPa
38053808
# The numerator is (p*10000*s*(v+z)) << 64.
38063809
shift = 64
38073810
s = subsidy
3808-
v = int(stakeDiff)
3809-
z = int(relayFee)
3811+
v = stakeDiff
3812+
z = relayFee
38103813
num = poolFeeInt
38113814
num *= s
38123815
vPlusZ = v + z

0 commit comments

Comments
 (0)