ledger API cleanup

This commit is contained in:
Lex Berezhny 2018-07-11 22:37:15 -04:00
parent feac754925
commit 32ba594e55
3 changed files with 55 additions and 77 deletions

View file

@ -45,10 +45,20 @@ class MockHeaders:
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):
def setUp(self):
self.ledger = MainNetLedger(db=MainNetLedger.database_class(':memory:'), headers_class=MockHeaders)
self.ledger = MainNetTestLedger()
return self.ledger.db.start()
@defer.inlineCallbacks

View file

@ -61,16 +61,16 @@ class BaseLedger(six.with_metaclass(LedgerRegistry)):
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.db = db or self.database_class(
self.db = self.config.get('db') or self.database_class(
os.path.join(self.path, "blockchain.db")
) # 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_status.listen(self.process_status)
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._on_transaction_controller = StreamController()
@ -263,41 +263,41 @@ class BaseLedger(six.with_metaclass(LedgerRegistry)):
yield lock.acquire()
try:
# 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])
save_tx = None
if raw is None:
_raw = yield self.network.get_transaction(hex_id)
tx = self.transaction_class(unhexlify(_raw))
save_tx = 'insert'
else:
tx = self.transaction_class(raw)
#try:
# 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])
save_tx = None
if raw is None:
_raw = yield self.network.get_transaction(hex_id)
tx = self.transaction_class(unhexlify(_raw))
save_tx = 'insert'
else:
tx = self.transaction_class(raw)
if remote_height > 0 and not is_verified:
is_verified = yield self.is_valid_transaction(tx, remote_height)
is_verified = 1 if is_verified else 0
if save_tx is None:
save_tx = 'update'
if remote_height > 0 and not is_verified:
is_verified = yield self.is_valid_transaction(tx, remote_height)
is_verified = 1 if is_verified else 0
if save_tx is None:
save_tx = 'update'
yield self.db.save_transaction_io(
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)
)
yield self.db.save_transaction_io(
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)
)
log.debug("{}: sync'ed tx {} for address: {}, height: {}, verified: {}".format(
self.get_id(), hex_id, address, remote_height, is_verified
))
self._on_transaction_controller.add(TransactionEvent(address, tx, remote_height, is_verified))
log.debug("{}: sync'ed tx {} for address: {}, height: {}, verified: {}".format(
self.get_id(), hex_id, address, remote_height, is_verified
))
self._on_transaction_controller.add(TransactionEvent(address, tx, remote_height, is_verified))
except:
log.exception('Failed to synchronize transaction:')
raise
#except:
# log.exception('Failed to synchronize transaction:')
# raise
finally:
lock.release()
if not lock.locked:
del self._transaction_processing_locks[hex_id]
#finally:
lock.release()
if not lock.locked:
del self._transaction_processing_locks[hex_id]
@defer.inlineCallbacks
def subscribe_history(self, address):

View file

@ -62,23 +62,17 @@ class WalletStorage:
LATEST_VERSION = 1
DEFAULT = {
'version': LATEST_VERSION,
'name': 'Wallet',
'accounts': []
}
def __init__(self, path=None, default=None):
self.path = path
self._default = default or self.DEFAULT.copy()
@property
def default(self):
return self._default.copy()
self._default = default or {
'version': self.LATEST_VERSION,
'name': 'My Wallet',
'accounts': []
}
def read(self):
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_dict = json.loads(json_data)
if json_dict.get('version') == self.LATEST_VERSION and \
@ -87,40 +81,14 @@ class WalletStorage:
else:
return self.upgrade(json_dict)
else:
return self.default
return self._default.copy()
@classmethod
def upgrade(cls, json_dict):
def upgrade(self, json_dict):
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)
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
if version == -1:
pass
upgraded = cls.DEFAULT
upgraded = self._default.copy()
upgraded.update(json_dict)
return json_dict