-
Notifications
You must be signed in to change notification settings - Fork 268
feat(consensus): Replace static ValidatorSet with dynamic L2 contract-based validator fetching
#3011
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
Merged
Merged
feat(consensus): Replace static ValidatorSet with dynamic L2 contract-based validator fetching
#3011
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ff4516
feat(consensus): add validator fetching from L2
t00ts 32c59de
feat(consensus): allow errors in `ValidatorSetProvider` trait
t00ts 44700d6
feat(consensus): wire dynamic validator set provide
t00ts 53ae0f3
chore: fmt + crate version update on rebase
t00ts 67ddfea
feat(fetch_validators): fetch block header inside the lib
t00ts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| mod consensus_task; | ||
| mod fetch_validators; | ||
| mod p2p_task; | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
crates/pathfinder/src/consensus/inner/fetch_validators.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| use pathfinder_common::{ChainId, ContractAddress}; | ||
| use pathfinder_consensus::{PublicKey, SigningKey, Validator, ValidatorSet}; | ||
| use pathfinder_storage::Storage; | ||
| use rand::rngs::OsRng; | ||
|
|
||
| use crate::config::ConsensusConfig; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct L2ValidatorSetProvider { | ||
| storage: Storage, | ||
| chain_id: ChainId, | ||
| config: ConsensusConfig, | ||
| } | ||
|
|
||
| impl L2ValidatorSetProvider { | ||
| pub fn new(storage: Storage, chain_id: ChainId, config: ConsensusConfig) -> Self { | ||
| Self { | ||
| storage, | ||
| chain_id, | ||
| config, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl pathfinder_consensus::ValidatorSetProvider<ContractAddress> for L2ValidatorSetProvider { | ||
| fn get_validator_set( | ||
| &self, | ||
| height: u64, | ||
| ) -> Result<ValidatorSet<ContractAddress>, anyhow::Error> { | ||
| fetch_validators(&self.storage, self.chain_id, height, &self.config) | ||
| } | ||
| } | ||
|
|
||
| // TODO: | ||
| // | ||
| // Currently, the validator fetching functionality lives in its own crate | ||
| // (validator-fetcher) because we have a temporary internal RPC method that we | ||
| // use for convenient testing. | ||
| // | ||
| // This separation allows us to easily expose and test the functionality through | ||
| // the RPC while the specification for validator fetching is still being | ||
| // finalized. | ||
| // | ||
| // Once we have a final spec, the functionality from the validator-fetcher crate | ||
| // will be migrated into this file and the temporary crate (along with its RPC | ||
| // method) will be removed. | ||
|
|
||
| /// Fetches validators for a given height | ||
| /// | ||
| /// Uses config-based validators if validator addresses are provided in config, | ||
| /// otherwise fetches validators from the contract. | ||
| pub fn fetch_validators( | ||
| storage: &Storage, | ||
| chain_id: ChainId, | ||
| height: u64, | ||
| config: &ConsensusConfig, | ||
| ) -> Result<ValidatorSet<ContractAddress>, anyhow::Error> { | ||
| if config.validator_addresses.is_empty() { | ||
| fetch_validators_from_l2(storage, chain_id, height) | ||
| } else { | ||
| create_validators_from_config(config) | ||
| } | ||
| } | ||
|
|
||
| /// Creates validators from consensus config | ||
| /// | ||
| /// This is the original logic that was in consensus_task.rs. | ||
| /// It creates validators with random keys and equal voting power. | ||
| fn create_validators_from_config( | ||
| config: &ConsensusConfig, | ||
| ) -> Result<ValidatorSet<ContractAddress>, anyhow::Error> { | ||
| let validator_address = config.my_validator_address; | ||
|
|
||
| let validators = std::iter::once(validator_address) | ||
| .chain(config.validator_addresses.clone()) | ||
| .map(|address| { | ||
| let sk = SigningKey::new(OsRng); | ||
| let vk = sk.verification_key(); | ||
| let public_key = PublicKey::from_bytes(vk.to_bytes()); | ||
|
|
||
| Validator { | ||
| address, | ||
| public_key, | ||
| voting_power: 1, | ||
| } | ||
| }) | ||
| .collect::<Vec<Validator<ContractAddress>>>(); | ||
|
|
||
| Ok(ValidatorSet::new(validators)) | ||
| } | ||
|
|
||
| /// Fetches validators from the L2 contract | ||
| /// | ||
| /// This logic is temporary until we have a final spec for validator fetching. | ||
| fn fetch_validators_from_l2( | ||
| storage: &Storage, | ||
| chain_id: ChainId, | ||
| height: u64, | ||
| ) -> Result<ValidatorSet<ContractAddress>, anyhow::Error> { | ||
| let validators = validator_fetcher::get_validators_at_height(storage, chain_id, height)?; | ||
| let validators = validators | ||
| .into_iter() | ||
| .map(|validator| Validator { | ||
| address: validator.address, | ||
| public_key: validator.public_key, | ||
| voting_power: validator.voting_power, | ||
| }) | ||
| .collect::<Vec<Validator<ContractAddress>>>(); | ||
| Ok(ValidatorSet::new(validators)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.