cashscript
cashscript copied to clipboard
Add global contract variables / constructor
Currently the only variables that are accessible to all contract functions are the constructor parameters. However, it might be useful to have some variables that are not necessarily constructor parameters, but that you still want to access in multiple functions, such as global constants.
Furthermore, it could be useful to have a constructor function that executes some specific code using the constructor parameters, while also being able to store global contract variables. This could be useful if some common code has to be executed in all functions, since it takes fewer opcodes to include the code just once.
Something to keep in mind though is what the scoping would be for constructor parameters. They would likely need to be assigned to global contract variables to be accessible to the rest of the contract. For simple constructors, this might be less efficient than the current set up.
it could be useful to have a constructor function
Want to confirm this! My sidechain covenant uses
require(this.activeInputIndex == 0);
on all three contract functions so that part is duplicated three times in the compiled script now.
It also uses the following three times
bytes23 out = new LockingBytecodeP2SH(hash160(constructedBytecode));
require(tx.outputs[0].lockingBytecode == out);
but the constructedBytecode variable differs for all three cases so the global function would need to run after the normal functions assigning something to the constructedBytecode variable.