Skip to content

Commit

Permalink
make test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp committed Jan 18, 2024
1 parent 36b3eeb commit 5d7e821
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
10 changes: 7 additions & 3 deletions precompiles/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
Runtime::AccountId: From<WrappedAccountId32>,
{
#[precompile::public("vest()")]
fn vest(handle: &mut impl PrecompileHandle) -> EvmResult<u8> {
fn vest(handle: &mut impl PrecompileHandle) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

// Make the call to vest the `msg.sender` account.
Expand All @@ -128,7 +128,7 @@ where
// Dispatch call (if enough gas).
RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok((0))
Ok(())
}

#[precompile::public("vestOther(bytes32)")]
Expand All @@ -150,7 +150,11 @@ where

#[precompile::public("vestedTransfer(bytes32,uint8)")]
#[precompile::public("vested_transfer(bytes32,uint8)")]
fn vested_transfer(handle: &mut impl PrecompileHandle, target: H256, index: u8) -> EvmResult<u8> {
fn vested_transfer(
handle: &mut impl PrecompileHandle,
target: H256,
index: u8,
) -> EvmResult<u8> {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

// First get the vesting schedule of the `msg.sender`
Expand Down
65 changes: 32 additions & 33 deletions precompiles/vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,29 @@
use super::*;
use crate::{VestingPrecompile, VestingPrecompileCall};
use frame_support::{
construct_runtime,
parameter_types,
traits::{ConstU64, Everything, InstanceFilter, WithdrawReasons, Hooks},
construct_runtime, parameter_types,
traits::{ConstU64, Everything, Hooks, InstanceFilter, WithdrawReasons},
weights::Weight,
};
use serde::{Serialize, Deserialize};
use frame_system::Account;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use pallet_evm::{EnsureAddressNever, EnsureAddressOrigin, SubstrateBlockHashMapping};
use precompile_utils::
precompile_set::{
AddressU64, PrecompileAt,
PrecompileSetBuilder,
}
;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use precompile_utils::precompile_set::{AddressU64, PrecompileAt, PrecompileSetBuilder};
use scale_info::TypeInfo;
use sp_runtime::{
curve::PiecewiseLinear,
testing::TestXt,
traits::{self, Extrinsic as ExtrinsicT, IdentifyAccount,
IdentityLookup, Verify, Zero, Identity},
AccountId32, BuildStorage, Perbill,
};
use serde::{Deserialize, Serialize};
use sp_core::{
self,
sr25519::{self, Public as sr25519Public, Signature},
ConstU32, Get, H160, H256, U256,
};
use sp_runtime::{
curve::PiecewiseLinear,
testing::TestXt,
traits::{
self, Extrinsic as ExtrinsicT, IdentifyAccount, Identity, IdentityLookup, Verify, Zero,
},
AccountId32, BuildStorage, Perbill,
};
use tangle_primitives::WrappedAccountId32;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub type Balance = u64;
Expand All @@ -56,7 +51,7 @@ pub type BlockNumber = u64;
type Block = frame_system::mocking::MockBlock<Runtime>;

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 @@ -84,8 +79,7 @@ pub enum TestAccount {
PrecompileAddress,
}

impl Default for TestAccount
{
impl Default for TestAccount {
fn default() -> Self {
Self::Empty
}
Expand Down Expand Up @@ -338,18 +332,22 @@ impl ExtBuilder {
.expect("Frame system builds valid default genesis config");

pallet_balances::GenesisConfig::<Runtime> {
balances: self.balances
balances: self
.balances
.iter()
.chain(vec![
(TestAccount::Alex.into(), 100_000),
(TestAccount::Bobo.into(), 100_000),
(TestAccount::Charlie.into(), 100_000),
].iter())
.chain(
vec![
(TestAccount::Alex.into(), 100_000),
(TestAccount::Bobo.into(), 100_000),
(TestAccount::Charlie.into(), 100_000),
]
.iter(),
)
.cloned()
.collect()
.collect(),
}
.assimilate_storage(&mut t)
.expect("Pallet balances storage can be assimilated");
.assimilate_storage(&mut t)
.expect("Pallet balances storage can be assimilated");

pallet_vesting::GenesisConfig::<Runtime> {
vesting: vec![
Expand All @@ -361,8 +359,9 @@ impl ExtBuilder {
(TestAccount::Bobo.into(), 10, 100, 0),
(TestAccount::Charlie.into(), 10, 100, 0),
],
}.assimilate_storage(&mut t)
.expect("Pallet vesting storage can be assimilated");
}
.assimilate_storage(&mut t)
.expect("Pallet vesting storage can be assimilated");

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
Expand All @@ -384,4 +383,4 @@ pub(crate) fn roll_to(n: BlockNumber) {
System::on_initialize(System::block_number());
Balances::on_initialize(System::block_number());
}
}
}
19 changes: 8 additions & 11 deletions precompiles/vesting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
// limitations under the License.

use crate::mock::{
AccountId, ExtBuilder, PCall, PrecompilesValue, Runtime, RuntimeCall, RuntimeEvent,
RuntimeOrigin, TestAccount, System, Balance, roll_to,
roll_to, AccountId, Balance, ExtBuilder, PCall, PrecompilesValue, Runtime, RuntimeCall,
RuntimeEvent, RuntimeOrigin, System, TestAccount,
};
use frame_support::{assert_ok, traits::OnFinalize};
use pallet_evm::{Call as EvmCall, AddressMapping};
use pallet_evm::{AddressMapping, Call as EvmCall};
use pallet_vesting::{
Call as VestingCall, Event as VestingEvent, Pallet as VestingPallet, Vesting, VestingInfo, MaxVestingSchedulesGet,
Call as VestingCall, Event as VestingEvent, MaxVestingSchedulesGet, Pallet as VestingPallet,
Vesting, VestingInfo,
};
use precompile_utils::{
assert_event_emitted, assert_event_not_emitted, precompile_set::AddressU64, prelude::*,
Expand Down Expand Up @@ -54,16 +55,12 @@ fn test_unimplemented_selector_reverts() {
#[test]
fn test_claim_vesting_schedule() {
ExtBuilder::default().build().execute_with(|| {
let schedules = Vesting::<Runtime>::get(sp_core::sr25519::Public::from(TestAccount::Alex)).unwrap();
let schedules =
Vesting::<Runtime>::get(sp_core::sr25519::Public::from(TestAccount::Alex)).unwrap();
assert!(!schedules.is_empty());
roll_to(1000);
PrecompilesValue::get()
.prepare_test(
TestAccount::Alex,
H160::from_low_u64_be(6),
PCall::vest {},
)
.prepare_test(TestAccount::Alex, H160::from_low_u64_be(1), PCall::vest {})
.execute_returns(());
});
}

0 comments on commit 5d7e821

Please sign in to comment.