Skip to content

Commit

Permalink
Remove relayFee parameter from purchaseTickets.
Browse files Browse the repository at this point in the history
The relay fee is already included in the req, which the caller has
defined. They can change the relay fee there.
  • Loading branch information
JoeGruffins committed Apr 27, 2020
1 parent 5744d56 commit 352e4a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
55 changes: 33 additions & 22 deletions decred/decred/dcr/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,50 @@ class TicketRequest:
"""
The TicketRequest is required to purchase tickets.
"""

def __init__(
self,
minConf,
expiry,
spendLimit,
poolAddress,
votingAddress,
ticketFee,
poolFees,
count,
txFee,
ticketFee=0,
):
# minConf is just a placeholder for now. Account minconf is 0 until
# I add the ability to change it.
"""
TicketRequest constructor.
Args:
minConf (int): minConf is just a placeholder for now. Account
minconf is 0 until I add the ability to change it.
expiry (int): expiry can be set to some reasonable block height.
This may be important when approaching the end of a ticket
window.
spendLimit (int): Price is calculated purely from the ticket count,
price, and fees, but cannot go over spendLimit.
poolAddress (str): The VSP fee payment address.
votingAddress (str): The P2SH voting address based on the 1-of-2.
multi-sig script you share with the VSP.
poolFees (int): poolFees are set by the VSP. If you don't set these
correctly, the VSP may not vote for you.
count (int): How many tickets to buy.
txFee (int): txFee is the transaction fee rate to pay the miner for
the split transaction required to fund the ticket.
ticketFee (int): Optional. Default is the network's default relay
fee. ticketFee is the transaction fee rate to pay the miner for
the ticket.
"""
self.minConf = minConf
# expiry can be set to some reasonable block height. This may be
# important when approaching the end of a ticket window.
self.expiry = expiry
# Price is calculated purely from the ticket count, price, and fees, but
# cannot go over spendLimit.
self.spendLimit = spendLimit
# The VSP fee payment address.
self.poolAddress = poolAddress
# The P2SH voting address based on the 1-of-2 multi-sig script you share
# with the VSP.
self.votingAddress = votingAddress
# ticketFee is the transaction fee rate to pay the miner for the ticket.
# Set to zero to use wallet's network default fee rate.
self.ticketFee = ticketFee
# poolFees are set by the VSP. If you don't set these correctly, the
# VSP may not vote for you.
self.poolFees = poolFees
# How many tickets to buy.
self.count = count
# txFee is the transaction fee rate to pay the miner for the split
# transaction required to fund the ticket.
# Set to zero to use wallet's network default fee rate.
self.txFee = txFee
self.ticketFee = ticketFee if ticketFee != 0 else DefaultRelayFeePerKb


class TicketStats:
Expand Down Expand Up @@ -1895,6 +1900,13 @@ def purchaseTickets(self, qty, price):
Account uses the blockchain to do the heavy lifting, but must prepare
the TicketRequest and KeySource and gather some other account- related
information.
Args:
qty (int): The number of tickets to buy.
price (int): The price per ticket in coins to pay.
Returs:
MsgTx: The sent split transaction.
"""
keysource = KeySource(
priv=self.privKeyForAddress, internal=self.nextInternalAddress,
Expand All @@ -1907,13 +1919,12 @@ def purchaseTickets(self, qty, price):
spendLimit=int(round(price * qty * 1.1 * 1e8)), # convert to atoms here
poolAddress=pi.poolAddress,
votingAddress=pi.ticketAddress,
ticketFee=DefaultRelayFeePerKb,
poolFees=pi.poolFees,
count=qty,
txFee=self.relayFee,
)
txs, spentUTXOs, newUTXOs = self.blockchain.purchaseTickets(
keysource, self.getUTXOs, req, self.relayFee
keysource, self.getUTXOs, req
)
# Add the split transactions
self.addMempoolTx(txs[0])
Expand Down
23 changes: 7 additions & 16 deletions decred/decred/dcr/dcrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def sendOutputs(
return newTx, utxos, newUTXOs

def purchaseTickets(
self, keysource, utxosource, req, relayFee, allowHighFees=False
self, keysource, utxosource, req, allowHighFees=False
):
"""
Based on dcrwallet (*Wallet).purchaseTickets.
Expand All @@ -1138,12 +1138,11 @@ def purchaseTickets(
available.
Args:
keysource account.KeySource: a source for private keys.
keysource (account.KeySource): a source for private keys.
utxosource func(int, filterFunc) -> (list(UTXO), bool): a source for
UTXOs. The filterFunc is an optional function to filter UTXOs,
and is of the form func(UTXO) -> bool.
req account.TicketRequest: the ticket data.
relayFee (int): Transaction fees in atoms per kb.
req (account.TicketRequest): the ticket data.
allowHighFees (bool): Optional. Default is False. Whether to allow
fees higher than txscript.HighFeeRate.
Expand All @@ -1157,7 +1156,8 @@ def purchaseTickets(
"""
if not allowHighFees:
self.checkFeeRate(relayFee)
self.checkFeeRate(req.txFee)
self.checkFeeRate(req.ticketFee)
self.updateTip()
# account minConf is zero for regular outputs for now. Need to make that
# adjustable.
Expand Down Expand Up @@ -1228,10 +1228,6 @@ def purchaseTickets(
"unsupported voting address type %s" % votingAddress.__class__.__name__
)

ticketFeeIncrement = req.ticketFee
if ticketFeeIncrement == 0:
ticketFeeIncrement = relayFee

# Make sure that we have enough funds. Calculate different
# ticket required amounts depending on whether or not a
# pool output is needed. If the ticket fee increment is
Expand All @@ -1255,7 +1251,7 @@ def purchaseTickets(
]
estSize = txscript.estimateSerializeSizeFromScriptSizes(inSizes, outSizes, 0)

ticketFee = txscript.calcMinRequiredTxRelayFee(ticketFeeIncrement, estSize)
ticketFee = txscript.calcMinRequiredTxRelayFee(req.ticketFee, estSize)
neededPerTicket = ticketFee + ticketPrice

# If we need to calculate the amount for a pool fee percentage,
Expand Down Expand Up @@ -1293,14 +1289,9 @@ def purchaseTickets(
# User amount.
splitOuts.append(msgtx.TxOut(value=userAmt, pkScript=splitPkScript,))

txFeeIncrement = req.txFee
if txFeeIncrement == 0:
txFeeIncrement = relayFee

# Send the split transaction.
# sendOutputs takes the fee rate in atoms/byte
splitTx, splitSpent, internalOutputs = self.sendOutputs(
splitOuts, keysource, utxosource, txFeeIncrement // 1000, allowHighFees
splitOuts, keysource, utxosource, req.txFee, allowHighFees
)

# Generate the tickets individually.
Expand Down
2 changes: 1 addition & 1 deletion decred/decred/dcr/txscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -3410,7 +3410,7 @@ def calcMinRequiredTxRelayFee(relayFeePerKb, txSerializeSize):
Args:
relayFeePerKb (int): The fee in atoms per kilobyte.
txSerializeSize int: (Size) of the byte-encoded transaction.
txSerializeSize (int): Size of the byte-encoded transaction.
Returns:
int: Fee in atoms.
Expand Down

0 comments on commit 352e4a7

Please sign in to comment.