Skip to content

Commit

Permalink
Add vesting pallet precompile (#442)
Browse files Browse the repository at this point in the history
* Add vesting pallet precompile

* make test pass

* clippy fix

* fix imports

* fix staking-precompile tests

* add more tests to vesting precompile

* fix vested transfer test

* non vested account cannot vest others

* clippy fix

* add some more tests

---------

Co-authored-by: salman01zp <[email protected]>
  • Loading branch information
drewstone and salman01zp authored Jan 22, 2024
1 parent 7876032 commit b3b9eb8
Show file tree
Hide file tree
Showing 14 changed files with 863 additions and 13 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ pallet-evm-precompile-preimage = { path = "precompiles/preimage", default-featur
pallet-evm-precompile-registry = { path = "precompiles/precompile-registry", default-features = false }
pallet-evm-precompile-staking = { path = "precompiles/staking", default-features = false }
pallet-evm-precompile-jobs = { path = "precompiles/jobs", default-features = false }
pallet-evm-precompile-vesting = { path = "precompiles/vesting", default-features = false }

# EVM & Ethereum
# (wasm)
Expand Down
7 changes: 3 additions & 4 deletions precompiles/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Block = frame_system::mocking::MockBlock<Runtime>;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

const PRECOMPILE_ADDRESS_BYTES: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
];

#[derive(
Expand Down Expand Up @@ -153,7 +153,7 @@ impl From<TestAccount> for H160 {
}
}
}
trait H160Conversion {
pub trait H160Conversion {
fn to_h160(&self) -> H160;
}

Expand Down Expand Up @@ -210,7 +210,6 @@ parameter_types! {
pub static SlashDeferDuration: EraIndex = 0;
pub static Period: BlockNumber = 5;
pub static Offset: BlockNumber = 0;

}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -285,7 +284,7 @@ parameter_types! {
}

pub type Precompiles<R> =
PrecompileSetBuilder<R, (PrecompileAt<AddressU64<5>, StakingPrecompile<R>>,)>;
PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, StakingPrecompile<R>>,)>;

pub type PCall = StakingPrecompileCall<Runtime>;

Expand Down
16 changes: 8 additions & 8 deletions precompiles/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn max_validator_count_works() {
precompiles()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::max_validator_count {},
)
.expect_cost(0)
Expand All @@ -50,7 +50,7 @@ fn current_era_works() {
start_session(3);
assert_eq!(active_era(), 2);
precompiles()
.prepare_test(TestAccount::Alex, H160::from_low_u64_be(5), PCall::current_era {})
.prepare_test(TestAccount::Alex, H160::from_low_u64_be(1), PCall::current_era {})
.expect_cost(0)
.expect_no_logs()
.execute_returns(3u32);
Expand All @@ -61,7 +61,7 @@ fn current_era_works() {
fn validator_count_works() {
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
precompiles()
.prepare_test(TestAccount::Alex, H160::from_low_u64_be(5), PCall::validator_count {})
.prepare_test(TestAccount::Alex, H160::from_low_u64_be(1), PCall::validator_count {})
.expect_cost(0)
.expect_no_logs()
.execute_returns(4u32);
Expand All @@ -74,7 +74,7 @@ fn max_nominator_count_works() {
precompiles()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::max_nominator_count {},
)
.expect_cost(0)
Expand All @@ -89,7 +89,7 @@ fn is_validator_works() {
precompiles()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::is_validator { validator: H160::from(TestAccount::Alex).into() },
)
.expect_cost(0)
Expand All @@ -109,7 +109,7 @@ fn eras_total_rewards_should_work() {
precompiles()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::eras_total_reward_points { era_index },
)
.expect_cost(0)
Expand All @@ -128,7 +128,7 @@ fn nominate_should_work() {
precompiles()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::nominate { targets: vec![H256::from(mock_pub_key(1))] },
)
.expect_no_logs()
Expand All @@ -149,7 +149,7 @@ fn bond_should_work() {
precompiles()
.prepare_test(
TestAccount::Eve,
H160::from_low_u64_be(5),
H160::from_low_u64_be(1),
PCall::bond {
value: U256::from(100),
payee: H256([
Expand Down
2 changes: 1 addition & 1 deletion precompiles/utils/src/testing/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'p, P: PrecompileSet> PrecompilesTester<'p, P> {
}
}

fn execute(&mut self) -> Option<PrecompileResult> {
pub fn execute(&mut self) -> Option<PrecompileResult> {
let handle = &mut self.handle;
handle.subcall_handle = self.subcall_handle.take();
handle.is_static = self.static_call;
Expand Down
63 changes: 63 additions & 0 deletions precompiles/vesting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[package]
name = "pallet-evm-precompile-vesting"
version = "0.1.0"
authors = { workspace = true }
edition = "2021"
description = "A Precompile to make pallet-vesting calls encoding accessible to pallet-evm"

[dependencies]
log = { workspace = true }
num_enum = { workspace = true }
rustc-hex = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true }

# Substrate
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
pallet-vesting = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

# Frontier
evm = { workspace = true, features = ["with-codec"] }
fp-evm = { workspace = true }
pallet-evm = { workspace = true, features = ["forbid-evm-reentrancy"] }

tangle-primitives = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
hex-literal = { workspace = true }
serde = { workspace = true }
sha3 = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true, features = ["std", "testing"] }

# Substrate
pallet-balances = { workspace = true, features = ["std"] }
pallet-timestamp = { workspace = true, features = ["std"] }
scale-info = { workspace = true, features = ["derive", "std"] }
sp-io = { workspace = true, features = ["std"] }

[features]
default = ["std"]
std = [
"fp-evm/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-evm/std",
"pallet-vesting/std",
"parity-scale-codec/std",
"precompile-utils/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"tangle-primitives/std",
]
26 changes: 26 additions & 0 deletions precompiles/vesting/Vesting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

/// @dev The Vesting contract's address.
address constant VESTING_ADDRESS = 0x0000000000000000000000000000000000000801;

/// @dev The Vesting contract's instance.
Vesting constant VESTING_CONTRACT = Vesting(VESTING_ADDRESS);

/// @author The Tangle Team
/// @title Pallet Vesting Interface
/// @title The interface through which solidity contracts will interact with the Vesting pallet
/// @custom:address 0x0000000000000000000000000000000000000801
interface Vesting {
/// @dev Unlock any vested funds of the sender account.
function vest() external returns (uint8);

/// @dev Unlock any vested funds of a `target` account.
/// @param target The address of the account to unlock vested funds for.
function vestOther(bytes32 target) external returns (uint8);

/// @dev Create a vested transfer.
/// @param target The address of the account to transfer funds to.
/// @param index The index of the vesting schedule to transfer.
function vestedTransfer(bytes32 target, uint8 index) external returns (uint8);
}
Loading

0 comments on commit b3b9eb8

Please sign in to comment.