Skip to content

Commit

Permalink
fix(tests): Resolved #60: Fix failing doc tests (#66)
Browse files Browse the repository at this point in the history
* fix(tests): Resolved #60: Fix failing doc tests

* fix: add proper code annotations where possible
  • Loading branch information
khorolets committed Jun 6, 2023
1 parent 770b01c commit bc139e3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 98 deletions.
80 changes: 28 additions & 52 deletions lake-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,26 @@ the NEAR Protocol data.

## Example

```ignore
use futures::StreamExt;
use near_lake_framework::LakeConfigBuilder;
#[tokio::main]
async fn main() -> Result<(), tokio::io::Error> {
// create a NEAR Lake Framework config
let config = LakeConfigBuilder::default()
```no_run
fn main() -> anyhow::Result<()> {
near_lake_framework::LakeBuilder::default()
.testnet()
.start_block_height(82422587)
.build()
.expect("Failed to build LakeConfig");
// instantiate the NEAR Lake Framework Stream
let (sender, stream) = near_lake_framework::streamer(config);
// read the stream events and pass them to a handler function with
// concurrency 1
let mut handlers = tokio_stream::wrappers::ReceiverStream::new(stream)
.map(|streamer_message| handle_streamer_message(streamer_message))
.buffer_unordered(1usize);
while let Some(_handle_message) = handlers.next().await {}
drop(handlers); // close the channel so the sender will stop
// propagate errors from the sender
match sender.await {
Ok(Ok(())) => Ok(()),
Ok(Err(e)) => Err(e),
Err(e) => Err(anyhow::Error::from(e)), // JoinError
}
.start_block_height(112205773)
.build()?
.run(handle_block)
}
// The handler function to take the entire `StreamerMessage`
// and print the block height and number of shards
async fn handle_streamer_message(
streamer_message: near_lake_framework::near_indexer_primitives::StreamerMessage,
) {
// The handler function to take the `Block`
// and print the block height
async fn handle_block(
block: near_lake_primitives::block::Block,
_context: near_lake_framework::LakeContext,
) -> anyhow::Result<()> {
eprintln!(
"{} / shards {}",
streamer_message.block.header.height,
streamer_message.shards.len()
"Block #{}",
block.block_height(),
);
# Ok(())
}
```

Expand Down Expand Up @@ -76,7 +53,7 @@ In order to be able to get objects from the AWS S3 bucket you need to provide th
```rust
use near_lake_framework::LakeBuilder;

# async fn main() {
# fn main() {
let credentials = aws_credential_types::Credentials::new(
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
Expand All @@ -88,9 +65,10 @@ let s3_config = aws_sdk_s3::Config::builder()
.credentials_provider(credentials)
.build();

let config = LakeBuilder::default()
let lake = LakeBuilder::default()
.s3_config(s3_config)
.s3_bucket_name("near-lake-data-custom")
.s3_region_name("eu-central-1")
.start_block_height(1)
.build()
.expect("Failed to build LakeConfig");
Expand Down Expand Up @@ -134,7 +112,7 @@ tokio = { version = "1.1", features = ["sync", "time", "macros", "rt-multi-threa
tokio-stream = { version = "0.1" }

# NEAR Lake Framework
near-lake-framework = "0.6.1"
near-lake-framework = "0.8.0"
```

### Custom S3 storage
Expand All @@ -144,28 +122,26 @@ In case you want to run your own [near-lake](https://github.com/near/near-lake)

- run minio

```text
```bash
$ mkdir -p /data/near-lake-custom && minio server /data
```

- pass custom `aws_sdk_s3::config::Config` to the [LakeConfigBuilder]
- pass custom `aws_sdk_s3::config::Config` to the [LakeBuilder]

```rust
use aws_sdk_s3::Endpoint;
use http::Uri;
```
use near_lake_framework::LakeBuilder;
# fn main() {
# #[tokio::main]
# async fn main() {
let aws_config = aws_config::from_env().load().await;
let mut s3_conf = aws_sdk_s3::config::Builder::from(&aws_config);
s3_conf = s3_conf
.endpoint_resolver(
Endpoint::immutable("http://0.0.0.0:9000".parse::<Uri>().unwrap()))
let mut s3_conf = aws_sdk_s3::config::Builder::from(&aws_config)
.endpoint_url("http://0.0.0.0:9000")
.build();
let config = LakeBuilder::default()
let lake = LakeBuilder::default()
.s3_config(s3_conf)
.s3_bucket_name("near-lake-data-custom")
.s3_region_name("eu-central-1")
.start_block_height(1)
.build()
.expect("Failed to build LakeConfig");
Expand Down
25 changes: 9 additions & 16 deletions lake-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,17 @@ pub(crate) mod types;
pub(crate) const LAKE_FRAMEWORK: &str = "near_lake_framework";

/// Creates `mpsc::channel` and returns the `receiver` to read the stream of `StreamerMessage`
/// ```
/// use near_lake_framework::LakeConfigBuilder;
/// use tokio::sync::mpsc;
///
/// # async fn main() {
/// let config = LakeConfigBuilder::default()
///```no_run
///# fn main() -> anyhow::Result<()> {
/// near_lake_framework::LakeBuilder::default()
/// .testnet()
/// .start_block_height(82422587)
/// .build()
/// .expect("Failed to build LakeConfig");
///
/// let (_, stream) = near_lake_framework::streamer(config);
/// .start_block_height(112205773)
/// .build()?
/// .run(handle_block)
///# }
///
/// while let Some(streamer_message) = stream.recv().await {
/// eprintln!("{:#?}", streamer_message);
/// }
/// # }
/// ```
/// # async fn handle_block(_block: near_lake_primitives::block::Block, _context: near_lake_framework::LakeContext) -> anyhow::Result<()> { Ok(()) }
///```
impl types::Lake {
pub fn run<Fut>(
self,
Expand Down
58 changes: 28 additions & 30 deletions lake-framework/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
pub type BlockHeight = u64;

/// Configuration struct for NEAR Lake Framework
/// NB! Consider using [`LakeConfigBuilder`]
/// Building the `LakeConfig` example:
/// NB! Consider using [`LakeBuilder`]
/// Building the `Lake` example:
/// ```
/// use near_lake_framework::LakeConfigBuilder;
/// use near_lake_framework::LakeBuilder;
///
/// # async fn main() {
/// let config = LakeConfigBuilder::default()
/// # fn main() {
/// let lake = LakeBuilder::default()
/// .testnet()
/// .start_block_height(82422587)
/// .build()
/// .expect("Failed to build LakeConfig");
/// .expect("Failed to build Lake");
/// # }
/// ```
#[derive(Default, Builder, Debug)]
Expand All @@ -30,24 +30,22 @@ pub struct Lake {
/// ## Use-case: custom endpoint
/// You might want to stream data from the custom S3-compatible source () . In order to do that you'd need to pass `aws_sdk_s3::config::Config` configured
/// ```
/// use aws_sdk_s3::Endpoint;
/// use http::Uri;
/// use near_lake_framework::LakeConfigBuilder;
/// use near_lake_framework::LakeBuilder;
///
/// # #[tokio::main]
/// # async fn main() {
/// let aws_config = aws_config::from_env().load().await;
/// let mut s3_conf = aws_sdk_s3::config::Builder::from(&aws_config);
/// s3_conf = s3_conf
/// .endpoint_resolver(
/// Endpoint::immutable("http://0.0.0.0:9000".parse::<Uri>().unwrap()))
/// let mut s3_conf = aws_sdk_s3::config::Builder::from(&aws_config)
/// .endpoint_url("http://0.0.0.0:9000")
/// .build();
///
/// let config = LakeConfigBuilder::default()
/// let lake = LakeBuilder::default()
/// .s3_config(s3_conf)
/// .s3_bucket_name("near-lake-data-custom")
/// .s3_region_name("eu-central-1")
/// .start_block_height(1)
/// .build()
/// .expect("Failed to build LakeConfig");
/// .expect("Failed to build Lake");
/// # }
/// ```
#[builder(setter(strip_option), default)]
Expand All @@ -57,16 +55,16 @@ pub struct Lake {
}

impl LakeBuilder {
/// Shortcut to set up [LakeConfigBuilder::s3_bucket_name] for mainnet
/// Shortcut to set up [LakeBuilder::s3_bucket_name] for mainnet
/// ```
/// use near_lake_framework::LakeConfigBuilder;
/// use near_lake_framework::LakeBuilder;
///
/// # async fn main() {
/// let config = LakeConfigBuilder::default()
/// # fn main() {
/// let lake = LakeBuilder::default()
/// .mainnet()
/// .start_block_height(65231161)
/// .build()
/// .expect("Failed to build LakeConfig");
/// .expect("Failed to build Lake");
/// # }
/// ```
pub fn mainnet(mut self) -> Self {
Expand All @@ -75,16 +73,16 @@ impl LakeBuilder {
self
}

/// Shortcut to set up [LakeConfigBuilder::s3_bucket_name] for testnet
/// Shortcut to set up [LakeBuilder::s3_bucket_name] for testnet
/// ```
/// use near_lake_framework::LakeConfigBuilder;
/// use near_lake_framework::LakeBuilder;
///
/// # async fn main() {
/// let config = LakeConfigBuilder::default()
/// # fn main() {
/// let lake = LakeBuilder::default()
/// .testnet()
/// .start_block_height(82422587)
/// .build()
/// .expect("Failed to build LakeConfig");
/// .expect("Failed to build Lake");
/// # }
/// ```
pub fn testnet(mut self) -> Self {
Expand All @@ -93,16 +91,16 @@ impl LakeBuilder {
self
}

/// Shortcut to set up [LakeConfigBuilder::s3_bucket_name] for betanet
/// Shortcut to set up [LakeBuilder::s3_bucket_name] for betanet
/// ```
/// use near_lake_framework::LakeConfigBuilder;
/// use near_lake_framework::LakeBuilder;
///
/// # async fn main() {
/// let config = LakeConfigBuilder::default()
/// # fn main() {
/// let lake = LakeBuilder::default()
/// .betanet()
/// .start_block_height(82422587)
/// .build()
/// .expect("Failed to build LakeConfig");
/// .expect("Failed to build Lake");
/// # }
/// ```
pub fn betanet(mut self) -> Self {
Expand Down

0 comments on commit bc139e3

Please sign in to comment.