Skip to content

Commit

Permalink
Stream Payment pallet (#391)
Browse files Browse the repository at this point in the history
* pallet layout

* open stream + update logic

* close stream + events

* refill + change rate

* adapter for fungible impl

* rework pallet to not use fungibles traits

* mock + some tests

* more tests

* refactor stream to prepare improved change requests

* refactor + allow to request bigger changes

* crate doc

* clippy + skip 0 payment + prevent funds stuck on overflow

* payment == deposit => drained

* cleanup tests

* rework deposit change + finish config change

* update tests

* wip bench

* fmt

* switch to holds

* merge change_deposit into request_change/accept_requested_change + cancel_change_request

* cleanup tests

* more tests

* update last_time_updated when changing time unit

* refactor possible immediate change

* immediate deposit change

* more tests

* fix CI

* support deadline in past

* for benches (wip)

* fix rustfmt issue

* mock ready for running benchmarks as tests

* all benches and weights

* fmt

* update docs

* add pallet to flashbox

* typescript api

* add integration tests

* fix test

* fmt

* clippy

---------

Co-authored-by: girazoki <[email protected]>
  • Loading branch information
nanocryk and girazoki authored Feb 16, 2024
1 parent d28aca5 commit 90433bb
Show file tree
Hide file tree
Showing 35 changed files with 6,603 additions and 837 deletions.
26 changes: 26 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 @@ -251,6 +253,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
58 changes: 29 additions & 29 deletions client/consensus/src/collators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@

pub mod basic;

use cumulus_client_collator::service::ServiceInterface as CollatorServiceInterface;
use cumulus_client_consensus_common::ParachainCandidate;
use cumulus_client_consensus_proposer::ProposerInterface;
use cumulus_primitives_core::{
relay_chain::Hash as PHash, DigestItem, ParachainBlockData, PersistedValidationData,
use {
crate::{find_pre_digest, AuthorityId, OrchestratorAuraWorkerAuxData},
cumulus_client_collator::service::ServiceInterface as CollatorServiceInterface,
cumulus_client_consensus_common::ParachainCandidate,
cumulus_client_consensus_proposer::ProposerInterface,
cumulus_primitives_core::{
relay_chain::Hash as PHash, DigestItem, ParachainBlockData, PersistedValidationData,
},
cumulus_primitives_parachain_inherent::ParachainInherentData,
cumulus_relay_chain_interface::RelayChainInterface,
futures::prelude::*,
nimbus_primitives::{CompatibleDigestItem as NimbusCompatibleDigestItem, NIMBUS_KEY_ID},
parity_scale_codec::{Codec, Encode},
polkadot_node_primitives::{Collation, MaybeCompressedPoV},
polkadot_primitives::Id as ParaId,
sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, StateAction},
sp_application_crypto::{AppCrypto, AppPublic},
sp_consensus::BlockOrigin,
sp_consensus_aura::{digests::CompatibleDigestItem, Slot},
sp_core::crypto::{ByteArray, Pair},
sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider},
sp_keystore::{Keystore, KeystorePtr},
sp_runtime::{
generic::Digest,
traits::{Block as BlockT, HashingFor, Header as HeaderT, Member, Zero},
},
sp_state_machine::StorageChanges,
sp_timestamp::Timestamp,
std::{convert::TryFrom, error::Error, time::Duration},
};
use cumulus_primitives_parachain_inherent::ParachainInherentData;
use cumulus_relay_chain_interface::RelayChainInterface;
use parity_scale_codec::{Codec, Encode};

use polkadot_node_primitives::{Collation, MaybeCompressedPoV};
use polkadot_primitives::Id as ParaId;

use crate::{find_pre_digest, AuthorityId, OrchestratorAuraWorkerAuxData};
use futures::prelude::*;
use nimbus_primitives::{CompatibleDigestItem as NimbusCompatibleDigestItem, NIMBUS_KEY_ID};
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, StateAction};
use sp_application_crypto::{AppCrypto, AppPublic};
use sp_consensus::BlockOrigin;
use sp_consensus_aura::{digests::CompatibleDigestItem, Slot};
use sp_core::crypto::{ByteArray, Pair};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_keystore::{Keystore, KeystorePtr};
use sp_runtime::{
generic::Digest,
traits::{Block as BlockT, HashingFor, Header as HeaderT, Member, Zero},
};
use sp_state_machine::StorageChanges;
use sp_timestamp::Timestamp;
use std::{convert::TryFrom, error::Error, time::Duration};

/// Parameters for instantiating a [`Collator`].
pub struct Params<BI, CIDP, RClient, Proposer, CS> {
Expand Down
63 changes: 32 additions & 31 deletions client/consensus/src/collators/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,39 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>.

use cumulus_client_collator::{
relay_chain_driven::CollationRequest, service::ServiceInterface as CollatorServiceInterface,
use {
crate::{
collators as collator_util, consensus_orchestrator::RetrieveAuthoritiesFromOrchestrator,
OrchestratorAuraWorkerAuxData,
},
cumulus_client_collator::{
relay_chain_driven::CollationRequest, service::ServiceInterface as CollatorServiceInterface,
},
cumulus_client_consensus_proposer::ProposerInterface,
cumulus_primitives_core::{
relay_chain::{BlockId as RBlockId, Hash as PHash},
PersistedValidationData,
},
cumulus_relay_chain_interface::RelayChainInterface,
futures::{channel::mpsc::Receiver, prelude::*},
parity_scale_codec::{Codec, Decode},
polkadot_node_primitives::CollationResult,
polkadot_overseer::Handle as OverseerHandle,
polkadot_primitives::{CollatorPair, Id as ParaId},
sc_client_api::{backend::AuxStore, BlockBackend, BlockOf},
sc_consensus::BlockImport,
sc_consensus_slots::InherentDataProviderExt,
sp_api::ProvideRuntimeApi,
sp_application_crypto::AppPublic,
sp_blockchain::HeaderBackend,
sp_consensus::SyncOracle,
sp_consensus_aura::SlotDuration,
sp_core::crypto::Pair,
sp_inherents::CreateInherentDataProviders,
sp_keystore::KeystorePtr,
sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member},
std::{convert::TryFrom, sync::Arc, time::Duration},
};
use cumulus_client_consensus_proposer::ProposerInterface;
use cumulus_primitives_core::{
relay_chain::{BlockId as RBlockId, Hash as PHash},
PersistedValidationData,
};
use cumulus_relay_chain_interface::RelayChainInterface;
use parity_scale_codec::{Codec, Decode};

use polkadot_node_primitives::CollationResult;
use polkadot_overseer::Handle as OverseerHandle;
use polkadot_primitives::{CollatorPair, Id as ParaId};

use futures::{channel::mpsc::Receiver, prelude::*};
use sc_client_api::{backend::AuxStore, BlockBackend, BlockOf};
use sc_consensus::BlockImport;
use sc_consensus_slots::InherentDataProviderExt;
use sp_api::ProvideRuntimeApi;
use sp_application_crypto::AppPublic;
use sp_blockchain::HeaderBackend;
use sp_consensus::SyncOracle;
use sp_consensus_aura::SlotDuration;
use sp_core::crypto::Pair;
use sp_inherents::CreateInherentDataProviders;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member};
use std::{convert::TryFrom, sync::Arc, time::Duration};

use crate::consensus_orchestrator::RetrieveAuthoritiesFromOrchestrator;
use crate::{collators as collator_util, OrchestratorAuraWorkerAuxData};

/// Parameters for [`run`].
pub struct Params<BI, CIDP, Client, RClient, SO, Proposer, CS, GOH> {
Expand Down
4 changes: 1 addition & 3 deletions client/consensus/src/consensus_orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
//! the ParachainConsensus trait to access the orchestrator-dicated authorities, and further
//! it implements the TanssiWorker to TanssiOnSlot trait. This trait is
use {
crate::AuthorityId,
crate::Pair,
crate::Slot,
crate::{AuthorityId, Pair, Slot},
sc_consensus_slots::{SimpleSlotWorker, SlotInfo, SlotResult},
sp_consensus::Proposer,
sp_runtime::traits::Block as BlockT,
Expand Down
16 changes: 9 additions & 7 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
//! slot_author returns the author based on the slot number and authorities provided (aura-like)
//! authorities retrieves the current set of authorities based on the first eligible key found in the keystore
use {sp_consensus_slots::Slot, sp_core::crypto::Pair};

pub mod collators;
mod consensus_orchestrator;
mod manual_seal;

#[cfg(test)]
mod tests;

pub use crate::consensus_orchestrator::OrchestratorAuraWorkerAuxData;
pub use sc_consensus_aura::CompatibilityMode;

pub use {
crate::consensus_orchestrator::OrchestratorAuraWorkerAuxData,
cumulus_primitives_core::ParaId,
manual_seal::{
get_aura_id_from_seed, ContainerManualSealAuraConsensusDataProvider,
OrchestratorManualSealAuraConsensusDataProvider,
},
pallet_registrar_runtime_api::OnDemandBlockProductionApi,
parity_scale_codec::{Decode, Encode},
sc_consensus_aura::find_pre_digest,
sc_consensus_aura::{slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotProportion},
sc_consensus_aura::{
find_pre_digest, slot_duration, AuraVerifier, BuildAuraWorkerParams, CompatibilityMode,
SlotProportion,
},
sc_consensus_slots::InherentDataProviderExt,
sp_api::{Core, ProvideRuntimeApi},
sp_application_crypto::AppPublic,
Expand All @@ -51,6 +50,9 @@ pub use {
std::hash::Hash,
tp_consensus::TanssiAuthorityAssignmentApi,
};

use {sp_consensus_slots::Slot, sp_core::crypto::Pair};

const LOG_TARGET: &str = "aura::tanssi";

type AuthorityId<P> = <P as Pair>::Public;
Expand Down
6 changes: 5 additions & 1 deletion pallets/collator-assignment/src/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ use {
cmp,
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
marker::PhantomData,
mem, vec,
mem,
vec::Vec,
},
tp_traits::{ParaId, RemoveInvulnerables as RemoveInvulnerablesT},
};

// Separate import of `sp_std::vec!` macro, which cause issues with rustfmt if grouped
// with `sp_std::vec::Vec`.
use sp_std::vec;

/// Helper methods to implement collator assignment algorithm
pub struct Assignment<T>(PhantomData<T>);

Expand Down
3 changes: 1 addition & 2 deletions pallets/registrar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ use {

#[frame_support::pallet]
pub mod pallet {
use super::*;
use tp_traits::SessionContainerChains;
use {super::*, tp_traits::SessionContainerChains};

#[pallet::pallet]
#[pallet::without_storage_info]
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",
]
Loading

0 comments on commit 90433bb

Please sign in to comment.