Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

support struct signature #10

Closed
wants to merge 2 commits into from
Closed

Conversation

cryptoAtwill
Copy link
Collaborator

Changes

Implements derive parameter struct method signature hash macro. It will generate a method hash string based on the types of the fields of the struct. Internally it is performing blake2 hashing then hex encoding. As an example, consider this struct:

struct Foo {
   a: String,
   b: usize,
}

It would first concate the types to "String, usize", then blake2 hash the string then hex encode.

Tests

check out src/lib.rs:7 as an example for demo, it is also part of cargo test.

Issues

Copy link
Contributor

@adlrocha adlrocha left a comment

Choose a reason for hiding this comment

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

Looking good. A few minor comments/clarifications regarding the struct derive macro, but I think we can move to the interface type computation to see how the end-to-end would look like. Thanks!

version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Copy link
Contributor

Choose a reason for hiding this comment

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

remove?

let name = &ast.ident;

// A failed experiment to access to the fields of MyStruct
// The next line doesn't compiles
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this? Is this still true?

.map(|field| field.ty.clone())
.collect::<Vec<_>>(),
_ => {
panic!("The StructSignature derive macro can only be applied to named fields.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Why named files? you are using types to compute the signature right?

version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Copy link
Contributor

Choose a reason for hiding this comment

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

remove?

pub test3: Option<String>,
}

println!("{:?}", Foo::signature());
Copy link
Contributor

Choose a reason for hiding this comment

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

What about adding an assert_eq! to validate that the hash computation is correct?

const SIGNATURE_STR: &'static str = stringify!(#(#field_types),*);

fn signature() -> String {
use interface_trait::blake2::{Blake2b512, Digest};
Copy link
Contributor

Choose a reason for hiding this comment

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

To prevent having to hash for every method in the interface (which is a lot of unnecessary computation), we can maybe return here directly the concatenated string of method arguments as the signature and then for the final method ID concat all method signatures. WDYT?

@adlrocha
Copy link
Contributor

adlrocha commented Jan 2, 2023

@adlrocha
Copy link
Contributor

Close for now (unless there is a strong opinion against it), we can use FRC-42 or an adaptation of it for this: https://github.com/helix-onchain/filecoin/tree/main/frc42_dispatch

@adlrocha adlrocha closed this Jan 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants