Skip to content

Commit

Permalink
Simple metadata to distinguish parachain template from standalone tem…
Browse files Browse the repository at this point in the history
…plate (#211)
  • Loading branch information
JoshOrndorff authored Apr 22, 2024
1 parent 9071010 commit eed8882
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
2 changes: 2 additions & 0 deletions tuxedo-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod executive;
pub mod constraint_checker;
pub mod genesis;
pub mod inherents;
pub mod metadata;
pub mod support_macros;
pub mod traits;
pub mod types;
Expand All @@ -21,6 +22,7 @@ pub use aggregator::{aggregate, tuxedo_constraint_checker, tuxedo_verifier};
pub use constraint_checker::{ConstraintChecker, SimpleConstraintChecker};
pub use executive::Executive;
pub use inherents::{InherentAdapter, InherentHooks};
pub use metadata::TuxedoMetadata;
pub use verifier::Verifier;

/// A Tuxedo-specific target for diagnostic node log messages
Expand Down
24 changes: 24 additions & 0 deletions tuxedo-core/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! A simple type to use as metadata. For now the metadata just communicates whether we
//! are dealing with a parachain or not.

use parity_scale_codec::{Decode, Encode};
#[derive(Default, Debug, Encode, Decode)]
pub struct TuxedoMetadata {
/// Placeholder for the scale info type registry that will hopefully eventually go here.
_registry: (),
/// Indicator of whether this chain is a parachain or not.
parachain: bool,
}

impl TuxedoMetadata {
pub fn new_parachain() -> Self {
Self {
_registry: (),
parachain: true,
}
}

pub fn is_parachain(&self) -> bool {
self.parachain
}
}
6 changes: 3 additions & 3 deletions tuxedo-parachain-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tuxedo_core::{
genesis::TuxedoGenesisConfigBuilder, tuxedo_constraint_checker, types::Block as TuxedoBlock,
types::Transaction as TuxedoTransaction, InherentAdapter,
};
use tuxedo_parachain_core::tuxedo_core;
use tuxedo_parachain_core::tuxedo_core::{self, TuxedoMetadata};

// We use the same aggregate verifier and opaque types from the inner_runtime.
// They do not contain anything parachain specific.
Expand Down Expand Up @@ -149,10 +149,10 @@ impl_runtime_apis! {
}
}

// Tuxedo does not yet support metadata
// Tuxedo metadata is pretty trivial atm
impl sp_api::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Default::default())
OpaqueMetadata::new(TuxedoMetadata::new_parachain().encode())
}

fn metadata_at_version(_version: u32) -> Option<OpaqueMetadata> {
Expand Down
6 changes: 3 additions & 3 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use tuxedo_core::{
tuxedo_constraint_checker, tuxedo_verifier,
types::Transaction as TuxedoTransaction,
verifier::{Sr25519Signature, ThresholdMultiSignature, UpForGrabs},
InherentAdapter,
InherentAdapter, TuxedoMetadata,
};

pub use amoeba;
Expand Down Expand Up @@ -273,10 +273,10 @@ impl_runtime_apis! {
}
}

// Tuxedo does not yet support metadata
// Tuxedo metadata is pretty trivial atm
impl sp_api::Metadata<Block> for Runtime {
fn metadata() -> OpaqueMetadata {
OpaqueMetadata::new(Default::default())
OpaqueMetadata::new(TuxedoMetadata::default().encode())
}

fn metadata_at_version(_version: u32) -> Option<OpaqueMetadata> {
Expand Down
4 changes: 0 additions & 4 deletions wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pub struct Cli {
/// The keystore will contain the development key Shawn.
pub dev: bool,

/// Use the Parachain template encoding instead of the regular node template encoding.
#[arg(long, short, verbatim_doc_comment)]
pub parachain: bool,

#[command(subcommand)]
pub command: Option<Command>,
}
Expand Down
15 changes: 10 additions & 5 deletions wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ async fn main() -> anyhow::Result<()> {
// https://github.com/paritytech/jsonrpsee/blob/master/examples/examples/http.rs
let client = HttpClientBuilder::default().build(cli.endpoint)?;

// Fetch the metadata and determine whether we are dealing with a parachain.
let metadata = rpc::node_get_metadata(&client).await?;

// Read node's genesis block.
let node_genesis_hash = rpc::node_get_block_hash(0, &client)
.await?
Expand Down Expand Up @@ -80,7 +83,7 @@ async fn main() -> anyhow::Result<()> {

if !sled::Db::was_recovered(&db) {
// This is a new instance, so we need to apply the genesis block to the database.
if cli.parachain {
if metadata.is_parachain() {
sync::apply_block::<_, ParachainConstraintChecker>(
&db,
node_genesis_block,
Expand All @@ -103,7 +106,7 @@ async fn main() -> anyhow::Result<()> {
if cli.no_sync {
log::warn!("Skipping sync with node. Using previously synced information.")
} else {
sync::synchronize(cli.parachain, &db, &client, &keystore_filter).await?;
sync::synchronize(metadata.is_parachain(), &db, &client, &keystore_filter).await?;

log::info!(
"Wallet database synchronized with node to height {:?}",
Expand All @@ -113,9 +116,11 @@ async fn main() -> anyhow::Result<()> {

// Dispatch to proper subcommand
match cli.command {
Some(Command::AmoebaDemo) => amoeba::amoeba_demo(cli.parachain, &client).await,
Some(Command::AmoebaDemo) => amoeba::amoeba_demo(metadata.is_parachain(), &client).await,
// Command::MultiSigDemo => multi_sig::multi_sig_demo(&client).await,
Some(Command::MintCoins(args)) => money::mint_coins(cli.parachain, &client, args).await,
Some(Command::MintCoins(args)) => {
money::mint_coins(metadata.is_parachain(), &client, args).await
}
Some(Command::VerifyCoin { output_ref }) => {
println!("Details of coin {}:", hex::encode(output_ref.encode()));

Expand All @@ -138,7 +143,7 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
Some(Command::SpendCoins(args)) => {
money::spend_coins(cli.parachain, &db, &client, &keystore, args).await
money::spend_coins(metadata.is_parachain(), &db, &client, &keystore, args).await
}
Some(Command::InsertKey { seed }) => crate::keystore::insert_key(&keystore, &seed),
Some(Command::GenerateKey { password }) => {
Expand Down
18 changes: 17 additions & 1 deletion wallet/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ use parity_scale_codec::{Decode, Encode};
use sp_core::H256;
use tuxedo_core::{
types::{OpaqueBlock, Output, OutputRef},
Verifier,
TuxedoMetadata, Verifier,
};

/// Get the node's metadata
pub async fn node_get_metadata(client: &HttpClient) -> anyhow::Result<TuxedoMetadata> {
// Don't provide a block height to use the best height.
let params = rpc_params![Option::<u32>::None];
let rpc_response: Option<String> = client.request("state_getMetadata", params).await?;
let metadata = metadata_from_string(&rpc_response.expect("metadata should be available."))?;
Ok(metadata)
}

/// Parse a string into a Tuxedo Metadata
pub(crate) fn metadata_from_string(s: &str) -> anyhow::Result<TuxedoMetadata> {
let s = strip_0x_prefix(s);
let bytes = hex::decode(s)?;
Ok(TuxedoMetadata::decode(&mut &bytes[..])?)
}

/// Typed helper to get the Node's block hash at a particular height
pub async fn node_get_block_hash(height: u32, client: &HttpClient) -> anyhow::Result<Option<H256>> {
let params = rpc_params![Some(height)];
Expand Down

0 comments on commit eed8882

Please sign in to comment.