Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion crates/rpc/src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ fn database_error_to_status(err: &DatabaseError) -> Status {
DatabaseError::AccountNotFoundInDb(_)
| DatabaseError::AccountsNotFoundInDb(_)
| DatabaseError::AccountNotPublic(_) => Status::not_found(message),
DatabaseError::TransactionPageExceedsPayloadLimit { .. } => Status::out_of_range(message),
DatabaseError::TransactionPageExceedsPayloadLimit { .. }
| DatabaseError::NullifierPageExceedsPayloadLimit { .. } => Status::out_of_range(message),
DatabaseError::RangeBeyondTip(_) => Status::invalid_argument(message),
_ => Status::internal(message),
}
Expand Down
12 changes: 9 additions & 3 deletions crates/store/src/db/models/queries/nullifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ pub(crate) fn select_nullifiers_by_prefix(
{
let last_block_num_i64 = last.block_num;

let nullifiers = vec_raw_try_into(
raw.into_iter().take_while(|row| row.block_num != last_block_num_i64),
)?;
let complete_len = raw.partition_point(|row| row.block_num < last_block_num_i64);

if complete_len == 0 {
return Err(DatabaseError::NullifierPageExceedsPayloadLimit {
block_num: BlockNumber::from_raw_sql(last_block_num_i64)?,
});
}

let nullifiers = vec_raw_try_into(raw.into_iter().take(complete_len))?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mirko-von-Leipzig these diffs seem sound (will take a closer look if we decide to go down this path).

But we can also alternatively just add a const check instead i.e.

    // Pagination reports the last fully-included block, so it only makes progress if every block
    // fits within a single page. A block that exceeded `MAX_ROWS` nullifiers would produce an
    // empty page and stall clients forever on that block.
    const _: () = assert!(
        miden_protocol::MAX_INPUT_NOTES_PER_BLOCK <= MAX_ROWS,
        "a block's nullifiers must fit in one response page or pagination cannot make progress"
    );

WDYT?


let last_block_included = BlockNumber::from_raw_sql(last_block_num_i64.saturating_sub(1))?;

Expand Down
37 changes: 37 additions & 0 deletions crates/store/src/db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,43 @@ fn select_nullifiers_by_prefix_works() {
assert_eq!(block_number_reached, block_number2);
}

#[test]
#[miden_node_test_macro::enable_logging]
fn select_nullifiers_by_prefix_errors_when_single_block_exceeds_page_limit() {
const PREFIX_LEN: u8 = 16;
const NULLIFIER_BYTES: usize = 32;
const BLOCK_NUM_BYTES: usize = 4;
const MAX_ROWS: usize =
miden_node_utils::limiter::MAX_RESPONSE_PAYLOAD_BYTES / (NULLIFIER_BYTES + BLOCK_NUM_BYTES);

let mut conn = create_db();
let block_number = BlockNumber::from(1);
create_block(&mut conn, block_number);

let prefix = utils::get_nullifier_prefix(&num_to_nullifier(1));
let nullifiers = (1..=MAX_ROWS + 1)
.map(|n| num_to_nullifier(u64::try_from(n).expect("test index fits in u64")))
.inspect(|nullifier| assert_eq!(utils::get_nullifier_prefix(nullifier), prefix))
.collect::<Vec<_>>();

for chunk in nullifiers.chunks(500) {
queries::insert_nullifiers_for_block(&mut conn, chunk, block_number).unwrap();
}

let result = queries::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[prefix],
BlockNumber::GENESIS..=block_number,
);

assert_matches!(
result,
Err(DatabaseError::NullifierPageExceedsPayloadLimit { block_num })
if block_num == block_number
);
}

#[test]
#[miden_node_test_macro::enable_logging]
fn db_block_header() {
Expand Down
5 changes: 5 additions & 0 deletions crates/store/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ pub enum DatabaseError {
use a stricter filter to reduce the number of transactions returned"
)]
TransactionPageExceedsPayloadLimit { block_num: BlockNumber },
#[error(
"nullifiers for block {block_num} would exceed maximum response size, \
use a stricter filter to reduce the number of nullifiers returned"
)]
NullifierPageExceedsPayloadLimit { block_num: BlockNumber },
#[error("data corrupted: {0}")]
DataCorrupted(String),
#[error(transparent)]
Expand Down