fuel-specs icon indicating copy to clipboard operation
fuel-specs copied to clipboard

Clarify how data should be passed into and out of a contract depending on its type

Open mohammadfawaz opened this issue 3 years ago • 4 comments

A contract call from an external context requires passing some data through the $rA register (see CALL).

Register $rA is a memory address from which the following fields are set (word-aligned):

bytes type value description
32 byte[32] to Contract ID to call.
8 byte[8] param1 First parameter.
8 byte[8] param2 Second parameter.

param2 is used to pass the arguments to the contract method.

In the special case where the contract method has a single argument that fits in a word, it is possible to store the value of that argument in param2 directly. In all other cases (single large argument or multiple arguments), param2 has to store a pointer to the pile of data containing the arguments.

The specs should be made clear as to how the above should be done. I think having param2 always containing a pointer to the data regardless of how the data looks like is more sensible than the above as it simplifies external callers.

mohammadfawaz avatar Apr 07 '22 00:04 mohammadfawaz

Relevant case: https://github.com/FuelLabs/fuels-rs/issues/201 for more context.

mohammadfawaz avatar Apr 07 '22 00:04 mohammadfawaz

In https://github.com/FuelLabs/sway/issues/1230 it's suggested that single element structs which fit in a register and so that should also be specified explicitly (if it isn't already). 'Fits in a word' is slightly open to interpretation.

otrho avatar Apr 13 '22 10:04 otrho

It is not just structs though right?.. so we have to be clear about other types as well such as strings.

mohammadfawaz avatar Apr 13 '22 12:04 mohammadfawaz

This issue: https://github.com/FuelLabs/sway/issues/1368 now has more information about the problem.

The spec needs to clarify two things:

  • How data is passed into the contract using param2
  • How data is passed out of the contract using ret/retd

As per the recent discussion around this, integers and booleans should be passed by value into the contract and should be returned using ret. All other types should be passed as a pointer (i.e. param2 contains a pointer to the data and not the data itself) and returned using retd.

mohammadfawaz avatar Apr 28 '22 18:04 mohammadfawaz

Closed in favor of https://github.com/FuelLabs/sway/issues/5512

IGI-111 avatar Feb 22 '24 15:02 IGI-111