Skip to content

Commit

Permalink
chore: add tests for ABCI error codes (#1754)
Browse files Browse the repository at this point in the history
## Summary
Added tests for the ABCI error codes.

## Background
The new snapshot test should serve to emphasize that the error codes are
part of the API and changes here would be breaking.

The other test should help avoid accidental duplication of a value.

## Changes
- Added a snapshot test.
- Added a test for unique values of error codes.

## Testing
These _are_ tests.

## Changelogs
No updates required - this is only adding unit tests.

## Related Issues
Closes #1634.

Co-authored-by: noot <[email protected]>
  • Loading branch information
Fraser999 and noot authored Dec 6, 2024
1 parent 3931de0 commit 3419c74
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 2 deletions.
48 changes: 46 additions & 2 deletions crates/astria-core/src/protocol/abci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ impl AbciErrorCode {
pub const VALUE_NOT_FOUND: Self = Self(unsafe { NonZeroU32::new_unchecked(8) });
pub const TRANSACTION_EXPIRED: Self = Self(unsafe { NonZeroU32::new_unchecked(9) });
pub const TRANSACTION_FAILED: Self = Self(unsafe { NonZeroU32::new_unchecked(10) });
pub const TRANSACTION_INSERTION_FAILED: Self = Self(unsafe { NonZeroU32::new_unchecked(11) });
pub const LOWER_NONCE_INVALIDATED: Self = Self(unsafe { NonZeroU32::new_unchecked(12) });
pub const TRANSACTION_INSERTION_FAILED: Self = Self(unsafe { NonZeroU32::new_unchecked(11) });
pub const LOWER_NONCE_INVALIDATED: Self = Self(unsafe { NonZeroU32::new_unchecked(12) });
pub const BAD_REQUEST: Self = Self(unsafe { NonZeroU32::new_unchecked(13) });
pub const ALREADY_PRESENT: Self = Self(unsafe { NonZeroU32::new_unchecked(14) });
pub const NONCE_TAKEN: Self = Self(unsafe { NonZeroU32::new_unchecked(15) });
pub const ACCOUNT_SIZE_LIMIT: Self = Self(unsafe { NonZeroU32::new_unchecked(16) });
pub const PARKED_FULL: Self = Self(unsafe { NonZeroU32::new_unchecked(17) });
// NOTE: When adding a new code, ensure it is added to `ALL_CODES` in the `tests` module below.
}

impl AbciErrorCode {
Expand Down Expand Up @@ -76,3 +77,46 @@ impl std::fmt::Display for AbciErrorCode {
write!(f, "{}: {}", self.0, self.info())
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;

use super::*;

const ALL_CODES: [AbciErrorCode; 17] = [
AbciErrorCode::UNKNOWN_PATH,
AbciErrorCode::INVALID_PARAMETER,
AbciErrorCode::INTERNAL_ERROR,
AbciErrorCode::INVALID_NONCE,
AbciErrorCode::TRANSACTION_TOO_LARGE,
AbciErrorCode::INSUFFICIENT_FUNDS,
AbciErrorCode::INVALID_CHAIN_ID,
AbciErrorCode::VALUE_NOT_FOUND,
AbciErrorCode::TRANSACTION_EXPIRED,
AbciErrorCode::TRANSACTION_FAILED,
AbciErrorCode::TRANSACTION_INSERTION_FAILED,
AbciErrorCode::LOWER_NONCE_INVALIDATED,
AbciErrorCode::BAD_REQUEST,
AbciErrorCode::ALREADY_PRESENT,
AbciErrorCode::NONCE_TAKEN,
AbciErrorCode::ACCOUNT_SIZE_LIMIT,
AbciErrorCode::PARKED_FULL,
];

#[test]
fn error_code_snapshots() {
for error_code in ALL_CODES {
let name = format!("AbciErrorCode::{}", error_code.value());
insta::assert_snapshot!(name, error_code);
}
}

#[test]
fn ensure_codes_are_unique() {
let mut values = BTreeSet::new();
for code in ALL_CODES {
assert!(values.insert(code.value()), "duplicate value for {code:?}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
1: provided path is unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
10: the transaction failed to execute in prepare_proposal()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
11: the transaction failed insertion into the mempool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
12: lower nonce was invalidated in mempool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
13: the request payload was malformed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
14: the transaction is already present in the mempool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
15: there is already a transaction with the same nonce for the account in the mempool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
16: the account has reached the maximum number of parked transactions
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
17: the mempool is out of space for more parked transactions
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
2: one or more path parameters were invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
3: an internal server error occurred
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
4: the provided nonce was invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
5: the provided transaction was too large
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
6: insufficient funds
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
7: the provided chain id was invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
8: the requested value was not found
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/astria-core/src/protocol/abci.rs
expression: error_code
---
9: the transaction expired in the app's mempool

0 comments on commit 3419c74

Please sign in to comment.