2021-02-19 19:19:58 +01:00
|
|
|
import typing
|
|
|
|
from typing import Optional
|
2021-06-22 23:25:23 +02:00
|
|
|
from lbry.wallet.server.db.revertable import RevertablePut, RevertableDelete, RevertableOp
|
2021-05-27 19:35:41 +02:00
|
|
|
from lbry.wallet.server.db.prefixes import Prefixes, ClaimTakeoverValue, EffectiveAmountPrefixRow
|
2021-06-22 23:25:23 +02:00
|
|
|
from lbry.wallet.server.db.prefixes import RepostPrefixRow, RepostedPrefixRow
|
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-06-04 23:41:26 +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
|
2021-07-01 02:09:17 +02:00
|
|
|
root_tx_num: int
|
|
|
|
root_position: int
|
2021-06-04 22:50:37 +02:00
|
|
|
channel_signature_is_valid: bool
|
2021-02-19 19:19:58 +01:00
|
|
|
signing_hash: Optional[bytes]
|
2021-06-02 17:00:27 +02:00
|
|
|
reposted_claim_hash: Optional[bytes]
|
2021-02-19 19:19:58 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_update(self) -> bool:
|
2021-07-01 02:09:17 +02:00
|
|
|
return (self.tx_num, self.position) != (self.root_tx_num, self.root_position)
|
2021-02-19 19:19:58 +01:00
|
|
|
|
|
|
|
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(
|
2021-07-01 02:09:17 +02:00
|
|
|
self.claim_hash, self.tx_num, self.position, self.root_tx_num, self.root_position,
|
2021-06-04 22:50:37 +02:00
|
|
|
self.amount, self.channel_signature_is_valid, 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(
|
2021-07-01 02:09:17 +02:00
|
|
|
self.name, self.claim_hash, self.root_tx_num, self.root_position, self.tx_num,
|
2021-05-20 19:31:40 +02:00
|
|
|
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-06-04 22:50:37 +02:00
|
|
|
ops.append(
|
2021-02-19 19:19:58 +01:00
|
|
|
# channel by stream
|
|
|
|
op(
|
2021-06-22 23:25:23 +02:00
|
|
|
*Prefixes.claim_to_channel.pack_item(
|
|
|
|
self.claim_hash, self.tx_num, self.position, self.signing_hash
|
|
|
|
)
|
2021-06-04 22:50:37 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
if self.channel_signature_is_valid:
|
|
|
|
ops.append(
|
|
|
|
# stream by channel
|
|
|
|
op(
|
|
|
|
*Prefixes.channel_to_claim.pack_item(
|
|
|
|
self.signing_hash, self.name, self.tx_num, self.position, self.claim_hash
|
|
|
|
)
|
2021-02-19 19:19:58 +01:00
|
|
|
)
|
|
|
|
)
|
2021-06-02 17:00:27 +02:00
|
|
|
if self.reposted_claim_hash:
|
|
|
|
ops.extend([
|
|
|
|
op(
|
|
|
|
*RepostPrefixRow.pack_item(self.claim_hash, self.reposted_claim_hash)
|
|
|
|
),
|
|
|
|
op(
|
|
|
|
*RepostedPrefixRow.pack_item(self.reposted_claim_hash, 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)
|
|
|
|
|