Skip to content

Commit

Permalink
fix: better errors and create free coins button (#626)
Browse files Browse the repository at this point in the history
Description
---
Propagates error from the indexer to the caller.
Adds a Create Free Test coins button in the DAN wallet UI so that you
can create a transaction easily.

Motivation and Context
---
Errors were being swallowed in JSONRPC
Also, it helps with testing to be able to press a single button to
create a transaction on the network, as well as adding test coins to
play with

How Has This Been Tested?
---
Using DAN testing

What process can a PR reviewer use to test or verify this change?
---
Start a DAN wallet and press the button. Go to the account page and see
the balance increase

Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
stringhandler committed Jul 20, 2023
1 parent b3bb9b0 commit 5d7f81a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import { ReactNode, useEffect, useState } from "react";
import { accountsClaimBurn, accountsCreate, accountsList } from "../../../utils/json_rpc";
import {accountsClaimBurn, accountsCreate, accountsCreateFreeTestCoins, accountsList} from "../../../utils/json_rpc";
import Error from "./Error";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
Expand Down Expand Up @@ -98,6 +98,10 @@ function Accounts() {
setAccountFormState({ ...accountFormState, [e.target.name]: e.target.value });
};

const onClaimFreeCoins = () => {
accountsCreateFreeTestCoins("TestAccount", 100000, 1000).then((response) => { loadAccounts(); });
}

const onClaimBurn = () => {
accountsClaimBurn(claimBurnFormState.account, JSON.parse(claimBurnFormState.claimProof), +claimBurnFormState.fee)
.then((response) => {
Expand Down Expand Up @@ -130,6 +134,11 @@ function Accounts() {
<Alert severity="error">{error}</Alert>
) : null}
<BoxHeading2>
<div className="flex-container">
<Button variant="outlined" startIcon={<AddIcon />} onClick={() => onClaimFreeCoins()}>
Claim free testnet coins
</Button>
</div>
{showAccountDialog && (
<Fade in={showAccountDialog}>
<Form onSubmit={onSubmitAddAccount} className="flex-container">
Expand Down
1 change: 1 addition & 0 deletions applications/tari_dan_wallet_web_ui/src/utils/json_rpc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const accountsCreate = (
fee,
is_default,
]);
export const accountsCreateFreeTestCoins = (accountName: string | undefined, amount: number | undefined, fee: number | undefined) => jsonRpc("accounts.create_free_test_coins", [{ "Name": accountName}, amount, fee]);
export const accountsList = (offset: number, limit: number) =>
jsonRpc('accounts.list', [offset, limit]);
export const accountsGetBalances = (accountName: string) =>
Expand Down
58 changes: 40 additions & 18 deletions applications/tari_indexer/src/json_rpc/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ impl JsonRpcHandlers {
let response = json!({ "vns": vns });
Ok(JsonRpcResponse::success(answer_id, response))
},
Err(e) => {
warn!(target: LOG_TARGET, "Failed to get all vns: {}", e);
Err(Self::generic_error_response(answer_id))
},
Err(e) => Err(Self::generic_error_response(
answer_id,
format!("Failed to get all vns: {}", e),
)),
}
}

Expand All @@ -190,18 +190,18 @@ impl JsonRpcHandlers {
&tari_comms::net_address::PeerAddressSource::Config,
)
.await
.map_err(|_| Self::generic_error_response(answer_id))?;
.map_err(|e| Self::generic_error_response(answer_id, format!("Could not update peer: {}", e)))?;
if wait_for_dial {
let _conn = connectivity
.dial_peer(node_id)
.await
.map_err(|_| Self::generic_error_response(answer_id))?;
.map_err(|e| Self::generic_error_response(answer_id, e.to_string()))?;
} else {
// Dial without waiting
connectivity
.request_many_dials(Some(node_id))
.await
.map_err(|_| Self::generic_error_response(answer_id))?;
.map_err(|e| Self::generic_error_response(answer_id, e.to_string()))?;
}

Ok(JsonRpcResponse::success(answer_id, AddPeerResponse {}))
Expand All @@ -216,7 +216,10 @@ impl JsonRpcHandlers {
},
Err(e) => {
warn!(target: LOG_TARGET, "Failed to get comms stats: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Failed to get comms stats: {}", e),
))
},
}
}
Expand Down Expand Up @@ -267,7 +270,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting substate: {}", e);
Self::generic_error_response(answer_id)
Self::generic_error_response(answer_id, format!("Error getting substate: {}", e))
})? {
Some(substate_resp) => Ok(JsonRpcResponse::success(answer_id, GetSubstateResponse {
address: substate_resp.address,
Expand Down Expand Up @@ -359,7 +362,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting substate: {}", e);
Self::generic_error_response(answer_id)
Self::generic_error_response(answer_id, format!("Error getting substate: {}", e))
})?
.ok_or_else(|| {
JsonRpcResponse::error(
Expand Down Expand Up @@ -394,7 +397,10 @@ impl JsonRpcHandlers {
Ok(addresses) => Ok(JsonRpcResponse::success(answer_id, addresses)),
Err(e) => {
warn!(target: LOG_TARGET, "Error getting addresses: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Error getting addresses: {}", e),
))
},
}
}
Expand All @@ -411,7 +417,10 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error adding address: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Error adding address: {}", e),
))
},
}
}
Expand All @@ -424,7 +433,10 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error deleting address: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Error deleting address: {}", e),
))
},
}
}
Expand All @@ -436,7 +448,10 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error clearing addresses: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Error clearing addresses: {}", e),
))
},
}
}
Expand All @@ -450,7 +465,10 @@ impl JsonRpcHandlers {
Ok(collections) => Ok(JsonRpcResponse::success(answer_id, collections)),
Err(e) => {
warn!(target: LOG_TARGET, "Error getting non fungible collections: {}", e);
Err(Self::generic_error_response(answer_id))
Err(Self::generic_error_response(
answer_id,
format!("Error getting non fungible collections: {}", e),
))
},
}
}
Expand All @@ -464,7 +482,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting non fungible count: {}", e);
Self::generic_error_response(answer_id)
Self::generic_error_response(answer_id, format!("Error getting non fungible count: {}", e))
})?;

Ok(JsonRpcResponse::success(answer_id, count))
Expand Down Expand Up @@ -557,7 +575,11 @@ impl JsonRpcHandlers {
}

// TODO: pass the error in here and log it instead of "squashing" it (switch to using Self::internal_error)
fn generic_error_response(answer_id: i64) -> JsonRpcResponse {
Self::error_response(answer_id, JsonRpcErrorReason::InternalError, "Something went wrong")
fn generic_error_response(answer_id: i64, error: String) -> JsonRpcResponse {
Self::error_response(
answer_id,
JsonRpcErrorReason::InternalError,
format!("Something went wrong: {}", error),
)
}
}
5 changes: 5 additions & 0 deletions applications/tari_validator_node/log4rs_sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ loggers:
- stdout
additive: false

tari::dan::engine:
level: debug
appenders:
- engine

# Route log events sent to the "comms" logger to the "network" appender
comms:
level: debug
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/runtime/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<TTemplateProvider: TemplateProvider<Template = LoadedTemplate>> RuntimeInte
let template_address = self.tracker.get_template_address()?;

let event = Event::new(component_address, template_address, tx_hash, topic, payload);

log::log!(target: "tari::dan::engine::runtime", log::Level::Debug, "{}", event.to_string());
self.tracker.add_event(event);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/wasm/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::{
},
};

const LOG_TARGET: &str = "tari::dan::wasm::process";
const LOG_TARGET: &str = "tari::dan::engine::wasm::process";

#[derive(Debug)]
pub struct WasmProcess {
Expand Down

0 comments on commit 5d7f81a

Please sign in to comment.