forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 12
Adding leader ban registry module in supra framework #290
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
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9116d2e
WIP: adding leader ban registry module in supra framework
dhaval-supraoracles 072f796
WIP: Resolve comments and added further implementation
dhaval-supraoracles 22e17c5
resolved comments
dhaval-supraoracles 4772550
Fixed compilation issue
dhaval-supraoracles aae7d9d
resolved comments
dhaval-supraoracles 9aea222
Minor formatiing
dhaval-supraoracles 1066535
added config tests
dhaval-supraoracles 4872aa3
resolved comments
dhaval-supraoracles 76b4a45
resolved comments
dhaval-supraoracles db6d394
added few more cases
dhaval-supraoracles 85b77a9
Merge branch 'dev' of github.com:Entropy-Foundation/aptos-core into f…
dhaval-supraoracles 648c630
resolved comments + formatted with movefmt
dhaval-supraoracles 023e496
updated with probation period
dhaval-supraoracles ebc737f
added docs from comments and removed reinstate proposer
dhaval-supraoracles 1311242
updated the flow for expiration of bans
dhaval-supraoracles a9e655a
Merge branch 'dev' into feat/leader_ban_registry
dhaval-supraoracles 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
124 changes: 124 additions & 0 deletions
124
aptos-move/framework/supra-framework/sources/configs/leader_ban_registry_config.move
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,124 @@ | ||
| /// Provides the config related to leader ban registry | ||
| module supra_framework::leader_ban_registry_config { | ||
| use std::error; | ||
| use std::vector; | ||
| use supra_std::decode_bcs; | ||
| use supra_framework::config_buffer; | ||
| use supra_framework::system_addresses; | ||
|
|
||
| friend supra_framework::genesis; | ||
| friend supra_framework::reconfiguration_with_dkg; | ||
|
|
||
| /// The provided on chain config bytes are empty or invalid | ||
| const EINVALID_CONFIG: u64 = 1; | ||
| /// The provided on chain config version should be equal or greater than existing | ||
| const EINVALID_VERSION: u64 = 2; | ||
| /// Decoding from version bytes failed | ||
| const EINVALID_VERSION_BYTES: u64 = 3; | ||
|
|
||
|
|
||
| struct BanRegistryParameters has drop, key, store { | ||
| config: vector<u8>, | ||
| version: u8 | ||
| } | ||
|
|
||
| struct BanRegistryParametersV0 has drop { | ||
| initial_elections_denied: u8, | ||
| max_elections_denied: u32, | ||
| minimum_unbanned_proposers: u8 | ||
| } | ||
|
|
||
| /// Publishes the BanRegistryParameters config. | ||
| public(friend) fun initialize(supra_framework: &signer, config: vector<u8>) { | ||
| system_addresses::assert_supra_framework(supra_framework); | ||
| assert!(vector::length(&config) != 0, error::invalid_argument(EINVALID_CONFIG)); | ||
| // we always init with version 0 | ||
| move_to(supra_framework, BanRegistryParameters { config, version: 0 }); | ||
| } | ||
|
|
||
| /// This can be called by on-chain governance to update on-chain configs for the next epoch. | ||
| /// Example usage: | ||
| /// ``` | ||
| /// supra_framework::leader_ban_registry_config::set_for_next_epoch(&framework_signer, some_config_bytes, version); | ||
| /// supra_framework::supra_governance::reconfigure(&framework_signer); | ||
| /// ``` | ||
| public fun set_for_next_epoch(account: &signer, config: vector<u8>, version: u8) acquires BanRegistryParameters { | ||
| system_addresses::assert_supra_framework(account); | ||
| assert!(vector::length(&config) != 0, error::invalid_argument(EINVALID_CONFIG)); | ||
| if (exists<BanRegistryParameters>(@supra_framework)) { | ||
| let ban_registry_params = borrow_global<BanRegistryParameters>(@supra_framework); | ||
| assert!(version >= ban_registry_params.version, error::invalid_argument(EINVALID_VERSION)); | ||
| }; | ||
| std::config_buffer::upsert<BanRegistryParameters>(BanRegistryParameters {config, version}); | ||
| } | ||
|
|
||
| /// Only used in reconfigurations to apply the pending `BanRegistryParameters`, if there is any. | ||
| public(friend) fun on_new_epoch(framework: &signer) acquires BanRegistryParameters { | ||
| system_addresses::assert_supra_framework(framework); | ||
| if (config_buffer::does_exist<BanRegistryParameters>()) { | ||
| let new_config = config_buffer::extract<BanRegistryParameters>(); | ||
| if (exists<BanRegistryParameters>(@supra_framework)) { | ||
| *borrow_global_mut<BanRegistryParameters>(@supra_framework) = new_config; | ||
| } else { | ||
| move_to(framework, new_config); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// Provide initial election denied value efficiently | ||
| /// by decdoing correct version and if not set then 0 | ||
| public fun get_initial_election_denied(): u8 acquires BanRegistryParameters { | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| if (!exists::<BanRegistryParameters>(@supra_framework)) { | ||
| let ban_registry_config = borrow_global<BanRegistryParameters>(@supra_framework); | ||
| if (ban_registry_config.version == 0) { | ||
| let ban_registry_params = deserialise_v1_param(ban_registry_config.config); | ||
| return ban_registry_params.initial_elections_denied; | ||
| } | ||
| }; | ||
| 0 | ||
| } | ||
|
|
||
| /// Provide max electoion denied value efficiently | ||
| /// by decdoing correct version and if not set then 0 | ||
| public fun get_max_elections_denied(): u32 acquires BanRegistryParameters { | ||
| if (!exists::<BanRegistryParameters>(@supra_framework)) { | ||
| let ban_registry_config = borrow_global<BanRegistryParameters>(@supra_framework); | ||
| if (ban_registry_config.version == 0) { | ||
| let ban_registry_params = deserialise_v1_param(ban_registry_config.config); | ||
| return ban_registry_params.max_elections_denied; | ||
| } | ||
| }; | ||
| 0 | ||
| } | ||
|
|
||
| /// Provide max electoion denied value efficiently | ||
| /// by decdoing correct version and if not set then 0 | ||
| public fun get_minimum_unbanned_proposers(): u8 acquires BanRegistryParameters { | ||
| if (!exists::<BanRegistryParameters>(@supra_framework)) { | ||
| let ban_registry_config = borrow_global<BanRegistryParameters>(@supra_framework); | ||
| if (ban_registry_config.version == 0) { | ||
| let ban_registry_params = deserialise_v1_param(ban_registry_config.config); | ||
| return ban_registry_params.minimum_unbanned_proposers; | ||
| } | ||
| }; | ||
| 0 | ||
| } | ||
|
|
||
| /// Decoding bytes to `BanRegistryParametersV0` using bcs | ||
| fun deserialise_v1_param(bytes: vector<u8>): BanRegistryParametersV0 { | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| let bcs_bytes = decode_bcs::new(bytes); | ||
| let initial_elections_denied: u8 = decode_bcs::peel_u8(&mut bcs_bytes); | ||
| let max_elections_denied: u32 = decode_bcs::peel_u32(&mut bcs_bytes); | ||
| let minimum_unbanned_proposers: u8 = decode_bcs::peel_u8(&mut bcs_bytes); | ||
| // making sure not bytes left to decode means correct parameter version | ||
| assert!( | ||
| vector::length(&decode_bcs::into_remainder_bytes(bcs_bytes)) == 0, | ||
| error::out_of_range(EINVALID_VERSION_BYTES) | ||
| ); | ||
| BanRegistryParametersV0 { | ||
| initial_elections_denied, | ||
| max_elections_denied, | ||
| minimum_unbanned_proposers | ||
| } | ||
| } | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
aptos-move/framework/supra-framework/sources/leader_ban_registry.move
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,101 @@ | ||
| /// Maintains the list of banned validators and updates counters on every epoch | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| module supra_framework::leader_ban_registry { | ||
| use std::error; | ||
| use supra_std::enumerable_map; | ||
| use supra_framework::system_addresses; | ||
|
|
||
| /// Leader ban registry already initialized | ||
| const EBAN_REGISTRY_ALREADY_EXISTS: u64 = 1; | ||
| /// Leader ban registry not initialized | ||
| const EBAN_REGISTRY_NOT_INITIALIZED: u64 = 2; | ||
| /// Validator already exists in registry | ||
| const EVALIDATOR_ALREADY_EXISTS: u64 = 3; | ||
| /// Validator not exists in registry | ||
| const EVALIDATOR_DOESNT_EXISTS: u64 = 4; | ||
|
|
||
| /// Holds metrics for banned round, epoch and round server in prev epoch | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| struct ActiveBan has store, drop { | ||
| epoch_earned: u64, // EPOCH | ||
| round_earned: u64, // ROUND | ||
| rounds_served_in_previous_epochs: u64 // ROUND | ||
| } | ||
|
|
||
| /// Holds validator metrics regarding duration pool address etc | ||
| struct ValidatorBansWithAddress has store, drop { | ||
| active: ActiveBan, | ||
| consecutive_bans: u32, | ||
| pool_address: address, | ||
| } | ||
|
|
||
| /// Holds ban registry | ||
| struct BanRegistry has drop, store, key { | ||
|
dhaval-supraoracles marked this conversation as resolved.
|
||
| bans: enumerable_map::EnumerableMap<address, ValidatorBansWithAddress> | ||
| } | ||
|
|
||
| /// Initialise leader ban registry | ||
| public(friend) fun initialize_leader_ban_registry(supra_framework: &signer) { | ||
| system_addresses::assert_supra_framework(supra_framework); | ||
| assert!( | ||
| !exists<BanRegistry>(@supra_framework), | ||
| error::already_exists(EBAN_REGISTRY_ALREADY_EXISTS) | ||
| ); | ||
| move_to(supra_framework, BanRegistry { bans: enumerable_map::new_map() }); | ||
| } | ||
|
|
||
| /// Adding an entry for new validator | ||
| public entry fun add_new_ban_entry( | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| supra_framework: &signer, validator_address: address, pool_address: address, epoch_earned: u64, round_earned: u64 | ||
| ) | ||
| acquires BanRegistry | ||
| { | ||
| // TODO: epoch can be retrived from reconfigure | ||
| // TODO: what about round | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| // TODO: Validator address and index can be fetched pool address see stake.move | ||
|
|
||
| system_addresses::assert_supra_framework(supra_framework); | ||
| assert_registry_initialized(); | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| let ban_registry = borrow_global_mut<BanRegistry>(@supra_framework); | ||
|
|
||
| if (enumerable_map::contains<address, ValidatorBansWithAddress>(&ban_registry.bans, validator_address)) { | ||
| abort error::already_exists(EVALIDATOR_ALREADY_EXISTS) | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| enumerable_map::add_value(&mut ban_registry.bans, validator_address, ValidatorBansWithAddress { | ||
| active: ActiveBan { | ||
| epoch_earned, | ||
| round_earned, | ||
| rounds_served_in_previous_epochs: 0 | ||
| }, | ||
| consecutive_bans:0, | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| pool_address | ||
| }) | ||
| } | ||
|
|
||
| /// Remvoing an entry of validator from registry | ||
| public entry fun remove_ban_entry(supra_framework: &signer, validator_address: address) | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| acquires BanRegistry | ||
| { | ||
| system_addresses::assert_supra_framework(supra_framework); | ||
| assert_registry_initialized(); | ||
| let ban_registry = borrow_global_mut<BanRegistry>(@supra_framework); | ||
|
|
||
| if (!enumerable_map::contains<address, ValidatorBansWithAddress>(&ban_registry.bans, validator_address)) { | ||
| abort error::not_found(EVALIDATOR_DOESNT_EXISTS) | ||
| }; | ||
|
|
||
| enumerable_map::remove_value(&mut ban_registry.bans, validator_address) | ||
| } | ||
|
|
||
| /// Update countes on every epoch | ||
| public(friend) fun on_new_epoch() { | ||
| /// TODO: What are the use cases configuration here? | ||
|
dhaval-supraoracles marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// Validates registry initialised if not aborted with `EBAN_REGISTRY_NOT_INITIALIZED` | ||
| fun assert_registry_initialized() { | ||
| assert!( | ||
| exists<BanRegistry>(@supra_framework), | ||
| error::invalid_state(EBAN_REGISTRY_NOT_INITIALIZED) | ||
| ); | ||
| } | ||
| } | ||
2 changes: 2 additions & 0 deletions
2
aptos-move/framework/supra-framework/tests/test_leader_ban_registry.move
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,2 @@ | ||
| #[test_only] | ||
| module std::test_leader_ban_registry {} |
Oops, something went wrong.
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.