@@ -7,7 +7,6 @@ use aurora_engine_types::{
77 U256 ,
88} ;
99use near_crypto:: InMemorySigner ;
10- #[ cfg( feature = "simple" ) ]
1110use near_crypto:: PublicKey ;
1211use near_jsonrpc_client:: methods;
1312#[ cfg( feature = "simple" ) ]
@@ -19,7 +18,7 @@ use near_jsonrpc_client::{
1918} ;
2019use near_jsonrpc_primitives:: types:: query:: QueryResponseKind ;
2120use near_primitives:: transaction:: { Action , SignedTransaction } ;
22- use near_primitives:: types:: { BlockReference , Finality } ;
21+ use near_primitives:: types:: { BlockReference , Finality , Nonce } ;
2322use near_primitives:: views:: BlockView ;
2423#[ cfg( feature = "simple" ) ]
2524use 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
6058impl 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,64 @@ 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 ) ;
499- drop ( nonces) ;
500-
501485 // Fetch latest block_hash since the previous one is now invalid for new transactions:
502486 let block = self . view_block ( Some ( Finality :: Final . into ( ) ) ) . await ?;
503487
504488 Ok ( ( block. header . hash , nonce + 1 ) )
505489 } else {
506- 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-
490+ let ( block_hash, nonce) = self . get_nonce_block_hash ( & cache_key) . await ?;
525491 // case where multiple writers end up at the same lock acquisition point and tries
526492 // to overwrite the cached value that a previous writer already wrote.
527493 let nonce = self
528494 . access_key_nonces
529495 . write ( )
530496 . await
531497 . 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 ) ;
498+ . or_insert_with ( || AtomicU64 :: new ( nonce + 1 ) )
499+ . fetch_max ( nonce + 1 , Ordering :: SeqCst )
500+ . max ( nonce + 1 ) ;
535501
536502 Ok ( ( block_hash, nonce) )
537503 }
538504 }
539505
506+ async fn view_block ( & self , block_ref : Option < BlockReference > ) -> anyhow:: Result < BlockView > {
507+ let block_reference = block_ref. unwrap_or_else ( || Finality :: None . into ( ) ) ;
508+ let block_view = self
509+ . client
510+ . call ( & methods:: block:: RpcBlockRequest { block_reference } )
511+ . await ?;
512+
513+ Ok ( block_view)
514+ }
515+
516+ async fn get_nonce_block_hash (
517+ & self ,
518+ cache_key : & ( AccountId , PublicKey ) ,
519+ ) -> anyhow:: Result < ( CryptoHash , Nonce ) > {
520+ let ( account_id, public_key) = cache_key. clone ( ) ;
521+ let request = near_jsonrpc_primitives:: types:: query:: RpcQueryRequest {
522+ block_reference : Finality :: Final . into ( ) ,
523+ request : views:: QueryRequest :: ViewAccessKey {
524+ account_id,
525+ public_key,
526+ } ,
527+ } ;
528+ let response = self . client . call ( request) . await ?;
529+ let block_hash = response. block_hash ;
530+ let QueryResponseKind :: AccessKey ( access_key) = response. kind else {
531+ anyhow:: bail!( "Wrong response kind: {:?}" , response. kind)
532+ } ;
533+
534+ Ok ( ( block_hash, access_key. nonce ) )
535+ }
536+
540537 fn signer ( & self ) -> anyhow:: Result < InMemorySigner > {
541538 std:: env:: var ( "NEAR_KEY_PATH" )
542539 . ok ( )
0 commit comments