Skip to content

Commit

Permalink
Merge branch 'development' into min-block-time
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnaveira committed Mar 21, 2024
2 parents 559c42e + 5ecf68d commit d7cc4a3
Show file tree
Hide file tree
Showing 58 changed files with 652 additions and 357 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/tari_dan_app_utilities/Cargo.toml
Expand Up @@ -27,6 +27,7 @@ tari_validator_node_client = { workspace = true }
tari_bor = { workspace = true, default-features = true }
tari_indexer_lib = { workspace = true }
tari_networking = { workspace = true}
tari_validator_node_rpc = { workspace = true }

anyhow = { workspace = true }
async-trait = { workspace = true }
Expand Down
Expand Up @@ -3,7 +3,7 @@

use serde_json as json;
use tari_engine_types::{
commit_result::ExecuteResult,
commit_result::FinalizeResult,
component::ComponentHeader,
non_fungible::NonFungibleContainer,
substate::{Substate, SubstateValue},
Expand All @@ -25,14 +25,13 @@ pub enum JsonEncodingError {

pub fn encode_finalized_result_into_json(result: &FinalizedResult) -> Result<Vec<json::Value>, JsonEncodingError> {
match &result.execute_result {
Some(res) => encode_execute_result_into_json(res),
Some(res) => encode_finalize_result_into_json(&res.finalize),
None => Ok(vec![]),
}
}

pub fn encode_execute_result_into_json(result: &ExecuteResult) -> Result<Vec<json::Value>, JsonEncodingError> {
result
.finalize
pub fn encode_finalize_result_into_json(finalize: &FinalizeResult) -> Result<Vec<json::Value>, JsonEncodingError> {
finalize
.execution_results
.iter()
.map(|r| serde_json::to_value(r.indexed.value()).map_err(JsonEncodingError::Serde))
Expand Down
1 change: 1 addition & 0 deletions applications/tari_dan_app_utilities/src/lib.rs
Expand Up @@ -23,6 +23,7 @@
pub mod base_layer_scanner;
pub mod configuration;
pub mod consensus_constants;
pub mod json_encoding;
pub mod keypair;
pub mod p2p_config;
pub mod seed_peer;
Expand Down
Expand Up @@ -87,7 +87,6 @@ where TTemplateProvider: TemplateProvider<Template = LoadedTemplate>
Ok(result) => result,
Err(err) => ExecuteResult {
finalize: FinalizeResult::new_rejected(tx_id, RejectReason::ExecutionFailure(err.to_string())),
fee_receipt: None,
},
};

Expand Down
8 changes: 4 additions & 4 deletions applications/tari_dan_wallet_cli/src/command/key.rs
Expand Up @@ -22,7 +22,7 @@

use clap::Subcommand;
use tari_common_types::types::PublicKey;
use tari_wallet_daemon_client::WalletDaemonClient;
use tari_wallet_daemon_client::{types::KeyBranch, WalletDaemonClient};

use crate::{table::Table, table_row};

Expand All @@ -42,11 +42,11 @@ impl KeysSubcommand {
use KeysSubcommand::*;
match self {
New => {
let key = client.create_key().await?;
let key = client.create_key(KeyBranch::Transaction).await?;
println!("New key pair {} created", key.public_key);
},
List => {
let resp = client.list_keys().await?;
let resp = client.list_keys(KeyBranch::Transaction).await?;
if resp.keys.is_empty() {
println!("No keys found. Use 'keys create' to create a new key pair");
return Ok(());
Expand All @@ -57,7 +57,7 @@ impl KeysSubcommand {
let resp = client.set_active_key(index).await?;
println!("Key {} ({}) is now active", index, resp.public_key);

let resp = client.list_keys().await?;
let resp = client.list_keys(KeyBranch::Transaction).await?;
print_keys(resp.keys);
},
}
Expand Down
115 changes: 48 additions & 67 deletions applications/tari_dan_wallet_daemon/src/handlers/accounts.rs
Expand Up @@ -17,7 +17,7 @@ use tari_dan_common_types::optional::Optional;
use tari_dan_wallet_crypto::ConfidentialProofStatement;
use tari_dan_wallet_sdk::{
apis::{jwt::JrpcPermission, key_manager, substate::ValidatorScanResult},
models::{ConfidentialOutputModel, OutputStatus, VersionedSubstateId},
models::{ConfidentialOutputModel, NewAccountInfo, OutputStatus, VersionedSubstateId},
storage::WalletStore,
DanWalletSdk,
};
Expand Down Expand Up @@ -79,7 +79,7 @@ use crate::{
wait_for_result_and_account,
},
indexer_jrpc_impl::IndexerJsonRpcNetworkInterface,
services::{NewAccountInfo, TransactionSubmittedEvent},
services::TransactionSubmittedEvent,
DEFAULT_FEE,
};

Expand Down Expand Up @@ -142,17 +142,15 @@ pub async fn handle_create(
.sign(&signing_key.key)
.build();

let tx_id = sdk.transaction_api().submit_transaction(transaction, vec![]).await?;

let mut events = context.notifier().subscribe();
context.notifier().notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: Some(NewAccountInfo {
let tx_id = context
.transaction_service()
.submit_transaction_with_new_account(transaction, vec![], NewAccountInfo {
name: req.account_name,
key_index: owner_key.key_index,
is_default: req.is_default,
}),
});
})
.await?;

let event = wait_for_result(&mut events, tx_id).await?;
if let Some(reject) = event.finalize.result.reject() {
Expand Down Expand Up @@ -245,13 +243,11 @@ pub async fn handle_invoke(
.sign(&signing_key.key)
.build();

let tx_id = sdk.transaction_api().submit_transaction(transaction, vec![]).await?;

let mut events = context.notifier().subscribe();
context.notifier().notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: None,
});
let tx_id = context
.transaction_service()
.submit_transaction(transaction, vec![])
.await?;

let mut finalized = wait_for_result(&mut events, tx_id).await?;
if let Some(reject) = finalized.finalize.result.reject() {
Expand Down Expand Up @@ -345,6 +341,7 @@ pub async fn handle_reveal_funds(
let sdk = context.wallet_sdk().clone();
sdk.jwt_api().check_auth(token, &[JrpcPermission::Admin])?;
let notifier = context.notifier().clone();
let transaction_service = context.transaction_service().clone();

// If the caller aborts the request early, this async block would be aborted at any await point. To avoid this, we
// spawn a task that will continue running.
Expand Down Expand Up @@ -457,13 +454,8 @@ pub async fn handle_reveal_funds(
sdk.confidential_outputs_api()
.proofs_set_transaction_hash(proof_id, *transaction.id())?;

let tx_id = sdk.transaction_api().submit_transaction(transaction, vec![]).await?;

let mut events = notifier.subscribe();
notifier.notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: None,
});
let tx_id = transaction_service.submit_transaction(transaction, vec![]).await?;

let finalized = wait_for_result(&mut events, tx_id).await?;
if let Some(reason) = finalized.finalize.reject() {
Expand Down Expand Up @@ -721,17 +713,20 @@ async fn finish_claiming<T: WalletStore>(
.with_inputs(inputs)
.sign(&account_secret_key.key)
.build();
let tx_id = sdk.transaction_api().submit_transaction(transaction, vec![]).await?;
let is_first_account = accounts_api.count()? == 0;
let mut events = context.notifier().subscribe();
context.notifier().notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: new_account_name.map(|name| NewAccountInfo {
name: Some(name),
key_index: account_secret_key.key_index,
is_default: is_first_account,
}),
});
let tx_id = context
.transaction_service()
.submit_transaction_with_opts(
transaction,
vec![],
new_account_name.map(|name| NewAccountInfo {
name: Some(name),
key_index: account_secret_key.key_index,
is_default: is_first_account,
}),
)
.await?;

// Wait for the monitor to pick up the new or updated account
let (finalized, _) = wait_for_result_and_account(&mut events, &tx_id, &account_address).await?;
Expand Down Expand Up @@ -958,44 +953,28 @@ pub async fn handle_transfer(
.sign(&account_secret_key.key)
.build();

// send the transaction
let required_inputs = inputs.into_iter().map(Into::into).collect();
// If dry run we can return the result immediately
if req.dry_run {
let result = sdk
.transaction_api()
let transaction_id = *transaction.id();
let execute_result = context
.transaction_service()
.submit_dry_run_transaction(transaction, required_inputs)
.await?;
let execute_result = result.result.into_execute_result().unwrap();
return Ok(TransferResponse {
transaction_id: result.transaction_id,
fee: execute_result
.fee_receipt
.clone()
.map(|fee_receipt| fee_receipt.total_fees_paid)
.unwrap_or_default(),
fee_refunded: execute_result
.fee_receipt
.clone()
.map(|fee_receipt| fee_receipt.total_fee_payment)
.unwrap_or_default() -
execute_result
.fee_receipt
.clone()
.map(|fee_receipt| fee_receipt.total_fees_paid)
.unwrap_or_default(),
result: execute_result.finalize,
transaction_id,
fee: execute_result.fee_receipt.total_fees_paid,
fee_refunded: execute_result.fee_receipt.total_fee_payment - execute_result.fee_receipt.total_fees_paid,
result: execute_result,
});
}
let tx_id = sdk
.transaction_api()
.submit_transaction(transaction, required_inputs)
.await?;

// Otherwise submit and wait for a result
let mut events = context.notifier().subscribe();
context.notifier().notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: None,
});
let tx_id = context
.transaction_service()
.submit_transaction(transaction, required_inputs)
.await?;

let finalized = wait_for_result(&mut events, tx_id).await?;

Expand Down Expand Up @@ -1070,6 +1049,7 @@ pub async fn handle_confidential_transfer(
if req.amount.is_negative() {
return Err(invalid_params("amount", Some("must be positive")));
}
let transaction_service = context.transaction_service().clone();

task::spawn(async move {
let outputs_api = sdk.confidential_outputs_api();
Expand Down Expand Up @@ -1217,26 +1197,27 @@ pub async fn handle_confidential_transfer(
.build();

if req.dry_run {
let result = sdk
let transaction = sdk
.transaction_api()
.submit_dry_run_transaction(transaction, inputs.into_iter().map(Into::into).collect())
.await?;
let execute_result = result.result.into_execute_result().unwrap();
let finalize = transaction
.finalize
.ok_or_else(|| anyhow!("No result for dry run transaction"))?;
return Ok(ConfidentialTransferResponse {
transaction_id: result.transaction_id,
fee: execute_result.fee_receipt.clone().unwrap().total_fees_paid,
result: execute_result.finalize,
transaction_id: *transaction.transaction.id(),
fee: finalize.fee_receipt.total_fees_paid,
result: finalize,
});
}

outputs_api.proofs_set_transaction_hash(proof_id, *transaction.id())?;

let tx_id = sdk
.transaction_api()
let mut events = notifier.subscribe();
let tx_id = transaction_service
.submit_transaction(transaction, inputs.into_iter().map(Into::into).collect())
.await?;

let mut events = notifier.subscribe();
notifier.notify(TransactionSubmittedEvent {
transaction_id: tx_id,
new_account: None,
Expand Down
9 changes: 8 additions & 1 deletion applications/tari_dan_wallet_daemon/src/handlers/context.rs
Expand Up @@ -8,13 +8,14 @@ use crate::{
config::WalletDaemonConfig,
indexer_jrpc_impl::IndexerJsonRpcNetworkInterface,
notify::Notify,
services::{AccountMonitorHandle, WalletEvent},
services::{AccountMonitorHandle, TransactionServiceHandle, WalletEvent},
};

#[derive(Debug, Clone)]
pub struct HandlerContext {
wallet_sdk: DanWalletSdk<SqliteWalletStore, IndexerJsonRpcNetworkInterface>,
notifier: Notify<WalletEvent>,
transaction_service: TransactionServiceHandle,
account_monitor: AccountMonitorHandle,
config: WalletDaemonConfig,
}
Expand All @@ -23,12 +24,14 @@ impl HandlerContext {
pub fn new(
wallet_sdk: DanWalletSdk<SqliteWalletStore, IndexerJsonRpcNetworkInterface>,
notifier: Notify<WalletEvent>,
transaction_service: TransactionServiceHandle,
account_monitor: AccountMonitorHandle,
config: WalletDaemonConfig,
) -> Self {
Self {
wallet_sdk,
notifier,
transaction_service,
account_monitor,
config,
}
Expand All @@ -46,6 +49,10 @@ impl HandlerContext {
&self.account_monitor
}

pub fn transaction_service(&self) -> &TransactionServiceHandle {
&self.transaction_service
}

pub fn config(&self) -> &WalletDaemonConfig {
&self.config
}
Expand Down
12 changes: 6 additions & 6 deletions applications/tari_dan_wallet_daemon/src/handlers/keys.rs
Expand Up @@ -18,15 +18,15 @@ use super::context::HandlerContext;
pub async fn handle_create(
context: &HandlerContext,
token: Option<String>,
value: KeysCreateRequest,
req: KeysCreateRequest,
) -> Result<KeysCreateResponse, anyhow::Error> {
let sdk = context.wallet_sdk();
sdk.jwt_api().check_auth(token, &[JrpcPermission::Admin])?;
let key_manager = sdk.key_manager_api();
let key = value
let key = req
.specific_index
.map(|idx| key_manager.derive_key(key_manager::TRANSACTION_BRANCH, idx))
.unwrap_or_else(|| key_manager.next_key(key_manager::TRANSACTION_BRANCH))?;
.map(|idx| key_manager.derive_key(req.branch.as_str(), idx))
.unwrap_or_else(|| key_manager.next_key(req.branch.as_str()))?;
Ok(KeysCreateResponse {
id: key.key_index,
public_key: PublicKey::from_secret_key(&key.key),
Expand All @@ -36,11 +36,11 @@ pub async fn handle_create(
pub async fn handle_list(
context: &HandlerContext,
token: Option<String>,
_value: KeysListRequest,
req: KeysListRequest,
) -> Result<KeysListResponse, anyhow::Error> {
let sdk = context.wallet_sdk();
sdk.jwt_api().check_auth(token, &[JrpcPermission::KeyList])?;
let keys = sdk.key_manager_api().get_all_keys(key_manager::TRANSACTION_BRANCH)?;
let keys = sdk.key_manager_api().get_all_keys(req.branch.as_str())?;
Ok(KeysListResponse { keys })
}

Expand Down

0 comments on commit d7cc4a3

Please sign in to comment.