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

fix(katana-rpc): include events from pending block in getEvents #2375

Merged
merged 12 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ flate2 = { workspace = true, optional = true }
katana-cairo.workspace = true

[dev-dependencies]
assert_matches.workspace = true
num-traits.workspace = true
similar-asserts.workspace = true

Expand Down
27 changes: 19 additions & 8 deletions crates/katana/primitives/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ pub struct OrderedEvent {
pub data: Vec<FieldElement>,
}

/// Represents a continuation token for implementing paging in event queries.
///
/// This struct stores the necessary information to resume fetching events
/// from a specific point relative to the given filter passed as parameter to the
/// `starknet_getEvents` API, [EventFilter][starknet::core::types::EventFilter].
///
/// There JSON-RPC specification does not specify the format of the continuation token,
/// so how the node should handle it is implementation specific.
#[derive(PartialEq, Eq, Debug, Default)]
pub struct ContinuationToken {
/// The block number to continue from.
pub block_n: u64,
/// The transaction number within the block to continue from.
pub txn_n: u64,
/// The event number within the transaction to continue from.
pub event_n: u64,
}

Expand All @@ -27,7 +38,7 @@ pub enum ContinuationTokenError {
}

impl ContinuationToken {
pub fn parse(token: String) -> Result<Self, ContinuationTokenError> {
pub fn parse(token: &str) -> Result<Self, ContinuationTokenError> {
let arr: Vec<&str> = token.split(',').collect();
if arr.len() != 3 {
return Err(ContinuationTokenError::InvalidToken);
Expand Down Expand Up @@ -66,7 +77,7 @@ mod test {
#[test]
fn parse_works() {
fn helper(token: &str) -> ContinuationToken {
ContinuationToken::parse(token.to_owned()).unwrap()
ContinuationToken::parse(&token).unwrap()
}
assert_eq!(helper("0,0,0"), ContinuationToken { block_n: 0, txn_n: 0, event_n: 0 });
assert_eq!(helper("1e,ff,4"), ContinuationToken { block_n: 30, txn_n: 255, event_n: 4 });
Expand All @@ -75,31 +86,31 @@ mod test {
#[test]
fn parse_should_fail() {
assert_eq!(
ContinuationToken::parse("100".to_owned()).unwrap_err(),
ContinuationToken::parse("100").unwrap_err(),
ContinuationTokenError::InvalidToken
);
assert_eq!(
ContinuationToken::parse("0,".to_owned()).unwrap_err(),
ContinuationToken::parse("0,").unwrap_err(),
ContinuationTokenError::InvalidToken
);
assert_eq!(
ContinuationToken::parse("0,0".to_owned()).unwrap_err(),
ContinuationToken::parse("0,0").unwrap_err(),
ContinuationTokenError::InvalidToken
);
}

#[test]
fn parse_u64_should_fail() {
matches!(
ContinuationToken::parse("2y,100,4".to_owned()).unwrap_err(),
ContinuationToken::parse("2y,100,4").unwrap_err(),
ContinuationTokenError::ParseFailed(_)
);
matches!(
ContinuationToken::parse("30,255g,4".to_owned()).unwrap_err(),
ContinuationToken::parse("30,255g,4").unwrap_err(),
ContinuationTokenError::ParseFailed(_)
);
matches!(
ContinuationToken::parse("244,1,fv".to_owned()).unwrap_err(),
ContinuationToken::parse("244,1,fv").unwrap_err(),
ContinuationTokenError::ParseFailed(_)
);
}
Expand Down
9 changes: 2 additions & 7 deletions crates/katana/rpc/rpc-types/src/error/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,8 @@ impl From<ProviderError> for StarknetApiError {
}

impl From<ContinuationTokenError> for StarknetApiError {
fn from(value: ContinuationTokenError) -> Self {
match value {
ContinuationTokenError::InvalidToken => StarknetApiError::InvalidContinuationToken,
ContinuationTokenError::ParseFailed(e) => {
StarknetApiError::UnexpectedError { reason: e.to_string() }
}
}
fn from(_: ContinuationTokenError) -> Self {
StarknetApiError::InvalidContinuationToken
kariy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading
Loading