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

Workspaces split #153

Merged
merged 6 commits into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions .gitignore
Expand Up @@ -6,8 +6,29 @@ rust/target
rust/.idea
binaryen/

rust/core/target/**
rust/core/Cargo.lock
rust/wasm/target/**
rust/wasm/Cargo.lock
rust/json-gen-split/target/**
target/

core/target/**
core/Cargo.lock

crypto/target/**
crypto/Cargo.lock

crypto-wasm/target/**
crypto-wasm/Cargo.lock
crypto-wasm/pkg/**

wasm/target/**
wasm/Cargo.lock
wasm/pkg/**

cip25/target/**
cip25/Cargo.lock

cip25-wasm/target/**
cip25-wasm/Cargo.lock

json-gen-split/target/**

cip25-json-gen/target/**

15 changes: 15 additions & 0 deletions Cargo.toml
@@ -0,0 +1,15 @@
[workspace]

# this is for the new crate structure. The legacy code (current CML) still resides in the `rust` directory.
members = [
"core",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll still need to figure out what to do with the JSON schema generating stuff as we currently have multiple in the project now with CIP25.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you share a little more context please? what's the problem? seems like I don't have full picture

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the WASM build in legacy CML we have as part of a build script a rust program in rust/json-gen that outputs JSON schemas that define the JSON format of on-chain data. We have a similar script in json-gen-split. We also have that with CIP-25 as well since we used the JSON functionality of cddl-codegen too. The program outputs those schemas then the rest of the build scripts piece those together (see the scripts/ folder + package.json for how the scripts/programs are composed) into the final output .ts file output by wasm-bindgen / wasm-pack to represent the types from JS/typescript. We're going to have to update the build scripts and such now that we'll have multiple wasm builds as well.

"crypto",
"cip25",
"cip25-wasm",
"wasm"
]

# exclude old crate structure to avoid error in it
exclude = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how the pipeline will be working? will we have one for old and one for new structure? we need to verify both old and new crates are compiling and working correctly

"rust"
]
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -21,3 +21,7 @@ We recommend using Ionic + Capacitor or an equivalent setup to have the WASM bin
## Documentation

TODO

# Crate Architecture

For current users, the `rust/ `crate is the main version of CML and is the only one that should be used. There is a workspace in the root directory with crates like `core`, `wasm` etc, which are a part of a big refactor and will eventually replace the rust crate at some point in the future, but are still quite WIP for now. The rust crate when used for WASM builds via the npm scripts in the root repo dir will utilize the `rust/json-gen` crate here in the build scripts to generate typescript definitions for the JSON conversion. The `json-gen-split` crate is the equivalent for the new `core`/`wasm` crates and is not called anywhere from the build scripts, but will someday replace the `rust/json-gen` crate once the refactoring is completed.
54 changes: 54 additions & 0 deletions chain/Cargo.toml
@@ -0,0 +1,54 @@
[package]
name = "cardano-multiplatform-lib-chain"
version = "0.1.0"
edition = "2018"

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

[dependencies]
cardano-multiplatform-lib-core = { "path" = "../core" }
cardano-multiplatform-lib-crypto = { "path" = "../crypto" }
cbor_event = "2.2.0"
linked-hash-map = "0.5.3"
gostkin marked this conversation as resolved.
Show resolved Hide resolved
derivative = "2.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.57"
schemars = "0.8.8"

bech32 = "0.7.2"
hex = "0.4.0"
itertools = "0.10.1"
getrandom = { version = "0.2.3", features = ["js"] }
rand = "0.8.5"
fraction = "0.10.0"
base64 = "0.13"
num-bigint = "0.4.0"
num-integer = "0.1.45"
#rand_os = "0.1"
thiserror = "1.0.37"
# These can be removed if we make wasm bindings for ALL functionality here.
# This was not done right now as there is a lot of existing legacy code e.g.
# for Byron that might need to be used from WASM and might not.
# We can remove this dependency when that is decided.
#
# The other use-case here is enums. Without this two enums would need to be defined
# despite wasm_bindgen supporting C-style enums (with non-negative values) 100%
# This could possibly be resolved with macros but maybe not.

# non-wasm
#[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]
#rand_os = "0.1"
#noop_proc_macro = "0.3.0"

# wasm
#[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "=0.2.82", features = ["serde-serialize"] }
#rand_os = { version = "0.1", features = ["wasm-bindgen"] }
#js-sys = "=0.3.59"


[dev-dependencies]
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"
rand_chacha = "0.3.1"
4 changes: 4 additions & 0 deletions chain/README.md
@@ -0,0 +1,4 @@
# Chain

This is the core cardano-multiplatform-lib crate for all on-chain data types.
This was generated from the `specs/babbage/` CDDL specs using cddl-codegen.
9 changes: 5 additions & 4 deletions rust/core/src/address.rs → chain/src/address.rs
Expand Up @@ -7,6 +7,7 @@ use derivative::Derivative;
//use crate::genesis::network_info::NetworkInfo;
use std::convert::TryInto;

use cardano_multiplatform_lib_crypto as cml_crypto;

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash, Copy, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct ProtocolMagic(pub(crate) u32);
Expand Down Expand Up @@ -249,16 +250,16 @@ impl Address {
(|| -> Result<Self, DeserializeError> {
let header = data[0];
let network = header & 0x0F;
const HASH_LEN: usize = Ed25519KeyHash::BYTE_COUNT;
const HASH_LEN: usize = cml_crypto::Ed25519KeyHash::BYTE_COUNT;
// should be static assert but it's maybe not worth importing a whole external crate for it now
assert_eq!(ScriptHash::BYTE_COUNT, HASH_LEN);
assert_eq!(cml_crypto::ScriptHash::BYTE_COUNT, HASH_LEN);
// checks the /bit/ bit of the header for key vs scripthash then reads the credential starting at byte position /pos/
let read_addr_cred = |bit: u8, pos: usize| {
let hash_bytes: [u8; HASH_LEN] = data[pos..pos+HASH_LEN].try_into().unwrap();
if header & (1 << bit) == 0 {
StakeCredential::Key(KeyStakeCredential::new(Ed25519KeyHash::from(hash_bytes)))
StakeCredential::Key(KeyStakeCredential::new(cml_crypto::Ed25519KeyHash::from(hash_bytes).into()))
} else {
StakeCredential::Script(ScriptStakeCredential::new(ScriptHash::from(hash_bytes)))
StakeCredential::Script(ScriptStakeCredential::new(cml_crypto::ScriptHash::from(hash_bytes).into()))
}
};
fn make_encoding(bytes_encoding: Option<StringEncoding>, trailing: Option<Vec<u8>>) -> Result<Option<AddressEncoding>, DeserializeError> {
Expand Down
10 changes: 6 additions & 4 deletions rust/core/src/block.rs → chain/src/block.rs
@@ -1,3 +1,5 @@
use super::*;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct Block {
pub header: Header,
Expand Down Expand Up @@ -46,7 +48,7 @@ pub struct HeaderBody {
pub slot: u64,
pub prev_hash: Option<BlockHeaderHash>,
pub issuer_vkey: Vkey,
pub vrf_vkey: VrfVkey,
pub vrf_vkey: VRFVKey,
pub vrf_result: VrfCert,
pub block_body_size: u64,
pub block_body_hash: BlockBodyHash,
Expand All @@ -57,7 +59,7 @@ pub struct HeaderBody {
}

impl HeaderBody {
pub fn new(block_number: u64, slot: u64, prev_hash: Option<BlockHeaderHash>, issuer_vkey: Vkey, vrf_vkey: VrfVkey, vrf_result: VrfCert, block_body_size: u64, block_body_hash: BlockBodyHash, operational_cert: OperationalCert, protocol_version: ProtocolVersion) -> Self {
pub fn new(block_number: u64, slot: u64, prev_hash: Option<BlockHeaderHash>, issuer_vkey: Vkey, vrf_vkey: VRFVKey, vrf_result: VrfCert, block_body_size: u64, block_body_hash: BlockBodyHash, operational_cert: OperationalCert, protocol_version: ProtocolVersion) -> Self {
Self {
block_number,
slot,
Expand All @@ -76,7 +78,7 @@ impl HeaderBody {

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct OperationalCert {
pub hot_vkey: KesVkey,
pub hot_vkey: KESVKey,
pub sequence_number: u64,
pub kes_period: u64,
pub sigma: Ed25519Signature,
Expand All @@ -85,7 +87,7 @@ pub struct OperationalCert {
}

impl OperationalCert {
pub fn new(hot_vkey: KesVkey, sequence_number: u64, kes_period: u64, sigma: Ed25519Signature) -> Self {
pub fn new(hot_vkey: KESVKey, sequence_number: u64, kes_period: u64, sigma: Ed25519Signature) -> Self {
Self {
hot_vkey,
sequence_number,
Expand Down
73 changes: 39 additions & 34 deletions rust/core/src/cbor_encodings.rs → chain/src/cbor_encodings.rs
@@ -1,4 +1,8 @@
use super::*;
use cardano_multiplatform_lib_core::{
serialization::{LenEncoding, StringEncoding},
};
use cbor_event::Sz;

#[derive(Clone, Debug, Default)]
pub struct AddressEncoding {
Expand Down Expand Up @@ -65,13 +69,6 @@ pub struct BlockEncoding {
pub invalid_transactions_elem_encodings: Vec<Option<cbor_event::Sz>>,
}

#[derive(Clone, Debug, Default)]
pub struct BootstrapWitnessEncoding {
pub len_encoding: LenEncoding,
pub chain_code_encoding: StringEncoding,
pub attributes_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct ConstrPlutusDataEncoding {
pub len_encoding: LenEncoding,
Expand Down Expand Up @@ -175,16 +172,6 @@ pub struct Ipv6Encoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct KesSignatureEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct KesVkeyEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct MoveInstantaneousRewardEncoding {
pub len_encoding: LenEncoding,
Expand All @@ -204,13 +191,6 @@ pub struct MultiHostNameEncoding {
pub index_0_encoding: Option<cbor_event::Sz>,
}

#[derive(Clone, Debug, Default)]
pub struct Nonce1Encoding {
pub len_encoding: LenEncoding,
pub index_0_encoding: Option<cbor_event::Sz>,
pub bytes_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct OperationalCertEncoding {
pub len_encoding: LenEncoding,
Expand Down Expand Up @@ -393,16 +373,6 @@ pub struct ShelleyTxOutEncoding {
pub len_encoding: LenEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct SignatureEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct SignkeyKESEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct SingleHostAddrEncoding {
pub len_encoding: LenEncoding,
Expand Down Expand Up @@ -538,6 +508,41 @@ pub struct ValueEncoding {
pub multiasset_value_encodings: BTreeMap<PolicyId, (LenEncoding, BTreeMap<AssetName, Option<cbor_event::Sz>>)>,
}


#[derive(Clone, Debug, Default)]
pub struct BootstrapWitnessEncoding {
pub len_encoding: LenEncoding,
pub chain_code_encoding: StringEncoding,
pub attributes_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct KesSignatureEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct KesVkeyEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct Nonce1Encoding {
pub len_encoding: LenEncoding,
pub index_0_encoding: Option<cbor_event::Sz>,
pub bytes_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct SignatureEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct SignkeyKESEncoding {
pub inner_encoding: StringEncoding,
}

#[derive(Clone, Debug, Default)]
pub struct VkeyEncoding {
pub pubkey_bytes_encoding: StringEncoding,
Expand Down
File renamed without changes.