-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: track nonce #2306
feat: track nonce #2306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,7 @@ pub trait ChainConnector: Send + Sync { | |
pub struct HttpChainConnector { | ||
client: Arc<jsonrpsee::http_client::HttpClient>, | ||
config: ChainConfig, | ||
tx_nonce_mutex: Arc<Mutex<()>>, | ||
tx_nonce_mutex: Arc<Mutex<Option<U256>>>, | ||
host_id: PeerId, | ||
} | ||
|
||
|
@@ -106,7 +106,7 @@ impl HttpChainConnector { | |
let connector = Arc::new(Self { | ||
client: Arc::new(HttpClientBuilder::default().build(&config.http_endpoint)?), | ||
config, | ||
tx_nonce_mutex: Arc::new(Default::default()), | ||
tx_nonce_mutex: Arc::new(Mutex::new(None)), | ||
host_id, | ||
}); | ||
|
||
|
@@ -458,8 +458,11 @@ impl HttpChainConnector { | |
let max_fee_per_gas = base_fee + max_priority_fee_per_gas; | ||
|
||
// We use this lock no ensure that we don't send two transactions with the same nonce | ||
let _lock = self.tx_nonce_mutex.lock().await; | ||
let nonce = self.get_tx_nonce().await?; | ||
let mut nonce_guard = self.tx_nonce_mutex.lock().await; | ||
let nonce = match *nonce_guard { | ||
None => self.get_tx_nonce().await?, | ||
Some(n) => n, | ||
}; | ||
|
||
// Create a new transaction | ||
let tx = Transaction::Eip1559 { | ||
|
@@ -487,12 +490,25 @@ impl HttpChainConnector { | |
self.config.wallet_key.to_address() | ||
); | ||
|
||
let resp: String = process_response( | ||
let result: Result<String> = process_response( | ||
self.client | ||
.request("eth_sendRawTransaction", rpc_params![format!("0x{}", tx)]) | ||
.await, | ||
)?; | ||
Ok(resp) | ||
); | ||
|
||
match result { | ||
Ok(resp) => { | ||
let new_nonce = nonce + Uint::from(1); | ||
*nonce_guard = Some(new_nonce); | ||
tracing::debug!(target: "chain-connector", "Incrementing nonce. New nonce {new_nonce}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe move to info for now? or better ask providers to enable debug for chain connector? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think better to ask for debug for now, I don't like to make it info temporary |
||
Ok(resp) | ||
} | ||
Err(err) => { | ||
tracing::warn!(target: "chain-connector", "Failed to send tx: {err}, resetting nonce"); | ||
*nonce_guard = None; | ||
Err(err) | ||
} | ||
} | ||
} | ||
|
||
pub async fn register_worker( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you could be fine with atomic u64, no mutex and probably no error/result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I need mutex to send only one tx at a time. In theory decider can send txs at the same time