Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: documented network config #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

akorchyn
Copy link
Collaborator

@akorchyn akorchyn commented Dec 20, 2024

@akorchyn akorchyn changed the title chore!: network config docs chore: network config docs Dec 20, 2024
@race-of-sloths
Copy link

race-of-sloths commented Dec 20, 2024

@akorchyn Thank you for your contribution! Your pull request is now a part of the Race of Sloths!

Shows inviting banner with latest news.

Shows profile picture for the author of the PR

Current status: waiting for merge
Reviewer Score
@dj8yfo 5

Your contribution is much appreciated with a final score of 5!
You have received 50 Sloth points for this contribution

@dj8yfo received 25 Sloth Points for reviewing and scoring this pull request.

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths and receive a reward
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@akorchyn akorchyn changed the title chore: network config docs docs: network config docs Dec 22, 2024
@akorchyn akorchyn changed the title docs: network config docs docs: documented network config Dec 22, 2024
Copy link
Collaborator

@dj8yfo dj8yfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@race-of-sloths score 5

pub api_key: Option<crate::types::ApiKey>,
/// Number of consecutive failures to move on to the next endpoint.
pub retries: u8,
/// Whether to use exponential backoff between retries
///
/// The formula is `d = initial_sleep * factor^retry`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's left unspecified what backoff behaviour will look like if exponential_backoff is false.
it can be defined more accurately with a doc comment or
by using an enum like the following instead of exponential_backoff: bool:

pub retry_algorithm: RetryAlgorithm,
....
pub enum RetryAlgorithm {
    ExponentialBackoff {
        initial_sleep: std::time::Duration,
        factor: u8,
    },
    SomeOtherWay { ... },
}

pub exponential_backoff: bool,
/// Multiplier for exponential backoff calculation
pub factor: u8,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the backoff multiplier is somewhat simplejack instead of e.g. an f32 (where a factor can be 1.2f32),
but this is not necessarily a bad thing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't love floats, to be honest

pub linkdrop_account_id: Option<near_primitives::types::AccountId>,
// https://docs.near.org/social/contract
/// Account ID of the [NEAR Social contract](https://docs.near.org/social/contract)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link for NEAR Social contract appears to be broken

pub fn testnet() -> Self {
Self {
network_name: "testnet".to_string(),
rpc_endpoints: vec![RPCEndpoint::testnet()],
linkdrop_account_id: Some("testnet".parse().unwrap()),
near_social_db_contract_account_id: Some("v1.social08.testnet".parse().unwrap()),
faucet_url: Some("https://helper.nearprotocol.com/account".parse().unwrap()),
meta_transaction_relayer_url: Some("http://localhost:3030/relay".parse().unwrap()),
meta_transaction_relayer_url: None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somewhat unrelated, but bon can be used here instead of ..NetworkConfig::mainnet() syntax,

// $ cargo add bon
// bon = { version= "3.3.1", features = ["experimental-overwritable"]}
use bon::Builder;
use network_builder::{SetName, SetRpcStuff};

#[derive(Builder)]
struct Network {
    name: String,
    #[builder(overwritable)]
    rpc_stuff: String,
}

impl Network {
    pub fn testnet() -> NetworkBuilder<SetRpcStuff<SetName>> {
        Self::builder()
            .name("testnet".into())
            .rpc_stuff("my_fast_near_rpc".to_string())
    }
}

fn main() {
    {
        let network = Network::testnet().build();

        assert_eq!(network.name, "testnet");
        assert_eq!(network.rpc_stuff, "my_fast_near_rpc");
    }
    {
        let network = Network::testnet()
            .rpc_stuff("very_slow_another_rpc".to_owned())
            .build();

        assert_eq!(network.name, "testnet");
        assert_eq!(network.rpc_stuff, "very_slow_another_rpc");
    }
}

referencing elastio/bon#149 , as it links to experimental feature needed.

without experimental-overwritable it kind of not possible to achieve the same with #[builder(default)] on the same Network type as there're different defaults for different networks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this crate being a lib, it won't be too great to use a dependency with experimental feature, which can break on minor releases,

though pinning it with ~3.3.1 (only patch releases updates allowed) or =3.3.1 (fully pinned) would allow to avoid problems for downstream consumers of the library

Copy link
Collaborator Author

@akorchyn akorchyn Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't really want ro pull some extra dep for that

Ideally is to remove as much deps as possible, and keep it as slim as possible

Copy link

@Veetaha Veetaha Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were to use bon builder for this, you could alternatively do it this way without the overwritable feature:

// A `#[derive(bon::Builder)]` here isn't required, but it's not prohibited!
// If you want to have both `Network::builder()` for regular code and
// `Network::testnet()` for test code, then both can coexist just fine
struct Network {
    name: String,
    rpc_stuff: String,
}

// Define a builder using the method syntax.
// There can be as many builders with as many different defaults as you need
// for different use cases.
#[bon::bon]
impl Network {
    #[builder]
    pub fn testnet(
        #[builder(default = "testnet".into())]
        name: String,
        #[builder(default = "my_fast_near_rpc".into())]
        rpc_stuff: String,
    ) -> Self {
        Self { name, rpc_stuff }
    }
}

// Both of these work as expected
Network::testnet().build();
Network::testnet().rpc_stuff("very_slow_another_rpc".to_owned()).build();

// If you were to add `#[derive(bon::Builder)]`, you'd also have this one.
// Here you'd probably make both `name` and `rpc_stuff` required:
// Network::builder().name("foo".into()).rpc_stuff("bar".into()).build();

cc @dj8yfo

Copy link
Collaborator

@dj8yfo dj8yfo Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akorchyn well, this dependency has a useful function, it will be pulled in exactly for that and nothing more, it's really convenient to configure options with this stuff, i've tried. Besides Veetaha just showed how to do this with stable features of bon. Anyway, this was just a suggestion how stuff could be improved, not how it must be improved imo.
After having near-*-0.28 in, bon wouldn't be too much of additional weight, cannot compile near-workspaces tests --workspace with default number of builders, only small number -j2 works, othewise it hangs and is later killed with oom.

staking_pools_factory_account_id: None,
}
}
}

#[derive(Debug)]
/// Represents the possible outcomes of a retryable operation.
pub enum RetryResponse<R, E> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type doesn't really bubble up to be eventually visible in documentation, does it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I just not really sure if it's needed to be public. But I kept documentation just in case I will decide to make it public

///
/// # Arguments
/// * `network` - The network configuration to use for the retryable operation.
/// * `task` - The task to retry.
pub async fn retry<R, E, T, F>(network: NetworkConfig, mut task: F) -> Result<R, RetryError<E>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, it doesn't really show up in documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants