+ Account.get_max_gap()

This commit is contained in:
Lex Berezhny 2018-07-25 23:29:41 -04:00
parent 40651322d0
commit 5a2a3727b2

View file

@ -24,6 +24,9 @@ class KeyManager(object):
self.account, self.chain_number, limit, max_used_times, order_by self.account, self.chain_number, limit, max_used_times, order_by
) )
def get_max_gap(self): # type: () -> defer.Deferred
raise NotImplementedError
def ensure_address_gap(self): # type: () -> defer.Deferred def ensure_address_gap(self): # type: () -> defer.Deferred
raise NotImplementedError raise NotImplementedError
@ -65,6 +68,19 @@ class KeyChain(KeyManager):
) )
defer.returnValue([key[1].address for key in new_keys]) defer.returnValue([key[1].address for key in new_keys])
@defer.inlineCallbacks
def get_max_gap(self):
addresses = yield self._query_addresses(order_by="position ASC")
max_gap = 0
current_gap = 0
for address in addresses:
if address['used_times'] == 0:
current_gap += 1
else:
max_gap = max(max_gap, current_gap)
current_gap = 0
defer.returnValue(max_gap)
@defer.inlineCallbacks @defer.inlineCallbacks
def ensure_address_gap(self): def ensure_address_gap(self):
addresses = yield self._query_addresses(self.gap, None, "position DESC") addresses = yield self._query_addresses(self.gap, None, "position DESC")
@ -100,6 +116,9 @@ class SingleKey(KeyManager):
# type: ('BaseAccount', PubKey) -> None # type: ('BaseAccount', PubKey) -> None
super(SingleKey, self).__init__(account, root_public_key, chain_number) super(SingleKey, self).__init__(account, root_public_key, chain_number)
def get_max_gap(self):
return defer.succeed(0)
@defer.inlineCallbacks @defer.inlineCallbacks
def ensure_address_gap(self): def ensure_address_gap(self):
exists = yield self.get_address_records() exists = yield self.get_address_records()
@ -265,5 +284,14 @@ class BaseAccount(object):
constraints.update({'height__lte': height, 'height__gt': 0}) constraints.update({'height__lte': height, 'height__gt': 0})
return self.ledger.db.get_balance_for_account(self, **constraints) return self.ledger.db.get_balance_for_account(self, **constraints)
@defer.inlineCallbacks
def get_max_gap(self):
change_gap = yield self.change.get_max_gap()
receiving_gap = yield self.receiving.get_max_gap()
defer.returnValue({
'max_change_gap': change_gap,
'max_receiving_gap': receiving_gap,
})
def get_unspent_outputs(self, **constraints): def get_unspent_outputs(self, **constraints):
return self.ledger.db.get_utxos_for_account(self, **constraints) return self.ledger.db.get_utxos_for_account(self, **constraints)