2021-02-19 19:19:58 +01:00
|
|
|
import typing
|
|
|
|
from typing import Optional
|
|
|
|
from lbry.wallet.server.db.revertable import RevertablePut, RevertableDelete, RevertableOp, delete_prefix
|
|
|
|
from lbry.wallet.server.db import DB_PREFIXES
|
2021-05-27 19:35:41 +02:00
|
|
|
from lbry.wallet.server.db.prefixes import Prefixes, ClaimTakeoverValue, EffectiveAmountPrefixRow
|
2021-05-26 23:25:03 +02:00
|
|
|
from lbry.wallet.server.db.prefixes import ACTIVATED_CLAIM_TXO_TYPE
|
2021-02-19 19:19:58 +01:00
|
|
|
|
2021-05-05 22:17:32 +02:00
|
|
|
|
2021-02-19 19:19:58 +01:00
|
|
|
def length_encoded_name(name: str) -> bytes:
|
|
|
|
encoded = name.encode('utf-8')
|
|
|
|
return len(encoded).to_bytes(2, byteorder='big') + encoded
|
|
|
|
|
|
|
|
|
|
|
|
class StagedClaimtrieSupport(typing.NamedTuple):
|
|
|
|
claim_hash: bytes
|
|
|
|
tx_num: int
|
|
|
|
position: int
|
|
|
|
amount: int
|
|
|
|
|
|
|
|
def _get_add_remove_support_utxo_ops(self, add=True):
|
|
|
|
"""
|
|
|
|
get a list of revertable operations to add or spend a support txo to the key: value database
|
|
|
|
|
|
|
|
:param add: if true use RevertablePut operations, otherwise use RevertableDelete
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
op = RevertablePut if add else RevertableDelete
|
|
|
|
return [
|
|
|
|
op(
|
|
|
|
*Prefixes.claim_to_support.pack_item(self.claim_hash, self.tx_num, self.position, self.amount)
|
|
|
|
),
|
|
|
|
op(
|
|
|
|
*Prefixes.support_to_claim.pack_item(self.tx_num, self.position, self.claim_hash)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_add_support_utxo_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_support_utxo_ops(add=True)
|
|
|
|
|
|
|
|
def get_spend_support_txo_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_support_utxo_ops(add=False)
|
|
|
|
|
|
|
|
|
2021-05-20 19:31:40 +02:00
|
|
|
class StagedActivation(typing.NamedTuple):
|
|
|
|
txo_type: int
|
|
|
|
claim_hash: bytes
|
|
|
|
tx_num: int
|
|
|
|
position: int
|
|
|
|
activation_height: int
|
|
|
|
name: str
|
|
|
|
amount: int
|
|
|
|
|
|
|
|
def _get_add_remove_activate_ops(self, add=True):
|
|
|
|
op = RevertablePut if add else RevertableDelete
|
2021-05-26 23:25:03 +02:00
|
|
|
print(f"\t{'add' if add else 'remove'} {'claim' if self.txo_type == ACTIVATED_CLAIM_TXO_TYPE else 'support'},"
|
|
|
|
f" {self.tx_num}, {self.position}, activation={self.activation_height}, {self.name}, "
|
|
|
|
f"amount={self.amount}")
|
2021-05-20 19:31:40 +02:00
|
|
|
return [
|
|
|
|
op(
|
|
|
|
*Prefixes.activated.pack_item(
|
|
|
|
self.txo_type, self.tx_num, self.position, self.activation_height, self.claim_hash, self.name
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
),
|
2021-05-20 19:31:40 +02:00
|
|
|
op(
|
|
|
|
*Prefixes.pending_activation.pack_item(
|
|
|
|
self.activation_height, self.txo_type, self.tx_num, self.position,
|
|
|
|
self.claim_hash, self.name
|
|
|
|
)
|
|
|
|
),
|
|
|
|
op(
|
|
|
|
*Prefixes.active_amount.pack_item(
|
|
|
|
self.claim_hash, self.txo_type, self.activation_height, self.tx_num, self.position, self.amount
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
)
|
2021-05-20 19:31:40 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_activate_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_activate_ops(add=True)
|
|
|
|
|
|
|
|
def get_remove_activate_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_activate_ops(add=False)
|
|
|
|
|
|
|
|
|
|
|
|
def get_remove_name_ops(name: str, claim_hash: bytes, height: int) -> typing.List[RevertableDelete]:
|
|
|
|
return [
|
|
|
|
RevertableDelete(
|
|
|
|
*Prefixes.claim_takeover.pack_item(
|
|
|
|
name, claim_hash, height
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
2021-02-19 19:19:58 +01:00
|
|
|
|
|
|
|
|
2021-05-05 22:17:32 +02:00
|
|
|
def get_takeover_name_ops(name: str, claim_hash: bytes, takeover_height: int,
|
2021-05-26 00:42:08 +02:00
|
|
|
previous_winning: Optional[ClaimTakeoverValue]):
|
2021-05-05 22:17:32 +02:00
|
|
|
if previous_winning:
|
|
|
|
return [
|
|
|
|
RevertableDelete(
|
|
|
|
*Prefixes.claim_takeover.pack_item(
|
|
|
|
name, previous_winning.claim_hash, previous_winning.height
|
|
|
|
)
|
|
|
|
),
|
|
|
|
RevertablePut(
|
|
|
|
*Prefixes.claim_takeover.pack_item(
|
|
|
|
name, claim_hash, takeover_height
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return [
|
|
|
|
RevertablePut(
|
|
|
|
*Prefixes.claim_takeover.pack_item(
|
|
|
|
name, claim_hash, takeover_height
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-05-27 19:35:41 +02:00
|
|
|
def get_remove_effective_amount_ops(name: str, effective_amount: int, tx_num: int, position: int, claim_hash: bytes):
|
|
|
|
return [
|
|
|
|
RevertableDelete(*EffectiveAmountPrefixRow.pack_item(name, effective_amount, tx_num, position, claim_hash))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_add_effective_amount_ops(name: str, effective_amount: int, tx_num: int, position: int, claim_hash: bytes):
|
|
|
|
return [
|
|
|
|
RevertablePut(*EffectiveAmountPrefixRow.pack_item(name, effective_amount, tx_num, position, claim_hash))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-02-19 19:19:58 +01:00
|
|
|
class StagedClaimtrieItem(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
claim_hash: bytes
|
|
|
|
amount: int
|
2021-02-21 23:26:13 +01:00
|
|
|
expiration_height: int
|
2021-02-19 19:19:58 +01:00
|
|
|
tx_num: int
|
|
|
|
position: int
|
|
|
|
root_claim_tx_num: int
|
|
|
|
root_claim_tx_position: int
|
|
|
|
signing_hash: Optional[bytes]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_update(self) -> bool:
|
|
|
|
return (self.tx_num, self.position) != (self.root_claim_tx_num, self.root_claim_tx_position)
|
|
|
|
|
|
|
|
def _get_add_remove_claim_utxo_ops(self, add=True):
|
|
|
|
"""
|
|
|
|
get a list of revertable operations to add or spend a claim txo to the key: value database
|
|
|
|
|
|
|
|
:param add: if true use RevertablePut operations, otherwise use RevertableDelete
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
op = RevertablePut if add else RevertableDelete
|
|
|
|
ops = [
|
|
|
|
# claim tip by claim hash
|
|
|
|
op(
|
|
|
|
*Prefixes.claim_to_txo.pack_item(
|
|
|
|
self.claim_hash, self.tx_num, self.position, self.root_claim_tx_num, self.root_claim_tx_position,
|
2021-05-20 19:31:40 +02:00
|
|
|
self.amount, self.name
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
),
|
|
|
|
# claim hash by txo
|
|
|
|
op(
|
|
|
|
*Prefixes.txo_to_claim.pack_item(self.tx_num, self.position, self.claim_hash, self.name)
|
2021-02-21 23:26:13 +01:00
|
|
|
),
|
|
|
|
# claim expiration
|
|
|
|
op(
|
|
|
|
*Prefixes.claim_expiration.pack_item(
|
|
|
|
self.expiration_height, self.tx_num, self.position, self.claim_hash,
|
|
|
|
self.name
|
|
|
|
)
|
2021-05-05 22:17:32 +02:00
|
|
|
),
|
2021-05-20 19:31:40 +02:00
|
|
|
# short url resolution
|
2021-05-05 22:17:32 +02:00
|
|
|
op(
|
2021-05-20 19:31:40 +02:00
|
|
|
*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.position
|
2021-05-05 22:17:32 +02:00
|
|
|
)
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
]
|
2021-05-20 19:31:40 +02:00
|
|
|
|
|
|
|
if self.signing_hash:
|
2021-02-19 19:19:58 +01:00
|
|
|
ops.extend([
|
|
|
|
# channel by stream
|
|
|
|
op(
|
|
|
|
*Prefixes.claim_to_channel.pack_item(self.claim_hash, self.signing_hash)
|
|
|
|
),
|
|
|
|
# stream by channel
|
|
|
|
op(
|
|
|
|
*Prefixes.channel_to_claim.pack_item(
|
2021-05-20 19:31:40 +02:00
|
|
|
self.signing_hash, self.name, self.tx_num, self.position, self.claim_hash
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
])
|
|
|
|
return ops
|
|
|
|
|
|
|
|
def get_add_claim_utxo_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_claim_utxo_ops(add=True)
|
|
|
|
|
|
|
|
def get_spend_claim_txo_ops(self) -> typing.List[RevertableOp]:
|
|
|
|
return self._get_add_remove_claim_utxo_ops(add=False)
|
|
|
|
|
|
|
|
def get_invalidate_channel_ops(self, db) -> typing.List[RevertableOp]:
|
|
|
|
if not self.signing_hash:
|
|
|
|
return []
|
|
|
|
return [
|
2021-05-20 19:31:40 +02:00
|
|
|
RevertableDelete(*Prefixes.claim_to_channel.pack_item(self.claim_hash, self.signing_hash))
|
|
|
|
] + delete_prefix(db, DB_PREFIXES.channel_to_claim.value + self.signing_hash)
|
2021-02-19 19:19:58 +01:00
|
|
|
|
|
|
|
def get_abandon_ops(self, db) -> typing.List[RevertableOp]:
|
|
|
|
packed_name = length_encoded_name(self.name)
|
|
|
|
delete_short_id_ops = delete_prefix(
|
|
|
|
db, DB_PREFIXES.claim_short_id_prefix.value + packed_name + self.claim_hash
|
|
|
|
)
|
|
|
|
delete_claim_ops = delete_prefix(db, DB_PREFIXES.claim_to_txo.value + self.claim_hash)
|
|
|
|
delete_supports_ops = delete_prefix(db, DB_PREFIXES.claim_to_support.value + self.claim_hash)
|
2021-05-20 19:31:40 +02:00
|
|
|
return delete_short_id_ops + delete_claim_ops + delete_supports_ops + self.get_invalidate_channel_ops(db)
|