lbry-sdk/lbrynet/wallet/ledger.py

105 lines
3.9 KiB
Python
Raw Normal View History

2018-10-15 17:16:43 -04:00
import asyncio
2018-07-10 01:30:13 -03:00
import logging
2018-06-12 11:53:29 -04:00
from binascii import unhexlify
2018-11-09 14:02:03 -05:00
from torba.client.baseledger import BaseLedger
from lbrynet.wallet.dewies import dewies_to_lbc
from lbrynet.wallet.resolve import Resolver
2019-03-31 18:40:38 -03:00
from lbrynet.wallet.account import Account
from lbrynet.wallet.network import Network
from lbrynet.wallet.database import WalletDatabase
from lbrynet.wallet.transaction import Transaction
from lbrynet.wallet.header import Headers, UnvalidatedHeaders
2018-07-10 01:30:13 -03:00
log = logging.getLogger(__name__)
2018-06-14 00:53:38 -04:00
class MainNetLedger(BaseLedger):
2018-06-12 11:53:29 -04:00
name = 'LBRY Credits'
symbol = 'LBC'
2018-06-14 00:53:38 -04:00
network_name = 'mainnet'
2018-06-12 11:53:29 -04:00
2018-10-15 17:16:43 -04:00
headers: Headers
2018-07-04 22:16:02 -04:00
account_class = Account
2018-06-12 11:53:29 -04:00
database_class = WalletDatabase
headers_class = Headers
network_class = Network
transaction_class = Transaction
2019-03-24 16:55:04 -04:00
db: WalletDatabase
2018-10-15 17:16:43 -04:00
secret_prefix = bytes((0x1c,))
pubkey_address_prefix = bytes((0x55,))
script_address_prefix = bytes((0x7a,))
2018-07-01 17:21:18 -04:00
extended_public_key_prefix = unhexlify('0488b21e')
extended_private_key_prefix = unhexlify('0488ade4')
2018-06-14 00:53:38 -04:00
max_target = 0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
genesis_hash = '9c89283ba0f3227f6c03b70216b9f665f0118d5e0fa729cedf4fb34d6a34f463'
genesis_bits = 0x1f00ffff
target_timespan = 150
2018-06-12 11:53:29 -04:00
default_fee_per_byte = 50
default_fee_per_name_char = 200000
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2018-06-12 11:53:29 -04:00
self.fee_per_name_char = self.config.get('fee_per_name_char', self.default_fee_per_name_char)
2019-04-19 19:16:21 -03:00
self.resolver = Resolver(self)
2019-03-29 18:07:02 -03:00
def resolve(self, page, page_size, *uris):
return self.resolver.resolve(page, page_size, *uris)
2018-10-15 17:16:43 -04:00
async def get_claim_by_claim_id(self, claim_id):
result = (await self.network.get_claims_by_ids(claim_id)).pop(claim_id, {})
2019-03-31 17:25:59 -03:00
return await self.resolver.parse_and_validate_claim_result(result)
2018-10-15 17:16:43 -04:00
async def get_claim_by_outpoint(self, txid, nout):
claims = (await self.network.get_claims_in_tx(txid)) or []
for claim in claims:
if claim['nout'] == nout:
2019-03-31 17:25:59 -03:00
return await self.resolver.parse_and_validate_claim_result(claim)
return 'claim not found'
2018-06-12 11:53:29 -04:00
2018-10-15 17:16:43 -04: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):
2018-10-12 18:11:47 -03:00
for account in self.accounts:
2018-10-15 17:16:43 -04:00
total_receiving = len((await account.receiving.get_addresses()))
total_change = len((await account.change.get_addresses()))
channel_count = await account.get_channel_count()
claim_count = await account.get_claim_count()
2018-11-07 10:39:13 -03:00
balance = dewies_to_lbc(await account.get_balance())
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,
2019-03-24 16:55:04 -04:00
channel_count, len(account.channel_keys), claim_count)
2018-07-11 23:18:59 -04:00
2018-06-12 11:53:29 -04:00
2018-06-14 00:53:38 -04:00
class TestNetLedger(MainNetLedger):
2018-06-12 11:53:29 -04:00
network_name = 'testnet'
2018-10-15 17:16:43 -04:00
pubkey_address_prefix = bytes((111,))
script_address_prefix = bytes((196,))
2018-06-12 11:53:29 -04:00
extended_public_key_prefix = unhexlify('043587cf')
extended_private_key_prefix = unhexlify('04358394')
2018-06-14 00:53:38 -04:00
class RegTestLedger(MainNetLedger):
2018-06-12 11:53:29 -04:00
network_name = 'regtest'
2018-08-16 01:38:28 -04:00
headers_class = UnvalidatedHeaders
2018-10-15 17:16:43 -04:00
pubkey_address_prefix = bytes((111,))
script_address_prefix = bytes((196,))
2018-06-12 11:53:29 -04: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