Skip to content

Commit

Permalink
feat: add and remove members from collective from evm precompile and …
Browse files Browse the repository at this point in the history
…function to get proposed code hash
  • Loading branch information
GabrielMartinezRodriguez committed Jan 22, 2024
1 parent 83afbd5 commit 14e1757
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pallets/upgrade-runtime-proposal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use frame_support::dispatch::UnfilteredDispatchable;
use frame_support::traits::EnsureOrigin;
use frame_system::pallet_prelude::BlockNumberFor;
use frame_system::pallet_prelude::OriginFor;
use frame_support::sp_runtime::traits::Hash;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -173,6 +174,12 @@ pub mod pallet {
<ApplicationBlockNumber<T>>::get()
}

pub fn hash_of_proposed_code() -> Option<T::Hash> {
<ProposedCode<T>>::get().map(|code| {
T::Hashing::hash(&code)
})
}

pub fn set_application_block_number(block_number: T::BlockNumber) {
<ApplicationBlockNumber<T>>::put(block_number);
}
Expand Down
1 change: 1 addition & 0 deletions precompiles/upgrade-runtime-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pallet-evm = { workspace = true, features = [ "forbid-evm-reentrancy" ] }
# Stability
pallet-upgrade-runtime-proposal = { workspace = true }
precompile-utils = { workspace = true }
pallet-collective = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ pragma solidity >=0.8.3;
interface UpgradeRuntimeController {
function setApplicationBlock(uint32 block) external; // onlyOwner
function rejectProposedCode() external; // onlyOwner
function addMemberToTechnicalCommittee(address member) external; // onlyOwner
function removeMemberFromTechnicalCommittee(address member) external; // onlyOwner
function getHashOfProposedCode() external view returns (bytes32);
}
83 changes: 81 additions & 2 deletions precompiles/upgrade-runtime-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use frame_support::parameter_types;
use frame_support::storage::types::{StorageValue, ValueQuery};

use frame_support::traits::StorageInstance;
use frame_support::traits::ChangeMembers;
use frame_support::inherent::Vec;
use precompile_utils::prelude::*;

use sp_core::Get;
Expand Down Expand Up @@ -83,13 +85,14 @@ pub struct UpgradeRuntimeControllerPrecompile<Runtime, DefaultOwner: Get<H160> +
impl<Runtime, DefaultOwner> UpgradeRuntimeControllerPrecompile<Runtime, DefaultOwner>
where
DefaultOwner: Get<H160> + 'static,
Runtime:
pallet_upgrade_runtime_proposal::Config + pallet_timestamp::Config + pallet_evm::Config,
Runtime: pallet_upgrade_runtime_proposal::Config + pallet_collective::Config<pallet_collective::Instance1> + pallet_timestamp::Config + pallet_evm::Config,
Runtime::RuntimeCall: From<pallet_upgrade_runtime_proposal::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<Runtime as pallet_timestamp::Config>::Moment: Into<U256>,
<Runtime as frame_system::Config>::BlockNumber: From<u32>,
<Runtime as frame_system::Config>::Hash: Into<H256>,
<Runtime as frame_system::Config>::AccountId: From<H160>,
{
#[precompile::public("owner()")]
#[precompile::view]
Expand Down Expand Up @@ -221,4 +224,80 @@ where

Ok(())
}

#[precompile::public("addMemberToTechnicalCommittee(address)")]
fn add_member_to_technical_committee(
handle: &mut impl PrecompileHandle,
member: Address,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let member_id: H160 = member.into();

let old_members = pallet_collective::Pallet::<Runtime, pallet_collective::Instance1>::members();

if old_members.contains(&member_id.into()) {
return Err(revert("Already a member"));
}

let mut new_members = old_members
.iter()
.cloned()
.chain(Some(member_id.into()))
.collect::<Vec<_>>();

new_members.sort();

pallet_collective::Pallet::<Runtime, pallet_collective::Instance1>::set_members_sorted(
&new_members,
&old_members
);

Ok(())
}

#[precompile::public("removeMemberFromTechnicalCommittee(address)")]
fn remove_member_to_technical_committee(
handle: &mut impl PrecompileHandle,
member: Address,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let member_id: H160 = member.into();
let member_account = <Runtime as frame_system::Config>::AccountId::from(member_id);

let old_members = pallet_collective::Pallet::<Runtime, pallet_collective::Instance1>::members();

if !old_members.contains(&member_account) {
return Err(revert("Not a member"));
}

let mut new_members = old_members
.iter()
.cloned()
.filter(|m| *m != member_account)
.collect::<Vec<_>>();

new_members.sort();

pallet_collective::Pallet::<Runtime, pallet_collective::Instance1>::set_members_sorted(
&new_members,
&old_members
);

Ok(())
}

#[precompile::public("getHashOfProposedCode()")]
#[precompile::view]
fn get_hash_of_proposed_code(handle: &mut impl PrecompileHandle) -> EvmResult<H256> {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let hash = pallet_upgrade_runtime_proposal::Pallet::<Runtime>::hash_of_proposed_code();

match hash {
Some(hash) => Ok(hash.into()),
None => Ok(H256::default()),
}
}
}

0 comments on commit 14e1757

Please sign in to comment.