Skip to content

Commit 5525745

Browse files
committed
chore: refactoring and optimizations v.1
1 parent d5e1ded commit 5525745

26 files changed

Lines changed: 283 additions & 223 deletions

File tree

src/api/controllers/authorization.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use aide::{OperationInput, OperationOutput};
44
use axum::body::Body;
5-
use axum::extract::{FromRequest, FromRequestParts, OriginalUri};
5+
use axum::extract::{FromRequestParts, OriginalUri};
66
use axum::http::request::Parts;
77
use axum::http::Request;
88
use axum::http::{Method, StatusCode};
@@ -95,9 +95,7 @@ async fn check_api_key(
9595
// Forward service id to request handler
9696
req.extensions_mut().insert(IdExtractor(service_id));
9797

98-
Ok(Request::from_request(req, &auth_service)
99-
.await
100-
.expect("can't fail"))
98+
Ok(req)
10199
}
102100

103101
#[derive(Debug, Clone)]

src/api/controllers/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub async fn post_prepare_generic_message(
114114
)
115115
.await?;
116116

117-
let message_hash = ctx.memory_storage.add_message(unsigned_message);
117+
let message_hash = ctx.memory_storage.add_message(unsigned_message)?;
118118

119119
let elapsed = start.elapsed();
120120
histogram!("execution_time_seconds", "method" => "prepareGenericMessage").record(elapsed);

src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Api {
8282
))
8383
.fallback(controllers::handler_404);
8484

85-
let listener = tokio::net::TcpListener::bind(server_addr).await.unwrap();
85+
let listener = tokio::net::TcpListener::bind(server_addr).await?;
8686

8787
let serve = axum::serve(listener, app);
8888

src/api/requests/transactions.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use bigdecimal::BigDecimal;
22
use derive_more::Constructor;
3-
use num_traits::FromPrimitive;
43

54
use schemars::JsonSchema;
65
use serde::Deserialize;
@@ -125,9 +124,7 @@ impl From<TonTokenTransactionSendRequest> for TokenTransactionSend {
125124
send_gas_to: c.send_gas_to,
126125
value: c.value,
127126
notify_receiver: c.notify_receiver.unwrap_or(false),
128-
fee: c
129-
.fee
130-
.unwrap_or_else(|| BigDecimal::from_u64(TOKEN_FEE).unwrap()),
127+
fee: c.fee.unwrap_or_else(|| BigDecimal::from(TOKEN_FEE)),
131128
payload: c.payload,
132129
}
133130
}
@@ -154,9 +151,7 @@ impl From<TonTokenTransactionBurnRequest> for TokenTransactionBurn {
154151
send_gas_to: c.send_gas_to,
155152
callback_to: c.callback_to,
156153
value: c.value,
157-
fee: c
158-
.fee
159-
.unwrap_or_else(|| BigDecimal::from_u64(TOKEN_FEE).unwrap()),
154+
fee: c.fee.unwrap_or_else(|| BigDecimal::from(TOKEN_FEE)),
160155
}
161156
}
162157
}
@@ -187,10 +182,8 @@ impl From<TonTokenTransactionMintRequest> for TokenTransactionMint {
187182
notify: c.notify.unwrap_or(false),
188183
deploy_wallet_value: c
189184
.deploy_wallet_value
190-
.unwrap_or_else(|| BigDecimal::from_u64(DEPLOY_TOKEN_VALUE).unwrap()),
191-
fee: c
192-
.fee
193-
.unwrap_or_else(|| BigDecimal::from_u64(TOKEN_FEE).unwrap()),
185+
.unwrap_or_else(|| BigDecimal::from(DEPLOY_TOKEN_VALUE)),
186+
fee: c.fee.unwrap_or_else(|| BigDecimal::from(TOKEN_FEE)),
194187
}
195188
}
196189
}

src/api/responses/address.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use std::str::FromStr;
2-
31
use bigdecimal::BigDecimal;
42
use derive_more::Constructor;
5-
use tycho_types::models::StdAddr;
63

74
use schemars::JsonSchema;
85
use serde::Serialize;
@@ -107,15 +104,12 @@ pub struct AddressBalanceDataResponse {
107104

108105
impl AddressBalanceDataResponse {
109106
pub fn new(a: AddressDb, b: NetworkAddressData) -> Self {
110-
let account = StdAddr::from_str(&format!("{}:{}", a.workchain_id, a.hex)).unwrap();
111-
let base64url = Address(account.display_base64_url(true).to_string());
112-
113107
Self {
114108
id: a.id,
115109
address: Account {
116110
workchain_id: a.workchain_id,
117111
hex: Address(a.hex),
118-
base64url,
112+
base64url: Address(a.base64url),
119113
},
120114
account_type: a.account_type,
121115
balance: a.balance,
@@ -171,15 +165,12 @@ pub struct AddressInfoDataResponse {
171165

172166
impl AddressInfoDataResponse {
173167
pub fn new(a: AddressDb) -> Self {
174-
let account = StdAddr::from_str(&format!("{}:{}", a.workchain_id, a.hex)).unwrap();
175-
let base64url = Address(account.display_base64_url(true).to_string());
176-
177168
Self {
178169
id: a.id,
179170
address: Account {
180171
workchain_id: a.workchain_id,
181172
hex: Address(a.hex),
182-
base64url,
173+
base64url: Address(a.base64url),
183174
},
184175
account_type: a.account_type,
185176
custodians: a.custodians,
@@ -234,17 +225,9 @@ pub struct TokenBalanceDataResponse {
234225

235226
impl TokenBalanceDataResponse {
236227
pub fn new(a: TokenBalanceFromDb, b: NetworkTokenAddressData) -> Self {
237-
let account =
238-
StdAddr::from_str(&format!("{}:{}", a.account_workchain_id, a.account_hex)).unwrap();
239-
let base64url = Address(account.display_base64_url(true).to_string());
240-
241228
Self {
242229
service_id: a.service_id,
243-
address: Account {
244-
workchain_id: a.account_workchain_id,
245-
hex: Address(a.account_hex),
246-
base64url,
247-
},
230+
address: Account::from_wc_and_address(a.account_workchain_id, a.account_hex),
248231
balance: a.balance,
249232
account_status: b.account_status,
250233
network_balance: b.network_balance,

src/api/responses/transactions.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ impl From<TransactionDb> for TransactionDataResponse {
7373
let sender = if let (Some(sender_hex), Some(sender_workchain_id)) =
7474
(c.sender_hex, c.sender_workchain_id)
7575
{
76-
let sender = StdAddr::from_str(&format!("{}:{}", sender_workchain_id, sender_hex))
77-
.unwrap_or_default();
78-
let sender_base64url = Address(sender.display_base64_url(true).to_string());
79-
Some(Account {
80-
workchain_id: sender_workchain_id,
81-
hex: Address(sender_hex),
82-
base64url: sender_base64url,
83-
})
76+
Some(Account::from_wc_and_address(
77+
sender_workchain_id,
78+
sender_hex,
79+
))
8480
} else {
8581
None
8682
};
@@ -112,21 +108,13 @@ impl From<TransactionDb> for TransactionDataResponse {
112108
None
113109
};
114110

115-
let account =
116-
StdAddr::from_str(&format!("{}:{}", c.account_workchain_id, c.account_hex)).unwrap();
117-
let base64url = Address(account.display_base64_url(true).to_string());
118-
119111
TransactionDataResponse {
120112
id: c.id,
121113
message_hash: c.message_hash,
122114
transaction_hash: c.transaction_hash,
123115
transaction_lt: c.transaction_lt.map(|v| v.to_string()),
124116
transaction_timeout: c.transaction_timeout,
125-
account: Account {
126-
workchain_id: c.account_workchain_id,
127-
hex: Address(c.account_hex),
128-
base64url,
129-
},
117+
account: Account::from_wc_and_address(c.account_workchain_id, c.account_hex),
130118
sender,
131119
value: c.value,
132120
original_value: c.original_value,
@@ -248,9 +236,6 @@ pub struct TokenTransactionDataResponse {
248236

249237
impl From<TokenTransactionFromDb> for TokenTransactionDataResponse {
250238
fn from(c: TokenTransactionFromDb) -> Self {
251-
let account =
252-
StdAddr::from_str(&format!("{}:{}", c.account_workchain_id, c.account_hex)).unwrap();
253-
let base64url = Address(account.display_base64_url(true).to_string());
254239
let payload = c
255240
.payload
256241
.map(|value| general_purpose::STANDARD.encode(value));
@@ -259,11 +244,7 @@ impl From<TokenTransactionFromDb> for TokenTransactionDataResponse {
259244
id: c.id,
260245
message_hash: c.message_hash,
261246
transaction_hash: c.transaction_hash,
262-
account: Account {
263-
workchain_id: c.account_workchain_id,
264-
hex: Address(c.account_hex),
265-
base64url,
266-
},
247+
account: Account::from_wc_and_address(c.account_workchain_id, c.account_hex),
267248
value: c.value,
268249
root_address: c.root_address,
269250
error: c.error,

src/client/callback/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct CallbackClient {
1313
impl CallbackClient {
1414
pub fn new() -> Self {
1515
Self {
16-
client: reqwest::ClientBuilder::new().build().unwrap(),
16+
client: reqwest::Client::new(),
1717
}
1818
}
1919
}
@@ -62,7 +62,7 @@ impl CallbackClient {
6262
}
6363

6464
fn calc_sign(body: String, url: String, timestamp_ms: i64, secret: String) -> String {
65-
let concat = format!("{}{}{}", timestamp_ms, url, body);
65+
let concat = format!("{timestamp_ms}{url}{body}");
6666
let calculated_signature = hmac_sha256::HMAC::mac(concat.as_bytes(), secret.as_bytes());
6767
general_purpose::STANDARD.encode(calculated_signature)
6868
}

src/client/ton/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ impl TonClient {
4949
.await?
5050
.into_iter()
5151
.map(|item| {
52-
StdAddr::from_str(&format!("{}:{}", item.workchain_id, item.hex))
53-
.map_err(From::from)
52+
let workchain_id = item.workchain_id;
53+
let hex = item.hex;
54+
StdAddr::from_str(&format!("{workchain_id}:{hex}")).map_err(From::from)
5455
})
5556
.collect::<anyhow::Result<Vec<StdAddr>>>()?;
5657

@@ -865,19 +866,15 @@ impl TonClient {
865866
pub async fn get_blockchain_info(&self) -> Result<BlockchainInfo, Error> {
866867
let gen_utime = self.ton_core.current_utime();
867868

868-
let network_id = match () {
869-
_ => TYCHO_TESTNET_CHAIN_ID,
870-
};
871-
872869
let subscriber_metrics = self.ton_core.context.ton_subscriber.metrics();
873870

874871
Ok(BlockchainInfo {
875-
network_id,
872+
network_id: subscriber_metrics.network_id,
876873
synced: subscriber_metrics.ready,
877874
subscriber_pending_messages: subscriber_metrics.pending_message_count,
878875
tip_block_ts: gen_utime,
879-
masterchain_height: 0,
880-
masterchain_last_updated: 0,
876+
masterchain_height: subscriber_metrics.mc_seqno,
877+
masterchain_last_updated: subscriber_metrics.masterchain_last_updated,
881878
})
882879
}
883880

@@ -1266,8 +1263,6 @@ fn build_token_transaction(
12661263
})
12671264
}
12681265

1269-
const TYCHO_TESTNET_CHAIN_ID: i32 = -4000;
1270-
12711266
#[derive(Debug)]
12721267
pub struct PrepareResult {
12731268
pub sent_transaction: SentTransaction,

src/cmd/run.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,14 @@ impl Cmd {
101101
cli::logger::init_logger(&node_config.logger_config, self.logger_config.clone())?;
102102
cli::logger::set_abort_with_tracing();
103103

104-
node_config.threads.init_reclaimer().unwrap();
105-
node_config.threads.init_global_rayon_pool().unwrap();
104+
node_config
105+
.threads
106+
.init_reclaimer()
107+
.context("failed to initialize memory reclaimer")?;
108+
node_config
109+
.threads
110+
.init_global_rayon_pool()
111+
.context("failed to initialize global rayon pool")?;
106112
node_config
107113
.threads
108114
.build_tokio_runtime()?
@@ -115,9 +121,10 @@ impl Cmd {
115121
}
116122

117123
// Build node.
118-
let keys = NodeKeys::load_or_create(self.keys.unwrap())?;
119-
let global_config = GlobalConfig::from_file(self.global_config.unwrap())
120-
.context("failed to load global config")?;
124+
let keys = NodeKeys::load_or_create(self.keys.context("no keys path")?)?;
125+
let global_config =
126+
GlobalConfig::from_file(self.global_config.context("no global config path")?)
127+
.context("failed to load global config")?;
121128
let public_ip = cli::resolve_public_ip(node_config.base.public_ip).await?;
122129
let public_addr = SocketAddr::new(public_ip, node_config.base.port);
123130

src/models/account_enums.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,36 @@ pub struct Account {
8585
pub base64url: Address,
8686
}
8787

88+
impl Account {
89+
pub fn from_wc_and_address(workchain_id: i32, address: String) -> Self {
90+
let account = format!("{workchain_id}:{address}");
91+
let base64url = match StdAddr::from_str(&account) {
92+
Ok(account) => Address(account.display_base64_url(true).to_string()),
93+
Err(error) => {
94+
tracing::error!(
95+
workchain_id,
96+
address,
97+
?error,
98+
"failed to format account as base64url"
99+
);
100+
Address(account)
101+
}
102+
};
103+
104+
Self {
105+
workchain_id,
106+
hex: Address(address),
107+
base64url,
108+
}
109+
}
110+
}
111+
88112
impl From<AddressDb> for Account {
89113
fn from(a: AddressDb) -> Self {
90-
let account = StdAddr::from_str(&format!("{}:{}", a.workchain_id, a.hex)).unwrap();
91-
let base64url = Address(account.display_base64_url(true).to_string());
92114
Self {
93115
workchain_id: a.workchain_id,
94116
hex: Address(a.hex),
95-
base64url,
117+
base64url: Address(a.base64url),
96118
}
97119
}
98120
}

0 commit comments

Comments
 (0)