dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
506 stars 98 forks source link

Consider adding __cdk_name #3462

Open lastmjs opened 2 years ago

lastmjs commented 2 years ago

I've opened this issue with 3 additional CDK projects:

As we've been developing CDKs (TypeScript/JavaScript with Azle and Python with Kybra), we've been thinking about how to measure adoption of various CDKs. In addition to GitHub metrics (such as analyzing dfx.json), it might be a powerful metric to know exactly how many canisters use a particular CDK. For Azle and Kybra, we're adding a simple method __cdk_name that returns a string with the name of the CDK. This will allow canister indexers like https://icscan.io/ to index canisters by CDK. This could allow viewing of various CDK metrics over time, which I think would be very interesting to many different parties on the IC.

Here's our suggested implementation of the method in cdk-rs:

#[ic_cdk_macros::query]
fn __cdk_name() -> String {
    "cdk-rs".to_string()
}

Here's our suggested implementation of the method in Azle:

#[ic_cdk_macros::query]
fn __cdk_name() -> String {
    "azle".to_string()
}

Here's our suggested implementation of the method in Kybra:

#[ic_cdk_macros::query]
fn __cdk_name() -> String {
    "kybra".to_string()
}

Do you have any thoughts on this pattern?

nomeata commented 2 years ago

I'd suggest to use an icp-custom section, like the one used to embed the Candid interface. The data is static and module-specific (not instance specific), so it's a better fit. And you get certification for free!

nomeata commented 2 years ago

Also, why not include version information?

lastmjs commented 2 years ago

I don't know much about how an icp-custom section would be implemented. Do you have any links to how to do this in Rust?

nomeata commented 2 years ago

@chenyan-dfinity, do you know?

chenyan-dfinity commented 2 years ago

You can use ic-wasm. But the easiest way to do this in Rust is as follows:

#[link_section = "icp:public cdk"]
pub static LANG = *b"cdk-rs v1.0";
lastmjs commented 2 years ago

I think LANG might be better as NAME since we may have multiple CDKs per language:

#[link_section = "icp:public cdk"]
pub static NAME = *b"kybra v0.0.1";

Unless that variable doesn't show up in the path?

chenyan-dfinity commented 2 years ago

Yep, variable name is just a placeholder. I won't show up in the path.

lastmjs commented 2 years ago

I've tried adding this:

``rust

[link_section = "icp:public cdk"]

    pub static NAME: [u8; 12] = *b"kybra v0.0.0";

On my machine everything was working fine, on another's machine, which happend to be an M1 Macbook, we got this error:

LLVM ERROR: Global variable '_ZN24kybra_generated_canister4NAME17h757f254ac293c59bE' has an invalid section specifier 'icp:public cdk': mach-o section specifier requires a segment and section separated by a comma.

chenyan-dfinity commented 2 years ago

Maybe try adding this line above #[link_section]:

#[cfg(target_arch = "wasm32")]

I don't have an M1, so cannot verify.

lastmjs commented 6 months ago

The discussion on a similar proposal is proceeding well here: https://forum.dfinity.org/t/rfc-tech-stack-canister-metadata-standard-extension/28692