Skip to content

Commit

Permalink
add @State restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
xhliu committed Dec 7, 2021
1 parent 704de75 commit 715ee8d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/state.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,56 @@ The following is an example contract that records the number of times ``mutate()
// only 1 input here
require(hash256(output) == Util.hashOutputs(txPreimage));
}
}
Restrictions
============
For any public function to access a stateful property, it must include a ``SighashPreimage`` parameter that is validated with ``Tx.checkPreimage*()``, i.e., with :ref:`OP_PUSH_TX<pushtx-label>` .
This does not apply to any non-public function, including constructors.

.. code-block:: solidity
contract Counter {
@state
int counter;
constructor(int counter) {
// OK: not a public function
this.counter = counter;
}
public function increment(SigHashPreimage txPreimage, int amount) {
// OK
this.counter++;
require(Tx.checkPreimage(txPreimage));
}
public function foo(SigHashPreimage txPreimage, int amount) {
require(Tx.checkPreimageOpt(txPreimage));
// OK
this.counter++;
require(true);
}
public function bar(SigHashPreimage txPreimage) {
// Not OK: missing Tx.checkPreimage*()
this.counter++;
require(true);
}
public function baz(int i) {
// Not OK: missing SigHashPreimage
this.counter++;
require(true);
}
function baz() : int {
// OK: not a public function
return this.counter;
}
}

0 comments on commit 715ee8d

Please sign in to comment.