rpc code simplifications (#6059)

* init

* nicer end to elif chain

* newlines after returns in elif chain

* pass comments

* error_msg

* simple error res
This commit is contained in:
Nikolaj Kuntner 2021-05-26 20:15:26 +02:00 committed by GitHub
parent 0b3124821f
commit cb1e5bdddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 17 deletions

View File

@ -86,7 +86,7 @@ class FullNodeRpcClient(RpcClient):
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in ((await self.fetch("get_coin_records_by_puzzle_hash", d))["coin_records"])
for coin in (await self.fetch("get_coin_records_by_puzzle_hash", d))["coin_records"]
]
async def get_coin_records_by_puzzle_hashes(
@ -104,7 +104,7 @@ class FullNodeRpcClient(RpcClient):
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in ((await self.fetch("get_coin_records_by_puzzle_hashes", d))["coin_records"])
for coin in (await self.fetch("get_coin_records_by_puzzle_hashes", d))["coin_records"]
]
async def get_additions_and_removals(self, header_hash: bytes32) -> Tuple[List[CoinRecord], List[CoinRecord]]:

View File

@ -44,11 +44,11 @@ class RpcServer:
await self.websocket.close()
async def _state_changed(self, *args):
change = args[0]
if self.websocket is None:
return None
payloads: List[Dict] = await self.rpc_api._state_changed(*args)
change = args[0]
if change == "add_connection" or change == "close_connection":
data = await self.get_connections({})
if data is not None:
@ -222,13 +222,10 @@ class RpcServer:
except Exception as e:
tb = traceback.format_exc()
self.log.warning(f"Error while handling message: {tb}")
if len(e.args) > 0:
error = {"success": False, "error": f"{e.args[0]}"}
else:
error = {"success": False, "error": f"{e}"}
if message is None:
return None
await websocket.send_str(format_response(message, error))
if message is not None:
error = e.args[0] if e.args else e
res = {"success": False, "error": f"{error}"}
await websocket.send_str(format_response(message, res))
async def connection(self, ws):
data = {"service": self.service_name}

View File

@ -346,6 +346,7 @@ class WalletRpcApi:
"colour": colour,
"wallet_id": cc_wallet.id(),
}
elif request["mode"] == "existing":
async with self.service.wallet_state_manager.lock:
cc_wallet = await CCWallet.create_wallet_for_cc(
@ -353,6 +354,10 @@ class WalletRpcApi:
)
asyncio.create_task(self._create_backup_and_upload(host))
return {"type": cc_wallet.type()}
else: # undefined mode
pass
elif request["wallet_type"] == "rl_wallet":
if request["rl_type"] == "admin":
log.info("Create rl admin wallet")
@ -374,6 +379,7 @@ class WalletRpcApi:
"origin": rl_admin.rl_info.rl_origin,
"pubkey": rl_admin.rl_info.admin_pubkey.hex(),
}
elif request["rl_type"] == "user":
log.info("Create rl user wallet")
async with self.service.wallet_state_manager.lock:
@ -385,6 +391,10 @@ class WalletRpcApi:
"type": rl_user.type(),
"pubkey": rl_user.rl_info.user_pubkey.hex(),
}
else: # undefined rl_type
pass
elif request["wallet_type"] == "did_wallet":
if request["did_type"] == "new":
backup_dids = []
@ -408,6 +418,7 @@ class WalletRpcApi:
"my_did": my_did,
"wallet_id": did_wallet.id(),
}
elif request["did_type"] == "recovery":
async with self.service.wallet_state_manager.lock:
did_wallet = await DIDWallet.create_new_did_wallet_from_recovery(
@ -434,6 +445,14 @@ class WalletRpcApi:
"num_verifications_required": did_wallet.did_info.num_of_backup_ids_needed,
}
else: # undefined did_type
pass
else: # undefined wallet_type
pass
return None
##########################################################################################
# Wallet
##########################################################################################
@ -747,22 +766,22 @@ class WalletRpcApi:
else:
new_amount_verifications_required = len(recovery_list)
async with self.service.wallet_state_manager.lock:
success = await wallet.update_recovery_list(recovery_list, new_amount_verifications_required)
update_success = await wallet.update_recovery_list(recovery_list, new_amount_verifications_required)
# Update coin with new ID info
updated_puz = await wallet.get_new_puzzle()
spend_bundle = await wallet.create_spend(updated_puz.get_tree_hash())
if spend_bundle is not None and success:
return {"success": True}
return {"success": False}
success = spend_bundle is not None and update_success
return {"success": success}
async def did_spend(self, request):
wallet_id = int(request["wallet_id"])
async with self.service.wallet_state_manager.lock:
wallet: DIDWallet = self.service.wallet_state_manager.wallets[wallet_id]
spend_bundle = await wallet.create_spend(request["puzzlehash"])
if spend_bundle is not None:
return {"success": True}
return {"success": False}
success = spend_bundle is not None
return {"success": success}
async def did_get_did(self, request):
wallet_id = int(request["wallet_id"])