Skip to content

Commit 563d149

Browse files
committed
fix: tiny refactoring
1 parent a5f649c commit 563d149

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

src/client/near.rs

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use aurora_engine_types::{
77
U256,
88
};
99
use near_crypto::InMemorySigner;
10-
#[cfg(feature = "simple")]
1110
use near_crypto::PublicKey;
1211
use near_jsonrpc_client::methods;
1312
#[cfg(feature = "simple")]
@@ -19,7 +18,7 @@ use near_jsonrpc_client::{
1918
};
2019
use near_jsonrpc_primitives::types::query::QueryResponseKind;
2120
use near_primitives::transaction::{Action, SignedTransaction};
22-
use near_primitives::types::{BlockReference, Finality};
21+
use near_primitives::types::{BlockReference, Finality, Nonce};
2322
use near_primitives::views::BlockView;
2423
#[cfg(feature = "simple")]
2524
use near_primitives::views::FinalExecutionStatus;
@@ -53,8 +52,7 @@ pub struct NearClient {
5352
client: JsonRpcClient,
5453
pub engine_account_id: AccountId,
5554
signer_key_path: Option<String>,
56-
57-
access_key_nonces: Arc<RwLock<HashMap<(AccountId, near_crypto::PublicKey), AtomicU64>>>,
55+
access_key_nonces: Arc<RwLock<HashMap<(AccountId, PublicKey), AtomicU64>>>,
5856
}
5957

6058
impl NearClient {
@@ -89,14 +87,17 @@ impl NearClient {
8987
let receiver_id = &self.engine_account_id;
9088
loop {
9189
let block_hash = {
92-
let request = near_jsonrpc_client::methods::block::RpcBlockRequest {
93-
block_reference: near_primitives::types::Finality::Final.into(),
90+
let request = methods::block::RpcBlockRequest {
91+
block_reference: Finality::Final.into(),
9492
};
9593
let response = self.client.call(request).await?;
9694
response.header.hash
9795
};
98-
let request = near_jsonrpc_client::methods::light_client_proof::RpcLightClientExecutionProofRequest {
99-
id: near_primitives::types::TransactionOrReceiptId::Receipt { receipt_id, receiver_id: receiver_id.clone() },
96+
let request = methods::light_client_proof::RpcLightClientExecutionProofRequest {
97+
id: near_primitives::types::TransactionOrReceiptId::Receipt {
98+
receipt_id,
99+
receiver_id: receiver_id.clone(),
100+
},
100101
light_client_head: block_hash,
101102
};
102103
let response = self.client.call(request).await?;
@@ -169,8 +170,8 @@ impl NearClient {
169170
method_name: &str,
170171
args: Vec<u8>,
171172
) -> anyhow::Result<views::CallResult> {
172-
let request = near_jsonrpc_client::methods::query::RpcQueryRequest {
173-
block_reference: near_primitives::types::Finality::Final.into(),
173+
let request = methods::query::RpcQueryRequest {
174+
block_reference: Finality::Final.into(),
174175
request: views::QueryRequest::CallFunction {
175176
account_id: self.engine_account_id.clone(),
176177
method_name: method_name.to_string(),
@@ -188,10 +189,8 @@ impl NearClient {
188189
#[cfg(feature = "simple")]
189190
pub async fn view_account(&self, account: &str) -> anyhow::Result<String> {
190191
let account_id: AccountId = account.parse()?;
191-
let request = near_jsonrpc_client::methods::query::RpcQueryRequest {
192-
block_reference: near_primitives::types::BlockReference::Finality(
193-
near_primitives::types::Finality::Final,
194-
),
192+
let request = methods::query::RpcQueryRequest {
193+
block_reference: BlockReference::Finality(Finality::Final),
195194
request: views::QueryRequest::ViewAccount { account_id },
196195
};
197196

@@ -477,66 +476,66 @@ impl NearClient {
477476
Ok(result)
478477
}
479478

480-
pub(crate) async fn view_block(
481-
&self,
482-
block_ref: Option<BlockReference>,
483-
) -> anyhow::Result<BlockView> {
484-
let block_reference = block_ref.unwrap_or_else(|| Finality::None.into());
485-
let block_view = self
486-
.client
487-
.call(&methods::block::RpcBlockRequest { block_reference })
488-
.await?;
489-
490-
Ok(block_view)
491-
}
492-
493479
pub async fn get_nonce(&self, signer: &InMemorySigner) -> anyhow::Result<(CryptoHash, u64)> {
494480
let nonces = self.access_key_nonces.read().await;
495481
let cache_key = (signer.account_id.clone(), signer.secret_key.public_key());
496482

497483
if let Some(nonce) = nonces.get(&cache_key) {
498484
let nonce = nonce.fetch_add(1, Ordering::SeqCst);
499485
drop(nonces);
500-
501486
// Fetch latest block_hash since the previous one is now invalid for new transactions:
502487
let block = self.view_block(Some(Finality::Final.into())).await?;
503488

504489
Ok((block.header.hash, nonce + 1))
505490
} else {
506491
drop(nonces);
507-
508-
let (account_id, public_key) = cache_key.clone();
509-
510-
let request = near_jsonrpc_primitives::types::query::RpcQueryRequest {
511-
block_reference: near_primitives::types::Finality::Final.into(),
512-
request: views::QueryRequest::ViewAccessKey {
513-
account_id,
514-
public_key,
515-
},
516-
};
517-
let response = self.client.call(request).await?;
518-
519-
let block_hash = response.block_hash;
520-
521-
let QueryResponseKind::AccessKey(access_key) = response.kind else {
522-
anyhow::bail!("Wrong response kind: {:?}", response.kind)
523-
};
524-
492+
let (block_hash, nonce) = self.get_nonce_block_hash(&cache_key).await?;
525493
// case where multiple writers end up at the same lock acquisition point and tries
526494
// to overwrite the cached value that a previous writer already wrote.
527495
let nonce = self
528496
.access_key_nonces
529497
.write()
530498
.await
531499
.entry(cache_key)
532-
.or_insert_with(|| AtomicU64::new(access_key.nonce + 1))
533-
.fetch_max(access_key.nonce + 1, Ordering::SeqCst)
534-
.max(access_key.nonce + 1);
500+
.or_insert_with(|| AtomicU64::new(nonce + 1))
501+
.fetch_max(nonce + 1, Ordering::SeqCst)
502+
.max(nonce + 1);
535503

536504
Ok((block_hash, nonce))
537505
}
538506
}
539507

508+
async fn view_block(&self, block_ref: Option<BlockReference>) -> anyhow::Result<BlockView> {
509+
let block_reference = block_ref.unwrap_or_else(|| Finality::None.into());
510+
let block_view = self
511+
.client
512+
.call(&methods::block::RpcBlockRequest { block_reference })
513+
.await?;
514+
515+
Ok(block_view)
516+
}
517+
518+
async fn get_nonce_block_hash(
519+
&self,
520+
cache_key: &(AccountId, PublicKey),
521+
) -> anyhow::Result<(CryptoHash, Nonce)> {
522+
let (account_id, public_key) = cache_key.clone();
523+
let request = near_jsonrpc_primitives::types::query::RpcQueryRequest {
524+
block_reference: Finality::Final.into(),
525+
request: views::QueryRequest::ViewAccessKey {
526+
account_id,
527+
public_key,
528+
},
529+
};
530+
let response = self.client.call(request).await?;
531+
let block_hash = response.block_hash;
532+
let QueryResponseKind::AccessKey(access_key) = response.kind else {
533+
anyhow::bail!("Wrong response kind: {:?}", response.kind)
534+
};
535+
536+
Ok((block_hash, access_key.nonce))
537+
}
538+
540539
fn signer(&self) -> anyhow::Result<InMemorySigner> {
541540
std::env::var("NEAR_KEY_PATH")
542541
.ok()

0 commit comments

Comments
 (0)