This repository was archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 727
Using pyethereum.tester
Karl Floersch edited this page Aug 1, 2017
·
10 revisions
It will be documented as if you did:
from ethereum.tools import tester as t
from ethereum import utils as u
c = t.Chain()
-
t.Chain()
creates a new test-blockchain with a genesis block.- Returns the chain tester.
-
t.State()
creates a new test-state with the genesis state passed in.- Can be used for lightweight testing when you don't need mining & block logic.
- Returns the state tester.
-
c.tx(private_key, to, value, data=b'')
sends a transaction using the given private key to the given address with the given value and data.- Returns the return value of the transaction execution
-
c.contract(code, language='evm')
creates a contract with the given code, in the given language. Acceptable arguments are 'evm', 'serpent', 'solidity' and 'viper'. If any of the latter three are used, returns an ABIContract object, of which you can then call functions. -
c.mine(n=1, coinbase=a0)
pretend-minesn
times, withcoinbase
the blockmaker.- Returns nothing; (
None
)
- Returns nothing; (
-
c.change_head(parent, coinbase=a0)
changes the tester'shead_state
to build on the block that you pass in asparent
.- Returns nothing; (
None
)
- Returns nothing; (
-
c.snapshot()
returns a snapshot object. -
c.revert(snapshot)
takes a snapshot as input and reverts to that snapshot.
The tester module generates ten keys and ten addresses: t.k0
... t.k9
and their corresponding addresses t.a0
... t.a9
.
-
c.head_state
returns the State object at the head. You can then access various methods of this includingget_code(addr)
,get_balance(addr)
,get_nonce(addr)
andget_storage_at(addr, key)
. -
c.block.transactions
returns the transactions included in the block -
c.head_state.receipts
returns the receipts
from ethereum.tools import tester
c = tester.Chain()
x = c.contract("""
def foo(x):
return x + 5
""", language='serpent')
assert x.foo(2) == 7
bn = c.head_state.block_number
c.mine(5)
assert c.head_state.block_number == bn + 5
x2 = c.contract("""
data moose
def increment_moose():
self.moose += 1
return self.moose
""", language='serpent')
assert x2.increment_moose() == 1
assert x2.increment_moose() == 2