Browse Source

Use AtomicU64 instead of `Mutex`

develop
Silvestr Predko 3 years ago
parent
commit
afc6470efb
1 changed files with 10 additions and 9 deletions
  1. 10
    9
      near-client/src/client.rs

+ 10
- 9
near-client/src/client.rs View File

23
 use serde_json::{json, Value};
23
 use serde_json::{json, Value};
24
 use url::Url;
24
 use url::Url;
25
 
25
 
26
-use std::sync::{Arc, Mutex};
27
-
28
-type AtomicNonce = Arc<Mutex<Nonce>>;
29
-
26
+use std::sync::{
27
+    atomic::{AtomicU64, Ordering},
28
+    Arc,
29
+};
30
 
30
 
31
+type AtomicNonce = Arc<AtomicU64>;
31
 /// Used for signing transactions
32
 /// Used for signing transactions
32
 pub struct Signer {
33
 pub struct Signer {
33
     keypair: Keypair,
34
     keypair: Keypair,
40
         Ok(Self {
41
         Ok(Self {
41
             keypair: Keypair::from_expanded_secret(secret_key).map_err(Error::CreateSigner)?,
42
             keypair: Keypair::from_expanded_secret(secret_key).map_err(Error::CreateSigner)?,
42
             account_id,
43
             account_id,
43
-            nonce: AtomicNonce::new(Mutex::new(nonce)),
44
+            nonce: AtomicNonce::new(AtomicU64::new(nonce)),
44
         })
45
         })
45
     }
46
     }
46
 
47
 
48
         Self {
49
         Self {
49
             keypair: Keypair::new(secret_key),
50
             keypair: Keypair::new(secret_key),
50
             account_id,
51
             account_id,
51
-            nonce: AtomicNonce::new(Mutex::new(nonce)),
52
+            nonce: AtomicNonce::new(AtomicU64::new(nonce)),
52
         }
53
         }
53
     }
54
     }
54
 
55
 
74
     }
75
     }
75
 
76
 
76
     pub fn nonce(&self) -> Nonce {
77
     pub fn nonce(&self) -> Nonce {
77
-        *self.nonce.lock().unwrap()
78
+        self.nonce.load(Ordering::Acquire)
78
     }
79
     }
79
 
80
 
80
     pub fn update_nonce(&self, nonce: Nonce) {
81
     pub fn update_nonce(&self, nonce: Nonce) {
81
-        *self.nonce.lock().unwrap() = nonce;
82
+        self.nonce.store(nonce, Ordering::Release);
82
     }
83
     }
83
 
84
 
84
     pub fn increment_nonce(&self, value: u64) {
85
     pub fn increment_nonce(&self, value: u64) {
85
-        *self.nonce.lock().unwrap() += value;
86
+        self.nonce.fetch_add(value, Ordering::AcqRel);
86
     }
87
     }
87
 }
88
 }
88
 
89
 

Loading…
Cancel
Save