-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,354 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fixtures/compressed_files_lz4_00008bc1d5adcf862d3967c1410001fb_705101000.pb.lz4 filter=lfs diff=lfs merge=lfs -text | ||
fixtures/compressed_files_lz4_0013c194ec4fdbfb8db7306170aac083_445907000.pb.lz4 filter=lfs diff=lfs merge=lfs -text | ||
fixtures/compressed_files_lz4_f3d880d9700c70d71fefe71aa9218aa9_301616000.pb.lz4 filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "aptos-transaction-filter" | ||
version = "0.1.0" | ||
|
||
# Workspace inherited keys | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
publish = { workspace = true } | ||
repository = { workspace = true } | ||
rust-version = { workspace = true } | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
aptos-protos = { workspace = true } | ||
|
||
derive_builder = { workspace = true } | ||
|
||
prost = { workspace = true } | ||
|
||
serde = { workspace = true } | ||
serde_json = { workspace = true } | ||
serde_yaml = { workspace = true } | ||
|
||
thiserror = { workspace = true } | ||
|
||
[dev-dependencies] | ||
# we only decompress the fixture protos in test | ||
lz4 = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Transaction Filter | ||
|
||
## Overview | ||
|
||
The goal of **transaction filtering** is to be able to save resources downstream of wherever filtering is used. | ||
For this to be true, the filtering itself must be **fast and use minimal resources**, and so we do a few things: | ||
|
||
1. We avoid clones, copies, etc as much as possible | ||
2. We do a single pass over the transaction data | ||
|
||
## Transaction Filtering | ||
|
||
There are a few different parts of a transaction that are queryable: | ||
|
||
1. The "root" level. This includes: | ||
- Transaction type | ||
- Success | ||
2. User Transactions. Each user transaction has: | ||
- Sender | ||
- Payload: we only support the entry function payload | ||
- Entry function (address, module, name) | ||
- Entry function ID string | ||
3. Events. Each event has: | ||
- Key | ||
- Type | ||
|
||
### Usage & Examples | ||
|
||
There are two different patterns for building a filter- you can either use the `TransactionFilterBuilder` or | ||
the `TransactionFilter` struct directly. | ||
|
||
The `TransactionFilterBuilder` is a more ergonomic way to build a filter, and is not significantly worse construction | ||
performance, assuming this is being done infrequently. | ||
|
||
``` | ||
use transaction_filter::filters::EventFilterBuilder; | ||
let ef = EventFilterBuilder::default() | ||
.data("spins") | ||
.struct_type( | ||
MoveStructTagFilterBuilder::default() | ||
.address("0x0077") | ||
.module("roulette") | ||
.name("spin") | ||
.build()?, | ||
) | ||
.build()?; | ||
``` | ||
|
||
The `TransactionFilter` struct is also available, but requires direct construction of the structs. | ||
|
||
``` | ||
use transaction_filter::filters::EventFilter; | ||
let ef = EventFilter { | ||
data: Some("spins".into()), | ||
struct_type: Some(MoveStructTagFilter { | ||
address: Some("0x0077".into()), | ||
module: Some("roulette".into()), | ||
name: Some("spin".into()), | ||
}), | ||
}; | ||
``` | ||
|
||
Once you have some filters built, you can combine them with the boolean operators `and`, `or`, and `not`. | ||
|
||
``` | ||
let trf = TransactionRootFilterBuilder::default() | ||
.success(true).build()?; | ||
let utf = UserTransactionFilterBuilder::default() | ||
.sender("0x0011".into()).build()?; | ||
let ef = EventFilterBuilder::default() | ||
.struct_type( | ||
MoveStructTagFilterBuilder::default() | ||
.address("0x0077") | ||
.module("roulette") | ||
.name("spin") | ||
.build()?, | ||
) | ||
.build()?; | ||
// Combine filters using logical operators! | ||
// (trf OR utf) | ||
let trf_or_utf = BooleanTransactionFilter::from(trf).or(utf); | ||
// ((trf OR utf) AND ef) | ||
let query = trf_or_utf.and(ef); | ||
let transactions: Vec<Transaction> = transaction_stream.next().await; | ||
let filtered_transactions = query.filter_vec(transactions); | ||
``` | ||
|
||
## API & Serialization | ||
|
||
`BooleanTransactionFilter` is the top level filter struct, and it uses `serde` for serialization and deserialization. | ||
|
||
This means we can use it across all of our projects, whether they be GRPC services, REST services, or CLI tools. | ||
|
||
The above example can be serialized to JSON like so: | ||
|
||
```json | ||
{ | ||
"and": [ | ||
{ | ||
"or": [ | ||
{ | ||
"type": "TransactionRootFilter", | ||
"success": true | ||
}, | ||
{ | ||
"type": "UserTransactionFilter", | ||
"sender": "0x0011" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "EventFilter", | ||
"struct_type": { | ||
"address": "0x0077", | ||
"module": "roulette", | ||
"name": "spin" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Or, if you prefer, as yaml: | ||
|
||
```yaml | ||
--- | ||
and: | ||
- or: | ||
- type: TransactionRootFilter | ||
success: true | ||
- type: UserTransactionFilter | ||
sender: '0x0011' | ||
- type: EventFilter | ||
struct_type: | ||
address: '0x0077' | ||
module: roulette | ||
name: spin | ||
``` | ||
3 changes: 3 additions & 0 deletions
3
...on-filter/fixtures/compressed_files_lz4_00008bc1d5adcf862d3967c1410001fb_705101000.pb.lz4
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...on-filter/fixtures/compressed_files_lz4_0013c194ec4fdbfb8db7306170aac083_445907000.pb.lz4
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...on-filter/fixtures/compressed_files_lz4_f3d880d9700c70d71fefe71aa9218aa9_301616000.pb.lz4
Git LFS file not shown
Oops, something went wrong.