Skip to content

Commit

Permalink
WIP: pallet rws, pallet digital twin
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuanyshbaev committed Feb 13, 2025
1 parent 72b27a6 commit faf8635
Show file tree
Hide file tree
Showing 21 changed files with 5,965 additions and 2,660 deletions.
7,816 changes: 5,495 additions & 2,321 deletions Cargo.lock

Large diffs are not rendered by default.

219 changes: 111 additions & 108 deletions Cargo.toml

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions frame/digital-twin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
//! Digital twin runtime module. This can be compiled with `#[no_std]`, ready for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
Expand Down Expand Up @@ -63,7 +61,6 @@ pub mod pallet {
StorageMap<_, Twox64Concat, u32, BTreeMap<H256, <T as frame_system::Config>::AccountId>>;

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

Expand Down
57 changes: 36 additions & 21 deletions frame/rws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::pallet_prelude::Weight;
use parity_scale_codec::{Decode, Encode, HasCompact};
use parity_scale_codec::{Decode, Encode, HasCompact, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;

//#[cfg(test)]
//mod tests;

pub use pallet::*;

#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub enum Subscription {
/// Lifetime subscription.
Lifetime {
Expand All @@ -52,8 +50,8 @@ impl Default for Subscription {
}
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
pub struct AuctionLedger<AccountId, Balance: HasCompact> {
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub struct AuctionLedger<AccountId: MaxEncodedLen, Balance: HasCompact + MaxEncodedLen> {
/// Auction winner address.
pub winner: Option<AccountId>,
/// Current best price.
Expand All @@ -63,7 +61,9 @@ pub struct AuctionLedger<AccountId, Balance: HasCompact> {
pub kind: Subscription,
}

impl<AccountId, Balance: HasCompact + Default> AuctionLedger<AccountId, Balance> {
impl<AccountId: MaxEncodedLen, Balance: HasCompact + MaxEncodedLen + Default>
AuctionLedger<AccountId, Balance>
{
pub fn new(kind: Subscription) -> Self {
Self {
winner: None,
Expand All @@ -81,8 +81,8 @@ impl<AccountId, Balance: HasCompact + Default> AuctionLedger<AccountId, Balance>
}
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
pub struct SubscriptionLedger<Moment: HasCompact> {
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub struct SubscriptionLedger<Moment: HasCompact + MaxEncodedLen> {
/// Free execution weights accumulator.
#[codec(compact)]
free_weight: u64,
Expand All @@ -96,7 +96,7 @@ pub struct SubscriptionLedger<Moment: HasCompact> {
kind: Subscription,
}

impl<Moment: HasCompact + Clone> SubscriptionLedger<Moment> {
impl<Moment: HasCompact + MaxEncodedLen + Clone> SubscriptionLedger<Moment> {
pub fn new(last_update: Moment, kind: Subscription) -> Self {
Self {
free_weight: Default::default(),
Expand Down Expand Up @@ -127,6 +127,7 @@ pub mod pallet {
>>::Balance;

const DAYS_TO_MS: u32 = 24 * 60 * 60 * 1000;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::config]
pub trait Config: frame_system::Config {
Expand All @@ -137,9 +138,9 @@ pub mod pallet {
/// Current time source.
type Time: Time<Moment = Self::Moment>;
/// Time should be aligned to weights for TPS calculations.
type Moment: Parameter + AtLeast32Bit + Into<u64>;
type Moment: Parameter + AtLeast32Bit + Into<u64> + MaxEncodedLen;
/// The auction index value.
type AuctionIndex: Parameter + AtLeast32Bit + Default;
type AuctionIndex: Parameter + AtLeast32Bit + Default + MaxEncodedLen;
/// The auction bid currency.
type AuctionCurrency: ReservableCurrency<Self::AccountId>;
/// The overarching event type.
Expand All @@ -156,6 +157,10 @@ pub mod pallet {
/// Minimal auction bid.
#[pallet::constant]
type MinimalBid: Get<BalanceOf<Self>>;
#[pallet::constant]
type MaxDevicesAmount: Get<u32>;
#[pallet::constant]
type MaxAuctionIndexesAmount: Get<u32>;
}

#[pallet::error]
Expand Down Expand Up @@ -205,13 +210,19 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn devices)]
/// Subscription linked devices.
pub(super) type Devices<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, Vec<T::AccountId>, ValueQuery>;
pub(super) type Devices<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
BoundedVec<T::AccountId, T::MaxDevicesAmount>,
ValueQuery,
>;

/// Ongoing subscription auctions.
#[pallet::storage]
#[pallet::getter(fn auction_queue)]
pub(super) type AuctionQueue<T: Config> = StorageValue<_, Vec<T::AuctionIndex>, ValueQuery>;
pub(super) type AuctionQueue<T: Config> =
StorageValue<_, BoundedVec<T::AuctionIndex, T::MaxAuctionIndexesAmount>, ValueQuery>;

/// Next auction index.
#[pallet::storage]
Expand All @@ -232,8 +243,7 @@ pub mod pallet {
pub(super) type UnspendBondValue<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

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

#[pallet::hooks]
Expand Down Expand Up @@ -335,11 +345,11 @@ pub mod pallet {
#[pallet::weight(100_000)]
pub fn set_devices(
origin: OriginFor<T>,
devices: Vec<T::AccountId>,
devices: BoundedVec<T::AccountId, T::MaxDevicesAmount>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
<Devices<T>>::insert(sender.clone(), devices.clone());
Self::deposit_event(Event::NewDevices(sender, devices));
Self::deposit_event(Event::NewDevices(sender, devices.to_vec()));
Ok(().into())
}

Expand Down Expand Up @@ -423,7 +433,7 @@ pub mod pallet {
<Auction<T>>::insert(&index, AuctionLedger::new(kind.clone()));

// insert auction into queue
<AuctionQueue<T>>::mutate(|queue| queue.push(index.clone()));
<AuctionQueue<T>>::mutate(|queue| queue.try_push(index.clone()));

// deposit descriptive event
Self::deposit_event(Event::NewAuction(kind, index));
Expand All @@ -443,7 +453,12 @@ pub mod pallet {
.partition(|(_, auction)| auction.winner.is_some());

// store auction indexes without bids to queue
<AuctionQueue<T>>::put(next.iter().map(|(i, _)| i).collect::<Vec<_>>());
//<AuctionQueue<T>>::put(next.iter().map(|(i, _)| i).collect::<Vec<_>>());
let mut indexes_without_bids = BoundedVec::new();
next.iter()
.map(|(i, _)| indexes_without_bids.try_push(i.clone()));
<AuctionQueue<T>>::put(indexes_without_bids);
//------------------------------------------------

for (_, auction) in finished.iter() {
if let Some(subscription_id) = &auction.winner {
Expand Down
4 changes: 2 additions & 2 deletions frame/xcm-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
xcm = { workspace = true }
staging-xcm = { workspace = true }

[features]
default = ["std"]
Expand All @@ -25,5 +25,5 @@ std = [
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
"xcm/std",
"staging-xcm/std",
]
5 changes: 4 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ sc-storage-monitor = { workspace = true }
sc-sysinfo = { workspace = true }
sc-telemetry = { workspace = true }
sp-core = { workspace = true }
sc-network = { workspace = true }
sp-runtime = { workspace = true }
frame-benchmarking-cli = { workspace = true }
sp-genesis-builder = { workspace = true }

# polkadot
# polkadot
polkadot-cli = { workspace = true }

# cumulus
cumulus-client-cli = { workspace = true }
cumulus-client-service = { workspace = true }
cumulus-primitives-core = { workspace = true }

[build-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions node/generic-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult,
ApplyExtrinsicResult, ExtrinsicInclusionMode,
};
use sp_version::RuntimeVersion;

Expand All @@ -42,7 +42,7 @@ impl_runtime_apis! {
unimplemented!()
}

fn initialize_block(_header: &<Block as BlockT>::Header) {
fn initialize_block(_header: &<Block as BlockT>::Header) -> ExtrinsicInclusionMode {
unimplemented!()
}
}
Expand Down
26 changes: 14 additions & 12 deletions node/rpc/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,32 @@ use std::sync::Arc;

use robonomics_primitives::{AccountId, Balance, Block, Nonce};

use jsonrpsee::RpcModule;
use sc_client_api::AuxStore;
pub use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};

/// A type representing all RPC extensions.
pub type RpcExtension = jsonrpsee::RpcModule<()>;

/// Core client dependencies.
pub struct CoreDeps<C, P> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Whether to deny unsafe calls.
pub deny_unsafe: DenyUnsafe,
// /// Whether to deny unsafe calls.
// pub deny_unsafe: DenyUnsafe,
/// RPC extensions
pub ext_rpc: RpcModule<()>,
pub ext_rpc: RpcExtension,
}

/// Instantiate Core RPC extensions.
pub fn create_core_rpc<C, P>(
deps: CoreDeps<C, P>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ HeaderBackend<Block>
Expand All @@ -53,24 +55,24 @@ where
+ Sync
+ Send
+ 'static,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
+ BlockBuilder<Block>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
C::Api: BlockBuilder<Block>,
P: TransactionPool + Sync + Send + 'static,
{
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};

let mut io = RpcModule::new(());
let mut io = RpcExtension::new(());
let CoreDeps {
client,
pool,
deny_unsafe,
// deny_unsafe,
ext_rpc,
} = deps;

io.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?;
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
io.merge(System::new(client.clone(), pool).into_rpc())?;
io.merge(TransactionPayment::new(client).into_rpc())?;
io.merge(ext_rpc)?;

Ok(io)
Expand Down
1 change: 1 addition & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cumulus-client-consensus-common = { workspace = true }
cumulus-client-consensus-relay-chain = { workspace = true }
cumulus-client-network = { workspace = true }
cumulus-client-service = { workspace = true }
cumulus-client-parachain-inherent = { workspace = true }
cumulus-primitives-core = { workspace = true }
cumulus-primitives-parachain-inherent = { workspace = true }
cumulus-relay-chain-interface = { workspace = true }
Expand Down
Loading

0 comments on commit faf8635

Please sign in to comment.