Skip to content

Commit

Permalink
pallet teerdays (#263)
Browse files Browse the repository at this point in the history
* first dispatchable for teerdays

* basic bonding and unbonding without unlockingperiod. type trouble

* fix type trouble

* fix unbonding

* cosmetics

* implement unlock period

* minimal benchmarks

* add bond_extra call

* refactor

* add unhappy tests

* pallet code docs

* cleanup

* cleanup

* doc pimp

* taplo

* taplo

* zepter+fmt

* fmt nightly

* fmt

* clippy

* review fixes

* clippy again

* clippy version mismatch?

* clippy version mismatch?

---------

Co-authored-by: Christian Langenbacher <[email protected]>
  • Loading branch information
brenzi and clangenb authored Jul 15, 2024
1 parent 9838947 commit 58b4c6a
Show file tree
Hide file tree
Showing 18 changed files with 1,102 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ jobs:
cargo test --all --features runtime-benchmarks,
cargo test --all --features dot,
cargo test --all --features ksm,
cargo fmt --all -- --check,
cargo +nightly fmt --all -- --check,
cargo clippy -- -D warnings
]
steps:
- uses: actions/checkout@v3

- name: Install nightly toolchain
run: rustup toolchain install nightly --profile minimal --component rustfmt

# With rustup's nice new toml format, we just need to run rustup show to install the toolchain
# https://github.com/actions-rs/toolchain/issues/126#issuecomment-782989659
- name: Setup Rust toolchain
Expand Down
34 changes: 34 additions & 0 deletions Cargo.lock

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

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ members = [
"asset-registry",
"claims",
"enclave-bridge",
"teerex",
"parentchain",
"sidechain",
"teerex/sgx-verify",
"teeracle",
"test-utils",
"xcm-transactor",
"primitives/claims",
"primitives/common",
"primitives/enclave-bridge",
"primitives/teerex",
"primitives/teeracle",
"primitives/common",
"primitives/teerdays",
"primitives/teerex",
"primitives/xcm",
"primitives/xcm-transactor",
"sidechain",
"teeracle",
"teerdays",
"teerex",
"teerex/sgx-verify",
"test-utils",
"xcm-transactor",
]

[workspace.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ pub mod pallet {
Some(AccountId32 { .. }) |
Some(AccountKey20 { .. }) |
Some(PalletInstance(_)) |
Some(Parachain(_)) | None
Some(Parachain(_)) |
None
);

check |
Expand Down
8 changes: 4 additions & 4 deletions claims/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ pub mod pallet {
priority: PRIORITY,
requires: vec![],
provides: vec![("claims", signer).encode()],
longevity: TransactionLongevity::max_value(),
longevity: TransactionLongevity::MAX,
propagate: true,
})
}
Expand Down Expand Up @@ -480,7 +480,7 @@ impl<T: Config> Pallet<T> {
}

// We first need to deposit the balance to ensure that the account exists.
CurrencyOf::<T>::deposit_creating(&dest, balance_due);
let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);

// Check if this claim should have a vesting schedule.
if let Some(vs) = vesting {
Expand Down Expand Up @@ -1317,7 +1317,7 @@ mod tests {
priority: 100,
requires: vec![],
provides: vec![("claims", eth(&alice())).encode()],
longevity: TransactionLongevity::max_value(),
longevity: TransactionLongevity::MAX,
propagate: true,
})
);
Expand Down Expand Up @@ -1350,7 +1350,7 @@ mod tests {
priority: 100,
requires: vec![],
provides: vec![("claims", eth(&dave())).encode()],
longevity: TransactionLongevity::max_value(),
longevity: TransactionLongevity::MAX,
propagate: true,
})
);
Expand Down
2 changes: 1 addition & 1 deletion enclave-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ pub mod pallet {
let new_status: ShardSignerStatusVec<T> = Self::shard_status(shard)
.ok_or(Error::<T>::ShardNotFound)?
.iter()
.filter(|&signer_status| signer_status.signer != subject)
.cloned()
.filter(|signer_status| signer_status.signer != subject)
.collect::<Vec<ShardSignerStatus<T>>>()
.try_into()
.expect("can only become smaller by filtering");
Expand Down
25 changes: 25 additions & 0 deletions primitives/teerdays/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "teerdays-primitives"
version = "0.1.0"
authors = ["Integritee AG <[email protected]>"]
homepage = "https://integritee.network/"
repository = "https://github.com/integritee-network/pallets/"
license = "Apache-2.0"
edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"sp-core/std",
"sp-runtime/std",
]
24 changes: 24 additions & 0 deletions primitives/teerdays/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{traits::AtLeast32BitUnsigned, Saturating};

#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, Default, sp_core::RuntimeDebug, TypeInfo)]
pub struct TeerDayBond<Balance, Moment> {
pub value: Balance,
pub last_updated: Moment,
// the unit here is actually balance * moments.
pub accumulated_tokentime: Balance,
}

impl<Balance, Moment> TeerDayBond<Balance, Moment>
where
Moment: Clone + Copy + Encode + Decode + Saturating,
Balance: AtLeast32BitUnsigned + Clone + Copy + Encode + Decode + Default + From<Moment>,
{
pub fn update(self, now: Moment) -> Self {
let elapsed: Balance = now.saturating_sub(self.last_updated).into();
let new_tokentime =
self.accumulated_tokentime.saturating_add(self.value.saturating_mul(elapsed));
Self { value: self.value, last_updated: now, accumulated_tokentime: new_tokentime }
}
}
2 changes: 1 addition & 1 deletion primitives/teerex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
MultiEnclave::Sgx(enclave) => match enclave.maybe_pubkey() {
Some(pubkey) =>
AnySigner::from(MultiSigner::from(sp_core::ed25519::Public::from_raw(pubkey))),
None => AnySigner::try_from(enclave.report_data.d).unwrap_or_default(),
None => AnySigner::from(enclave.report_data.d),
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion primitives/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl<AssetId, AssetIdInfoGetter, AssetsPallet, BalancesPallet, XcmPallet, Accoun
XcmPallet,
AccountId,
Weigher,
> where
>
where
AssetIdInfoGetter: AssetLocationGetter<AssetId>,
AssetsPallet: Inspect<AccountId, AssetId = AssetId>,
BalancesPallet: Currency<AccountId>,
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "nightly-2023-06-01"
channel = "stable"
targets = ["wasm32-unknown-unknown"]
profile = "default" # include rustfmt, clippy
72 changes: 72 additions & 0 deletions teerdays/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[package]
name = "pallet-teerdays"
version = "0.1.0"
authors = ["Integritee AG <[email protected]>"]
homepage = "https://integritee.network/"
repository = "https://github.com/integritee-network/pallets/"
license = "(GPL-3.0-only)"
edition = "2021"

[dependencies]
log = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true, optional = true }

teerdays-primitives = { path = "../primitives/teerdays", default-features = false }

# substrate dependencies
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

# benchmarking
frame-benchmarking = { workspace = true, optional = true }
hex-literal = { workspace = true, optional = true }

[dev-dependencies]
env_logger = { workspace = true }
sp-keyring = { workspace = true }

[features]
default = ["std"]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"pallet-timestamp/std",
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"sp-core/std",
"sp-io/std",
"sp-keyring/std",
"sp-runtime/std",
"sp-std/std",
"teerdays-primitives/std",
]

try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"hex-literal",
"pallet-balances/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
79 changes: 79 additions & 0 deletions teerdays/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2021 Integritee AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! TeerDays pallet benchmarking

#![cfg(any(test, feature = "runtime-benchmarks"))]

use super::*;

use crate::Pallet as TeerDays;
use frame_benchmarking::{account, benchmarks};
use frame_system::RawOrigin;
use sp_std::prelude::*;

benchmarks! {
where_clause { where T::AccountId: From<[u8; 32]>, T::Hash: From<[u8; 32]> }
bond {
pallet_timestamp::Pallet::<T>::set_timestamp(0u32.into());
let signer: T::AccountId = account("alice", 1, 1);
T::Currency::make_free_balance_be(&signer, 10_000u32.into());
}: _(RawOrigin::Signed(signer.clone()), 1_000u32.into())
verify {
assert!(TeerDays::<T>::teerday_bonds(&signer).is_some());
}

unbond {
pallet_timestamp::Pallet::<T>::set_timestamp(0u32.into());
let signer: T::AccountId = account("alice", 1, 1);
T::Currency::make_free_balance_be(&signer, 10_000u32.into());
TeerDays::<T>::bond(RawOrigin::Signed(signer.clone()).into(), 1_000u32.into())?;
}: _(RawOrigin::Signed(signer.clone()), 500u32.into())
verify {
assert!(TeerDays::<T>::teerday_bonds(&signer).is_some());
}

update_other {
pallet_timestamp::Pallet::<T>::set_timestamp(0u32.into());
let signer: T::AccountId = account("alice", 1, 1);
T::Currency::make_free_balance_be(&signer, 10_000u32.into());
TeerDays::<T>::bond(RawOrigin::Signed(signer.clone()).into(), 1_000u32.into())?;
}: _(RawOrigin::Signed(signer.clone()), signer.clone())
verify {
assert!(TeerDays::<T>::teerday_bonds(&signer).is_some());
}
withdraw_unbonded {
pallet_timestamp::Pallet::<T>::set_timestamp(42u32.into());
let signer: T::AccountId = account("alice", 1, 1);
T::Currency::make_free_balance_be(&signer, 10_000u32.into());
T::Currency::set_lock(TEERDAYS_ID, &signer, 1_000u32.into(), WithdrawReasons::all());
PendingUnlock::<T>::insert::<_, (T::Moment, BalanceOf<T>)>(&signer, (42u32.into(), 1_000u32.into()));
}: _(RawOrigin::Signed(signer.clone()))
verify {
assert!(TeerDays::<T>::pending_unlock(&signer).is_none());
}

}

#[cfg(test)]
use crate::{Config, Pallet as PalletModule};

#[cfg(test)]
use frame_benchmarking::impl_benchmark_test_suite;

#[cfg(test)]
impl_benchmark_test_suite!(PalletModule, crate::mock::new_test_ext(), crate::mock::Test,);
Loading

0 comments on commit 58b4c6a

Please sign in to comment.