While reading the whitepaper and the wiki, as a programmer I tried to figure out what exactly happens in the contract examples. I thought it might be a nice idea to annotate the examples so we and other people can learn from it.
Lets take the
one from the wiki below ('
A real example - simple forwarding contract to create a transferable account').
I made a start with the annotations (and assume '//' starts a comment). I made the parts I don't fully understand bold. I hope someone can clarify these parts.
// If the transaction value is less then 200 times the base fee; stop.
if tx.value < tx.basefee * 200:
exit
// Otherwise get the stored contract sender (see below) at the end
// of the contract storage. (why use the large array index?)
// If that value is not the sender, and the sender is specified
// (I guess that is wat is being checked, but could this ever
// not be the case?); stop
else:
a = contract.storage[2^256 - 1]
!(tx.sender == a) && (tx.sender > 0):
exit
// If the first input is some big number, store the data in the second
// input in the contract storage (what is the purpose of this?).
if tx.data[0] == 2^160:
contract.storage[2^256 - 1] = tx.data[1]
else:
// If the sender has not been set before; store the sender of the
// transaction at the end of the contract storage array.
if a == 0:
contract.storage[2^256 - 1] = tx.sender
// Create a new transaction that (does what?)
o = array()
i = 0
while i < tx.datan - 1:
o[i] = tx.data[i+1]
i = i + 1
mktx(tx.data[0],tx.value - tx.basefee * 250,o,i)
Comments
Also, what's up with the '250' in the last line?
It seems to me that it would make sense to use some intermediate variables, such as an intermediate variable for tx.data[0] which has a bit more verbose name to help non-experts.
In the whitepaper it is also mentioned at least 100 * basefee should be send to a contract for a transaction to be valid. So if you have a small contract that does not need a lot of storage and cryptographic functions, 100 * basefee might be enough.
Thinking about it, I think we really need to have some tools to test contracts and see the fee required based on various input transactions.
In the example above I noticed something strange. The first if suggests 200 * basefee is enough for the input value. But in the last line the contract tries to send a transaction with a value of input value - 250 * basefee which can turn out to be negative.
When I read Vetruvian's comment I imagined of tool to preprocess contracts to explicitly or implicitly indicate how many cycles will be used with various inputs. I like this idea; it will be invaluable.
The language is basically a stripped down version of Python, so looking into syntax there really helps. C-like language is a misnomer. The wiki page says as much
https://github.com/Jam10o/contracts