Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Using pyethereum.tester

o-jasper edited this page Nov 19, 2014 · 10 revisions

It will be documented as if you did:

import pyethereum
t = pyethereum.tester
u = pyethereum.utils
s = t.state()

Slot values(including return values)

Currently they are provided as lists of integers.(Python integers go that high) Would be more convenient if they would auto-convert from strings aswel. (but doesnt do that yet)

Interacting with the blockchain

  • t.state() creates a new test-blockchain with a genesis block.
    • Returns the state.
  • s.send(private_key, to, value, data=[]) sends a transaction using the private key.
    • Returns the return value of the contract.
  • s.contract(code, sender=k0, endowment=0) creates a contract, given a serpent file, and with endowment coins.
    • Returns the contract address.
  • s.evm(...) same as s.send, but takes raw evm code as input.
  • s.profile(....) same as s.send, but..
    • Return {"time": time_passed, "gas":gas_used, "output":contract_return_value}
  • s.mine(n=1, coinbase=a0) pretend-mines n times, with coinbase the blockmaker. If you notice you might be running out of block gas,(will likely take a lot) you need this, more likely you're doing something with block information, for instance you want time to pass in the test.
    • Returns nothing; (None)
More advanced

I see a s.snapshot() and s.revert(snapshot_data), those could be combined with the s.send to do a 's.stateless_send', which doesnt exist yet. There is also SPV stuff; s.mkspv, s.verifyspv.

Getting data out of the state

The tester itself does not provide access to other data like the block timestamp. However, this is perfectly fine, as the pyethereum code itself does. Note that i list only the ones important for testing.

  • s.block.get_storage_data(address, index) gets storage data index index from contract at address.

    • Returns: the slot value. Zero if the address doesnt exist.
  • s.block.get_balance(address)

    • Returns: the balance of that address.
  • s.block.timestamp timestamp of the block. (approximately the current time)

  • s.block.number the number of the block. (zero is genesis)

  • s.block.coinbase blockmakers address.

  • s.block.prevhash hash of previous block.

  • Gas stuff: s.block.min_gas_price, s.block.gas_limit, s.block.gas_used

Additional useful functions

TODO find these in the pyethereum source.

  • u.privtoaddr(privkey)
    • Returns: the address corresponding to a private key.

WARNING this is for this pull request about strings, Serpent master currently puts the string at the right side. Alethzero does it on the left side, and that is how strings work in C/C++ so i go that way.

def i(str):
    s,f = 0, 1
    for i in range(len(str)):
        s += f*ord(str[len(str)-i-1])
        f *= 256
    for i in range(32 - len(str)): # Right pad instead of left.
        s *= 256;
    return s

def stri(i):
    while i > 0:
        s += chr(i%256)
        i /=256
    return "".join(reversed(s))

We also want an easy way to sha3 things with lists of slots as-EVM-does, i currently use an Ethereum contract and the s.send return value for that.

Clone this wiki locally