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

feat: follower connection #157

Merged
merged 4 commits into from Feb 26, 2024
Merged

feat: follower connection #157

merged 4 commits into from Feb 26, 2024

Conversation

cong-or
Copy link
Contributor

@cong-or cong-or commented Feb 23, 2024

Currently if the follower cannot find the mithril snapshot, it just returns an error Error: MithrilSnapshot

However, I was hoping we could abstract this failure and return a follower regardless i.e if it cant bootstrap from the snapshot it should return a follower which is consuming from a network.

Maybe i'm wrong and there's an easier cleaner way, but I had to do the following to mimic the behavior described above on the consumer side.

The PR requires the FollowerConfig is cloneable for this code to work because of the move.

We can just use the below code for now (if there is no other way) and add the suggestions to the follower at a later date or address it soon on the follower side of things.

I could be way off and missing something. Please let me know.

/// In the context of setting up the follower connection.
/// If there is metadata present which allows us to bootstrap from a point in time
/// We start from there, if not; we start from genesis.
async fn follower_connection(
    start_from: (Option<SlotNumber>, Option<BlockHash>), snapshot: &str, network: Network,
    relay: &str,
) -> Result<Follower, Box<dyn Error>> {
    let mut follower_cfg = if start_from.0.is_none() || start_from.1.is_none() {
        // start from genesis, no previous followers, hence no starting points.
        FollowerConfigBuilder::default()
            .mithril_snapshot_path(PathBuf::from(snapshot))
            .build()
    } else {
        // start from given point
        FollowerConfigBuilder::default()
            .follow_from(Point::new(
                start_from.0.ok_or("Slot number not present")?.try_into()?,
                hex::decode(start_from.1.ok_or("Block Hash not present")?)?,
            ))
            .mithril_snapshot_path(PathBuf::from(snapshot))
            .build()
    };

    let follower = match Follower::connect(&relay, network, follower_cfg).await {
        Ok(follower) => follower,
        Err(err) => {
            error!(
                "Unable to bootstrap via mithril snapshot {}. Trying network..",
                err
            );
           
            // We know bootstrapping from the snapshot fails, remove path for it to try from network
            follower_cfg.mithril_snapshot_path = None;
            Follower::connect(&relay, network, follower_cfg).await?
        },
    };

    Ok(follower)
}

@cong-or cong-or changed the title Follower connection feat: follower connection Feb 23, 2024
@minikin minikin added the review me PR is ready for review label Feb 26, 2024
@minikin minikin added this to the M2: Hermes Foundations milestone Feb 26, 2024
Copy link
Contributor

@FelipeRosa FelipeRosa left a comment

Choose a reason for hiding this comment

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

LGTM

@minikin minikin merged commit 5b61a71 into main Feb 26, 2024
21 of 22 checks passed
@minikin minikin deleted the follower-cfg branch February 26, 2024 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review me PR is ready for review
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants