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

[Sync] Sync task #881

Closed
Voxelot opened this issue Jan 5, 2023 · 0 comments · Fixed by #889
Closed

[Sync] Sync task #881

Voxelot opened this issue Jan 5, 2023 · 0 comments · Fixed by #889
Assignees

Comments

@Voxelot
Copy link
Member

Voxelot commented Jan 5, 2023

The sync task receives information about the block height of peers via a stream from P2P. It primarily filters out previously seen block heights and then updates a mutexed shared state with the import task. It will never generate back pressure because it can always update the state and notify import without awaiting.

flowchart LR
	H{Heatbeat} --> |BlockHeight|S
	S --> |height > best_height|F[stream::filter ]
	F -.->|Update| ST{{State}}
	F -.->|Notify| Import{Import}
Loading

Shared State:

/// State that is shared between the sync and import tasks
struct State {
	/// The highest seen height so far.
  best_seen_height: Option<Height>,
	/// The range of heights that are currently being imported.
  in_flight_heights: Option<Range<Height>>,
}

// Empty state means we have not observed any heights and
// we have not imported any headers.
State {
	best_seen_height: None,
	in_flight_height: None,
}

// There is a new height that has been observed but not imported.
State {
	best_seen_height: Some(5),
	in_flight_height: None,
}

// Currently importing headers 0..=5.
State {
	best_seen_height: Some(5),
	in_flight_height: Some(0..=5),
}

// Have imported headers 0..=3 but still need to import 4..=5.
State {
	best_seen_height: Some(5),
	in_flight_height: Some(3..=5),
}

// A newer height has been seen while the stream is importing headers.
State {
	best_seen_height: Some(7),
	in_flight_height: Some(4..=5),
}

// All caught up with the current best seen.
State {
	best_seen_height: Some(7),
	in_flight_height: Some(7..=7),
}

Ports

trait P2P {
    /// Get a stream of block heights reported by peers
    async fn block_heights(&self) -> BoxStream<BlockHeight>;
}
@Voxelot Voxelot mentioned this issue Jan 5, 2023
2 tasks
@Voxelot Voxelot linked a pull request Jan 16, 2023 that will close this issue
@freesig freesig mentioned this issue Jan 17, 2023
freesig added a commit that referenced this issue Jan 19, 2023
- Closes #881 
- Closes #882 
- Closes #880

Co-authored-by: green <xgreenx9999@gmail.com>
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 a pull request may close this issue.

2 participants