FuelLabs / sway

🌴 Empowering everyone to build reliable and efficient smart contracts.
https://docs.fuel.network/docs/sway/
Apache License 2.0
62.98k stars 5.31k forks source link

Consider adding checks to functions using `fp` #6124

Open bitzoic opened 2 weeks ago

bitzoic commented 2 weeks ago

Currently, when calling the following functions outside of out a contract, the function will revert. In order to prevent misuse we should consider adding a check on the context before using the fp opcode

  1. https://github.com/FuelLabs/sway/blob/be5e1344d1a1ac976f9a0add77d07f68739a3c5e/sway-lib-std/src/contract_id.sw#L115
  2. https://github.com/FuelLabs/sway/blob/be5e1344d1a1ac976f9a0add77d07f68739a3c5e/sway-lib-std/src/asset_id.sw#L121
  3. https://github.com/FuelLabs/sway/blob/be5e1344d1a1ac976f9a0add77d07f68739a3c5e/sway-lib-std/src/call_frames.sw#L39
  4. https://github.com/FuelLabs/sway/blob/be5e1344d1a1ac976f9a0add77d07f68739a3c5e/sway-lib-std/src/call_frames.sw#L67
SwayStar123 commented 2 weeks ago

I feel this should be compile time, because for 99% of the use cases where these functions will be used correctly, they will incur unnecessary gas costs on every call. And if someone does mistakenly use these functions in the wrong context, it makes much more sense for the error to be at compile time rather than in the form of a result