From d615bad5abb36fe2ccfb32a147ab64cf2850362e Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Fri, 3 Jun 2022 02:12:14 -0400 Subject: [PATCH] Fixed all doctests, remove all ignores, move docs in substreams-macro directly to `substreams::handlers` --- .cargo/config.toml | 2 + Cargo.lock | 6 +-- rust/substreams-macro/src/lib.rs | 61 ----------------------- rust/substreams/src/handlers.rs | 83 +++++++++++++++++++++++++++++++- rust/substreams/src/lib.rs | 23 ++++----- rust/substreams/src/proto.rs | 2 - rust/substreams/src/store.rs | 16 ------ 7 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..435ed755e --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 678860199..779671343 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,7 +306,7 @@ dependencies = [ [[package]] name = "substreams" -version = "0.0.10-beta" +version = "0.0.11" dependencies = [ "bigdecimal", "hex", @@ -321,7 +321,7 @@ dependencies = [ [[package]] name = "substreams-macro" -version = "0.0.10-beta" +version = "0.0.11" dependencies = [ "proc-macro2", "quote", @@ -356,7 +356,7 @@ dependencies = [ [[package]] name = "testing-substreams" -version = "0.1.0" +version = "0.0.11" dependencies = [ "bigdecimal", "hex", diff --git a/rust/substreams-macro/src/lib.rs b/rust/substreams-macro/src/lib.rs index a822597e2..a3ddbf304 100644 --- a/rust/substreams-macro/src/lib.rs +++ b/rust/substreams-macro/src/lib.rs @@ -5,72 +5,11 @@ mod errors; mod handler; mod store; -/// Marks function to setup substream map handler wasm boilerplate -/// -/// ## Usage -/// -/// -/// ```rust,ignore -/// #[substreams::handlers::map] -/// fn map_handler(blk: eth::Block) -> Result { -/// Ok(blk.transactions_traces[0]) -/// } -/// ``` -/// -/// Equivalent code not using `#[substream::handlers::map]` -/// -/// ```rust,ignore -/// #[no_mangle] -/// pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) { -/// substreams::register_panic_hook(); -/// let func = || -> Result { -/// let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap(); -/// { -/// Ok(blk.transactions_traces[0]) -/// } -/// }; -/// let result = func(); -/// if result.is_err() { -/// panic!(result.err().unwrap()) -/// } -/// substreams::output(substreams::proto::encode(result).unwrap()); -/// } -/// ``` #[proc_macro_attribute] pub fn map(args: TokenStream, item: TokenStream) -> TokenStream { return handler::main(args, item, config::ModuleType::Map); } -/// Marks function to setup substream store handler wasm boilerplate -/// ## Usage -/// -/// -/// ```rust,ignore -/// use substreams::{log, store}; -/// -/// #[substreams::handlers::store] -/// fn build_nft_state(transfers: erc721::Transfers, s: store::StoreAddInt64, pairs: store::Reader, tokens: store::Reader) { -/// log::info!("Length {}", transfers.len()); -/// } -/// ``` -/// -/// Equivalent code not using `#[substream::handlers::store]` -/// -/// ```rust,ignore -/// use substreams::{log, store}; -/// -/// #[no_mangle] -/// pub extern "C" fn build_nft_state(transfers_ptr: *mut u8, transfers_len: usize, pairs_idx: u32, tokens_idx: u32) { -/// substreams::register_panic_hook(); -/// let transfers: erc721::Transfers = substreams::proto::decode_ptr(transfers_ptr, transfers_len).unwrap(); -/// let pairs: store::Reader = store::Reader::new(pairs_idx); -/// let tokens: store::Reader = store::Reader::new(tokens_idx); -/// let s: store::StoreAddInt64 = store::StoreAddInt64::new(); -/// { -/// log::info!("Length {}", transfers.len()); -/// } -/// } -/// ``` #[proc_macro_attribute] pub fn store(args: TokenStream, item: TokenStream) -> TokenStream { return handler::main(args, item, config::ModuleType::Store); diff --git a/rust/substreams/src/handlers.rs b/rust/substreams/src/handlers.rs index afca95806..91ada508d 100644 --- a/rust/substreams/src/handlers.rs +++ b/rust/substreams/src/handlers.rs @@ -3,4 +3,85 @@ //! This create exports useful macros that you can utilize to develop //! Substreams handlers. Theses macros' goal is to significantly reduce boilerplate //! code and ensure that your handler are more readeable -pub use substreams_macro::{map, store}; \ No newline at end of file + +/// Marks function to setup substream map handler WASM boilerplate +/// +/// ## Usage +/// +/// +/// ```rust +/// # mod eth { pub type Block = (); } +/// # mod proto { pub type Custom = (); } +/// +/// #[substreams::handlers::map] +/// fn map_handler(blk: eth::Block) -> Result { +/// unimplemented!("do something"); +/// } +/// ``` +/// +/// Equivalent code not using `#[substream::handlers::map]` +/// +/// ```rust +/// # mod eth { pub type Block = (); } +/// # mod proto { +/// # #[derive(Debug)] +/// # pub struct Custom(u8); +/// # impl prost::Message for Custom { +/// # fn encode_raw(&self, _: &mut B) where Self: Sized { todo!() } +/// # fn merge_field(&mut self, _: u32, _: prost::encoding::WireType, _: &mut B, _: prost::encoding::DecodeContext) -> Result<(), prost::DecodeError> where Self: Sized { todo!() } +/// # fn encoded_len(&self) -> usize { todo!() } +/// # fn clear(&mut self) { todo!() } +/// # } +/// # } +/// +/// #[no_mangle] +/// pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) { +/// substreams::register_panic_hook(); +/// let func = || -> Result { +/// let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap(); +/// { +/// unimplemented!("do something"); +/// } +/// }; +/// let result = func(); +/// if result.is_err() { +/// panic!(result.err().unwrap()) +/// } +/// substreams::output(substreams::proto::encode(&result.unwrap()).unwrap()); +/// } +/// ``` +pub use substreams_macro::map; + +/// Marks function to setup substream store handler WASM boilerplate +/// ## Usage +/// +/// +/// ```rust +/// use substreams::{log, store}; +/// # mod proto { pub type Custom = (); } +/// +/// #[substreams::handlers::store] +/// fn build_nft_state(data: proto::Custom, s: store::StoreAddInt64, pairs: store::StoreGet, tokens: store::StoreGet) { +/// unimplemented!("do something"); +/// } +/// ``` +/// +/// Equivalent code not using `#[substream::handlers::store]` +/// +/// ```rust +/// use substreams::{log, store}; +/// # mod proto { pub type Custom = (); } +/// +/// #[no_mangle] +/// pub extern "C" fn build_nft_state(data_ptr: *mut u8, data_len: usize, pairs_idx: u32, tokens_idx: u32) { +/// substreams::register_panic_hook(); +/// let data: proto::Custom = substreams::proto::decode_ptr(data_ptr, data_len).unwrap(); +/// let pairs: store::StoreGet = store::StoreGet::new(pairs_idx); +/// let tokens: store::StoreGet = store::StoreGet::new(tokens_idx); +/// let s: store::StoreAddInt64 = store::StoreAddInt64::new(); +/// { +/// unimplemented!("do something"); +/// } +/// } +/// ``` +pub use substreams_macro::store; diff --git a/rust/substreams/src/lib.rs b/rust/substreams/src/lib.rs index 225968b2f..268394da5 100644 --- a/rust/substreams/src/lib.rs +++ b/rust/substreams/src/lib.rs @@ -15,29 +15,31 @@ //! //! ```no_run //! use substreams::{errors::Error, store}; +//! # mod eth { pub type Block = (); } +//! # mod proto { pub type Custom = (); } //! -//! // map handler that takes a source as input +//! /// Map handler that takes a source as input //! #[substreams::handlers::map] //! fn map_transfers(blk: eth::Block) -> Result { -//! // do something +//! unimplemented!("do something"); //! } //! -//! // map handler that takes a source, and a store in get mode as inputs +//! /// Map handler that takes a source, and a store in get mode as inputs //! #[substreams::handlers::map] //! fn map_ownerships(blk: eth::Block, store: store::StoreGet) -> Result { -//! // do something +//! unimplemented!("do something"); //! } //! -//! // map handler that takes a source, another map, and a store in get mode as inputs +//! /// Map handler that takes a source, another map, and a store in get mode as inputs //! #[substreams::handlers::map] //! fn map_mints(blk: eth::Block, mints: proto::Custom, store: store::StoreGet) -> Result { -//! // do something +//! unimplemented!("do something"); //! } -//! //! -//! // map handler that takes a source, another map, and a store in delta mode as inputs +//! +//! /// Map handler that takes a source, another map, and a store in delta mode as inputs //! #[substreams::handlers::map] //! fn map_db(blk: eth::Block, mints: proto::Custom, store_deltas: store::Deltas) -> Result { -//! // do something +//! unimplemented!("do something"); //! } //! ``` //! @@ -49,6 +51,7 @@ //! //! ```no_run //! use substreams::store; +//! # mod proto { pub type Custom = (); } //! //! #[substreams::handlers::store] //! fn store_transfers(objects: proto::Custom, output: store::StoreAddInt64) { @@ -65,8 +68,6 @@ //! // to something //! } //!``` -//! -//! pub mod errors; mod externs; pub mod handlers; diff --git a/rust/substreams/src/proto.rs b/rust/substreams/src/proto.rs index b9cd543ee..b24d3024f 100644 --- a/rust/substreams/src/proto.rs +++ b/rust/substreams/src/proto.rs @@ -4,8 +4,6 @@ //! is used across Substreams //! - - use prost::{DecodeError, EncodeError}; /// Given an array of bytes, it will decode data in a Protobuf Message diff --git a/rust/substreams/src/store.rs b/rust/substreams/src/store.rs index 6a7e17920..4f8e88290 100644 --- a/rust/substreams/src/store.rs +++ b/rust/substreams/src/store.rs @@ -18,7 +18,6 @@ pub type Deltas = Vec; #[derive(StoreWriter)] pub struct StoreSet {} impl StoreSet { - /// Set a given key to a given value, if the key existed before, it will be replaced. pub fn set(&self, ord: u64, key: String, value: &Vec) { state::set(ord as i64, key, value); @@ -37,7 +36,6 @@ impl StoreSet { #[derive(StoreWriter)] pub struct StoreSetIfNotExists {} impl StoreSetIfNotExists { - /// Set a given key to a given value, if the key existed before, it will be ignored and not set. pub fn set_if_not_exists(&self, ord: u64, key: String, value: &Vec) { state::set_if_not_exists(ord as i64, key, value); @@ -56,7 +54,6 @@ impl StoreSetIfNotExists { #[derive(StoreWriter)] pub struct StoreAddInt64 {} impl StoreAddInt64 { - /// Will add the value to the already present value at the key (or default to /// zero if the key was not set) pub fn add(&self, ord: u64, key: String, value: i64) { @@ -77,7 +74,6 @@ impl StoreAddInt64 { #[derive(StoreWriter)] pub struct StoreAddFloat64 {} impl StoreAddFloat64 { - /// Will add the value to the already present value at the key (or default to /// zero if the key was not set) pub fn add(&self, ord: u64, key: String, value: f64) { @@ -98,7 +94,6 @@ impl StoreAddFloat64 { #[derive(StoreWriter)] pub struct StoreAddBigFloat {} impl StoreAddBigFloat { - /// Will add the value to the already present value at the key (or default to /// zero if the key was not set) pub fn add(&self, ord: u64, key: String, value: &BigDecimal) { @@ -119,7 +114,6 @@ impl StoreAddBigFloat { #[derive(StoreWriter)] pub struct StoreAddBigInt {} impl StoreAddBigInt { - /// Will add the value to the already present value of the keys (or default to /// zero if the key was not set) pub fn add(&self, ord: u64, key: String, value: &BigInt) { @@ -140,7 +134,6 @@ impl StoreAddBigInt { #[derive(StoreWriter)] pub struct StoreMaxInt64 {} impl StoreMaxInt64 { - /// max will set the provided key in the store only if the value received in /// parameter is bigger than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -154,7 +147,6 @@ impl StoreMaxInt64 { #[derive(StoreWriter)] pub struct StoreMaxBigInt {} impl StoreMaxBigInt { - /// Will set the provided key in the store only if the value received in /// parameter is bigger than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -168,7 +160,6 @@ impl StoreMaxBigInt { #[derive(StoreWriter)] pub struct StoreMaxFloat64 {} impl StoreMaxFloat64 { - /// Will set the provided key in the store only if the value received in /// parameter is bigger than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -182,7 +173,6 @@ impl StoreMaxFloat64 { #[derive(StoreWriter)] pub struct StoreMaxBigFloat {} impl StoreMaxBigFloat { - /// Will set the provided key in the store only if the value received in /// parameter is bigger than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -196,8 +186,6 @@ impl StoreMaxBigFloat { #[derive(StoreWriter)] pub struct StoreMinInt64 {} impl StoreMinInt64 { - - /// Will set the provided key in the store only if the value received in /// parameter is smaller than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -211,7 +199,6 @@ impl StoreMinInt64 { #[derive(StoreWriter)] pub struct StoreMinBigInt {} impl StoreMinBigInt { - /// Will set the provided key in the store only if the value received in /// parameter is smaller than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -225,7 +212,6 @@ impl StoreMinBigInt { #[derive(StoreWriter)] pub struct StoreMinFloat64 {} impl StoreMinFloat64 { - /// Will set the provided key in the store only if the value received in /// parameter is smaller than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -239,7 +225,6 @@ impl StoreMinFloat64 { #[derive(StoreWriter)] pub struct StoreMinBigFloat {} impl StoreMinBigFloat { - /// Will set the provided key in the store only if the value received in /// parameter is smaller than the one already present in the store, with /// a default of the zero value when the key is absent. @@ -254,7 +239,6 @@ pub struct StoreGet { } impl StoreGet { - /// Return a StoreGet object with a store index set pub fn new(idx: u32) -> StoreGet { StoreGet { idx }