[API] Fixes for block_show and claim_show (#1390)

block_show using block hash
block_show using height
claim_show from claim_id
claim_show from outpoint
This commit is contained in:
shyba 2018-08-27 21:03:08 -03:00 committed by Lex Berezhny
parent fdc98fc746
commit 5e371e8e6a
5 changed files with 52 additions and 10 deletions

View file

@ -487,7 +487,7 @@ class Daemon(AuthJSONRPCServer):
Resolve a name and return the estimated stream cost Resolve a name and return the estimated stream cost
""" """
resolved = (yield self.wallet.resolve(uri))[uri] resolved = yield self.wallet.resolve(uri)
if resolved: if resolved:
claim_response = resolved[uri] claim_response = resolved[uri]
else: else:
@ -2514,17 +2514,13 @@ class Daemon(AuthJSONRPCServer):
""" """
if blockhash is not None: if blockhash is not None:
d = self.wallet.get_block(blockhash) return self.wallet.get_block(blockhash)
elif height is not None: elif height is not None:
d = self.wallet.get_block_info(height) return self.wallet.get_block_info(height)
d.addCallback(lambda b: self.wallet.get_block(b))
else: else:
# TODO: return a useful error message # TODO: return a useful error message
return server.failure return server.failure
d.addCallback(lambda r: self._render_response(r))
return d
@requires(WALLET_COMPONENT, DHT_COMPONENT, BLOB_COMPONENT, RATE_LIMITER_COMPONENT, PAYMENT_RATE_COMPONENT, @requires(WALLET_COMPONENT, DHT_COMPONENT, BLOB_COMPONENT, RATE_LIMITER_COMPONENT, PAYMENT_RATE_COMPONENT,
conditions=[WALLET_IS_UNLOCKED]) conditions=[WALLET_IS_UNLOCKED])
@defer.inlineCallbacks @defer.inlineCallbacks

View file

@ -49,6 +49,11 @@ class MainNetLedger(BaseLedger):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fee_per_name_char = self.config.get('fee_per_name_char', self.default_fee_per_name_char) self.fee_per_name_char = self.config.get('fee_per_name_char', self.default_fee_per_name_char)
@property
def resolver(self):
return Resolver(self.headers.claim_trie_root, self.headers.height, self.transaction_class,
hash160_to_address=self.hash160_to_address, network=self.network)
@defer.inlineCallbacks @defer.inlineCallbacks
def resolve(self, page, page_size, *uris): def resolve(self, page, page_size, *uris):
for uri in uris: for uri in uris:
@ -57,9 +62,20 @@ class MainNetLedger(BaseLedger):
except URIParseError as err: except URIParseError as err:
defer.returnValue({'error': err.message}) defer.returnValue({'error': err.message})
resolutions = yield self.network.get_values_for_uris(self.headers.hash().decode(), *uris) resolutions = yield self.network.get_values_for_uris(self.headers.hash().decode(), *uris)
resolver = Resolver(self.headers.claim_trie_root, self.headers.height, self.transaction_class, return (yield self.resolver._handle_resolutions(resolutions, uris, page, page_size))
hash160_to_address=lambda x: self.hash160_to_address(x), network=self.network)
defer.returnValue((yield resolver._handle_resolutions(resolutions, uris, page, page_size))) @defer.inlineCallbacks
def get_claim_by_claim_id(self, claim_id):
result = (yield self.network.get_claims_by_ids(claim_id)).pop(claim_id, {})
return (yield self.resolver.get_certificate_and_validate_result(result))
@defer.inlineCallbacks
def get_claim_by_outpoint(self, txid, nout):
claims = (yield self.network.get_claims_in_tx(txid)) or []
for claim in claims:
if claim['nout'] == nout:
return (yield self.resolver.get_certificate_and_validate_result(claim))
return 'claim not found'
@defer.inlineCallbacks @defer.inlineCallbacks
def start(self): def start(self):

View file

@ -291,6 +291,19 @@ class LbryWalletManager(BaseWalletManager):
for wallet in self.wallets: for wallet in self.wallets:
wallet.save() wallet.save()
def get_block(self, block_hash):
return self.ledger.network.get_block(block_hash)
def get_block_info(self, height):
block_hash = self.ledger.headers.hash(height).decode()
return self.get_block(block_hash)
def get_claim_by_claim_id(self, claim_id):
return self.ledger.get_claim_by_claim_id(claim_id)
def get_claim_by_outpoint(self, txid, nout):
return self.ledger.get_claim_by_outpoint(txid, nout)
class ClientRequest: class ClientRequest:
def __init__(self, request_dict, response_identifier=None): def __init__(self, request_dict, response_identifier=None):

View file

@ -3,6 +3,9 @@ from torba.basenetwork import BaseNetwork
class Network(BaseNetwork): class Network(BaseNetwork):
def get_block(self, block_hash):
return self.rpc('blockchain.block.get_block', block_hash)
def get_server_height(self): def get_server_height(self):
return self.rpc('blockchain.block.get_server_height') return self.rpc('blockchain.block.get_server_height')
@ -11,3 +14,6 @@ class Network(BaseNetwork):
def get_claims_by_ids(self, *claim_ids): def get_claims_by_ids(self, *claim_ids):
return self.rpc('blockchain.claimtrie.getclaimsbyids', *claim_ids) return self.rpc('blockchain.claimtrie.getclaimsbyids', *claim_ids)
def get_claims_in_tx(self, txid):
return self.rpc('blockchain.claimtrie.getclaimsintx', txid)

View file

@ -145,6 +145,17 @@ class Resolver:
defer.returnValue(result) defer.returnValue(result)
@defer.inlineCallbacks
def get_certificate_and_validate_result(self, claim_result):
if not claim_result or 'value' not in claim_result:
return claim_result
certificate = None
certificate_id = smart_decode(claim_result['value']).certificate_id
if certificate_id:
certificate = yield self.network.get_claims_by_ids(certificate_id.decode())
certificate = certificate.pop(certificate_id.decode()) if certificate else None
return self.parse_and_validate_claim_result(claim_result, certificate=certificate)
def parse_and_validate_claim_result(self, claim_result, certificate=None, raw=False): def parse_and_validate_claim_result(self, claim_result, certificate=None, raw=False):
if not claim_result or 'value' not in claim_result: if not claim_result or 'value' not in claim_result:
return claim_result return claim_result