hide You are viewing an archived web page, collected at the request of Ethereum Foundation using Archive-It. This page was captured on 00:01:19 Jun 22, 2021 , and is part of the Community collection. The information on this web page may be out of date. See All versions of this archived page. Loading media information

Let's annotate the contract examples

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:
// 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
a = contract.storage[2^256 - 1]
!(tx.sender == a) && (tx.sender > 0):

// 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]
// 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)


  • ChristianPeelChristianPeel Member Posts: 26
    Great questions and starting point. Since it seems you understand, could you explain the first line?
    if tx.value < tx.basefee * 200: 
    Is this just to make certain the tx has enough ops to run the contract? If so, the number 200 seems a bit arbitrary; is it somehow derived from the fees listed in the whitepaper?

    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.
  • VetruvianVetruvian Member Posts: 17
    I think the 200 is arbitrary, but it looks to be a 'safe' number. It is used in most examples in the whitepaper (there's just one other number used (100).

    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.
  • MilanMilan San Diego, CAMember Posts: 46
    edited January 2014
    I love the idea of a contract simulator! A little sandbox in which we could develop our contracts, monitoring their ether balance, storage required, etc. while they run. Should be possible, right?
  • ChristianPeelChristianPeel Member Posts: 26
    edited January 2014
    A test blockchain should be simple to set up and could be used as a sandbox.

    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.
  • joeyjoey Member Posts: 6
    I've been wondering about these numbers as well. Shouldn't these be dynamic depends on the requirements of each contract?
  • rverbeerverbee Member Posts: 2
    Any further work on annotating the examples from the whitepaper? I have a few questions. For instance, in many of the examples contract.storage[1000] seems to be used as a true/false value. Why the array index of 1000? Just convention?
  • Jam10oJam10o Member Posts: 61 ✭✭
    It's arbitrary, yes :)
  • FreddyFenderFreddyFender Member Posts: 39
    Jam10o, you seem to have a solid grasp on the contract code (pseudo and actual) and the git is looking awesome. What were some resources that gave you the necessary heads up on contract logic?
  • Jam10oJam10o Member Posts: 61 ✭✭
    The contract examples in the whitepaper, this thread, and a similar thread on the Ethereum subreddit.
    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 :)
  • FreddyFenderFreddyFender Member Posts: 39
    I grew up on perl and php, ooofph! Learning python is a plus, but I see the stripped down ECLL as almost a break from the dregs of perl regular expressions. thx
  • Jam10oJam10o Member Posts: 61 ✭✭
    I've added commented versions of some simple contracts I've written for my "dynamic naming contract" project at the request of @cphi. the more material the better I guess.


  • cphicphi Member Posts: 46
    @Jam10o That was quick! Thanks for taking the time to do that. Your sample contracts are great.
Sign In or Register to comment.