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

Add signature verification + additional validation in xcm-core-buyer #559

Merged
merged 8 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pallet-services-payment-runtime-api = { path = "pallets/services-payment/runtime
pallet-stream-payment = { path = "pallets/stream-payment", default-features = false }
pallet-stream-payment-runtime-api = { path = "pallets/stream-payment/runtime-api", default-features = false }
pallet-xcm-core-buyer = { path = "pallets/xcm-core-buyer", default-features = false }
pallet-xcm-core-buyer-runtime-api = { path = "pallets/xcm-core-buyer/runtime-api", default-features = false }

container-chain-template-frontier-runtime = { path = "container-chains/runtime-templates/frontier", default-features = false }
container-chain-template-simple-runtime = { path = "container-chains/runtime-templates/simple", default-features = false }
Expand All @@ -83,6 +84,7 @@ tp-container-chain-genesis-data = { path = "primitives/container-chain-genesis-d
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 }
tp-xcm-core-buyer = { path = "primitives/xcm-core-buyer", default-features = false }

# Dancekit (wasm)
ccp-authorities-noting-inherent = { git = "https://github.com/moondance-labs/dancekit", branch = "tanssi-polkadot-v1.6.0", default-features = false }
Expand Down
12 changes: 5 additions & 7 deletions client/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ where
{
let runtime_api = client.runtime_api();

let min_slot_freq = runtime_api.min_slot_freq(*parent_hash, para_id).ok()?;
log::debug!(
"min_slot_freq for para {:?} is {:?}",
para_id,
min_slot_freq
);
min_slot_freq
let slot_frequency = runtime_api
.parathread_slot_frequency(*parent_hash, para_id)
.ok()?;
log::debug!("slot_freq for para {:?} is {:?}", para_id, slot_frequency);
slot_frequency.map(|slot_frequency| u64::from(slot_frequency.min).into())
}

use nimbus_primitives::{NimbusId, NimbusPair, NIMBUS_KEY_ID};
Expand Down
33 changes: 17 additions & 16 deletions pallets/author-noting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ use {
sp_inherents::{InherentIdentifier, IsFatalError},
sp_runtime::{traits::Header, DigestItem, DispatchResult, RuntimeString},
tp_author_noting_inherent::INHERENT_IDENTIFIER,
tp_traits::{AuthorNotingHook, GetContainerChainAuthor, GetCurrentContainerChains},
tp_traits::{
AuthorNotingHook, ContainerChainBlockInfo, GetContainerChainAuthor,
GetCurrentContainerChains,
},
};

#[cfg(test)]
Expand All @@ -63,6 +66,7 @@ mod benchmarks;
mod mock_proof;

pub use pallet::*;
use tp_traits::LatestAuthorInfoFetcher;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -175,7 +179,9 @@ pub mod pallet {
Ok(block_info) => {
LatestAuthor::<T>::mutate(
para_id,
|maybe_old_block_info: &mut Option<ContainerChainBlockInfo<T>>| {
|maybe_old_block_info: &mut Option<
ContainerChainBlockInfo<T::AccountId>,
>| {
if let Some(ref mut old_block_info) = maybe_old_block_info {
if block_info.block_number > old_block_info.block_number {
// We only reward author if the block increases
Expand Down Expand Up @@ -277,18 +283,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn latest_author)]
pub(super) type LatestAuthor<T: Config> =
StorageMap<_, Blake2_128Concat, ParaId, ContainerChainBlockInfo<T>, OptionQuery>;

/// Information extracted from the latest container chain header
#[derive(
Clone, Encode, Decode, PartialEq, sp_core::RuntimeDebug, scale_info::TypeInfo, MaxEncodedLen,
)]
#[scale_info(skip_type_params(T))]
pub struct ContainerChainBlockInfo<T: Config> {
pub block_number: BlockNumber,
pub author: T::AccountId,
pub latest_slot_number: Slot,
}
StorageMap<_, Blake2_128Concat, ParaId, ContainerChainBlockInfo<T::AccountId>, OptionQuery>;

/// Was the containerAuthorData set?
#[pallet::storage]
Expand Down Expand Up @@ -331,7 +326,7 @@ impl<T: Config> Pallet<T> {
relay_state_proof: &GenericStateProof<cumulus_primitives_core::relay_chain::Block>,
para_id: ParaId,
tanssi_slot: Slot,
) -> Result<ContainerChainBlockInfo<T>, Error<T>> {
) -> Result<ContainerChainBlockInfo<T::AccountId>, Error<T>> {
let bytes = para_id.twox_64_concat();
// CONCAT
let key = [PARAS_HEADS_INDEX, bytes.as_slice()].concat();
Expand Down Expand Up @@ -378,7 +373,7 @@ impl<T: Config> Pallet<T> {
para_id: ParaId,
author_header: &sp_runtime::generic::Header<BlockNumber, BlakeTwo256>,
tanssi_slot: Slot,
) -> Result<ContainerChainBlockInfo<T>, Error<T>> {
) -> Result<ContainerChainBlockInfo<T::AccountId>, Error<T>> {
// We decode the digest as pre-runtime digest
let (id, mut data) = aura_digest
.as_pre_runtime()
Expand Down Expand Up @@ -433,3 +428,9 @@ impl InherentError {
}
}
}

impl<T: Config> LatestAuthorInfoFetcher<T::AccountId> for Pallet<T> {
fn get_latest_author_info(para_id: ParaId) -> Option<ContainerChainBlockInfo<T::AccountId>> {
LatestAuthor::<T>::get(para_id)
}
}
2 changes: 1 addition & 1 deletion pallets/authority-mapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn authority_id_mapping)]
pub(super) type AuthorityIdMapping<T: Config> = StorageMap<
pub type AuthorityIdMapping<T: Config> = StorageMap<
_,
Twox64Concat,
T::SessionIndex,
Expand Down
2 changes: 2 additions & 0 deletions pallets/registrar/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-api = { workspace = true }
tp-container-chain-genesis-data = { workspace = true }
tp-traits = { workspace = true }

[features]
default = [ "std" ]
Expand All @@ -29,4 +30,5 @@ std = [
"scale-info/std",
"sp-api/std",
"tp-container-chain-genesis-data/std",
"tp-traits/std",
]
13 changes: 9 additions & 4 deletions pallets/registrar/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use tp_container_chain_genesis_data::ContainerChainGenesisData;
use tp_traits::SlotFrequency;
use {frame_support::traits::Get, scale_info::prelude::vec::Vec};

sp_api::decl_runtime_apis! {
Expand All @@ -42,13 +43,17 @@ sp_api::decl_runtime_apis! {
ParaId: parity_scale_codec::Codec,
Slot: parity_scale_codec::Codec,
{
/// Return the minimum number of slots that must pass between to blocks before parathread collators can propose
/// the next block.
/// Returns slot frequency for particular para thread. Slot frequency specifies amount of slot
/// need to be passed between two parathread blocks. It is expressed as `(min, max)` pair where `min`
/// indicates amount of slot must pass before we produce another block and `max` indicates amount of
/// blocks before this parathread must produce the block.
///
/// Simply put, parathread must produce a block after `min` but before `(min+max)` slots.
///
/// # Returns
///
/// * `Some(min)`, where the condition for the slot to be valid is `(slot - parent_slot) >= min`.
/// * `Some(slot_frequency)`.
/// * `None` if the `para_id` is not a parathread.
fn min_slot_freq(para_id: ParaId) -> Option<Slot>;
fn parathread_slot_frequency(para_id: ParaId) -> Option<SlotFrequency>;
}
}
7 changes: 7 additions & 0 deletions pallets/xcm-core-buyer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ dp-core = { workspace = true }
log = { workspace = true }
serde = { workspace = true, optional = true }
tp-traits = { workspace = true }
tp-xcm-core-buyer = { workspace = true }

# Polkadot
pallet-xcm = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-keystore = { workspace = true }
staging-xcm = { workspace = true }

# Substrate
Expand Down Expand Up @@ -57,13 +60,16 @@ std = [
"scale-info/std",
"serde",
"serde?/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-io/std",
"sp-keystore/std",
"sp-runtime/std",
"sp-std/std",
"staging-xcm/std",
"staging-xcm/std",
"tp-traits/std",
"tp-xcm-core-buyer/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand All @@ -75,6 +81,7 @@ runtime-benchmarks = [
"pallet-xcm/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"tp-traits/runtime-benchmarks",
"tp-xcm-core-buyer/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
35 changes: 35 additions & 0 deletions pallets/xcm-core-buyer/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "pallet-xcm-core-buyer-runtime-api"
authors = { workspace = true }
description = "Runtime api for xcm-core-buyer runtime api"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

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

[lints]
workspace = true

[dependencies]
frame-support = { workspace = true }
pallet-xcm-core-buyer = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true, optional = true, features = [ "derive" ] }
sp-api = { workspace = true }
thiserror = { workspace = true, optional = true }

[features]
default = [ "std" ]
std = [
"frame-support/std",
"pallet-xcm-core-buyer/std",
"parity-scale-codec/std",
"scale-info/std",
"serde",
"serde?/std",
"sp-api/std",
"thiserror",
]
27 changes: 27 additions & 0 deletions pallets/xcm-core-buyer/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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/>

//! Runtime API for XCM core buyer pallet

#![cfg_attr(not(feature = "std"), no_std)]

use pallet_xcm_core_buyer::BuyingError;

sp_api::decl_runtime_apis! {
girazoki marked this conversation as resolved.
Show resolved Hide resolved
pub trait XCMCoreBuyerApi<BlockNumber, ParaId> where ParaId: parity_scale_codec::Codec, BlockNumber: parity_scale_codec::Codec, BuyingError<BlockNumber>: parity_scale_codec::Codec {
fn is_core_buying_allowed(para_id: ParaId) -> Result<(), BuyingError<BlockNumber>>;
}
}
Loading
Loading