AlacrisIO / meta

Internal management of Legicash/Legilogic/Alacris
0 stars 0 forks source link

Implement `net` and standard library for JS output #72

Closed jeapostrophe closed 5 years ago

jeapostrophe commented 5 years ago

Alex

Faré + Matt

jeapostrophe commented 5 years ago

Please update this with what is done

fare commented 5 years ago

What is done is the groundwork so the manual and automatic backends can share a lot of code including the frontend (that needs more work). The stdlib is complete, except that I didn't implement the inverses to msg-cat, that we don't actually need so far. I was blocked a bit by the issues I raised (#81 I believe), so I stopped at that point (also it was 4am). I have been thinking about what send and receive (and the factory equivalent make and attach) would look like. In both cases, we watch the chain for messages; in the case of the first make, there is an interface offering the user an option to create a contract with interact. In the case of attach, an option for the matching user to accept it. All other sends are the result of the continuation to a receive that watches the chain, computes a next step, and either takes an action, or registers some more input to be done with interact.

fare commented 5 years ago

I did the stdlib part, except the bytes left and right, that require knowing the size of the left, either statically (which our compiler doesn't provide) or dynamically (which the current Solidity implementation doesn't do—we use abi.encode, which doesn't).

fare commented 5 years ago

On my part, see my comments on #dsl about the need for descriptors. I can fake them for now.

jeapostrophe commented 5 years ago

I did the stdlib part, except the bytes left and right, that require knowing the size of the left, either statically (which our compiler doesn't provide) or dynamically (which the current Solidity implementation doesn't do—we use abi.encode, which doesn't).

The fact that abi.encode throws away the information means that it is not a valid implementation for cat. You could make cat x y = abi.encode( x.length, bytes-cat( x, y )) then use the length to split the bytes string.

fare commented 5 years ago

Yes. If we want to save space, we can make the length field 16-bit, since the quadratic cost of memory makes anything more than that size unaffordable, anyway.

PS: A block has a gas limit of the order of 8e6 GAS. The formula for memory cost is 3*(a+floor(a/512)), which for 65535 is already over 24e6. And that's just the memory cost (though it would dominate in this case.) Thus data of size 64K or larger are not affordably representable on the Ethereum blockchain (at the moment; that may or may not change in future years).

fare commented 5 years ago

If we're doing that way, maybe we want to call the functions pair, fst, snd, more in line with haskell

fare commented 5 years ago

(Haskell uses left and right for sums)

AlexKnauth commented 5 years ago

My work on this so far is on this branch: https://github.com/AlacrisIO/alacrity/tree/msg-cat Where I have msg_cat, msg_left, and msg_right.

fare commented 5 years ago

If we're doing that way, maybe we want to call the functions pair, fst, snd, more in line with haskell, that uses left and right for its binary sum.

AlexKnauth commented 5 years ago

The names pair, fst, and snd to me would imply that they're fully polymorphic to allow pairing any two values together. We will want to add fully polymorphic pairs/tuples eventually; these monomorphic bytes operations are not those.

fare commented 5 years ago
  1. Give it monomorphic names msgPair, msgFst, msgSnd if you want. It's the underlying runtime substrate to the polymorphic concept.
  2. The file js/stdlib.mjs is the wrong place. Put it in examples/rps-demo/common-{utils,runtime}.mjs for now, until we factor the runtime out of that directory when the demo is complete.
  3. The implementation is not complete until EmitSol.hs emits code that is congruent with EmitJS.hs.
AlexKnauth commented 5 years ago

(1) That would make sense to me. msg-pair makes mores sense than msg-cat or bytes-cat since it correctly conveys that the two elements are still separate, and can be extracted unambiguously. (2) The stdlib.mjs functions should be part of the language runtime, not part of an "example use" of the language, so I don't think examples/rps-demo is the right place for it. (3) Yes. Though that's covered in a separate issue: https://github.com/AlacrisIO/meta/issues/69.

fare commented 5 years ago

(2) That's where it is now, and where you should put it to interact with the code we're writing, and where the other functions of the stdlib are as well as the previous iteration on this code. No doubt the stdlib will move at some point before release.

fare commented 5 years ago

(1) Actually, I prefer msgCons, msgCar, msgCdr — the names are appropriately low-level, shall speak to fellow language implementers, and is invisible from the end-user, anyway.

jeapostrophe commented 5 years ago

@AlexKnauth

1) Please have the names match the names in Alacrity as much as possible given the identifier restrictions in JS/Sol

2) Please put them in js/stdlib.mjs

3) Yes, #69 is the other task too

fare commented 5 years ago
  1. stdlib complete in 79e8480, both JS and Solidity.

  2. You can't put it in js/stdlib.mjs without moving in library import and initialization, and abstraction of whether they are imported from nodejs vs browser (ongoing work by matt), since crypto and bignumber support are not portable but provided by various libraries. That's not something we should work on before matt is done.

  3. I did the solidity too. Needs tests and optimizations at some point. Not now (though, add tests at some point in the near future)

jeapostrophe commented 5 years ago

Great

fare commented 5 years ago

PS: regarding throwing away information, I was somewhat confused with abi.encodePacked that throws away length information. abi.encode doesn't; it has an (inefficient) protocol for representing arbitrary length data of size n bytes as 32*(2+ceiling(n/32)) bytes. In both cases, though, the type information is otherwise dropped, such that it is trivial to create an object of a different type with the same encoding and thus the same digest.

PPS: actually, abi.encodePacked throws away dynamic length information while making use of static length information, while abi.encode throws away static length information while making use of dynamic length information. Go figure.

fare commented 5 years ago

web3.js 1.0.0 exposes web3.eth.abi.encodeParameters which supports the equivalent of Solidity's abi.encode. Unhappily, we must use web3 0.20.x which is what metamask provides, and it is lacking this part (or failing to expose it). For the purposes of the rps demo, I will implement just the uint256 type, which is the only one we use (for now), and is trivial to support. Hopefully, by the time we need more, we will have something better than web3.js 0.20.x to deal with.

mattaudesse commented 5 years ago

Based on today's meeting with Jay we seem to be in agreement this is closable.