Skip to content

Commit

Permalink
events: Remove unsupported filters
Browse files Browse the repository at this point in the history
## Description

This PR removes EventFilters from JSON-RPC that aren't supported
already by fullnode-backed JSON-RPC:

- Combination filters (`All`, `Any`, `Not`, `And`, `Or`)
- `Package`
- `EventField`

These filters were, however, supported by the now-deprecated
subscription system, so a question remains whether they are safe to
remove.

## Test plan

Manually tested, in particular `All: []` support, which we do still need
to keep as a way to get all filters (this was also added to the
`IndexerReader` implementation):

```
sui$ cargo run --bin sui -- --force-regenesis \
  --with-indexer --with-faucet
```

...run for some time, so that some system events accumulate, then test
that we get them from both the fullnode-backed JSONRPC and
indexer-backed:

Fullnode:
```
curl -LX POST  "http://localhost:9000" \
        --header 'Content-Type: application/json' \
        --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "suix_queryEvents",
  "params": [
    {
      "All": []
    },
    null,
    50,
    true
  ]
}' | jq .
```

Indexer:
```
curl -LX POST  "http://localhost:9124" \
        --header 'Content-Type: application/json' \
        --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "suix_queryEvents",
  "params": [
    {
      "All": []
    },
    null,
    50,
    true
  ]
}' | jq .
```
  • Loading branch information
amnn committed Sep 30, 2024
1 parent d3fff82 commit 0a4c5b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 67 deletions.
20 changes: 3 additions & 17 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3923,17 +3923,8 @@ impl AuthorityState {

let limit = limit + 1;
let mut event_keys = match query {
EventFilter::All(filters) => {
if filters.is_empty() {
index_store.all_events(tx_num, event_num, limit, descending)?
} else {
return Err(SuiError::UserInputError {
error: UserInputError::Unsupported(
"This query type does not currently support filter combinations"
.to_string(),
),
});
}
EventFilter::All(fs) if fs.is_empty() => {
index_store.all_events(tx_num, event_num, limit, descending)?
}
EventFilter::Transaction(digest) => {
index_store.events_by_transaction(&digest, tx_num, event_num, limit, descending)?
Expand Down Expand Up @@ -3966,12 +3957,7 @@ impl AuthorityState {
limit,
descending,
)?,
// not using "_ =>" because we want to make sure we remember to add new variants here
EventFilter::Package(_)
| EventFilter::MoveEventField { .. }
| EventFilter::Any(_)
| EventFilter::And(_, _)
| EventFilter::Or(_, _) => {
EventFilter::All(_) => {
return Err(SuiError::UserInputError {
error: UserInputError::Unsupported(
"This query type is not supported by the full node.".to_string(),
Expand Down
14 changes: 5 additions & 9 deletions crates/sui-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,9 @@ impl IndexerReader {
.await;
} else {
let main_where_clause = match filter {
EventFilter::Package(package_id) => {
format!("package = '\\x{}'::bytea", package_id.to_hex())
EventFilter::All(fs) if fs.is_empty() => {
// No filter
"1 = 1".to_string()
}
EventFilter::MoveModule { package, module } => {
format!(
Expand All @@ -1037,14 +1038,9 @@ impl IndexerReader {
// Processed above
unreachable!()
}
EventFilter::MoveEventField { .. }
| EventFilter::All(_)
| EventFilter::Any(_)
| EventFilter::And(_, _)
| EventFilter::Or(_, _)
| EventFilter::TimeRange { .. } => {
EventFilter::All(_) | EventFilter::TimeRange { .. } => {
return Err(IndexerError::NotSupportedError(
"This type of EventFilter is not supported.".into(),
"This type of EventFilter is not supported.".to_owned(),
));
}
};
Expand Down
50 changes: 9 additions & 41 deletions crates/sui-json-rpc-types/src/sui_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,16 @@ fn try_into_byte(v: &Value) -> Option<u8> {
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub enum EventFilter {
/// Query by events that match all the given filters, or return all events if no filters are
/// supplied.
All(Vec<EventFilter>),
/// Query by sender address.
Sender(SuiAddress),
/// Return events emitted by the given transaction.
Transaction(
///digest of the transaction, as base-64 encoded string
TransactionDigest,
),
/// Return events emitted in a specified Package.
Package(ObjectID),
/// Return events emitted in a specified Move module.
/// If the event is defined in Module A but emitted in a tx with Module B,
/// query `MoveModule` by module B returns the event.
Expand Down Expand Up @@ -244,10 +245,6 @@ pub enum EventFilter {
#[serde_as(as = "DisplayFromStr")]
module: Identifier,
},
MoveEventField {
path: String,
value: Value,
},
/// Return events emitted in [start_time, end_time] interval
#[serde(rename_all = "camelCase")]
TimeRange {
Expand All @@ -260,33 +257,18 @@ pub enum EventFilter {
#[serde_as(as = "BigInt<u64>")]
end_time: u64,
},

All(Vec<EventFilter>),
Any(Vec<EventFilter>),
And(Box<EventFilter>, Box<EventFilter>),
Or(Box<EventFilter>, Box<EventFilter>),
}

impl EventFilter {
fn try_matches(&self, item: &SuiEvent) -> SuiResult<bool> {
Ok(match self {
impl Filter<SuiEvent> for EventFilter {
fn matches(&self, item: &SuiEvent) -> bool {
let _scope = monitored_scope("EventFilter::matches");
match self {
EventFilter::All(fs) => fs.iter().all(|f| f.matches(item)),
EventFilter::MoveEventType(event_type) => &item.type_ == event_type,
EventFilter::MoveEventField { path, value } => {
matches!(item.parsed_json.pointer(path), Some(v) if v == value)
}
EventFilter::Sender(sender) => &item.sender == sender,
EventFilter::Package(object_id) => &item.package_id == object_id,
EventFilter::MoveModule { package, module } => {
&item.transaction_module == module && &item.package_id == package
}
EventFilter::All(filters) => filters.iter().all(|f| f.matches(item)),
EventFilter::Any(filters) => filters.iter().any(|f| f.matches(item)),
EventFilter::And(f1, f2) => {
EventFilter::All(vec![*(*f1).clone(), *(*f2).clone()]).matches(item)
}
EventFilter::Or(f1, f2) => {
EventFilter::Any(vec![*(*f1).clone(), *(*f2).clone()]).matches(item)
}
EventFilter::Transaction(digest) => digest == &item.id.tx_digest,

EventFilter::TimeRange {
Expand All @@ -302,21 +284,7 @@ impl EventFilter {
EventFilter::MoveEventModule { package, module } => {
&item.type_.module == module && &ObjectID::from(item.type_.address) == package
}
})
}

pub fn and(self, other_filter: EventFilter) -> Self {
Self::All(vec![self, other_filter])
}
pub fn or(self, other_filter: EventFilter) -> Self {
Self::Any(vec![self, other_filter])
}
}

impl Filter<SuiEvent> for EventFilter {
fn matches(&self, item: &SuiEvent) -> bool {
let _scope = monitored_scope("EventFilter::matches");
self.try_matches(item).unwrap_or_default()
}
}
}

Expand Down

0 comments on commit 0a4c5b3

Please sign in to comment.