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

Expose voter state #114

Merged
merged 31 commits into from
Apr 30, 2020
Merged

Conversation

octol
Copy link
Contributor

@octol octol commented Mar 24, 2020

Implement #55 and support paritytech/substrate#4921 / paritytech/substrate#5375

Expose voter state to the outside world. Useful for querying diagnostics

Left to do:

@parity-cla-bot
Copy link

It looks like @octol hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement at the following URL: https://cla.parity.io

Once you've signed, please reply to this thread with [clabot:check] to prove it.

Many thanks,

Parity Technologies CLA Bot

@octol
Copy link
Contributor Author

octol commented Mar 24, 2020

It looks like @octol hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement at the following URL: https://cla.parity.io

Once you've signed, please reply to this thread with [clabot:check] to prove it.

Many thanks,

Parity Technologies CLA Bot

[clabot:check]

@parity-cla-bot
Copy link

It looks like @octol signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@octol octol force-pushed the jon/issue-55-expose-voter-state branch from 2fe5d65 to e8b45e5 Compare April 15, 2020 09:53
@octol octol marked this pull request as ready for review April 17, 2020 08:27
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

overall lgtm. could you try adding a test where we spin up a set of voters and check the voter state afterwards? have a look at this test: https://github.com/paritytech/finality-grandpa/blob/master/src/voter/mod.rs#L948

@@ -286,6 +290,13 @@ impl<H, N, E: Environment<H, N>> PastRounds<H, N, E> where
}
}

pub(super) fn voting_rounds(&self) -> impl Iterator<Item = &VotingRound<H, N, E>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

needs docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree!

src/voter/mod.rs Outdated
@@ -420,6 +423,76 @@ fn instantiate_last_round<H, N, E: Environment<H, N>>(
}
}

pub trait VoterState<Id> {
Copy link
Contributor

Choose a reason for hiding this comment

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

These and the types below need docs :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

src/testing.rs Outdated
@@ -198,11 +199,19 @@ pub mod environment {
}

impl crate::voter::Environment<&'static str, u32> for Environment {
type Timer = BoxFuture<'static, Result<(),Error>>;
type Timer = Pin<Box<dyn Future<Output = Result<(), Error>> + Unpin + Send + Sync>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I need to double check why we need to pin this and the stream/sink below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in latest

@octol
Copy link
Contributor Author

octol commented Apr 22, 2020

I updated the tests so that now the check for background_rounds should be reliable

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

lgtm. @rphmeier do you want to give this a look?

src/voter/mod.rs Outdated
Comment on lines 819 to 820
inner.best_round.round_number(),
inner.best_round.round_number() + 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
inner.best_round.round_number(),
inner.best_round.round_number() + 1,
inner.best_round.round_number(),
inner.best_round.round_number() + 1,

src/voter/mod.rs Show resolved Hide resolved
src/voter/mod.rs Show resolved Hide resolved
src/voter/mod.rs Outdated
#[cfg_attr(test, derive(Debug))]
pub struct RoundState<Id: Eq + std::hash::Hash> {
pub total_weight: VoterWeight,
pub threshold_weight: VoterWeight,
Copy link
Contributor

@rphmeier rphmeier Apr 24, 2020

Choose a reason for hiding this comment

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

missing docs on these fields, VoterState fields, and the mod report, and fn voter_state. There are even more missing docs - remember the guideline that all public items should be documented.

(yes, I know it's a PITA but it makes the code much more maintainable and readable)

src/voter/mod.rs Outdated
}
}

pub struct Inner<H, N, E> where
Copy link
Contributor

@rphmeier rphmeier Apr 24, 2020

Choose a reason for hiding this comment

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

Exposing a struct Inner from voter.rs seems a little ambiguous. Likewise, implementation on Arc<RwLock<...>> directly seems difficult from a backwards-compatibility perspective. Could you elaborate on the goal of the trait VoterState rather than having a simple struct?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's used in struct Voter as well, but it's called inner although there are other fields. This would not have to be exposed as public with a wrapper struct, meaning it could gain a more descriptive name than Inner.

Copy link
Contributor

Choose a reason for hiding this comment

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

The first approach Jon tried used a struct, the problem was that this struct had to be typed on H, N, E, and we'd have to propagate these types throughout the substrate codebase, it was especially hairy because types like Environment are currently not explicitly typed anywhere but would have to be due to the RPC initialization process. In substrate this trait is used as a trait object to avoid the type proliferation.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I assumed something like that. Still, the API could be improved substantially with a wrapping struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback @rphmeier :)
I think we can actually remove the pub from Inner if I'm not mistaken? I'll add the missing docs

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO I think it's better to keep the struct anyway (although not exported) to be able to lock inner as a whole for correctness (rather than best and background distinctly).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the docs and removed the pub from Inner.

OK, I assumed something like that. Still, the API could be improved substantially with a wrapping struct.

Sorry, do you mean that the API could have been improved if using a simple struct instead of the trait would have been practical? Or that the API could be improved by creating a struct wrapping Arc<RwLock<...>> just so that we don't have to impl directly on Arc<RwLock<...>>?

I started sketching on the latter but I got stuck on finding a good name ... :/
Probably VoterState would be appropriate for such a struct, but then the trait would need to be renamed ... VoterStateQueryable? Doesn't roll of the tongue very well ...

Copy link
Contributor

Choose a reason for hiding this comment

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

struct SharedVoterState<...>(Arc<RwLock<Inner<...>>>); and then you implement VoterState for SharedVoterState.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the SharedVoterState to wrap Arc<RwLock<Inner>>

Copy link
Contributor

@rphmeier rphmeier left a comment

Choose a reason for hiding this comment

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

Looks fine except for grumbles

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

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

thanks for adding all the missing docs to this crate, just some last minor nits.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/voter/voting_round.rs Outdated Show resolved Hide resolved
src/voter/mod.rs Outdated Show resolved Hide resolved
src/voter/mod.rs Outdated Show resolved Hide resolved
src/voter/mod.rs Outdated Show resolved Hide resolved
src/voter/mod.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@andresilva andresilva merged commit d4351d1 into paritytech:master Apr 30, 2020
andresilva added a commit that referenced this pull request Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants