This commit is contained in:
Jack Robison 2021-06-30 20:09:17 -04:00 committed by Victor Shyba
parent 02563db2c7
commit 90cfcaac40
4 changed files with 42 additions and 28 deletions

View file

@ -530,7 +530,7 @@ class BlockProcessor:
# print(f"\tupdate {claim_hash.hex()} {tx_hash[::-1].hex()} {txo.amount}") # print(f"\tupdate {claim_hash.hex()} {tx_hash[::-1].hex()} {txo.amount}")
if (prev_tx_num, prev_idx) in self.pending_claims: if (prev_tx_num, prev_idx) in self.pending_claims:
previous_claim = self.pending_claims.pop((prev_tx_num, prev_idx)) previous_claim = self.pending_claims.pop((prev_tx_num, prev_idx))
root_tx_num, root_idx = previous_claim.root_claim_tx_num, previous_claim.root_claim_tx_position root_tx_num, root_idx = previous_claim.root_tx_num, previous_claim.root_position
else: else:
v = self.db.get_claim_txo( v = self.db.get_claim_txo(
claim_hash claim_hash
@ -638,7 +638,7 @@ class BlockProcessor:
if (tx_num, nout) in self.pending_claims: if (tx_num, nout) in self.pending_claims:
pending = self.pending_claims.pop((tx_num, nout)) pending = self.pending_claims.pop((tx_num, nout))
self.staged_pending_abandoned[pending.claim_hash] = pending self.staged_pending_abandoned[pending.claim_hash] = pending
claim_root_tx_num, claim_root_idx = pending.root_claim_tx_num, pending.root_claim_tx_position claim_root_tx_num, claim_root_idx = pending.root_tx_num, pending.root_position
prev_amount, prev_signing_hash = pending.amount, pending.signing_hash prev_amount, prev_signing_hash = pending.amount, pending.signing_hash
reposted_claim_hash = pending.reposted_claim_hash reposted_claim_hash = pending.reposted_claim_hash
expiration = self.coin.get_expiration_height(self.height) expiration = self.coin.get_expiration_height(self.height)
@ -672,8 +672,8 @@ class BlockProcessor:
if claim_hash in self.staged_pending_abandoned: if claim_hash in self.staged_pending_abandoned:
continue continue
self.signatures_changed.add(claim_hash) self.signatures_changed.add(claim_hash)
if claim_hash in self.pending_claims: if claim_hash in self.pending_claim_txos:
claim = self.pending_claims[claim_hash] claim = self.pending_claims[self.pending_claim_txos[claim_hash]]
else: else:
claim = self.db.get_claim_txo(claim_hash) claim = self.db.get_claim_txo(claim_hash)
assert claim is not None assert claim is not None
@ -1131,32 +1131,35 @@ class BlockProcessor:
# use the cumulative changes to update bid ordered resolve # use the cumulative changes to update bid ordered resolve
for removed in self.removed_claims_to_send_es: for removed in self.removed_claims_to_send_es:
removed_claim = self.db.get_claim_txo(removed) removed_claim = self.db.get_claim_txo(removed)
if not removed_claim: if removed_claim:
continue amt = self.db.get_url_effective_amount(
amt = self._cached_get_effective_amount(removed) removed_claim.name, removed_claim.tx_num, removed_claim.position, removed
if amt <= 0: )
continue if amt and amt > 0:
ops.extend(get_remove_effective_amount_ops( self.claimtrie_stash.extend(get_remove_effective_amount_ops(
removed_claim.name, amt, removed_claim.tx_num, removed_claim.name, amt, removed_claim.tx_num,
removed_claim.position, removed removed_claim.position, removed
)) ))
for touched in self.touched_claims_to_send_es: for touched in self.touched_claims_to_send_es:
if touched in self.pending_claim_txos: if touched in self.pending_claim_txos:
pending = self.pending_claims[self.pending_claim_txos[touched]] pending = self.pending_claims[self.pending_claim_txos[touched]]
name, tx_num, position = pending.name, pending.tx_num, pending.position name, tx_num, position = pending.name, pending.tx_num, pending.position
claim_from_db = self.db.get_claim_txo(touched) claim_from_db = self.db.get_claim_txo(touched)
if claim_from_db: if claim_from_db:
amount = self._cached_get_effective_amount(touched) amount = self.db.get_url_effective_amount(
if amount > 0: name, claim_from_db.tx_num, claim_from_db.position, touched
prev_tx_num, prev_position = claim_from_db.tx_num, claim_from_db.position )
if amount and amount > 0:
self.claimtrie_stash.extend(get_remove_effective_amount_ops( self.claimtrie_stash.extend(get_remove_effective_amount_ops(
name, amount, prev_tx_num, prev_position, touched name, amount, claim_from_db.tx_num, claim_from_db.position, touched
)) ))
else: else:
v = self.db.get_claim_txo(touched) v = self.db.get_claim_txo(touched)
if not v:
continue
name, tx_num, position = v.name, v.tx_num, v.position name, tx_num, position = v.name, v.tx_num, v.position
amt = self._cached_get_effective_amount(touched) amt = self.db.get_url_effective_amount(name, tx_num, position, touched)
if amt > 0: if amt and amt > 0:
self.claimtrie_stash.extend(get_remove_effective_amount_ops( self.claimtrie_stash.extend(get_remove_effective_amount_ops(
name, amt, tx_num, position, touched name, amt, tx_num, position, touched
)) ))

View file

@ -133,15 +133,15 @@ class StagedClaimtrieItem(typing.NamedTuple):
expiration_height: int expiration_height: int
tx_num: int tx_num: int
position: int position: int
root_claim_tx_num: int root_tx_num: int
root_claim_tx_position: int root_position: int
channel_signature_is_valid: bool channel_signature_is_valid: bool
signing_hash: Optional[bytes] signing_hash: Optional[bytes]
reposted_claim_hash: Optional[bytes] reposted_claim_hash: Optional[bytes]
@property @property
def is_update(self) -> bool: def is_update(self) -> bool:
return (self.tx_num, self.position) != (self.root_claim_tx_num, self.root_claim_tx_position) return (self.tx_num, self.position) != (self.root_tx_num, self.root_position)
def _get_add_remove_claim_utxo_ops(self, add=True): def _get_add_remove_claim_utxo_ops(self, add=True):
""" """
@ -155,7 +155,7 @@ class StagedClaimtrieItem(typing.NamedTuple):
# claim tip by claim hash # claim tip by claim hash
op( op(
*Prefixes.claim_to_txo.pack_item( *Prefixes.claim_to_txo.pack_item(
self.claim_hash, self.tx_num, self.position, self.root_claim_tx_num, self.root_claim_tx_position, self.claim_hash, self.tx_num, self.position, self.root_tx_num, self.root_position,
self.amount, self.channel_signature_is_valid, self.name self.amount, self.channel_signature_is_valid, self.name
) )
), ),
@ -173,7 +173,7 @@ class StagedClaimtrieItem(typing.NamedTuple):
# short url resolution # short url resolution
op( op(
*Prefixes.claim_short_id.pack_item( *Prefixes.claim_short_id.pack_item(
self.name, self.claim_hash, self.root_claim_tx_num, self.root_claim_tx_position, self.tx_num, self.name, self.claim_hash, self.root_tx_num, self.root_position, self.tx_num,
self.position self.position
) )
) )

View file

@ -91,16 +91,19 @@ class RevertableOpStack:
inverted = op.invert() inverted = op.invert()
if self._items[op.key] and inverted == self._items[op.key][-1]: if self._items[op.key] and inverted == self._items[op.key][-1]:
self._items[op.key].pop() self._items[op.key].pop()
elif self._items[op.key] and self._items[op.key][-1] == op: # duplicate of last op
pass # raise an error?
else: else:
if op.is_put: if op.is_put:
stored = self._get(op.key) stored = self._get(op.key)
if stored is not None: if stored is not None:
assert RevertableDelete(op.key, stored) in self._items[op.key], f"db op ties to add on top of existing key={op}" assert RevertableDelete(op.key, stored) in self._items[op.key], \
f"db op tries to add on top of existing key: {op}"
self._items[op.key].append(op) self._items[op.key].append(op)
else: else:
stored = self._get(op.key) stored = self._get(op.key)
if stored is not None and stored != op.value: if stored is not None and stored != op.value:
assert RevertableDelete(op.key, stored) in self._items[op.key] assert RevertableDelete(op.key, stored) in self._items[op.key], f"delete {op}"
else: else:
assert stored is not None, f"db op tries to delete nonexistent key: {op}" assert stored is not None, f"db op tries to delete nonexistent key: {op}"
assert stored == op.value, f"db op tries to delete with incorrect value: {op}" assert stored == op.value, f"db op tries to delete with incorrect value: {op}"

View file

@ -300,9 +300,8 @@ class LevelDB:
return return
def _resolve_claim_in_channel(self, channel_hash: bytes, normalized_name: str): def _resolve_claim_in_channel(self, channel_hash: bytes, normalized_name: str):
prefix = DB_PREFIXES.channel_to_claim.value + channel_hash + length_encoded_name(normalized_name)
candidates = [] candidates = []
for k, v in self.db.iterator(prefix=prefix): for k, v in self.db.iterator(prefix=Prefixes.channel_to_claim.pack_partial_key(channel_hash, normalized_name)):
key = Prefixes.channel_to_claim.unpack_key(k) key = Prefixes.channel_to_claim.unpack_key(k)
stream = Prefixes.channel_to_claim.unpack_value(v) stream = Prefixes.channel_to_claim.unpack_value(v)
effective_amount = self.get_effective_amount(stream.claim_hash) effective_amount = self.get_effective_amount(stream.claim_hash)
@ -395,6 +394,15 @@ class LevelDB:
return support_only return support_only
return support_amount + self._get_active_amount(claim_hash, ACTIVATED_CLAIM_TXO_TYPE, self.db_height + 1) return support_amount + self._get_active_amount(claim_hash, ACTIVATED_CLAIM_TXO_TYPE, self.db_height + 1)
def get_url_effective_amount(self, name: str, tx_num: int, position: int, claim_hash: bytes):
for _k, _v in self.db.iterator(prefix=Prefixes.effective_amount.pack_partial_key(name)):
v = Prefixes.effective_amount.unpack_value(_v)
if v.claim_hash == claim_hash:
k = Prefixes.effective_amount.unpack_key(_k)
if k.tx_num == tx_num and k.position == position:
return k.effective_amount
return
def get_claims_for_name(self, name): def get_claims_for_name(self, name):
claims = [] claims = []
for _k, _v in self.db.iterator(prefix=Prefixes.claim_short_id.pack_partial_key(name)): for _k, _v in self.db.iterator(prefix=Prefixes.claim_short_id.pack_partial_key(name)):