forked from LBRYCommunity/lbry-sdk
move resolve from ledger to resolve.py
This commit is contained in:
parent
d16e2716c8
commit
ca17d8597d
2 changed files with 24 additions and 22 deletions
|
@ -51,26 +51,11 @@ class MainNetLedger(BaseLedger):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def resolver(self):
|
def resolver(self):
|
||||||
return Resolver(self.headers.claim_trie_root, self.headers.height, self.transaction_class,
|
return Resolver(self.headers, self.transaction_class,
|
||||||
hash160_to_address=self.hash160_to_address, network=self.network, ledger=self)
|
hash160_to_address=self.hash160_to_address, network=self.network, ledger=self)
|
||||||
|
|
||||||
async def resolve(self, page, page_size, *uris):
|
def resolve(self, page, page_size, *uris):
|
||||||
for uri in uris:
|
return self.resolver.resolve(page, page_size, *uris)
|
||||||
try:
|
|
||||||
parsed_uri = parse_lbry_uri(uri)
|
|
||||||
if parsed_uri.claim_id:
|
|
||||||
validate_claim_id(parsed_uri.claim_id)
|
|
||||||
except URIParseError as err:
|
|
||||||
return {'error': err.args[0]}
|
|
||||||
except Exception as e:
|
|
||||||
return {'error': str(e)}
|
|
||||||
try:
|
|
||||||
resolver = self.resolver
|
|
||||||
resolutions = await self.network.get_values_for_uris(self.headers.hash().decode(), *uris)
|
|
||||||
return await resolver._handle_resolutions(resolutions, uris, page, page_size)
|
|
||||||
except Exception as e:
|
|
||||||
log.exception(e)
|
|
||||||
return {'error': str(e)}
|
|
||||||
|
|
||||||
async def get_claim_by_claim_id(self, claim_id):
|
async def get_claim_by_claim_id(self, claim_id):
|
||||||
result = (await self.network.get_claims_by_ids(claim_id)).pop(claim_id, {})
|
result = (await self.network.get_claims_by_ids(claim_id)).pop(claim_id, {})
|
||||||
|
|
|
@ -2,11 +2,13 @@ import logging
|
||||||
|
|
||||||
from cryptography.exceptions import InvalidSignature
|
from cryptography.exceptions import InvalidSignature
|
||||||
from binascii import unhexlify, hexlify
|
from binascii import unhexlify, hexlify
|
||||||
|
|
||||||
|
from lbrynet.wallet.account import validate_claim_id
|
||||||
from lbrynet.wallet.dewies import dewies_to_lbc
|
from lbrynet.wallet.dewies import dewies_to_lbc
|
||||||
from lbrynet.error import UnknownNameError, UnknownClaimID, UnknownURI, UnknownOutpoint
|
from lbrynet.error import UnknownNameError, UnknownClaimID, UnknownURI, UnknownOutpoint
|
||||||
from lbrynet.schema.claim import Claim
|
from lbrynet.schema.claim import Claim
|
||||||
from google.protobuf.message import DecodeError
|
from google.protobuf.message import DecodeError
|
||||||
from lbrynet.schema.uri import parse_lbry_uri
|
from lbrynet.schema.uri import parse_lbry_uri, URIParseError
|
||||||
from lbrynet.wallet.claim_proofs import verify_proof, InvalidProofError
|
from lbrynet.wallet.claim_proofs import verify_proof, InvalidProofError
|
||||||
from lbrynet.wallet.transaction import Transaction
|
from lbrynet.wallet.transaction import Transaction
|
||||||
|
|
||||||
|
@ -15,14 +17,29 @@ log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Resolver:
|
class Resolver:
|
||||||
|
|
||||||
def __init__(self, claim_trie_root, height, transaction_class, hash160_to_address, network, ledger):
|
def __init__(self, headers, transaction_class, hash160_to_address, network, ledger):
|
||||||
self.claim_trie_root = claim_trie_root
|
self.claim_trie_root = headers.claim_trie_root
|
||||||
self.height = height
|
self.height = headers.height
|
||||||
|
self.header_hash = headers.hash().decode()
|
||||||
self.transaction_class = transaction_class
|
self.transaction_class = transaction_class
|
||||||
self.hash160_to_address = hash160_to_address
|
self.hash160_to_address = hash160_to_address
|
||||||
self.network = network
|
self.network = network
|
||||||
self.ledger = ledger
|
self.ledger = ledger
|
||||||
|
|
||||||
|
async def resolve(self, page, page_size, *uris):
|
||||||
|
try:
|
||||||
|
for uri in uris:
|
||||||
|
parsed_uri = parse_lbry_uri(uri)
|
||||||
|
if parsed_uri.claim_id:
|
||||||
|
validate_claim_id(parsed_uri.claim_id)
|
||||||
|
resolutions = await self.network.get_values_for_uris(self.header_hash, *uris)
|
||||||
|
return await self._handle_resolutions(resolutions, uris, page, page_size)
|
||||||
|
except URIParseError as err:
|
||||||
|
return {'error': err.args[0]}
|
||||||
|
except Exception as e:
|
||||||
|
log.exception(e)
|
||||||
|
return {'error': str(e)}
|
||||||
|
|
||||||
async def _handle_resolutions(self, resolutions, requested_uris, page, page_size):
|
async def _handle_resolutions(self, resolutions, requested_uris, page, page_size):
|
||||||
results = {}
|
results = {}
|
||||||
for uri in requested_uris:
|
for uri in requested_uris:
|
||||||
|
|
Loading…
Reference in a new issue