Skip to content

Commit

Permalink
Merge pull request #24 from perun-network/update_substrate
Browse files Browse the repository at this point in the history
feat: update substrate sdk version to polkadot v1.9
  • Loading branch information
NhoxxKienn committed Apr 25, 2024
2 parents 9ef1476 + 09452ee commit af1f336
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 186 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ jobs:
KCOV_OUT: "target/cov/pallet_perun"
KCOV: "~/cache/usr/local/bin/kcov"
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 2 # CodeCov needs this
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly

- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2
name: Setup Cache

- uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
args: --all-targets --all-features

- name: kcov cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/cache
key: ${{ runner.os }}-kcov-wget
Expand Down Expand Up @@ -141,7 +141,8 @@ jobs:
$KCOV --merge "$KCOV_OUT/merged" "$KCOV_OUT"/unmerged-*
- name: CodeCov upload
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: "${{ env.KCOV_OUT }}/merged"
fail_ci_if_error: true
51 changes: 33 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,54 +1,69 @@
[package]
name = "pallet-perun"
version = "4.0.0-dev"
authors = ["PolyCrypt GmbH <info@polycry.pt>"]
edition = "2018"
license = "Apache-2.0"
version = "0.0.0"
homepage = "https://polycry.pt/"
keywords = ["blockchain", "channel", "perun"]
repository = "https://github.com/perun-network/perun-polkadot-pallet"
description = "FRAME pallet for Perun State Channels"
readme = "README.md"
edition = "2021"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-core = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-runtime = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-std = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-io = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
sp-std = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }

frame-support = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
frame-system = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
frame-benchmarking = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1', optional=true}
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false, optional = true }

pallet-balances = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
pallet-timestamp = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }

codec = {package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"]}
syn = "=1.0.76"
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.10.0", default-features = false, features = [
"derive",
] }

[dev-dependencies]
sp-io = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-runtime = {default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-09+1'}
sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0" }


[features]
default = ["std"]
# Used for testing only.
expose_privates = []
# Enable Benchmarks.
runtime-benchmarks = ['frame-benchmarking']
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
"codec/std",
"sp-core/std",
"sp-std/std",
"sp-io/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-timestamp/std",
]
try-runtime = ['frame-support/try-runtime']
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
48 changes: 30 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub mod pallet {
dispatch::DispatchResult,
traits::{ExistenceRequirement, Get},
};
use sp_core::ByteArray;
use sp_runtime::traits::{CheckedAdd, Member};

#[pallet::config]
Expand All @@ -81,34 +82,39 @@ pub mod pallet {
type ParticipantNum: Get<Range<ParticipantIndex>>;

/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// On-Chain currency that should be used by the Perun Pallet.
type Currency: Currency<Self::AccountId>;

/// Type of a [Params::nonce].
type Nonce: Encode + Decode + Member;
type Nonce: Encode + Decode + Member + TypeInfo;

/// Type of a [State::version].
type Version: Encode + Decode + Member + PartialOrd + CheckedAdd + From<u32>;
type Version: Encode + Decode + Member + TypeInfo + PartialOrd + CheckedAdd + From<u32>;

/// Cryptographically secure hashing algorithm that is used to calculate the
/// ChannelId and FundingId.
type Hasher: sp_core::Hasher<Out = Self::HashValue>;

/// Define the output of the Hashing algorithm.
/// The `FullCodec` ensures that it is usable as a `StorageMap` key.
type HashValue: FullCodec + Member + Copy;
type HashValue: FullCodec + Member + Copy + TypeInfo;

/// Off-Chain signature type.
///
/// Must be possible to verify that a [Config::PK] created a signature.
type Signature: Encode + Decode + Member + Verify<Signer = Self::PK>;
type Signature: Encode + Decode + Member + TypeInfo + Verify<Signer = Self::PK>;
/// PK of a [Config::Signature].
type PK: Encode + Decode + Member + IdentifyAccount<AccountId = Self::PK>;
type PK: Encode
+ Decode
+ Member
+ ByteArray
+ TypeInfo
+ IdentifyAccount<AccountId = Self::PK>;

/// Represent a time duration in seconds.
type Seconds: FullCodec + Member + CheckedAdd + PartialOrd + From<u64>;
type Seconds: FullCodec + Member + TypeInfo + CheckedAdd + PartialOrd + From<u64>;

/// Weight info for extrinsics in this pallet.
type WeightInfo: WeightInfo;
Expand All @@ -122,7 +128,7 @@ pub mod pallet {
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::storage]
Expand Down Expand Up @@ -242,6 +248,7 @@ pub mod pallet {
///
/// Emits an [Event::Deposited] event on success.
#[pallet::weight(WeightInfoOf::<T>::deposit())]
#[pallet::call_index(0)]
pub fn deposit(
origin: OriginFor<T>,
funding_id: FundingIdOf<T>,
Expand All @@ -250,7 +257,7 @@ pub mod pallet {
let who = ensure_signed(origin)?;
ensure!(amount >= T::MinDeposit::get(), Error::<T>::DepositTooSmall);
// Check that a deposit would not overflow, return on failure.
let holding = <Deposits<T>>::get(&funding_id).unwrap_or_default();
let holding = <Deposits<T>>::get(funding_id).unwrap_or_default();
// An overflow here can happen if a user wants to deposit more than he has.
let new_holdings = holding
.checked_add(&amount)
Expand All @@ -259,7 +266,7 @@ pub mod pallet {
let account_id = Self::account_id();
T::Currency::transfer(&who, &account_id, amount, ExistenceRequirement::KeepAlive)?;
// Update the holdings in the deposits map.
<Deposits<T>>::insert(&funding_id, &new_holdings);
<Deposits<T>>::insert(funding_id, new_holdings);
// Emit the 'Deposited' event.
Self::deposit_event(Event::Deposited(funding_id, new_holdings));
Ok(())
Expand All @@ -278,6 +285,7 @@ pub mod pallet {
/// Emits an [Event::Disputed] event on success.
#[pallet::weight(WeightInfoOf::<T>::dispute(
cmp::min(state_sigs.len() as u32, T::ParticipantNum::get().end)))]
#[pallet::call_index(1)]
pub fn dispute(
origin: OriginFor<T>,
params: ParamsOf<T>,
Expand All @@ -291,7 +299,7 @@ pub mod pallet {
let channel_id = state.channel_id;

let now = Self::now();
match <StateRegister<T>>::get(&channel_id) {
match <StateRegister<T>>::get(channel_id) {
None => {
let timeout = now
.checked_add(&params.challenge_duration)
Expand Down Expand Up @@ -341,6 +349,7 @@ pub mod pallet {
///
/// Emits an [Event::Progressed] event on success.
#[pallet::weight(WeightInfoOf::<T>::progress::<T>(params))]
#[pallet::call_index(2)]
pub fn progress(
origin: OriginFor<T>,
params: ParamsOf<T>,
Expand Down Expand Up @@ -400,10 +409,11 @@ pub mod pallet {
///
/// Emits an [Event::Concluded] event on success.
#[pallet::weight(WeightInfoOf::<T>::conclude(params.participants.len() as u32))]
#[pallet::call_index(3)]
pub fn conclude(origin: OriginFor<T>, params: ParamsOf<T>) -> DispatchResult {
ensure_signed(origin)?;
let channel_id = params.channel_id::<T::Hasher>();
match <StateRegister<T>>::get(&channel_id) {
match <StateRegister<T>>::get(channel_id) {
Some(dispute) => {
if dispute.phase == Phase::Conclude {
return Ok(());
Expand Down Expand Up @@ -446,6 +456,7 @@ pub mod pallet {
///
/// Emits an [Event::Concluded] event on success.
#[pallet::weight(WeightInfoOf::<T>::conclude_final(params.participants.len() as u32))]
#[pallet::call_index(4)]
pub fn conclude_final(
origin: OriginFor<T>,
params: ParamsOf<T>,
Expand All @@ -459,7 +470,7 @@ pub mod pallet {
ensure!(state.finalized, Error::<T>::StateNotFinal);

// Check if this channel is being disputed.
if let Some(dispute) = <StateRegister<T>>::get(&channel_id) {
if let Some(dispute) = <StateRegister<T>>::get(channel_id) {
if dispute.phase == Phase::Conclude {
ensure!(
dispute.state.version == state.version,
Expand Down Expand Up @@ -493,6 +504,7 @@ pub mod pallet {
///
/// Emits an [Event::Withdrawn] event on success.
#[pallet::weight(WeightInfoOf::<T>::withdraw())]
#[pallet::call_index(5)]
pub fn withdraw(
origin: OriginFor<T>,
withdrawal: WithdrawalOf<T>,
Expand Down Expand Up @@ -536,7 +548,7 @@ impl<T: Config> Pallet<T> {
/// Returns the account of the pallet.
/// Cache it if it needed multiple times.
fn account_id() -> T::AccountId {
T::PalletId::get().into_account()
T::PalletId::get().into_account_truncating()
}

/// Returns the current time in seconds since
Expand Down Expand Up @@ -575,7 +587,7 @@ impl<T: Config> Pallet<T> {
for (i, part) in parts.iter().enumerate() {
let fid = Self::calc_funding_id(channel, part);
fids.push(fid);
let deposit = <Deposits<T>>::get(&fid).unwrap_or_default();
let deposit = <Deposits<T>>::get(fid).unwrap_or_default();

sum_outcome = sum_outcome
.checked_add(&outcome[i])
Expand All @@ -595,7 +607,7 @@ impl<T: Config> Pallet<T> {
if sum_deposit >= sum_outcome {
// We redistribute the funds according to the outcome.
for (i, fid) in fids.iter().enumerate() {
<Deposits<T>>::insert(&fid, outcome[i]);
<Deposits<T>>::insert(fid, outcome[i]);
}
}
Ok(())
Expand Down Expand Up @@ -682,14 +694,14 @@ impl<T: Config> Pallet<T> {
require!(cur_acc == next_acc);
frame_support::runtime_print!("PerunPallet:after check balances");

return T::AppRegistry::valid_transition(params, current, next, signer);
T::AppRegistry::valid_transition(params, current, next, signer)
}

fn accumulate_balances(balances: &[BalanceOf<T>]) -> BalanceOf<T> {
let mut acc = BalanceOf::<T>::default();
for b in balances.iter() {
acc += *b;
}
return acc;
acc
}
}
Loading

0 comments on commit af1f336

Please sign in to comment.