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

Bug: from_candid following to_candid fails. #4420

Open
AStepanov25 opened this issue Feb 27, 2024 · 4 comments
Open

Bug: from_candid following to_candid fails. #4420

AStepanov25 opened this issue Feb 27, 2024 · 4 comments

Comments

@AStepanov25
Copy link

AStepanov25 commented Feb 27, 2024

from_candid called after to_candid produces null.

// @testmode wasi

type GlobalId = (streamId : Nat, queueNumber : Nat);
type TxOutput = { #ftTransfer : { amount : Nat; fee : Nat } };

let a = to_candid (((3, 0), #ftTransfer({ amount = 0; fee = 0 })) : (GlobalId, TxOutput));

assert (from_candid a : ?(GlobalId, TxOutput)) != null;

Minified example:

// @testmode wasi

type T = (Nat, Nat);

let a = (0, 0) : T;

assert (from_candid (to_candid (a)) : ?T) == ?a;

mops.toml:

[toolchain]
moc = "0.10.3"
wasmtime = "16.0.0"
@chenyan-dfinity
Copy link
Contributor

It's a tricky question, but working as intended. from_candid is using the top-level tuple to represent multiple values. So in your minified example, from_candid is asking for two values, Nat and Nat, not a single tuple.

A related forum discussion: https://forum.dfinity.org/t/candid-and-tuples/17800/7?u=chenyan

@crusso
Copy link
Contributor

crusso commented Feb 28, 2024

Related, re-opened issue #4130 that suggest two possible enhancements.

@crusso
Copy link
Contributor

crusso commented Feb 28, 2024

Possible rewrite, tried in playground:

https://play.motoko.org/?tag=3119111542

actor {

  public func test1() : async () { // fails
    type GlobalId = (streamId : Nat, queueNumber : Nat);
    type TxOutput = { #ftTransfer : { amount : Nat; fee : Nat } };

    let a = to_candid (((3, 0), #ftTransfer({ amount = 0; fee = 0 })) : (GlobalId, TxOutput));

    assert (from_candid a : ?(GlobalId, TxOutput)) != null;
  };

  public func test2() : async () { // succeeds
    type GlobalId = (streamId : Nat, queueNumber : Nat);
    type TxOutput = { #ftTransfer : { amount : Nat; fee : Nat } };

    let a = to_candid ((3, 0) : GlobalId, #ftTransfer({ amount = 0; fee = 0 }) : TxOutput);

    assert (from_candid a : ?(GlobalId, TxOutput)) != null;
  }

}

@AStepanov25
Copy link
Author

Also this works:

  public func test3() : async () { // succeeds
    type GlobalId = (streamId : Nat, queueNumber : Nat);
    type TxOutput = { #ftTransfer : { amount : Nat; fee : Nat } };

    let a = to_candid ((3, 0) : GlobalId, #ftTransfer({ amount = 0; fee = 0 }) : TxOutput);

    assert (from_candid a : ??(GlobalId, TxOutput)) != null;
  };

And this:

// @testmode wasi
let a : (Nat, Nat) = (0, 0);

assert (from_candid to_candid (a.0, a.1) : ?(Nat, Nat)) == ?a;
assert (from_candid to_candid ((a.0, a.1)) : ??(Nat, Nat)) == ??a;
assert (from_candid to_candid ((a.0, a.1), a) : ?((Nat, Nat), (Nat, Nat))) == ?(a, a);
assert (from_candid to_candid (a) : ??(Nat, Nat)) == ??a;

let b : Blob = to_candid ((0,0) : (Nat,Nat));
let x : ?{ _0_ : Nat; _1_ : Nat } = from_candid b; 


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants