Skip to content
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

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions crates/chain-connector/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,
Copy link
Member

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

Copy link
Member Author

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

host_id: PeerId,
}

Expand All @@ -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,
});

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}");
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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(
Expand Down
Loading