Skip to content

Commit 99dd978

Browse files
authored
Merge pull request #13 from ABlockOfficial/znp_update
Updating Network to ZNP Public Merge
2 parents 866cef8 + 2d27be9 commit 99dd978

18 files changed

+811
-326
lines changed

src/api/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum ApiErrorType {
1616
CannotParseAddress,
1717
CannotAccessWallet,
1818
CannotAccessUserNode,
19+
CannotAccessMinerNode,
1920
CannotAccessComputeNode,
2021
CannotAccessPeerUserNode,
2122
CannotSaveAddressesToWallet,
@@ -57,6 +58,7 @@ impl std::fmt::Display for ApiErrorType {
5758
ApiErrorType::CannotParseAddress => write!(f, "Cannot parse address"),
5859
ApiErrorType::CannotAccessWallet => write!(f, "Cannot access wallet"),
5960
ApiErrorType::CannotAccessUserNode => write!(f, "Cannot access user node"),
61+
ApiErrorType::CannotAccessMinerNode => write!(f, "Cannot access miner node"),
6062
ApiErrorType::CannotAccessComputeNode => write!(f, "Cannot access compute node"),
6163
ApiErrorType::CannotAccessPeerUserNode => write!(f, "Cannot access peer user node"),
6264
ApiErrorType::CannotSaveAddressesToWallet => {

src/api/handlers.rs

+56-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::constants::LAST_BLOCK_HASH_KEY;
1111
use crate::db_utils::SimpleDb;
1212
use crate::interfaces::{
1313
node_type_as_str, AddressesWithOutPoints, BlockchainItem, BlockchainItemMeta,
14-
BlockchainItemType, ComputeApi, DebugData, DruidPool, OutPointData, StoredSerializingBlock,
15-
UserApiRequest, UserRequest, UtxoFetchType,
14+
BlockchainItemType, ComputeApi, DebugData, DruidPool, MineApiRequest, MineRequest, NodeType,
15+
OutPointData, StoredSerializingBlock, UserApiRequest, UserRequest, UtxoFetchType,
1616
};
1717
use crate::miner::{BlockPoWReceived, CurrentBlockWithMutex};
1818
use crate::storage::{get_stored_value_from_db, indexed_block_hash_key};
@@ -55,6 +55,11 @@ pub struct Addresses {
5555
#[derive(Debug, Clone, Serialize, Deserialize)]
5656
struct WalletInfo {
5757
running_total: f64,
58+
running_total_tokens: u64,
59+
locked_total: f64,
60+
locked_total_tokens: u64,
61+
available_total: f64,
62+
available_total_tokens: u64,
5863
item_total: BTreeMap<String, u64>, /* DRS tx hash - amount */
5964
addresses: AddressesWithOutPoints,
6065
}
@@ -71,6 +76,7 @@ pub struct EncapsulatedPayment {
7176
pub address: String,
7277
pub amount: TokenAmount,
7378
pub passphrase: String,
79+
pub locktime: Option<u64>,
7480
}
7581

7682
/// Item asset creation structure received from client
@@ -172,7 +178,7 @@ pub async fn get_wallet_info(
172178
) -> Result<JsonReply, JsonReply> {
173179
let r = CallResponse::new(route, &call_id);
174180

175-
let fund_store = match wallet_db.get_fund_store_err() {
181+
let mut fund_store = match wallet_db.get_fund_store_err() {
176182
Ok(fund) => fund,
177183
Err(_) => return r.into_err_internal(ApiErrorType::CannotAccessWallet),
178184
};
@@ -200,15 +206,20 @@ pub async fn get_wallet_info(
200206
.or_default()
201207
.push(OutPointData::new(out_point.clone(), asset.clone()));
202208
}
203-
204-
let total = fund_store.running_total();
205-
let (running_total, item_total) = (
206-
total.tokens.0 as f64 / D_DISPLAY_PLACES,
207-
total.items.clone(),
208-
);
209+
let locked_coinbase = wallet_db.get_locked_coinbase().await;
210+
let total = fund_store.running_total().clone();
211+
let available = {
212+
fund_store.filter_locked_coinbase(&locked_coinbase);
213+
fund_store.running_total()
214+
};
209215
let send_val = WalletInfo {
210-
running_total,
211-
item_total,
216+
running_total: total.tokens.0 as f64 / D_DISPLAY_PLACES,
217+
running_total_tokens: total.tokens.0,
218+
locked_total: (total.tokens.0 - available.tokens.0) as f64 / D_DISPLAY_PLACES,
219+
locked_total_tokens: (total.tokens.0 - available.tokens.0),
220+
available_total: available.tokens.0 as f64 / D_DISPLAY_PLACES,
221+
available_total_tokens: available.tokens.0,
222+
item_total: total.items,
212223
addresses,
213224
};
214225

@@ -401,6 +412,7 @@ pub async fn post_block_by_num(
401412

402413
/// Post to import new keypairs to the connected wallet
403414
pub async fn post_import_keypairs(
415+
peer: Node,
404416
db: WalletDb,
405417
keypairs: Addresses,
406418
route: &'static str,
@@ -409,6 +421,7 @@ pub async fn post_import_keypairs(
409421
let response_keys: Vec<String> = keypairs.addresses.keys().cloned().collect();
410422
let response_data = json_serialize_embed(response_keys);
411423
let r = CallResponse::new(route, &call_id);
424+
let addresses: Vec<String> = keypairs.addresses.keys().cloned().collect();
412425

413426
let mut key_pairs_converted = BTreeMap::new();
414427
for (address, address_store_hex) in keypairs.addresses.into_iter() {
@@ -439,6 +452,34 @@ pub async fn post_import_keypairs(
439452
}
440453
}
441454

455+
match peer.get_node_type() {
456+
NodeType::Miner => {
457+
// Update running total from compute node
458+
if let Err(e) = peer.inject_next_event(
459+
peer.local_address(),
460+
MineRequest::MinerApi(MineApiRequest::RequestUTXOSet(UtxoFetchType::AnyOf(
461+
addresses,
462+
))),
463+
) {
464+
error!("route:update_running_total error: {:?}", e);
465+
return r.into_err_internal(ApiErrorType::CannotAccessMinerNode);
466+
}
467+
}
468+
NodeType::User => {
469+
// Update running total from compute node
470+
if let Err(e) = peer.inject_next_event(
471+
peer.local_address(),
472+
UserRequest::UserApi(UserApiRequest::UpdateWalletFromUtxoSet {
473+
address_list: UtxoFetchType::AnyOf(addresses),
474+
}),
475+
) {
476+
error!("route:update_running_total error: {:?}", e);
477+
return r.into_err_internal(ApiErrorType::CannotAccessUserNode);
478+
}
479+
}
480+
_ => return r.into_err_internal(ApiErrorType::InternalError),
481+
}
482+
442483
r.into_ok("Key-pairs successfully imported", response_data)
443484
}
444485

@@ -454,6 +495,7 @@ pub async fn post_make_payment(
454495
address,
455496
amount,
456497
passphrase,
498+
locktime,
457499
} = encapsulated_data;
458500

459501
let r = CallResponse::new(route, &call_id);
@@ -462,6 +504,7 @@ pub async fn post_make_payment(
462504
Ok(_) => UserRequest::UserApi(UserApiRequest::MakePayment {
463505
address: address.clone(),
464506
amount,
507+
locktime,
465508
}),
466509
Err(e) => {
467510
return wallet_db_error(e, r);
@@ -491,6 +534,7 @@ pub async fn post_make_ip_payment(
491534
address,
492535
amount,
493536
passphrase,
537+
locktime,
494538
} = encapsulated_data;
495539

496540
let r = CallResponse::new(route, &call_id);
@@ -506,6 +550,7 @@ pub async fn post_make_ip_payment(
506550
Ok(_) => UserRequest::UserApi(UserApiRequest::MakeIpPayment {
507551
payment_peer,
508552
amount,
553+
locktime,
509554
}),
510555
Err(e) => {
511556
return wallet_db_error(e, r);

src/api/routes.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub fn transactions_by_key(
349349
pub fn import_keypairs(
350350
dp: &mut DbgPaths,
351351
db: WalletDb,
352+
node: Node,
352353
routes_pow: RoutesPoWInfo,
353354
api_keys: ApiKeys,
354355
cache: ReplyCache,
@@ -358,13 +359,14 @@ pub fn import_keypairs(
358359
.and(warp::post())
359360
.and(auth_request(routes_pow, api_keys))
360361
.and(with_node_component(db))
362+
.and(with_node_component(node))
361363
.and(warp::body::json())
362364
.and(with_node_component(cache))
363-
.and_then(move |call_id: String, db, kp, cache| {
365+
.and_then(move |call_id: String, db, node, kp, cache| {
364366
map_api_res_and_cache(
365367
call_id.clone(),
366368
cache,
367-
handlers::post_import_keypairs(db, kp, route, call_id),
369+
handlers::post_import_keypairs(node, db, kp, route, call_id),
368370
)
369371
})
370372
.with(post_cors())
@@ -800,6 +802,7 @@ pub fn user_node_routes(
800802
.or(import_keypairs(
801803
dp,
802804
db.clone(),
805+
node.clone(),
803806
routes_pow_info.clone(),
804807
api_keys.clone(),
805808
cache.clone(),
@@ -1030,6 +1033,7 @@ pub fn miner_node_routes(
10301033
.or(import_keypairs(
10311034
dp,
10321035
db.clone(),
1036+
node.clone(),
10331037
routes_pow_info.clone(),
10341038
api_keys.clone(),
10351039
cache.clone(),
@@ -1126,6 +1130,7 @@ pub fn miner_node_with_user_routes(
11261130
.or(import_keypairs(
11271131
dp,
11281132
db.clone(),
1133+
user_node.clone(),
11291134
routes_pow_info.clone(),
11301135
api_keys.clone(),
11311136
cache.clone(),

0 commit comments

Comments
 (0)