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

Update RelayEncoder to be 9430 compatible #2376

Merged
merged 17 commits into from
Jul 13, 2023
101 changes: 45 additions & 56 deletions precompiles/relay-encoder/RelayEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ RelayEncoder constant RELAY_ENCODER_CONTRACT = RelayEncoder(
/// @custom:address 0x0000000000000000000000000000000000000805
interface RelayEncoder {
/// @dev Encode 'bond' relay call
/// @custom:selector a82948d4
/// @param controllerAddress: Address of the controller
/// @custom:selector 72a9fbc6
/// @param amount: The amount to bond
/// @param rewardDestination: the account that should receive the reward
/// @return result The bytes associated with the encoded call
function encodeBond(
uint256 controllerAddress,
uint256 amount,
bytes memory rewardDestination
) external pure returns (bytes memory result);
Expand All @@ -32,47 +30,43 @@ interface RelayEncoder {
/// @custom:selector 813667a0
/// @param amount: The extra amount to bond
/// @return result The bytes associated with the encoded call
function encodeBondExtra(uint256 amount)
external
pure
returns (bytes memory result);
function encodeBondExtra(
uint256 amount
) external pure returns (bytes memory result);

/// @dev Encode 'unbond' relay call
/// @custom:selector 51b14e57
/// @param amount The amount to unbond
/// @return result The bytes associated with the encoded call
function encodeUnbond(uint256 amount)
external
pure
returns (bytes memory result);
function encodeUnbond(
uint256 amount
) external pure returns (bytes memory result);

/// @dev Encode 'withdrawUnbonded' relay call
/// @custom:selector d5ad108e
/// @param slashes Weight hint, number of slashing spans
/// @return result The bytes associated with the encoded call
function encodeWithdrawUnbonded(uint32 slashes)
external
pure
returns (bytes memory result);
function encodeWithdrawUnbonded(
uint32 slashes
) external pure returns (bytes memory result);

/// @dev Encode 'validate' relay call
/// @custom:selector bb64ca0c
/// @param comission: Comission of the validator as partsPerBillion
/// @param commission: Commission of the validator as partsPerBillion
/// @param blocked: Whether or not the validator is accepting more nominations
/// @return result The bytes associated with the encoded call
function encodeValidate(uint256 comission, bool blocked)
external
pure
returns (bytes memory result);
function encodeValidate(
uint256 commission,
bool blocked
) external pure returns (bytes memory result);

/// @dev Encode 'nominate' relay call
/// @custom:selector d2ea7b08
/// @param nominees: An array of AccountIds corresponding to the accounts we will nominate
/// @return result The bytes associated with the encoded call
function encodeNominate(uint256[] memory nominees)
external
pure
returns (bytes memory result);
function encodeNominate(
uint256[] memory nominees
fgamundi marked this conversation as resolved.
Show resolved Hide resolved
) external pure returns (bytes memory result);

/// @dev Encode 'chill' relay call
/// @custom:selector b5eaac43
Expand All @@ -83,64 +77,59 @@ interface RelayEncoder {
/// @custom:selector 414be337
/// @param rewardDestination: the account that should receive the reward
/// @return result The bytes associated with the encoded call
function encodeSetPayee(bytes memory rewardDestination)
external
pure
returns (bytes memory result);
function encodeSetPayee(
bytes memory rewardDestination
Copy link
Collaborator

Choose a reason for hiding this comment

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

this would better be bytes32 I think as well, what do you think? now that we are breaking the API..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could update it, but I feel that having it static size makes it harder to manually test it. We would also need to change encodeBond to match rewardDestination as bytes32. If we decide to do it, maybe we could in another PR

) external pure returns (bytes memory result);

/// @dev Encode 'setController' relay call
/// @custom:selector 07f7c6dc
/// @param controller: The controller address
/// @custom:selector 15490616
/// @return result The bytes associated with the encoded call
function encodeSetController(uint256 controller)
external
pure
returns (bytes memory result);
function encodeSetController() external pure returns (bytes memory result);

/// @dev Encode 'rebond' relay call
/// @custom:selector 0922ee17
/// @param amount: The amount to rebond
/// @return result The bytes associated with the encoded call
function encodeRebond(uint256 amount)
external
pure
returns (bytes memory result);
function encodeRebond(
uint256 amount
) external pure returns (bytes memory result);

/// @dev Encode 'hrmp.init_open_channel' relay call
/// @custom:selector e5e20a64
/// @param recipient: The paraId to whom we want to initiate the open channel
/// @param maxCapacity: The maximum capacity for the channel
/// @param maxMessageSize: The maximum message size for the channel
/// @return result The bytes associated with the encoded call
function encodeHrmpInitOpenChannel(uint32 recipient, uint32 maxCapacity, uint32 maxMessageSize)
external
pure
returns (bytes memory result);

function encodeHrmpInitOpenChannel(
uint32 recipient,
uint32 maxCapacity,
uint32 maxMessageSize
) external pure returns (bytes memory result);

/// @dev Encode 'hrmp.accept_open_channel' relay call
/// @custom:selector 98a76477
/// @param sender: The paraId from which we want to accept the channel
function encodeHrmpAcceptOpenChannel(uint32 sender)
external
pure
returns (bytes memory result);
function encodeHrmpAcceptOpenChannel(
uint32 sender
) external pure returns (bytes memory result);

/// @dev Encode 'hrmp.close_channel' relay call
/// @custom:selector 9cfbdfc5
/// @param sender: The paraId of the sender
/// @param sender: The paraId of the recipient
function encodeHrmpCloseChannel(uint32 sender, uint32 recipient)
external
pure
returns (bytes memory result);
function encodeHrmpCloseChannel(
uint32 sender,
uint32 recipient
) external pure returns (bytes memory result);

/// @dev Encode 'hrmp.cancel_open_request' relay call
/// @custom:selector 8fd5ce49
/// @param sender: The paraId of the sender
/// @param recipient: The paraId of the recipient
/// @param openRequests: The number of open requests
function encodeHrmpCancelOpenRequest(uint32 sender, uint32 recipient, uint32 openRequests)
external
pure
returns (bytes memory result);
function encodeHrmpCancelOpenRequest(
uint32 sender,
uint32 recipient,
uint32 openRequests
) external pure returns (bytes memory result);
}
42 changes: 15 additions & 27 deletions precompiles/relay-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ mod tests;

pub enum AvailableStakeCalls {
Bond(
relay_chain::AccountId,
relay_chain::Balance,
pallet_staking::RewardDestination<relay_chain::AccountId>,
),
Expand All @@ -55,7 +54,7 @@ pub enum AvailableStakeCalls {
Nominate(Vec<relay_chain::AccountId>),
Chill,
SetPayee(pallet_staking::RewardDestination<relay_chain::AccountId>),
SetController(relay_chain::AccountId),
SetController,
Rebond(relay_chain::Balance),
}

Expand All @@ -79,30 +78,25 @@ where
Runtime: pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
#[precompile::public("encodeBond(uint256,uint256,bytes)")]
#[precompile::public("encode_bond(uint256,uint256,bytes)")]
#[precompile::public("encodeBond(uint256,bytes)")]
#[precompile::public("encode_bond(uint256,bytes)")]
#[precompile::view]
fn encode_bond(
handle: &mut impl PrecompileHandle,
controller_address: U256,
amount: U256,
reward_destination: RewardDestinationWrapper,
) -> EvmResult<UnboundedBytes> {
// No DB access but lot of logical stuff
// To prevent spam, we charge an arbitrary amount of gas
handle.record_cost(1000)?;

let address: [u8; 32] = controller_address.into();
let relay_amount = u256_to_relay_amount(amount)?;
let reward_destination = reward_destination.into();

let encoded = RelayRuntime::encode_call(AvailableStakeCalls::Bond(
address.into(),
relay_amount,
reward_destination,
))
.as_slice()
.into();
let encoded =
RelayRuntime::encode_call(AvailableStakeCalls::Bond(relay_amount, reward_destination))
.as_slice()
.into();

Ok(encoded)
}
Expand Down Expand Up @@ -169,14 +163,14 @@ where
#[precompile::view]
fn encode_validate(
handle: &mut impl PrecompileHandle,
comission: Convert<U256, u32>,
commission: Convert<U256, u32>,
blocked: bool,
) -> EvmResult<UnboundedBytes> {
// No DB access but lot of logical stuff
// To prevent spam, we charge an arbitrary amount of gas
handle.record_cost(1000)?;

let fraction = Perbill::from_parts(comission.converted());
let fraction = Perbill::from_parts(commission.converted());
let encoded = RelayRuntime::encode_call(AvailableStakeCalls::Validate(
pallet_staking::ValidatorPrefs {
commission: fraction,
Expand Down Expand Up @@ -250,23 +244,17 @@ where
Ok(encoded)
}

#[precompile::public("encodeSetController(uint256)")]
#[precompile::public("encode_set_controller(uint256)")]
#[precompile::public("encodeSetController()")]
#[precompile::public("encode_set_controller()")]
#[precompile::view]
fn encode_set_controller(
handle: &mut impl PrecompileHandle,
controller: U256,
) -> EvmResult<UnboundedBytes> {
fn encode_set_controller(handle: &mut impl PrecompileHandle) -> EvmResult<UnboundedBytes> {
// No DB access but lot of logical stuff
// To prevent spam, we charge an arbitrary amount of gas
handle.record_cost(1000)?;

let controller: [u8; 32] = controller.into();

let encoded =
RelayRuntime::encode_call(AvailableStakeCalls::SetController(controller.into()))
.as_slice()
.into();
let encoded = RelayRuntime::encode_call(AvailableStakeCalls::SetController)
.as_slice()
.into();

Ok(encoded)
}
Expand Down
11 changes: 4 additions & 7 deletions precompiles/relay-encoder/src/test_relay_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub enum StakeCall {
#[codec(index = 0u16)]
// the index should match the position of the dispatchable in the target pallet
Bond(
<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source,
#[codec(compact)] cumulus_primitives_core::relay_chain::Balance,
pallet_staking::RewardDestination<AccountId32>,
),
Expand All @@ -54,7 +53,7 @@ pub enum StakeCall {
#[codec(index = 7u16)]
SetPayee(pallet_staking::RewardDestination<AccountId32>),
#[codec(index = 8u16)]
SetController(<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source),
SetController,
#[codec(index = 19u16)]
Rebond(#[codec(compact)] cumulus_primitives_core::relay_chain::Balance),
}
Expand All @@ -77,9 +76,7 @@ pub struct TestEncoder;
impl StakeEncodeCall for TestEncoder {
fn encode_call(call: AvailableStakeCalls) -> Vec<u8> {
match call {
AvailableStakeCalls::Bond(a, b, c) => {
RelayCall::Stake(StakeCall::Bond(a.into(), b, c)).encode()
}
AvailableStakeCalls::Bond(b, c) => RelayCall::Stake(StakeCall::Bond(b, c)).encode(),

AvailableStakeCalls::BondExtra(a) => RelayCall::Stake(StakeCall::BondExtra(a)).encode(),

Expand All @@ -97,8 +94,8 @@ impl StakeEncodeCall for TestEncoder {
RelayCall::Stake(StakeCall::SetPayee(a.into())).encode()
}

AvailableStakeCalls::SetController(a) => {
RelayCall::Stake(StakeCall::SetController(a.into())).encode()
AvailableStakeCalls::SetController => {
RelayCall::Stake(StakeCall::SetController).encode()
}

AvailableStakeCalls::Rebond(a) => {
Expand Down
15 changes: 3 additions & 12 deletions precompiles/relay-encoder/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fn test_encode_bond() {
Alice,
Precompile1,
PCall::encode_bond {
controller_address: [1u8; 32].into(),
amount: 100.into(),
reward_destination: RewardDestinationWrapper(RewardDestination::Controller),
},
Expand All @@ -99,7 +98,6 @@ fn test_encode_bond() {
.expect_no_logs()
.execute_returns(UnboundedBytes::from(
TestEncoder::encode_call(AvailableStakeCalls::Bond(
[1u8; 32].into(),
100u32.into(),
RewardDestination::Controller,
))
Expand Down Expand Up @@ -198,18 +196,11 @@ fn test_encode_set_controller() {
.build()
.execute_with(|| {
precompiles()
.prepare_test(
Alice,
Precompile1,
PCall::encode_set_controller {
controller: [1u8; 32].into(),
},
)
.prepare_test(Alice, Precompile1, PCall::encode_set_controller {})
.expect_cost(1000)
.expect_no_logs()
.execute_returns(UnboundedBytes::from(
TestEncoder::encode_call(AvailableStakeCalls::SetController([1u8; 32].into()))
.as_slice(),
TestEncoder::encode_call(AvailableStakeCalls::SetController).as_slice(),
))
});
}
Expand Down Expand Up @@ -270,7 +261,7 @@ fn test_encode_validate() {
Alice,
Precompile1,
PCall::encode_validate {
comission: 100.into(),
commission: 100.into(),
blocked: true,
},
)
Expand Down
Loading