-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: NEAR Lake Helper (high-level Lake Framework) (#51) * feat: NEAR Lake Helper (high-level Lake Framework) * cover all top-level primitives * Update changelog * Add getters to fields in primitives, update the required function, change Event structure * chore: Closes #61: Changes made to the lib should be picked to 0.8.0 * fix(tests): Resolved #60: Fix failing doc tests (#66) * fix(tests): Resolved #60: Fix failing doc tests * fix: add proper code annotations where possible * feat(primitives): Add support for DelegateAction (NEP-366) + LEP-002 (Focus on action and Events) (#67) * feat(primitives): Add support for DelegateAction (NEP-336) * Refactor the structures completely (LEP-002) * (examples) Address review suggestions * remove refactoring commented leftovers * (primitives) Address review suggestion, optimize the structure, do some renaming * Return iterators from methods instead of clone, improve return types, handle tx actions * add todo for later to create dedicated structures for possible errors of Failure ExecutionStatus * DelegateAction struct, split receipts file * remove commented code * expose delegate_actions mod * feat: Expose concurrency parameter to the Lake structure (and builder) (#69) * feat: LEP-001 Introduce Lake Context (#68) * feat: Implement LEP-001 (Context) * clean up Lake impl to reuse code * Update docstrings * refactor: Resolves #42 replace anyhow with LakeError enum (thiserror) (#70) * fix: typo in README * fix: CHANGELOG typos and bring back the concurrency usage lost during merging * feat: ParentTransactionCache context and example (#74) * feat: ParentTransactionCache context and example * Update README for ParentTransactionCache about advanced usage * refactor: Add LakeContext derive and introduce a concept of LakeContext with calls before and after user function * Update lake-parent-transaction-cache/README.md Co-authored-by: Morgan McCauley <morgan@mccauley.co.nz> * address review suggestion rename field accounts_id -> account_ids * add comments to the example, add READMEs, drop accidental artifact * Update documentation, add docs about the LakeContext * Add ParentTransactionCache to the Lake Framework README * refactor Lake::run method to drop code duplication --------- Co-authored-by: Morgan McCauley <morgan@mccauley.co.nz> * docs: Lake Primitives documentation added (#75) * feat: ParentTransactionCache context and example * Update documentation, add docs about the LakeContext * docs: Lake Primitives documentation added * chore: Upgrade near primitives to 0.17.0 version (#77) * chore: Prepare 0.8.0-beta.1 release (#78) * chore: Prepare 0.8.0-beta.1 release * Change to use CUSTOM_GITHUB_TOKEN and add a link to the workflow for the reasoning * Setup versioning and add release-plz.toml * Drop the release-plz job limitation on the CI job * Add missing versions to the changelog * Add 0.7.x branch to the CI --------- Co-authored-by: Morgan McCauley <morgan@mccauley.co.nz>
- Loading branch information
1 parent
fb662d6
commit 67fede7
Showing
38 changed files
with
4,379 additions
and
269 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,40 +1,18 @@ | ||
[package] | ||
name = "near-lake-framework" | ||
version = "0.0.0" # managed by cargo-workspaces, see below | ||
[workspace] | ||
members = [ | ||
"lake-framework", | ||
"lake-primitives", | ||
"lake-parent-transaction-cache", | ||
"lake-context-derive", | ||
] | ||
|
||
# cargo-workspaces | ||
[workspace.package] | ||
version = "0.8.0" | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/near/near-lake-framework" | ||
description = "Library to connect to the NEAR Lake S3 and stream the data" | ||
categories = ["asynchronous", "api-bindings", "network-programming"] | ||
keywords = ["near", "near-lake", "near-indexer"] | ||
authors = ["Near Inc <hello@nearprotocol.com>"] | ||
edition = "2021" | ||
rust-version = "1.58.1" | ||
|
||
# cargo-workspaces | ||
[workspace.metadata.workspaces] | ||
version = "0.7.2" | ||
|
||
[dependencies] | ||
anyhow = "1.0.51" | ||
aws-config = "0.53.0" | ||
aws-types = "0.53.0" | ||
aws-credential-types = "0.53.0" | ||
aws-sdk-s3 = "0.23.0" | ||
async-stream = "0.3.3" | ||
async-trait = "0.1.64" | ||
derive_builder = "0.11.2" | ||
futures = "0.3.23" | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1.0.75" | ||
thiserror = "1.0.38" | ||
tokio = { version = "1.28.2", features = ["sync", "time", "rt", "macros"] } | ||
tokio-stream = { version = "0.1" } | ||
tracing = "0.1.13" | ||
|
||
near-indexer-primitives = "0.17" | ||
|
||
[dev-dependencies] | ||
aws-smithy-http = "0.53.0" | ||
|
||
[lib] | ||
doctest = false | ||
rust-version = "1.69.0" |
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,12 @@ | ||
[package] | ||
name = "lake-context-derive" | ||
version.workspace = true | ||
# version = "0.0.0" # managed by cargo-workspaces | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" |
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,51 @@ | ||
# Lake Context Derive | ||
|
||
Lake Context Derive is a Rust crate that provides a derive macro for easy and convenient implementation of the `near_lake_framework::LakeContextExt` trait. This trait has two functions: `execute_before_run` and `execute_after_run` that are executed before and after the user-provided indexer function respectively. | ||
|
||
## Usage | ||
|
||
The Lake Context Derive macro can be utilized by annotating the context struct with `#[derive(LakeContext)]`. This trait implementation will then facilitate the combination of different contexts. For instance, to use a `ParentTransactionCache` with some additional data, one would define a context like: | ||
|
||
```no_run | ||
use lake_parent_transaction_cache::ParentTransactionCache; | ||
#[derive(LakeContext)] | ||
struct MyContext { | ||
db_connection_string: String, | ||
parent_tx_cache: ParentTransactionCache, | ||
} | ||
``` | ||
|
||
### Instantiation | ||
|
||
You can create an instance of your context as follows: | ||
|
||
```no_run | ||
let my_context = MyContext { | ||
db_connection_string: String::from("postgres://user:pass@host/db"), | ||
parent_tx_cache: ParentTransactionCache::default().build().unwrap(), | ||
}; | ||
``` | ||
|
||
### User Indexer Function | ||
|
||
This will simplify your indexer function signature. It now needs only the context as an additional parameter: | ||
|
||
```no_run | ||
async fn handle_block( | ||
mut block: Block, | ||
ctx: &MyContext, | ||
) -> anyhow::Result<()> { | ||
// body | ||
} | ||
``` | ||
|
||
The Lake Context Derive will look for all fields in the struct that implement `LakeContextExt`, and will append their trait methods to the top-level calls. For `execute_before_run`, it is done in ascending order, and for `execute_after_run` in descending order. | ||
|
||
## Purpose | ||
|
||
The purpose of the Lake Context Derive crate is to alleviate some of the common pain points in context development and usage in Rust. By encapsulating and standardizing the handling of these function calls, we aim to create a more accessible and user-friendly approach to context implementation. | ||
|
||
## Collaboration | ||
|
||
We hope that this tool will be useful for the Rust community and look forward to seeing how it can be used in a range of different projects. We encourage community contributions, whether that's through sharing your own unique context implementations or by providing feedback and suggestions for how we can continue to improve the Lake Context Derive. |
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,80 @@ | ||
#![doc = include_str!("../README.md")] | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
|
||
#[proc_macro_derive(LakeContext)] | ||
pub fn lake_context_derive(input: TokenStream) -> TokenStream { | ||
let input = syn::parse_macro_input!(input as syn::DeriveInput); | ||
|
||
// Used in the quasi-quotation below as `#name`. | ||
let name = input.ident; | ||
|
||
// Build the trait impl. | ||
// Iterate over all fields and for each field generate a call to `execute_before_run`. | ||
// if the field is a an impl of LakeContext, then call `execute_before_run` on the struct. | ||
|
||
let fields = if let syn::Data::Struct(syn::DataStruct { | ||
fields: syn::Fields::Named(syn::FieldsNamed { named, .. }), | ||
.. | ||
}) = &input.data | ||
{ | ||
named | ||
} else { | ||
unimplemented!(); | ||
}; | ||
|
||
let calls_before_run = fields | ||
.iter() | ||
.filter(|f| { | ||
let ty = &f.ty; | ||
if let syn::Type::Path(syn::TypePath { path, .. }) = ty { | ||
if let Some(ident) = path.get_ident() { | ||
ident == "LakeContext" | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
}) | ||
.map(|f| { | ||
let name = &f.ident; | ||
quote! { self.#name.execute_before_run(block); } | ||
}); | ||
|
||
let calls_after_run = fields | ||
.iter() | ||
.rev() | ||
.filter(|f| { | ||
let ty = &f.ty; | ||
if let syn::Type::Path(syn::TypePath { path, .. }) = ty { | ||
if let Some(ident) = path.get_ident() { | ||
ident == "LakeContext" | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
}) | ||
.map(|f| { | ||
let name = &f.ident; | ||
quote! { self.#name.execute_after_run(); } | ||
}); | ||
|
||
let expanded = quote! { | ||
// The generated impl. | ||
impl near_lake_framework::LakeContextExt for #name { | ||
fn execute_before_run(&self, block: &mut near_lake_primitives::block::Block) { | ||
#( #calls_before_run )* | ||
} | ||
|
||
fn execute_after_run(&self) { | ||
#( #calls_after_run )* | ||
} | ||
} | ||
}; | ||
|
||
// Hand the output tokens back to the compiler. | ||
proc_macro::TokenStream::from(expanded) | ||
} |
Oops, something went wrong.