lbry-sdk/lbry/lbrynet/wallet/ledger.py

133 lines
5.3 KiB
Python
Raw Normal View History

2018-10-15 23:16:43 +02:00
import asyncio
2018-07-10 06:30:13 +02:00
import logging
2018-06-12 17:53:29 +02:00
from binascii import unhexlify
2019-04-29 06:38:58 +02:00
from typing import Tuple, List, Dict
2018-06-12 17:53:29 +02:00
2018-11-09 20:02:03 +01:00
from torba.client.baseledger import BaseLedger
from torba.client.baseaccount import SingleKey
2019-04-29 06:38:58 +02:00
from lbrynet.schema.result import Outputs
from lbrynet.schema.url import URL
from lbrynet.wallet.dewies import dewies_to_lbc
from lbrynet.wallet.resolve import Resolver
2019-03-31 23:40:38 +02:00
from lbrynet.wallet.account import Account
from lbrynet.wallet.network import Network
from lbrynet.wallet.database import WalletDatabase
2019-03-31 00:40:01 +01:00
from lbrynet.wallet.transaction import Transaction, Output
from lbrynet.wallet.header import Headers, UnvalidatedHeaders
2018-07-10 06:30:13 +02:00
log = logging.getLogger(__name__)
2018-06-14 06:53:38 +02:00
class MainNetLedger(BaseLedger):
2018-06-12 17:53:29 +02:00
name = 'LBRY Credits'
symbol = 'LBC'
2018-06-14 06:53:38 +02:00
network_name = 'mainnet'
2018-06-12 17:53:29 +02:00
2018-10-15 23:16:43 +02:00
headers: Headers
2018-07-05 04:16:02 +02:00
account_class = Account
2018-06-12 17:53:29 +02:00
database_class = WalletDatabase
headers_class = Headers
network_class = Network
transaction_class = Transaction
2019-03-24 21:55:04 +01:00
db: WalletDatabase
2018-10-15 23:16:43 +02:00
secret_prefix = bytes((0x1c,))
pubkey_address_prefix = bytes((0x55,))
script_address_prefix = bytes((0x7a,))
2018-07-01 23:21:18 +02:00
extended_public_key_prefix = unhexlify('0488b21e')
extended_private_key_prefix = unhexlify('0488ade4')
2018-06-14 06:53:38 +02:00
max_target = 0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
genesis_hash = '9c89283ba0f3227f6c03b70216b9f665f0118d5e0fa729cedf4fb34d6a34f463'
genesis_bits = 0x1f00ffff
target_timespan = 150
2018-06-12 17:53:29 +02:00
default_fee_per_byte = 50
default_fee_per_name_char = 200000
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2018-06-12 17:53:29 +02:00
self.fee_per_name_char = self.config.get('fee_per_name_char', self.default_fee_per_name_char)
2019-04-20 00:16:21 +02:00
self.resolver = Resolver(self)
2019-04-29 06:38:58 +02:00
async def _inflate_outputs(self, query):
outputs = Outputs.from_base64(await query)
txs = []
if len(outputs.txs) > 0:
txs = await asyncio.gather(*(self.cache_transaction(*tx) for tx in outputs.txs))
return outputs.inflate(txs), outputs.offset, outputs.total
2018-10-15 23:16:43 +02:00
2019-04-29 06:38:58 +02:00
async def resolve(self, urls):
txos = (await self._inflate_outputs(self.network.resolve(urls)))[0]
assert len(urls) == len(txos), "Mismatch between urls requested for resolve and responses received."
result = {}
for url, txo in zip(urls, txos):
2019-05-05 05:24:41 +02:00
if txo and URL.parse(url).has_stream_in_channel:
if not txo.channel or not txo.is_signed_by(txo.channel, self):
txo = None
if txo:
result[url] = txo
else:
result[url] = {'error': f'{url} did not resolve to a claim'}
return result
2018-10-15 23:16:43 +02:00
2019-04-29 06:38:58 +02:00
async def claim_search(self, **kwargs) -> Tuple[List, int, int]:
return await self._inflate_outputs(self.network.claim_search(**kwargs))
2019-03-31 00:40:01 +01:00
2019-04-29 06:38:58 +02:00
async def get_claim_by_claim_id(self, claim_id) -> Dict[str, Output]:
for claim in (await self.claim_search(claim_id=claim_id))[0]:
return claim
2018-06-12 17:53:29 +02:00
2018-10-15 23:16:43 +02:00
async def start(self):
await super().start()
await asyncio.gather(*(a.maybe_migrate_certificates() for a in self.accounts))
await asyncio.gather(*(a.save_max_gap() for a in self.accounts))
await self._report_state()
async def _report_state(self):
try:
for account in self.accounts:
balance = dewies_to_lbc(await account.get_balance())
channel_count = await account.get_channel_count()
claim_count = await account.get_claim_count()
if isinstance(account.receiving, SingleKey):
log.info("Loaded single key account %s with %s LBC. "
"%d channels, %d certificates and %d claims",
account.id, balance, channel_count, len(account.channel_keys), claim_count)
else:
total_receiving = len((await account.receiving.get_addresses()))
total_change = len((await account.change.get_addresses()))
log.info("Loaded account %s with %s LBC, %d receiving addresses (gap: %d), "
"%d change addresses (gap: %d), %d channels, %d certificates and %d claims. ",
account.id, balance, total_receiving, account.receiving.gap, total_change,
account.change.gap, channel_count, len(account.channel_keys), claim_count)
except:
log.exception(
'Failed to display wallet state, please file issue '
'for this bug along with the traceback you see below:')
2018-07-12 05:18:59 +02:00
2018-06-12 17:53:29 +02:00
2018-06-14 06:53:38 +02:00
class TestNetLedger(MainNetLedger):
2018-06-12 17:53:29 +02:00
network_name = 'testnet'
2018-10-15 23:16:43 +02:00
pubkey_address_prefix = bytes((111,))
script_address_prefix = bytes((196,))
2018-06-12 17:53:29 +02:00
extended_public_key_prefix = unhexlify('043587cf')
extended_private_key_prefix = unhexlify('04358394')
2018-06-14 06:53:38 +02:00
class RegTestLedger(MainNetLedger):
2018-06-12 17:53:29 +02:00
network_name = 'regtest'
2018-08-16 07:38:28 +02:00
headers_class = UnvalidatedHeaders
2018-10-15 23:16:43 +02:00
pubkey_address_prefix = bytes((111,))
script_address_prefix = bytes((196,))
2018-06-12 17:53:29 +02:00
extended_public_key_prefix = unhexlify('043587cf')
extended_private_key_prefix = unhexlify('04358394')
max_target = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
genesis_hash = '6e3fcf1299d4ec5d79c3a4c91d624a4acf9e2e173d95a1a0504f677669687556'
genesis_bits = 0x207fffff
target_timespan = 1