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

Stream Payment pallet #391

Merged
merged 44 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e2ff7e8
pallet layout
nanocryk Jan 3, 2024
6c4faad
open stream + update logic
nanocryk Jan 4, 2024
56dd914
close stream + events
nanocryk Jan 4, 2024
0f75ed0
refill + change rate
nanocryk Jan 5, 2024
594ca74
adapter for fungible impl
nanocryk Jan 8, 2024
ce37805
rework pallet to not use fungibles traits
nanocryk Jan 15, 2024
ebe7a44
mock + some tests
nanocryk Jan 15, 2024
ffc0936
more tests
nanocryk Jan 17, 2024
61fb7df
refactor stream to prepare improved change requests
nanocryk Jan 22, 2024
322e1b6
refactor + allow to request bigger changes
nanocryk Jan 25, 2024
427cc35
crate doc
nanocryk Jan 25, 2024
515f2d0
clippy + skip 0 payment + prevent funds stuck on overflow
nanocryk Jan 25, 2024
b0c4291
payment == deposit => drained
nanocryk Jan 25, 2024
ed077d0
cleanup tests
nanocryk Jan 25, 2024
fb3c59b
rework deposit change + finish config change
nanocryk Jan 30, 2024
d7b60b8
update tests
nanocryk Jan 30, 2024
01e0c66
wip bench
nanocryk Jan 30, 2024
2c3a52e
Merge remote-tracking branch 'origin/master' into jeremy-stream-payme…
nanocryk Jan 30, 2024
f9fcaf1
fmt
nanocryk Jan 30, 2024
f4bebeb
switch to holds
nanocryk Jan 31, 2024
83c7e38
merge change_deposit into request_change/accept_requested_change + ca…
nanocryk Feb 2, 2024
011f7ff
cleanup tests
nanocryk Feb 2, 2024
7c74948
more tests
nanocryk Feb 5, 2024
3c61c41
update last_time_updated when changing time unit
nanocryk Feb 5, 2024
b8e859e
refactor possible immediate change
nanocryk Feb 6, 2024
eeca2ad
immediate deposit change
nanocryk Feb 7, 2024
e9db81d
more tests
nanocryk Feb 7, 2024
057a3ce
fix CI
nanocryk Feb 7, 2024
34b9207
support deadline in past
nanocryk Feb 9, 2024
b17754e
for benches (wip)
nanocryk Feb 9, 2024
4ebb9c8
fix rustfmt issue
nanocryk Feb 9, 2024
d704100
mock ready for running benchmarks as tests
girazoki Feb 9, 2024
3f9925b
all benches and weights
nanocryk Feb 9, 2024
0ff2f2a
Merge remote-tracking branch 'origin/master' into jeremy-stream-payme…
nanocryk Feb 9, 2024
5668b8e
fmt
nanocryk Feb 9, 2024
d27de8e
update docs
nanocryk Feb 9, 2024
376f30f
add pallet to flashbox
nanocryk Feb 9, 2024
3e5f1b1
typescript api
nanocryk Feb 12, 2024
c6feb68
Merge remote-tracking branch 'origin/master' into jeremy-stream-payme…
nanocryk Feb 13, 2024
c55bdf7
add integration tests
nanocryk Feb 14, 2024
0e0b000
Merge remote-tracking branch 'origin/master' into jeremy-stream-payme…
nanocryk Feb 15, 2024
f7185fa
fix test
nanocryk Feb 15, 2024
232ebe9
fmt
nanocryk Feb 15, 2024
b41db8f
clippy
nanocryk Feb 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pallet-pooled-staking = { path = "pallets/pooled-staking", default-features = fa
pallet-registrar = { path = "pallets/registrar", default-features = false }
pallet-registrar-runtime-api = { path = "pallets/registrar/rpc/runtime-api", default-features = false }
pallet-services-payment = { path = "pallets/services-payment", default-features = false }
pallet-stream-payment = { path = "pallets/stream-payment", default-features = false }

container-chain-template-frontier-runtime = { path = "container-chains/templates/frontier/runtime", default-features = false }
container-chain-template-simple-runtime = { path = "container-chains/templates/simple/runtime", default-features = false }
Expand All @@ -47,6 +48,7 @@ tc-consensus = { path = "client/consensus" }
tp-author-noting-inherent = { path = "primitives/author-noting-inherent", default-features = false }
tp-consensus = { path = "primitives/consensus", default-features = false }
tp-container-chain-genesis-data = { path = "primitives/container-chain-genesis-data", default-features = false }
tp-fungibles-ext = { path = "primitives/fungibles-ext", default-features = false }
tp-maths = { path = "primitives/maths", default-features = false }
tp-traits = { path = "primitives/traits", default-features = false }

Expand Down Expand Up @@ -248,6 +250,7 @@ num_enum = { version = "0.7.1", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
serde = { version = "1.0.152", default-features = false }
smallvec = "1.10.0"
tap = "1.0.1"

# General (client)
async-io = "1.3"
Expand Down
4 changes: 2 additions & 2 deletions pallets/collator-assignment/src/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use {
cmp,
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
marker::PhantomData,
mem, vec,
vec::Vec,
mem,
nanocryk marked this conversation as resolved.
Show resolved Hide resolved
vec::{self, Vec},
},
tp_traits::{ParaId, RemoveInvulnerables as RemoveInvulnerablesT},
};
Expand Down
2 changes: 1 addition & 1 deletion pallets/collator-assignment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use dp_collator_assignment::AssignedCollators;
use {
crate::{mock::*, CollatorContainerChain, Event, PendingCollatorContainerChain},
dp_collator_assignment::AssignedCollators,
std::collections::BTreeMap,
};

Expand Down
6 changes: 4 additions & 2 deletions pallets/collator-assignment/src/tests/assign_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use crate::assignment::AssignmentError;
use {
crate::{assignment::Assignment, tests::Test},
crate::{
assignment::{Assignment, AssignmentError},
tests::Test,
},
sp_std::collections::btree_map::BTreeMap,
};

Expand Down
66 changes: 66 additions & 0 deletions pallets/stream-payment/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[package]
name = "pallet-stream-payment"
authors = { workspace = true }
description = "Stream payment pallet"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

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

[dependencies]
log = { workspace = true }
serde = { workspace = true, optional = true }

dp-core = { workspace = true }
tp-maths = { workspace = true }
tp-traits = { workspace = true }

# Substrate
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
num-traits = { workspace = true }
pallet-balances = { workspace = true, features = [ "std" ] }
similar-asserts = { workspace = true }
sp-io = { workspace = true, features = [ "std" ] }
tap = { workspace = true }

[features]
default = [ "std" ]
std = [
"dp-core/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"parity-scale-codec/std",
"scale-info/std",
"serde",
"serde?/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"tp-maths/std",
"tp-traits/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"tp-maths/runtime-benchmarks",
"tp-traits/runtime-benchmarks",
]
127 changes: 127 additions & 0 deletions pallets/stream-payment/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tanssi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use {
crate::{Assets, Call, Config, Event, Pallet, StreamConfig, TimeProvider},
frame_benchmarking::{account, impl_benchmark_test_suite, v2::*, BenchmarkError},
frame_support::{assert_ok, dispatch::RawOrigin},
frame_system::EventRecord,
sp_std::vec,
};

/// Create a funded user.
fn create_funded_user<T: Config>(
string: &'static str,
n: u32,
asset_id: &T::AssetId,
amount: T::Balance,
) -> T::AccountId {
const SEED: u32 = 0;
let user = account(string, n, SEED);
T::Assets::bench_set_balance(asset_id, &user, amount);
user
}

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn open_stream() -> Result<(), BenchmarkError> {
let asset_id = T::Assets::bench_asset_id();
let time_unit = T::TimeProvider::bench_time_unit();

let source = create_funded_user::<T>("source", 1, &asset_id, 1_000_000_000u32.into());
let target = create_funded_user::<T>("target", 2, &asset_id, 1_000_000_000u32.into());

#[extrinsic_call]
_(
RawOrigin::Signed(source.clone()),
target,
StreamConfig {
time_unit,
asset_id,
rate: 100u32.into(),
},
1_000_000u32.into(),
);

assert_last_event::<T>(
Event::StreamOpened {
stream_id: 0u32.into(),
}
.into(),
);

Ok(())
}

#[benchmark]
fn close_stream() -> Result<(), BenchmarkError> {
nanocryk marked this conversation as resolved.
Show resolved Hide resolved
// Worst case is closing a stream with a pending payment.
let time_unit = T::TimeProvider::bench_time_unit();
let asset_id = T::Assets::bench_asset_id();

let source = create_funded_user::<T>("source", 1, &asset_id, 1_000_000_000u32.into());
let target = create_funded_user::<T>("target", 2, &asset_id, 1_000_000_000u32.into());

let rate = 100u32.into();
let initial_deposit = 1_000_000u32.into();

assert_ok!(Pallet::<T>::open_stream(
RawOrigin::Signed(source.clone()).into(),
target,
StreamConfig {
time_unit: time_unit.clone(),
asset_id,
rate,
},
initial_deposit,
));

// Change time to trigger payment.
let now = T::TimeProvider::now(&time_unit).expect("can fetch time");
let delta: T::Balance = 10u32.into();
T::TimeProvider::bench_set_now(now + delta);

#[extrinsic_call]
_(RawOrigin::Signed(source.clone()), 0u32.into());

assert_last_event::<T>(
Event::StreamClosed {
stream_id: 0u32.into(),
refunded: initial_deposit - (rate * delta),
}
.into(),
);

Ok(())
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::ExtBuilder::default().build(),
crate::mock::Runtime,
);
}
Loading
Loading