diff --git a/client/src/lib.rs b/client/src/lib.rs index b81164cca8..b92bae2a50 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -63,22 +63,21 @@ impl SequencerClient { block: Option, ) -> anyhow::Result { // Get the block height to query at, defaulting to the latest block. - let block = if let Some(block) = block { - block - 1 - } else { - self.0 - .get::("node/block-height") - .send() - .await - .context("getting block height")? - - 1 + let mut block = match block { + Some(block) => block, + None => self.get_height().await?, }; - + // As of block zero the state is empty, and the balance will be zero. + if block == 0 { + return Ok(0.into()); + } + // Block is non-zero, we can safely decrement to query the state as of the previous block. + block -= 1; // Download the Merkle path for this fee account at the specified block height. Transient errors // are possible (for example, if we are fetching from the latest block, the block height might - // get incremented slightly before the state becomes available) so retry a few times. + // get incremented shortly before the state becomes available) so retry a few times. let mut retry = 0; - let max_retries = 5; + let max_retries = 10; let proof = loop { tracing::debug!(%address, block, retry, "fetching Espresso balance"); match self @@ -107,3 +106,21 @@ impl SequencerClient { Ok(balance) } } + +#[cfg(test)] +mod tests { + use super::*; + // Regression test for a bug where the block number underflowed. This test would panic + // on the previous implementation, as long as overflow checks are enabled. + #[async_std::test] + async fn test_regression_block_number_underflow() { + let client = SequencerClient::new("http://dummy-url:3030".parse().unwrap()); + assert_eq!( + client + .get_espresso_balance(Address::zero(), Some(0)) + .await + .unwrap(), + 0.into() + ) + } +} diff --git a/docker-compose.yaml b/docker-compose.yaml index fb61411338..c12d4c5c3e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -63,6 +63,8 @@ services: - ACCOUNT_INDEX=$ESPRESSO_BUILDER_ETH_ACCOUNT_INDEX - AMOUNT=1000000000000000000 - CONFIRMATIONS=1 + - RUST_LOG + - RUST_LOG_FORMAT depends_on: deploy-sequencer-contracts: condition: service_completed_successfully diff --git a/sequencer/src/bin/espresso-bridge.rs b/sequencer/src/bin/espresso-bridge.rs index 6e3421b50f..92292ff93f 100644 --- a/sequencer/src/bin/espresso-bridge.rs +++ b/sequencer/src/bin/espresso-bridge.rs @@ -159,7 +159,7 @@ async fn deposit(opt: Deposit) -> anyhow::Result<()> { let initial_balance = espresso .get_espresso_balance(l1.address(), None) .await - .context("getting Espresso block height")?; + .context("getting Espresso balance")?; tracing::debug!(%initial_balance, "initial balance"); // Send the deposit transaction.