mirror of
https://github.com/LBRYFoundation/lbry-sdk.git
synced 2025-08-29 16:31:25 +00:00
ledger API cleanup
This commit is contained in:
parent
feac754925
commit
32ba594e55
3 changed files with 55 additions and 77 deletions
|
@ -45,10 +45,20 @@ class MockHeaders:
|
||||||
return {'merkle_root': 'abcd04'}
|
return {'merkle_root': 'abcd04'}
|
||||||
|
|
||||||
|
|
||||||
|
class MainNetTestLedger(MainNetLedger):
|
||||||
|
headers_class = MockHeaders
|
||||||
|
network_name = 'unittest'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(MainNetLedger, self).__init__({
|
||||||
|
'db': MainNetLedger.database_class(':memory:')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class TestSynchronization(unittest.TestCase):
|
class TestSynchronization(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.ledger = MainNetLedger(db=MainNetLedger.database_class(':memory:'), headers_class=MockHeaders)
|
self.ledger = MainNetTestLedger()
|
||||||
return self.ledger.db.start()
|
return self.ledger.db.start()
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
|
|
@ -61,16 +61,16 @@ class BaseLedger(six.with_metaclass(LedgerRegistry)):
|
||||||
|
|
||||||
default_fee_per_byte = 10
|
default_fee_per_byte = 10
|
||||||
|
|
||||||
def __init__(self, config=None, db=None, network=None, headers_class=None):
|
def __init__(self, config=None):
|
||||||
self.config = config or {}
|
self.config = config or {}
|
||||||
self.db = db or self.database_class(
|
self.db = self.config.get('db') or self.database_class(
|
||||||
os.path.join(self.path, "blockchain.db")
|
os.path.join(self.path, "blockchain.db")
|
||||||
) # type: basedatabase.BaseDatabase
|
) # type: basedatabase.BaseDatabase
|
||||||
self.network = network or self.network_class(self)
|
self.network = self.config.get('network') or self.network_class(self)
|
||||||
self.network.on_header.listen(self.process_header)
|
self.network.on_header.listen(self.process_header)
|
||||||
self.network.on_status.listen(self.process_status)
|
self.network.on_status.listen(self.process_status)
|
||||||
self.accounts = set()
|
self.accounts = set()
|
||||||
self.headers = (headers_class or self.headers_class)(self)
|
self.headers = self.config.get('headers') or self.headers_class(self)
|
||||||
self.fee_per_byte = self.config.get('fee_per_byte', self.default_fee_per_byte)
|
self.fee_per_byte = self.config.get('fee_per_byte', self.default_fee_per_byte)
|
||||||
|
|
||||||
self._on_transaction_controller = StreamController()
|
self._on_transaction_controller = StreamController()
|
||||||
|
@ -263,41 +263,41 @@ class BaseLedger(six.with_metaclass(LedgerRegistry)):
|
||||||
|
|
||||||
yield lock.acquire()
|
yield lock.acquire()
|
||||||
|
|
||||||
try:
|
#try:
|
||||||
# see if we have a local copy of transaction, otherwise fetch it from server
|
# see if we have a local copy of transaction, otherwise fetch it from server
|
||||||
raw, local_height, is_verified = yield self.db.get_transaction(unhexlify(hex_id)[::-1])
|
raw, local_height, is_verified = yield self.db.get_transaction(unhexlify(hex_id)[::-1])
|
||||||
save_tx = None
|
save_tx = None
|
||||||
if raw is None:
|
if raw is None:
|
||||||
_raw = yield self.network.get_transaction(hex_id)
|
_raw = yield self.network.get_transaction(hex_id)
|
||||||
tx = self.transaction_class(unhexlify(_raw))
|
tx = self.transaction_class(unhexlify(_raw))
|
||||||
save_tx = 'insert'
|
save_tx = 'insert'
|
||||||
else:
|
else:
|
||||||
tx = self.transaction_class(raw)
|
tx = self.transaction_class(raw)
|
||||||
|
|
||||||
if remote_height > 0 and not is_verified:
|
if remote_height > 0 and not is_verified:
|
||||||
is_verified = yield self.is_valid_transaction(tx, remote_height)
|
is_verified = yield self.is_valid_transaction(tx, remote_height)
|
||||||
is_verified = 1 if is_verified else 0
|
is_verified = 1 if is_verified else 0
|
||||||
if save_tx is None:
|
if save_tx is None:
|
||||||
save_tx = 'update'
|
save_tx = 'update'
|
||||||
|
|
||||||
yield self.db.save_transaction_io(
|
yield self.db.save_transaction_io(
|
||||||
save_tx, tx, remote_height, is_verified, address, self.address_to_hash160(address),
|
save_tx, tx, remote_height, is_verified, address, self.address_to_hash160(address),
|
||||||
''.join('{}:{}:'.format(tx_id.decode(), tx_height) for tx_id, tx_height in synced_history)
|
''.join('{}:{}:'.format(tx_id.decode(), tx_height) for tx_id, tx_height in synced_history)
|
||||||
)
|
)
|
||||||
|
|
||||||
log.debug("{}: sync'ed tx {} for address: {}, height: {}, verified: {}".format(
|
log.debug("{}: sync'ed tx {} for address: {}, height: {}, verified: {}".format(
|
||||||
self.get_id(), hex_id, address, remote_height, is_verified
|
self.get_id(), hex_id, address, remote_height, is_verified
|
||||||
))
|
))
|
||||||
self._on_transaction_controller.add(TransactionEvent(address, tx, remote_height, is_verified))
|
self._on_transaction_controller.add(TransactionEvent(address, tx, remote_height, is_verified))
|
||||||
|
|
||||||
except:
|
#except:
|
||||||
log.exception('Failed to synchronize transaction:')
|
# log.exception('Failed to synchronize transaction:')
|
||||||
raise
|
# raise
|
||||||
|
|
||||||
finally:
|
#finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
if not lock.locked:
|
if not lock.locked:
|
||||||
del self._transaction_processing_locks[hex_id]
|
del self._transaction_processing_locks[hex_id]
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def subscribe_history(self, address):
|
def subscribe_history(self, address):
|
||||||
|
|
|
@ -62,23 +62,17 @@ class WalletStorage:
|
||||||
|
|
||||||
LATEST_VERSION = 1
|
LATEST_VERSION = 1
|
||||||
|
|
||||||
DEFAULT = {
|
|
||||||
'version': LATEST_VERSION,
|
|
||||||
'name': 'Wallet',
|
|
||||||
'accounts': []
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, path=None, default=None):
|
def __init__(self, path=None, default=None):
|
||||||
self.path = path
|
self.path = path
|
||||||
self._default = default or self.DEFAULT.copy()
|
self._default = default or {
|
||||||
|
'version': self.LATEST_VERSION,
|
||||||
@property
|
'name': 'My Wallet',
|
||||||
def default(self):
|
'accounts': []
|
||||||
return self._default.copy()
|
}
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
if self.path and os.path.exists(self.path):
|
if self.path and os.path.exists(self.path):
|
||||||
with open(self.path, "r") as f:
|
with open(self.path, 'r') as f:
|
||||||
json_data = f.read()
|
json_data = f.read()
|
||||||
json_dict = json.loads(json_data)
|
json_dict = json.loads(json_data)
|
||||||
if json_dict.get('version') == self.LATEST_VERSION and \
|
if json_dict.get('version') == self.LATEST_VERSION and \
|
||||||
|
@ -87,40 +81,14 @@ class WalletStorage:
|
||||||
else:
|
else:
|
||||||
return self.upgrade(json_dict)
|
return self.upgrade(json_dict)
|
||||||
else:
|
else:
|
||||||
return self.default
|
return self._default.copy()
|
||||||
|
|
||||||
@classmethod
|
def upgrade(self, json_dict):
|
||||||
def upgrade(cls, json_dict):
|
|
||||||
json_dict = json_dict.copy()
|
json_dict = json_dict.copy()
|
||||||
|
|
||||||
def _rename_property(old, new):
|
|
||||||
if old in json_dict:
|
|
||||||
json_dict[new] = json_dict[old]
|
|
||||||
del json_dict[old]
|
|
||||||
|
|
||||||
version = json_dict.pop('version', -1)
|
version = json_dict.pop('version', -1)
|
||||||
|
if version == -1:
|
||||||
if version == -1: # upgrading from electrum wallet
|
|
||||||
json_dict = {
|
|
||||||
'accounts': [{
|
|
||||||
'ledger': 'lbc_mainnet',
|
|
||||||
'encrypted': json_dict['use_encryption'],
|
|
||||||
'seed': json_dict['seed'],
|
|
||||||
'seed_version': json_dict['seed_version'],
|
|
||||||
'private_key': json_dict['master_private_keys']['x/'],
|
|
||||||
'public_key': json_dict['master_public_keys']['x/'],
|
|
||||||
'certificates': json_dict['claim_certificates'],
|
|
||||||
'receiving_gap': 20,
|
|
||||||
'change_gap': 6,
|
|
||||||
'receiving_maximum_use_per_address': 2,
|
|
||||||
'change_maximum_use_per_address': 2
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
||||||
elif version == 1: # upgrade from version 1 to version 2
|
|
||||||
pass
|
pass
|
||||||
|
upgraded = self._default.copy()
|
||||||
upgraded = cls.DEFAULT
|
|
||||||
upgraded.update(json_dict)
|
upgraded.update(json_dict)
|
||||||
return json_dict
|
return json_dict
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue