Merge branch 'offline-2' of https://github.com/kyuupichan/electrum into kyuupichan-offline-2
This commit is contained in:
commit
e06b3ba3ce
|
@ -25,48 +25,25 @@ import util
|
||||||
from bitcoin import *
|
from bitcoin import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SPV(util.DaemonThread):
|
class SPV(util.DaemonThread):
|
||||||
""" Simple Payment Verification """
|
""" Simple Payment Verification """
|
||||||
|
|
||||||
def __init__(self, network, storage):
|
def __init__(self, network, wallet):
|
||||||
util.DaemonThread.__init__(self)
|
util.DaemonThread.__init__(self)
|
||||||
self.storage = storage
|
self.wallet = wallet
|
||||||
self.network = network
|
self.network = network
|
||||||
self.transactions = {} # requested verifications (with height sent by the requestor)
|
|
||||||
self.verified_tx = storage.get('verified_tx3',{}) # height, timestamp of verified transactions
|
|
||||||
self.merkle_roots = {} # hashed by me
|
self.merkle_roots = {} # hashed by me
|
||||||
self.lock = threading.Lock()
|
|
||||||
self.queue = Queue.Queue()
|
self.queue = Queue.Queue()
|
||||||
|
|
||||||
def get_height(self, tx_hash):
|
|
||||||
with self.lock:
|
|
||||||
v = self.verified_tx.get(tx_hash)
|
|
||||||
height = v[0] if v else None
|
|
||||||
return height
|
|
||||||
|
|
||||||
|
|
||||||
def add(self, tx_hash, tx_height):
|
|
||||||
""" add a transaction to the list of monitored transactions. """
|
|
||||||
assert tx_height > 0
|
|
||||||
with self.lock:
|
|
||||||
if tx_hash not in self.transactions.keys():
|
|
||||||
self.transactions[tx_hash] = tx_height
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
requested_merkle = []
|
requested_merkle = set()
|
||||||
while self.is_running():
|
while self.is_running():
|
||||||
# request missing tx
|
unverified = self.wallet.get_unverified_txs()
|
||||||
for tx_hash, tx_height in self.transactions.items():
|
for (tx_hash, tx_height) in unverified:
|
||||||
if tx_hash not in self.verified_tx:
|
|
||||||
# do not request merkle branch before headers are available
|
|
||||||
if tx_height > self.network.get_local_height():
|
|
||||||
continue
|
|
||||||
if self.merkle_roots.get(tx_hash) is None and tx_hash not in requested_merkle:
|
if self.merkle_roots.get(tx_hash) is None and tx_hash not in requested_merkle:
|
||||||
if self.network.send([ ('blockchain.transaction.get_merkle',[tx_hash, tx_height]) ], self.queue.put):
|
if self.network.send([ ('blockchain.transaction.get_merkle',[tx_hash, tx_height]) ], self.queue.put):
|
||||||
self.print_error('requesting merkle', tx_hash)
|
self.print_error('requesting merkle', tx_hash)
|
||||||
requested_merkle.append(tx_hash)
|
requested_merkle.add(tx_hash)
|
||||||
try:
|
try:
|
||||||
r = self.queue.get(timeout=0.1)
|
r = self.queue.get(timeout=0.1)
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
|
@ -102,12 +79,8 @@ class SPV(util.DaemonThread):
|
||||||
|
|
||||||
# we passed all the tests
|
# we passed all the tests
|
||||||
self.merkle_roots[tx_hash] = merkle_root
|
self.merkle_roots[tx_hash] = merkle_root
|
||||||
timestamp = header.get('timestamp')
|
self.print_error("verified %s" % tx_hash)
|
||||||
with self.lock:
|
self.wallet.add_verified_tx(tx_hash, (tx_height, header.get('timestamp'), pos))
|
||||||
self.verified_tx[tx_hash] = (tx_height, timestamp, pos)
|
|
||||||
self.print_error("verified %s"%tx_hash)
|
|
||||||
self.storage.put('verified_tx3', self.verified_tx, True)
|
|
||||||
self.network.trigger_callback('updated')
|
|
||||||
|
|
||||||
|
|
||||||
def hash_merkle_root(self, merkle_s, target_hash, pos):
|
def hash_merkle_root(self, merkle_s, target_hash, pos):
|
||||||
|
@ -118,15 +91,8 @@ class SPV(util.DaemonThread):
|
||||||
return hash_encode(h)
|
return hash_encode(h)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def undo_verifications(self, height):
|
def undo_verifications(self, height):
|
||||||
with self.lock:
|
tx_hashes = selt.wallet.undo_verifications(height)
|
||||||
items = self.verified_tx.items()[:]
|
for tx_hash in tx_hashes:
|
||||||
for tx_hash, item in items:
|
|
||||||
tx_height, timestamp, pos = item
|
|
||||||
if tx_height >= height:
|
|
||||||
self.print_error("redoing", tx_hash)
|
self.print_error("redoing", tx_hash)
|
||||||
with self.lock:
|
self.merkle_roots.pop(tx_hash, None)
|
||||||
self.verified_tx.pop(tx_hash)
|
|
||||||
if tx_hash in self.merkle_roots:
|
|
||||||
self.merkle_roots.pop(tx_hash)
|
|
||||||
|
|
|
@ -182,6 +182,11 @@ class Abstract_Wallet(object):
|
||||||
|
|
||||||
# spv
|
# spv
|
||||||
self.verifier = None
|
self.verifier = None
|
||||||
|
# Transactions pending verification. Each value is the transaction height. Access with self.lock.
|
||||||
|
self.unverified_tx = {}
|
||||||
|
# Verified transactions. Each value is a (height, timestamp, block_pos) tuple. Access with self.lock.
|
||||||
|
self.verified_tx = storage.get('verified_tx3',{})
|
||||||
|
|
||||||
# there is a difference between wallet.up_to_date and interface.is_up_to_date()
|
# there is a difference between wallet.up_to_date and interface.is_up_to_date()
|
||||||
# interface.is_up_to_date() returns true when all requests have been answered and processed
|
# interface.is_up_to_date() returns true when all requests have been answered and processed
|
||||||
# wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
|
# wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
|
||||||
|
@ -388,19 +393,45 @@ class Abstract_Wallet(object):
|
||||||
return decrypted
|
return decrypted
|
||||||
|
|
||||||
def add_unverified_tx(self, tx_hash, tx_height):
|
def add_unverified_tx(self, tx_hash, tx_height):
|
||||||
if self.verifier and tx_height > 0:
|
if tx_height > 0:
|
||||||
self.verifier.add(tx_hash, tx_height)
|
with self.lock:
|
||||||
|
self.unverified_tx[tx_hash] = tx_height
|
||||||
|
|
||||||
|
def add_verified_tx(self, tx_hash, info):
|
||||||
|
with self.lock:
|
||||||
|
self.verified_tx[tx_hash] = info # (tx_height, timestamp, pos)
|
||||||
|
self.storage.put('verified_tx3', self.verified_tx, True)
|
||||||
|
self.network.trigger_callback('updated')
|
||||||
|
|
||||||
|
def get_unverified_txs(self):
|
||||||
|
'''Returns a list of tuples (tx_hash, height) that are unverified and not beyond local height'''
|
||||||
|
txs = []
|
||||||
|
with self.lock:
|
||||||
|
for tx_hash, tx_height in self.unverified_tx.items():
|
||||||
|
# do not request merkle branch before headers are available
|
||||||
|
if tx_hash not in self.verified_tx and tx_height <= self.network.get_local_height():
|
||||||
|
txs.append((tx_hash, tx_height))
|
||||||
|
return txs
|
||||||
|
|
||||||
|
def undo_verifications(self, height):
|
||||||
|
'''Used by the verifier when a reorg has happened'''
|
||||||
|
txs = []
|
||||||
|
with self.lock:
|
||||||
|
for tx_hash, item in self.verified_tx:
|
||||||
|
tx_height, timestamp, pos = item
|
||||||
|
if tx_height >= height:
|
||||||
|
self.verified_tx.pop(tx_hash, None)
|
||||||
|
txs.append(tx_hash)
|
||||||
|
return txs
|
||||||
|
|
||||||
def get_confirmations(self, tx):
|
def get_confirmations(self, tx):
|
||||||
""" return the number of confirmations of a monitored transaction. """
|
""" return the number of confirmations of a monitored transaction. """
|
||||||
if not self.verifier:
|
with self.lock:
|
||||||
return (None, None)
|
if tx in self.verified_tx:
|
||||||
with self.verifier.lock:
|
height, timestamp, pos = self.verified_tx[tx]
|
||||||
if tx in self.verifier.verified_tx:
|
|
||||||
height, timestamp, pos = self.verifier.verified_tx[tx]
|
|
||||||
conf = (self.network.get_local_height() - height + 1)
|
conf = (self.network.get_local_height() - height + 1)
|
||||||
if conf <= 0: timestamp = None
|
if conf <= 0: timestamp = None
|
||||||
elif tx in self.verifier.transactions:
|
elif tx in self.unverified_tx:
|
||||||
conf = -1
|
conf = -1
|
||||||
timestamp = None
|
timestamp = None
|
||||||
else:
|
else:
|
||||||
|
@ -411,9 +442,9 @@ class Abstract_Wallet(object):
|
||||||
|
|
||||||
def get_txpos(self, tx_hash):
|
def get_txpos(self, tx_hash):
|
||||||
"return position, even if the tx is unverified"
|
"return position, even if the tx is unverified"
|
||||||
with self.verifier.lock:
|
with self.lock:
|
||||||
x = self.verifier.verified_tx.get(tx_hash)
|
x = self.verified_tx.get(tx_hash)
|
||||||
y = self.verifier.transactions.get(tx_hash)
|
y = self.unverified_tx.get(tx_hash)
|
||||||
if x:
|
if x:
|
||||||
height, timestamp, pos = x
|
height, timestamp, pos = x
|
||||||
return height, pos
|
return height, pos
|
||||||
|
@ -1000,7 +1031,8 @@ class Abstract_Wallet(object):
|
||||||
self.add_unverified_tx (tx_hash, tx_height)
|
self.add_unverified_tx (tx_hash, tx_height)
|
||||||
|
|
||||||
# if we are on a pruning server, remove unverified transactions
|
# if we are on a pruning server, remove unverified transactions
|
||||||
vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
|
with self.lock:
|
||||||
|
vr = self.verified_tx.keys() + self.unverified_tx.keys()
|
||||||
for tx_hash in self.transactions.keys():
|
for tx_hash in self.transactions.keys():
|
||||||
if tx_hash not in vr:
|
if tx_hash not in vr:
|
||||||
print_error("removing transaction", tx_hash)
|
print_error("removing transaction", tx_hash)
|
||||||
|
@ -1035,7 +1067,8 @@ class Abstract_Wallet(object):
|
||||||
if not tx: continue
|
if not tx: continue
|
||||||
|
|
||||||
# already verified?
|
# already verified?
|
||||||
if self.verifier.get_height(tx_hash):
|
with self.lock:
|
||||||
|
if tx_hash in self.verified_tx:
|
||||||
continue
|
continue
|
||||||
# unconfirmed tx
|
# unconfirmed tx
|
||||||
print_error("new history is orphaning transaction:", tx_hash)
|
print_error("new history is orphaning transaction:", tx_hash)
|
||||||
|
@ -1065,7 +1098,7 @@ class Abstract_Wallet(object):
|
||||||
from verifier import SPV
|
from verifier import SPV
|
||||||
self.network = network
|
self.network = network
|
||||||
if self.network is not None:
|
if self.network is not None:
|
||||||
self.verifier = SPV(self.network, self.storage)
|
self.verifier = SPV(self.network, self)
|
||||||
self.verifier.start()
|
self.verifier.start()
|
||||||
self.set_verifier(self.verifier)
|
self.set_verifier(self.verifier)
|
||||||
self.synchronizer = WalletSynchronizer(self, network)
|
self.synchronizer = WalletSynchronizer(self, network)
|
||||||
|
|
Loading…
Reference in New Issue