Skip to content

Commit

Permalink
Rename shared to virto-common
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Dec 15, 2023
1 parent b308454 commit 4e8f8e3
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 114 deletions.
124 changes: 66 additions & 58 deletions Cargo.lock

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

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
members = [
"node",
"runtime/kreivo",
"runtime/virto"
"shared",
"runtime/virto",
"common",
]

exclude = ["xcm-emulator"]
Expand Down Expand Up @@ -58,8 +58,7 @@ pallet-lockdown-mode = { default-features = false, path = "pallets/lockdown-mode
pallet-payments = { default-features = false, path = "pallets/payments" }
pallet-communities = { default-features = false, path = "pallets/communities" }

# Virto common
virto-common = { default-features = false, path = "runtime/common" }
runtime-common = { default-features = false, path = "runtime/common" }

# Substrate std
try-runtime-cli = { git = "https://github.com/virto-network/polkadot-sdk", branch = "virto-crates-io-v1.2.0" }
Expand Down
1 change: 1 addition & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out/
5 changes: 4 additions & 1 deletion shared/Cargo.toml → common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[package]
name = "virto-shared"
name = "virto-common"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
bs58 = { version = "0.5.0", default-features = false }
wasm-bindgen = { version = "0.2.87", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Virto Common

This repo holds common primitive types that are shared between the parachain
runtime and client side applications including the web and `no_std` embedded targets.
7 changes: 7 additions & 0 deletions common/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build-web:
@mkdir -p out
RUSTFLAGS="--cfg nightly" cargo +nightly build --release --target wasm32-unknown-unknown --features js --out-dir out -Z unstable-options
wasm-bindgen --out-dir out --target web --no-typescript --remove-name-section out/virto_common.wasm

test:
RUSTFLAGS="--cfg nightly" cargo +nightly test --lib test
8 changes: 8 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(nightly, feature(ascii_char))]

#[cfg(feature = "alloc")]
extern crate alloc;

mod payment_id;
pub use payment_id::PaymentId;
20 changes: 12 additions & 8 deletions shared/src/payment_id.rs → common/src/payment_id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{fmt, str::FromStr};
#[cfg(feature = "js")]
use wasm_bindgen::prelude::*;

/// A compact identifier for payment
#[cfg_attr(feature = "js", wasm_bindgen)]
#[derive(Debug, Default, Clone, Copy, PartialEq)]
#[repr(C)]
Expand All @@ -14,6 +14,7 @@ pub struct PaymentId {
#[cfg_attr(feature = "js", wasm_bindgen)]
impl PaymentId {
#[cfg_attr(feature = "js", wasm_bindgen(constructor))]
#[cfg(nightly)]
pub fn new(id: &str) -> PaymentId {
id.parse().unwrap_or(Default::default())
}
Expand All @@ -33,7 +34,7 @@ impl PaymentId {
self.index as u32
}

#[cfg(feature = "alloc")]
#[cfg(all(nightly, feature = "alloc"))]
pub fn encode(&self, pretty: bool) -> alloc::string::String {
if pretty {
alloc::format!("{self:#}")
Expand Down Expand Up @@ -90,7 +91,8 @@ impl AsRef<[u8]> for PaymentId {
}
}

impl FromStr for PaymentId {
#[cfg(nightly)]
impl core::str::FromStr for PaymentId {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -109,11 +111,13 @@ impl FromStr for PaymentId {
}
}

impl fmt::Display for PaymentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(nightly)]
impl core::fmt::Display for PaymentId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use core::fmt::Error;
let mut out = [0u8; 12];
let n = bs58::encode(self).onto(&mut out[..]).map_err(|_| fmt::Error)?;
let out = out[..n].as_ascii().ok_or(fmt::Error)?.as_str();
let n = bs58::encode(self).onto(&mut out[..]).map_err(|_| Error)?;
let out = out[..n].as_ascii().ok_or(Error)?.as_str();
write!(f, "{}", &out[..5])?;
if f.alternate() {
write!(f, "-")?;
Expand All @@ -122,7 +126,7 @@ impl fmt::Display for PaymentId {
}
}

#[cfg(test)]
#[cfg(all(test, nightly))]
mod tests {
extern crate alloc;
use super::*;
Expand Down
12 changes: 4 additions & 8 deletions shared/test.html → common/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@
}

output {
flex: 1;
background: whitesmoke;
padding: 0 0.5rem;
line-height: 2.5rem;
color: gray;
font-family: monospace;
line-height: 2.5rem;
min-width: fit-content;
overflow: hidden;
padding: 0 0.5rem;
text-overflow: ellipsis;
white-space: nowrap;
}

output[name=id]:before {
content: '🆔 ';
}
</style>
</head>

Expand All @@ -63,7 +59,7 @@
</body>

<script type="module">
import init, {PaymentId} from '../target/wasm32-unknown-unknown/release/virto_shared.js'
import init, {PaymentId} from './out/virto_common.js'
init().then(() => console.info('wasm ready'))

const form = document.idConvert
Expand Down
5 changes: 2 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ _task-selector:
rustup component add clippy

check: _check_deps
cargo clippy --all-targets -- --deny warnings
cargo fmt --all -- --check

cargo clippy -p kreivo-runtime -p virto-node -- --deny warnings
cargo +nightly fmt --all -- --check

@test crate="" *rest="":
cargo test (if not ("{{crate}}" | is-empty) { "-p" } else {""}) {{crate}} {{ rest }}
Expand Down
7 changes: 3 additions & 4 deletions pallets/asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ pallet-balances = { workspace = true }
xcm = { workspace = true }
pallet-xcm = { workspace = true }


virto-common = { workspace = true }
runtime-common = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
Expand All @@ -44,9 +43,9 @@ std = [
"frame-support/std",
"frame-system/std",
"scale-info/std",
"virto-common/std",
"runtime-common/std",
"xcm/std",
"pallet-xcm/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
try-runtime = ["frame-support/try-runtime"]
2 changes: 1 addition & 1 deletion pallets/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use virto_common::impls::AssetMultiLocationGetter;
pub use runtime_common::impls::AssetMultiLocationGetter;
pub use weights::*;

#[frame_support::pallet]
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "virto-common"
name = "runtime-common"
description = "Common logic for all virto runtimes"
version = "0.1.0"
authors = ['Virto Team <[email protected]>']
Expand Down
Loading

0 comments on commit 4e8f8e3

Please sign in to comment.