Skip to content

Commit

Permalink
Namecoin: Add transaction queueing
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Oct 17, 2018
1 parent 0156afe commit 5455a4b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions electrum_nmc/address_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ def add_value_from_prev_output():
self.transactions[tx_hash] = tx
return True

def queue_transaction(self, tx_hash, tx_queue_item):
with self.transaction_lock:
self.queued_transactions[tx_hash] = tx_queue_item
return True

def unqueue_transaction(self, tx_hash):
with self.transaction_lock:
if tx_hash in self.queued_transactions:
del self.queued_transactions[tx_hash]
return True

def remove_transaction(self, tx_hash):
def remove_from_spent_outpoints():
# undo spends in spent_outpoints
Expand Down Expand Up @@ -414,6 +425,7 @@ def load_transactions(self):
if spending_txid not in self.transactions:
continue # only care about txns we have
self.spent_outpoints[prevout_hash][prevout_n] = spending_txid
self.queued_transactions = self.storage.get('queued_transactions', {})

@profiler
def load_local_history(self):
Expand Down Expand Up @@ -460,6 +472,7 @@ def save_transactions(self, write=False):
self.storage.put('tx_fees', self.tx_fees)
self.storage.put('addr_history', self.history)
self.storage.put('spent_outpoints', self.spent_outpoints)
self.storage.put('queued_transactions', self.queued_transactions)
if write:
self.storage.write()

Expand Down
73 changes: 73 additions & 0 deletions electrum_nmc/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,77 @@ def addtransaction(self, tx):
self.wallet.save_transactions()
return tx.txid()

@command('w')
def queuetransaction(self, tx, trigger_depth, trigger_txid = None, trigger_name = None):
""" Queue a transaction for later broadcast """
if trigger_txid is None and trigger_name is None:
raise Exception("You must specify exactly one of trigger_txid or trigger_name.")
if trigger_txid is not None and trigger_name is not None:
raise Exception("You must specify exactly one of trigger_txid or trigger_name.")

txid = Transaction(tx).txid()
# TODO: handle non-ASCII trigger_name
send_when = {
"txid": trigger_txid,
"name": trigger_name,
"name_encoding": "ascii",
"confirmations": trigger_depth,
}
queue_item = {
"tx": tx,
"sendWhen": send_when
}
if not self.wallet.queue_transaction(txid, queue_item):
return False
self.wallet.save_transactions()
return txid

@command('wn')
def updatequeuedtransactions(self):
errors = {}

to_unqueue = []

for txid in self.wallet.queued_transactions:
queue_item = self.wallet.queued_transactions[txid]
send_when = queue_item["sendWhen"]

trigger_txid = send_when["txid"]
trigger_name = send_when["name"]
trigger_depth = send_when["confirmations"]

chain_height = self.network.blockchain().height()

current_depth = 0

if trigger_name is not None:
# TODO: handle non-ASCII trigger_name
try:
current_height = self.name_show(trigger_name)["height"]
current_depth = chain_height - current_height + 1
except NameNotFoundError:
current_depth = 36000
except Exception:
continue

if trigger_txid is not None:
current_depth = self.wallet.get_tx_height(trigger_txid).conf

if current_depth >= trigger_depth:
tx = queue_item["tx"]
status, msg = self.broadcast(tx)
if not status:
errors[txid] = msg

to_unqueue.append(txid)

for txid in to_unqueue:
self.wallet.unqueue_transaction(txid)
self.wallet.save_transactions()

success = (errors == {})
return success, errors

@command('wp')
def signrequest(self, address, password=None):
"Sign payment request with an OpenAlias"
Expand Down Expand Up @@ -967,6 +1038,8 @@ def help(self):
'allow_early': (None, "Allow submitting a name registration while its pre-registration is still pending. This increases the risk of an attacker stealing your name registration."),
'identifier': (None, "The requested name identifier"),
'value': (None, "The value to assign to the name"),
'trigger_txid':(None, "Broadcast the transaction when this txid reaches the specified number of confirmations"),
'trigger_name':(None, "Broadcast the transaction when this name reaches the specified number of confirmations"),
}


Expand Down

0 comments on commit 5455a4b

Please sign in to comment.