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

Workspaces split #153

merged 6 commits into from Dec 14, 2022

Conversation

rooooooooob
Copy link
Contributor

Splits the existing rust/core and rust/wasm into multiple crates at a root workspace. This also adds in the cip25-lib repo.

Going on the previous two commits bringing in exisitng code over to the
workspace structure, this makes it work together using each other as
dependencies and such + misc fixes so it compiles. This includes the
CIP25 crate as well.
@rooooooooob rooooooooob added the Rust/WASM Refactor Relating to the refactor from a single crate to two separate crates (rust-only + wasm bindings). label Nov 30, 2022

# 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.


/// Bytes of a structure using the CBOR bytes as per the CDDL spec
/// which for foo = bytes will include the CBOR bytes type/len, etc.
fn to_cbor_bytes(&self, force_canonical: bool) -> Vec<u8> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One thing we have different is that CIP25 was generated without encoding preservation so there's this force_canonical concept on serialization. It's ignored by the auto-implementation of Serialize if cbor_event::Serialize is implemented, which is the trait that CIP25 uses. Is that the behavior we wanted? CIP25 was generated without the encoding preservation since it didn't really need it, from what I thought you wouldn't be storing the CBOR here and you'd be using it more for parsing existing metadata. The force_canonical is there since it's used for hardware wallets for encoding tx bodies but might not be too useful in other places.

Copy link
Contributor

Choose a reason for hiding this comment

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

if it was generated with the encoding preservation, would this return just the original rep? canonical means https://www.rfc-editor.org/rfc/rfc7049#section-3.9 canonical cbor from the standard, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the stuff in chain right now if you pass in false will return whatever format was used when it was deserialized, or canonical if there was no format (e.g. was created from code right now not deserialized). If you pass in true it will turn it into canonical CBOR regardless of how it was created.

In CIP25 whatever you pass in it'll end up being canonical (afaik). So if you deserialize something it won't preserve the format, which was fine before when it was on its own since you're not going to be storing anything here, as this is more just for parsing without reencoding ever.

We could regenerate it with preserve-encdoings=true but that won't work with how it's used in Cardano as the CDDL file for the spec is actually a subset of the input bytes since it only parses the subset of a given tx metadata that conforms to the spec. But to make round-tripping consistent you'd need to figure out a way to remember the encodings of all other parts of the metadata NOT involved in the cip25 spec. We could potentially look at modifying cddl-codegen to figure out a way to do that but I'm not sure it's worth the time or complexity it adds to the crate. It would be nice if it was preserve-encodings for consistency now that we have a mono-repo and it's a part of CML, but on the other hand we could just document that it's not for round-tripping unless we think this is something important. @SebastienGllmt

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, handling this kind of subset operation is a ticket we have no plans to address at the moment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I was thinking about two options then:
a) Leave it how it is and document that the cip-25 crate doesn't preserve encodings and should not be used for round-tripping.
b) get rid of Serialize::to_cbor_bytes(force_canonical: bool) and instead make 2 other traits SerializeOriginal::to_original_cbor_cbytes() and SerializeCanonical::to_canonical_cbor_bytes() but it's feeling awfully pedantic now. In this case cip-25 would only implement the canonical one and chain would implement both. Although this doesn't explicitly cover the fact that cip-25 ignores fields not part of the spec either.

or something along those lines

@gostkin
Copy link
Contributor

gostkin commented Dec 1, 2022

cargo test fails if we run it from workspace folder

cannot find attribute `quickcheck` in this scope
   --> crypto/src/chain_crypto/algorithms/ed25519_derive.rs:116:7
    |
116 |     #[quickcheck]

@gostkin
Copy link
Contributor

gostkin commented Dec 1, 2022

let's merge #150 , so we can see how the things are with checks before review, @rooooooooob ?

All things specified in babbage/crypto.cddl are now back in the chain
crate.

To work around some things e.g. signatures, hashes being uses in the
crypto crate we have those being their base locations with chain/crypto
having a `ChainCrypto<T>` wrapper around this.

We have the to/from raw bytes in a `RawBytesEncoding` trait to help out
as well for when things are raw bytes.

All crypto bindings are still in the `wasm` crate. We will need to look
into a separate `crypto-wasm` crate and first test out how those would
interact and if it's possible/a good idea.
@rooooooooob
Copy link
Contributor Author

I pushed another commit that moves the on-chain crypto primitives into the chain crate in a crypto module. I kept base (not caring about encodings) versions of these in the crypto crate with ChainCrypto<T> wrappers around these in the chain/crypto module that take care of the encoding information relevant to their on-chain-ness.

let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
T::from_raw_hex(&s)
.map(Into::into)
.map_err(|_e| serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"hex bytes for signature"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops copy-paste error. I'll change this message so it doesn't say signature (since this could be a hash too).

@@ -200,9 +188,9 @@ pub mod certs;
pub use certs::*;


pub mod crypto;
//pub mod crypto;
Copy link
Contributor

Choose a reason for hiding this comment

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

can be removed , we have public exposure above

@gostkin
Copy link
Contributor

gostkin commented Dec 2, 2022

looks good

Crypto wasm bindings moved into separate `crypto-wasm` crate. The
on-chain representations are still in `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

@gostkin gostkin merged commit 1461038 into develop Dec 14, 2022
@SebastienGllmt SebastienGllmt deleted the workspaces-split branch December 15, 2022 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust/WASM Refactor Relating to the refactor from a single crate to two separate crates (rust-only + wasm bindings).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants