Skip to content

Commit

Permalink
Merge pull request #60 from AIBlockOfficial/user_wallet_fix
Browse files Browse the repository at this point in the history
User Wallet Update
  • Loading branch information
BHouwens authored May 1, 2024
2 parents 29e465f + dc20a8c commit 5b94ad9
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ pub async fn get_total_supply(
)
}

/// GET The last constructed transaction
pub async fn get_outgoing_txs(
route: &'static str,
db: WalletDb,
call_id: String,
) -> Result<JsonReply, JsonReply> {
let r = CallResponse::new(route, &call_id);

match db.get_outgoing_txs() {
Ok(tx) => r.into_ok(
"Successfully fetched last constructed transaction",
json_serialize_embed(tx),
),
Err(_e) => r.into_err_internal(ApiErrorType::CannotAccessWallet),
}
}

//======= POST HANDLERS =======//

/// Post to retrieve items from the blockchain db by hash key
Expand Down Expand Up @@ -642,6 +659,7 @@ pub async fn post_update_running_total(
_ => addresses,
};

debug!("Updating running total for addresses: {:?}", address_list);
let request = UserRequest::UserApi(UserApiRequest::UpdateWalletFromUtxoSet {
address_list: UtxoFetchType::AnyOf(address_list),
});
Expand Down
38 changes: 38 additions & 0 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,30 @@ pub fn get_shared_config(
.with(get_cors())
}

/// GET last constructed transaction
pub fn get_outgoing_txs(
dp: &mut DbgPaths,
db: WalletDb,
routes_pow: RoutesPoWInfo,
api_keys: ApiKeys,
cache: ReplyCache,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
let route = "outgoing_transactions";
warp_path(dp, route)
.and(warp::get())
.and(auth_request(routes_pow, api_keys))
.and(with_node_component(db))
.and(with_node_component(cache))
.and_then(move |call_id: String, db, cache| {
map_api_res_and_cache(
call_id.clone(),
cache,
handlers::get_outgoing_txs(route, db, call_id),
)
})
.with(get_cors())
}

//======= POST ROUTES =======//

// POST CORS
Expand Down Expand Up @@ -817,6 +841,13 @@ pub fn user_node_routes(
api_keys.clone(),
cache.clone(),
)
.or(get_outgoing_txs(
dp,
db.clone(),
routes_pow_info.clone(),
api_keys.clone(),
cache.clone(),
))
.or(make_payment(
dp,
db.clone(),
Expand Down Expand Up @@ -1167,6 +1198,13 @@ pub fn miner_node_with_user_routes(
api_keys.clone(),
cache.clone(),
))
.or(get_outgoing_txs(
dp,
db.clone(),
routes_pow_info.clone(),
api_keys.clone(),
cache.clone(),
))
// .or(make_ip_payment(
// dp,
// db.clone(),
Expand Down
13 changes: 10 additions & 3 deletions src/api/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,7 @@ async fn test_post_update_running_total() {
// Arrange
//
let (mut self_node, _self_socket) = new_self_node(NodeType::User).await;
let db = get_wallet_db("old_passphrase").await;

let addresses = vec![COMMON_PUB_ADDR.to_string()];

Expand All @@ -1764,9 +1765,15 @@ async fn test_post_update_running_total() {
let ks = to_api_keys(Default::default());
let cache = create_new_cache(CACHE_LIVE_TIME);

let filter =
routes::update_running_total(&mut dp(), self_node.clone(), Default::default(), ks, cache)
.recover(handle_rejection);
let filter = routes::update_running_total(
&mut dp(),
self_node.clone(),
db,
Default::default(),
ks,
cache,
)
.recover(handle_rejection);
let res = request.reply(&filter).await;

//
Expand Down
2 changes: 1 addition & 1 deletion src/bin/node_settings_mainnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ address = "52.40.82.170:8080"
address = "52.27.248.13:12340"

[[user_nodes]]
address = "127.0.0.1:12342"
address = "127.0.0.1:12349"
11 changes: 4 additions & 7 deletions src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,13 +1774,10 @@ impl MempoolNode {
.await;
}

if self.miners_changed {
info!(
"Change in no. of connected miners: {:?}",
self.get_connected_miners().await.len()
);
self.miners_changed = false;
}
info!(
"Number of connected miners: {:?}",
self.get_connected_miners().await.len()
);

Ok(())
}
Expand Down
26 changes: 22 additions & 4 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use tw_chain::primitives::block::Block;
use tw_chain::primitives::druid::{DdeValues, DruidExpectation};
use tw_chain::primitives::transaction::{GenesisTxHashSpec, Transaction, TxIn, TxOut};
use tw_chain::utils::transaction_utils::{
construct_item_create_tx, construct_rb_payments_send_tx, construct_rb_receive_payment_tx, construct_tx_core, construct_tx_ins_address, update_input_signatures, ReceiverInfo
construct_item_create_tx, construct_rb_payments_send_tx, construct_rb_receive_payment_tx,
construct_tx_core, construct_tx_ins_address, update_input_signatures, ReceiverInfo,
};

use std::{collections::BTreeMap, error::Error, fmt, future::Future, net::SocketAddr};
Expand Down Expand Up @@ -1090,6 +1091,8 @@ impl UserNode {
let key_material = self.wallet_db.get_key_material(&tx_ins);
let final_tx_ins = update_input_signatures(&tx_ins, &tx_outs, &key_material);
let payment_tx = construct_tx_core(final_tx_ins, tx_outs, None);

self.wallet_db.set_last_construct_tx(payment_tx.clone());
self.next_payment = Some((peer, payment_tx));

Response {
Expand Down Expand Up @@ -1723,8 +1726,15 @@ pub fn make_rb_payment_item_tx_and_response(
genesis_hash: None,
};

let rb_receive_tx =
construct_rb_receive_payment_tx(tx_ins, tx_outs, None, sender_address, 0, dde_values, &BTreeMap::new());
let rb_receive_tx = construct_rb_receive_payment_tx(
tx_ins,
tx_outs,
None,
sender_address,
0,
dde_values,
&BTreeMap::new(),
);

let rb_payment_response = RbPaymentResponseData {
receiver_address,
Expand Down Expand Up @@ -1769,7 +1779,15 @@ pub fn make_rb_payment_send_transaction(
asset: sender_asset,
};

construct_rb_payments_send_tx(tx_ins, tx_outs, None, receiver, 0, druid_values, &BTreeMap::new())
construct_rb_payments_send_tx(
tx_ins,
tx_outs,
None,
receiver,
0,
druid_values,
&BTreeMap::new(),
)
}

fn make_transaction_gen(setup: UserAutoGenTxSetup) -> Option<AutoGenTx> {
Expand Down
13 changes: 11 additions & 2 deletions src/wallet/fund_store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::wallet::LockedCoinbase;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tracing::warn;
use tracing::{debug, warn};
use tw_chain::primitives::asset::{Asset, AssetValues};
use tw_chain::primitives::transaction::OutPoint;

Expand Down Expand Up @@ -46,6 +46,13 @@ impl FundStore {
&self.transactions
}

pub fn reset(&mut self) {
self.running_total = Default::default();
self.transactions = Default::default();
self.transaction_pages = vec![BTreeMap::new()];
self.spent_transactions = Default::default();
}

/// Filters out locked coinbase transactions, updating the running total.
///
/// Returns amount of filtered out coinbase transactions due to locktime
Expand Down Expand Up @@ -122,7 +129,8 @@ impl FundStore {
.insert(out_p.clone(), asset_to_save.clone())
{
if old_amount != amount {
panic!("Try to insert existing transaction with different amount");
warn!("Try to insert existing transaction with different amount");
return;
}
} else {
//Adds the entry to transaction pages
Expand All @@ -131,6 +139,7 @@ impl FundStore {
page.insert(out_p, asset_to_save.clone());
}
self.running_total.update_add(&asset_to_save);
debug!("Running total after internal add: {:?}", self.running_total);
}
}

Expand Down
Loading

0 comments on commit 5b94ad9

Please sign in to comment.