Skip to content

Commit

Permalink
Added DAO tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vub authored and vub committed Jul 16, 2016
1 parent 7f4e326 commit eff0f4d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions dao_tests/bytecode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60606040526000357c0100000000000000000000000000000000000000000000000000000000900480632e6e504a1461005a5780633ccfd60b14610069578063eedcf50a14610078578063fdf97cb2146100b157610058565b005b61006760048050506100ea565b005b6100766004805050610277565b005b6100856004805050610424565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100be600480505061043c565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073bb9bc244d798123fde783fcc1c72d3bb8c18941373ffffffffffffffffffffffffffffffffffffffff166318160ddd604051817c01000000000000000000000000000000000000000000000000000000000281526004018090506020604051808303816000876161da5a03f115610002575050506040518051906020015073bb9bc244d798123fde783fcc1c72d3bb8c18941373ffffffffffffffffffffffffffffffffffffffff166370a0823130604051827c0100000000000000000000000000000000000000000000000000000000028152600401808273ffffffffffffffffffffffffffffffffffffffff1681526020019150506020604051808303816000876161da5a03f11561000257505050604051805190602001503073ffffffffffffffffffffffffffffffffffffffff16310103604051809050600060405180830381858888f19350505050505b565b600073bb9bc244d798123fde783fcc1c72d3bb8c18941373ffffffffffffffffffffffffffffffffffffffff166370a0823133604051827c0100000000000000000000000000000000000000000000000000000000028152600401808273ffffffffffffffffffffffffffffffffffffffff1681526020019150506020604051808303816000876161da5a03f1156100025750505060405180519060200150905073bb9bc244d798123fde783fcc1c72d3bb8c18941373ffffffffffffffffffffffffffffffffffffffff166323b872dd333084604051847c0100000000000000000000000000000000000000000000000000000000028152600401808473ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff16815260200182815260200193505050506020604051808303816000876161da5a03f1156100025750505060405180519060200150158061041657503373ffffffffffffffffffffffffffffffffffffffff16600082604051809050600060405180830381858888f19350505050155b1561042057610002565b5b50565b73bb9bc244d798123fde783fcc1c72d3bb8c18941381565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff168156
1 change: 1 addition & 0 deletions dao_tests/dao_dump.json

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions dao_tests/fork_tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from ethereum.state import State
import json
from ethereum import abi
from ethereum.utils import normalize_address
from ethereum.state_transition import apply_transaction, apply_const_message
from ethereum.vm import Message, CallData
from ethereum.config import Env
from ethereum.parse_genesis_declaration import mk_basic_state
from ethereum.transactions import Transaction

account_dict = json.load(open('dao_dump.json'))
withdrawer_code = '0x' + open('bytecode.txt').read().strip()
true, false = True, False
withdrawer_ct = abi.ContractTranslator([{"constant":false,"inputs":[],"name":"trusteeWithdraw","outputs":[],"type":"function"},{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"mainDAO","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":true,"inputs":[],"name":"trustee","outputs":[{"name":"","type":"address"}],"type":"function"}])
dao_ct = abi.ContractTranslator([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_amount","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":true,"inputs":[],"name":"standard","outputs":[{"name":"","type":"string"}],"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_amount","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_amount","type":"uint256"}],"name":"Approval","type":"event"}])

# Initialize state

dao = "0xbb9bc244d798123fde783fcc1c72d3bb8c189413"
withdrawer = "0xbf4ed7b27f1d666546e30d74d50d173d20bca754"
my_account = "0x1db3439a222c519ab44bb1144fc28167b4fa6ee6"
my_other_account = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
curator = "0xda4a4626d3e16e094de3225a751aab7128e96526"

state = mk_basic_state({
dao: account_dict,
withdrawer: {
"code": withdrawer_code,
"balance": "12072877497524777000000000",
"storage": {
"0x": "0xda4a4626d3e16e094de3225a751aab7128e96526"
}
},
}, {
"number": 1920001,
"gas_limit": 4712388,
"gas_used": 0,
"timestamp": 1467446877,
"difficulty": 2**25,
"hash": '00' * 32,
"uncles_hash": '00' * 32
}, Env())

import sys
state.log_listeners.append(lambda x: sys.stdout.write(str(dao_ct.listen(x))+'\n'))
state.log_listeners.append(lambda x: sys.stdout.write(str(withdrawer_ct.listen(x))+'\n'))

print 'State created'

# Check pre-balance

pre_balance = state.get_balance(my_account)
pre_dao_tokens = dao_ct.decode('balanceOf', ''.join(map(chr, apply_const_message(state, Message(normalize_address(my_account), normalize_address(dao), 0, 1000000, CallData([ord(x) for x in dao_ct.encode('balanceOf', [my_account])]), code_address=dao)))))[0]

print 'Pre ETH (wei) balance: %d' % pre_balance
print 'Pre DAO (base unit) balance: %d' % pre_dao_tokens

# Approve the withdrawal

tx1 = Transaction(state.get_nonce(my_account), 0, 1000000, dao, 0, dao_ct.encode('approve', [withdrawer, 100000 * 10**18])).sign('\x33' * 32)
tx1._sender = normalize_address(my_account)
apply_transaction(state, tx1)

# Check allowance

allowance = dao_ct.decode('allowance', ''.join(map(chr, apply_const_message(state, Message(normalize_address(my_account), normalize_address(dao), 0, 1000000, CallData([ord(x) for x in dao_ct.encode('allowance', [my_account, withdrawer])]), code_address=dao)))))[0]
assert allowance == 100000 * 10**18, allowance
print 'Allowance verified'

# Claim the ETH

tx2 = Transaction(state.get_nonce(my_account), 0, 1000000, withdrawer, 0, withdrawer_ct.encode('withdraw', [])).sign('\x33' * 32)
tx2._sender = normalize_address(my_account)
apply_transaction(state, tx2)

# Compare post_balance

post_balance = state.get_balance(my_account)
post_dao_tokens = dao_ct.decode('balanceOf', ''.join(map(chr, apply_const_message(state, Message(normalize_address(my_account), normalize_address(dao), 0, 1000000, CallData([ord(x) for x in dao_ct.encode('balanceOf', [my_account])]), code_address=dao)))))[0]

print 'Post ETH (wei) balance: %d' % post_balance
print 'Post DAO (base unit) balance: %d' % post_dao_tokens

assert post_dao_tokens == 0
assert post_balance - pre_balance == pre_dao_tokens

print 'Withdrawing once works'

# Try to claim post_balance again, should have no effect

tx3 = Transaction(state.get_nonce(my_account), 0, 1000000, withdrawer, 0, withdrawer_ct.encode('withdraw', [])).sign('\x33' * 32)
tx3._sender = normalize_address(my_account)
apply_transaction(state, tx3)

post_balance2 = state.get_balance(my_account)
post_dao_tokens2 = dao_ct.decode('balanceOf', ''.join(map(chr, apply_const_message(state, Message(normalize_address(my_account), normalize_address(dao), 0, 1000000, CallData([ord(x) for x in dao_ct.encode('balanceOf', [my_account])]), code_address=dao)))))[0]

assert post_balance2 == post_balance
assert post_dao_tokens2 == post_dao_tokens

# Curator withdraw

pre_curator_balance = state.get_balance(curator)
pre_withdrawer_balance = state.get_balance(withdrawer)

# from ethereum.slogging import LogRecorder, configure_logging, set_level
# config_string = ':info,eth.vm.log:trace,eth.vm.op:trace,eth.vm.stack:trace,eth.vm.exit:trace,eth.pb.msg:trace,eth.pb.tx:debug'
# configure_logging(config_string=config_string)

tx4 = Transaction(0, 0, 1000000, withdrawer, 0, withdrawer_ct.encode('trusteeWithdraw', [])).sign('\x33' * 32)
apply_transaction(state, tx4)

post_curator_balance = state.get_balance(curator)
post_withdrawer_balance = state.get_balance(withdrawer)
print 'Curator withdrawn', post_curator_balance - pre_curator_balance

assert 500000 * 10**18 < post_curator_balance - pre_curator_balance < 600000 * 10**18
assert pre_curator_balance + pre_withdrawer_balance == post_curator_balance + post_withdrawer_balance

tx5 = Transaction(1, 0, 1000000, withdrawer, 0, withdrawer_ct.encode('trusteeWithdraw', [])).sign('\x33' * 32)
apply_transaction(state, tx5)

post_curator_balance2 = state.get_balance(curator)
post_withdrawer_balance2 = state.get_balance(withdrawer)
assert post_curator_balance2 == post_curator_balance
assert post_withdrawer_balance2 == post_withdrawer_balance

print 'Second withdrawal has no effect as expected'

# Withdraw again, and try curator withdrawing again

tx6 = Transaction(state.get_nonce(my_other_account), 0, 1000000, dao, 0, dao_ct.encode('approve', [withdrawer, 100000 * 10**18])).sign('\x33' * 32)
tx6._sender = normalize_address(my_other_account)
apply_transaction(state, tx6)

tx7 = Transaction(state.get_nonce(my_other_account), 0, 1000000, withdrawer, 0, withdrawer_ct.encode('withdraw', [])).sign('\x33' * 32)
tx7._sender = normalize_address(my_other_account)
apply_transaction(state, tx7)

post_withdrawer_balance3 = state.get_balance(withdrawer)
print 'Another %d wei withdrawn' % (post_withdrawer_balance2 - post_withdrawer_balance3)
assert post_withdrawer_balance3 < post_withdrawer_balance2

tx8 = Transaction(2, 0, 1000000, withdrawer, 0, withdrawer_ct.encode('trusteeWithdraw', [])).sign('\x33' * 32)
apply_transaction(state, tx8)

post_curator_balance3 = state.get_balance(curator)
assert post_curator_balance2 == post_curator_balance

print 'Third withdrawal has no effect as expected'

0 comments on commit eff0f4d

Please sign in to comment.