def inject_tx(txhex):
    tx = rlp.decode(utils.decode_hex(txhex[2:]), transactions.Transaction)
    s.state.set_balance(tx.sender, tx.startgas * tx.gasprice)
    state_transition.apply_transaction(s.state, tx)
    contract_address = utils.mk_contract_address(tx.sender, 0)
    assert s.state.get_code(contract_address)
    return contract_address
Example #2
0
def get_contract_code(init_code):
    s = State(env=Env(config=casper_config))
    s.gas_limit = 10**9
    apply_transaction(s, Transaction(0, 0, 10**8, '', 0, init_code))
    addr = utils.mk_metropolis_contract_address(
        casper_config['METROPOLIS_ENTRY_POINT'], init_code)
    o = s.get_code(addr)
    assert o
    return o
Example #3
0
def casper_start_epoch(state):
    ct = get_casper_ct()
    t = Transaction(
        0,
        0,
        10**8,
        casper_config['CASPER_ADDR'],
        0,
        ct.encode(
            'newEpoch',
            [0]))
    t._sender = casper_config['CASPER_ADDR']
    apply_transaction(state, t)
Example #4
0
def test_multisends(payouts, transactions):
    s = tester.state()
    roottx = transactions[0]
    s.state.set_balance(roottx.sender, roottx.value + roottx.startgas * roottx.gasprice)
    gas_used = 0
    for i, tx in enumerate(transactions):
        s.state.get_balance(roottx.sender)
        state_transition.apply_transaction(s.state, tx)
        print "Applying transaction number %d consumed %d gas out of %d" % (i, s.state.gas_used - gas_used, tx.startgas)
        gas_used = s.state.gas_used

    for addr, value in payouts:
        balance = s.state.get_balance(utils.normalize_address(addr))
        assert balance == value, (addr, balance, value)
    return s.state.gas_used
Example #5
0
def casper_setup_block(chain,
                       state=None,
                       timestamp=None,
                       coinbase='\x35' * 20,
                       extra_data='moo ha ha says the laughing cow.'):
    state = state or chain.state
    blk = Block(BlockHeader())
    now = timestamp or chain.time()
    prev_blknumber = call_casper(state, 'getBlockNumber')
    blk.header.number = prev_blknumber + 1
    blk.header.difficulty = 1
    blk.header.gas_limit = call_casper(state, 'getGasLimit')
    blk.header.timestamp = max(now, state.prev_headers[0].timestamp + 1)
    blk.header.prevhash = apply_const_message(
        state,
        sender=casper_config['METROPOLIS_ENTRY_POINT'],
        to=casper_config['METROPOLIS_BLOCKHASH_STORE'],
        data=utils.encode_int32(prev_blknumber))
    blk.header.coinbase = coinbase
    blk.header.extra_data = extra_data
    blk.header.bloom = 0
    blk.uncles = []
    initialize(state, blk)
    for tx in get_dunkle_candidates(chain, state):
        assert apply_transaction(state, tx)
        blk.transactions.append(tx)
    log_bc.info('Block set up with number %d and prevhash %s, %d dunkles' %
                (blk.header.number, utils.encode_hex(
                    blk.header.prevhash), len(blk.transactions)))
    return blk
Example #6
0
def validator_inject(state, vcode, deposit_size,
                     randao_commitment, address, nonce=0, ct=None):
    if not ct:
        ct = get_casper_ct()
    state.set_balance(utils.int_to_addr(1), deposit_size)
    t = Transaction(nonce, 0, 10**8, casper_config['CASPER_ADDR'], deposit_size,
                    ct.encode('deposit', [vcode, randao_commitment, address]))
    t._sender = utils.int_to_addr(1)
    success, output = apply_transaction(state, t)
    assert success
Example #7
0
def casper_contract_bootstrap(state,
                              timestamp=0,
                              epoch_length=100,
                              number=0,
                              gas_limit=4712388,
                              nonce=0):
    ct = get_casper_ct()
    # Set genesis time, and initialize epoch number
    t = Transaction(
        nonce, 0, 10**8, casper_config['CASPER_ADDR'], 0,
        ct.encode('initialize', [timestamp, epoch_length, number, gas_limit]))
    success, output = apply_transaction(state, t)
    assert success
Example #8
0
assert skip_count == 0
b2 = make_block(chains[0], privkeys[next_validator_id],
                randaos[next_validator_id], vchashes[next_validator_id], skip_count)
assert chains[0].add_block(b2)
vids.append(next_validator_id)
print('Second block added to chain')
# Make a dunkle and include it in a transaction
next_validator = call_casper(chains[1].state, 'getValidator', [1])
next_validator_id = vchashes.index(next_validator)
skip_count, timestamp = get_skips_and_block_making_time(chains[1].state, next_validator)
assert skip_count == 1
b3 = make_block(chains[1], privkeys[next_validator_id],
                randaos[next_validator_id], vchashes[next_validator_id], skip_count)
print('Dunkle produced')
t = Transaction(0, 0, 10**6, casper_config['CASPER_ADDR'], 0, ct.encode('includeDunkle', [rlp.encode(b3.header)])).sign(privkeys[0])
apply_transaction(chains[0].state, t)
assert call_casper(chains[0].state, 'isDunkleIncluded', [utils.sha3(rlp.encode(b3.header))])
print('Dunkle added successfully')
# Try (and fail) to add the dunkle again
x = chains[0].state.gas_used
t = Transaction(1, 0, 10**6, casper_config['CASPER_ADDR'], 0, ct.encode('includeDunkle', [rlp.encode(b3.header)])).sign(privkeys[0])
apply_transaction(chains[0].state, t)
x2 = chains[0].state.gas_used
assert x2 - x == t.startgas, (x2 - x, t.startgas)
print('Dunkle addition failed, as expected, since dunkle is a duplicate')
# Induct a new validator
k, a, ds, r = privkeys[-1], addrs[-1], deposit_sizes[-1], randaos[-1]
vc = generate_validation_code(a)
chains[0].state.set_balance(a, (ds + 1) * 10**18)
t2 = Transaction(chains[0].state.get_nonce(a), 0, 1000000, casper_config['CASPER_ADDR'],
                 ds * 10**18, ct.encode('deposit', [vc, r.get(9999)])).sign(k)
Example #9
0
s.gas_limit = 10**9
s.prev_headers[0].timestamp = 2
s.timestamp = 2
s.prev_headers[0].difficulty = 1
s.block_difficulty = 1
s.set_code(casper_config['CASPER_ADDR'], get_casper_code())
s.set_code(casper_config['RLP_DECODER_ADDR'], get_rlp_decoder_code())
s.set_code(casper_config['HASH_WITHOUT_BLOOM_ADDR'], get_hash_without_ed_code())
ct = get_casper_ct()
# Add all validators
for k, r, ds in zip(keys, randaos, deposit_sizes):
    a = privtoaddr(k)
    # Leave 1 eth to pay txfees
    s.set_balance(a, (ds + 1) * 10**18)
    t = Transaction(0, 0, 10**8, casper_config['CASPER_ADDR'], ds * 10**18, ct.encode('deposit', [generate_validation_code(a), r.get(9999)])).sign(k)
    success, gas, logs = apply_transaction(s, t)
s.commit()
g = s.to_snapshot()
print 'Genesis state created'

validators = [Validator(g, k, n, Env(config=casper_config), time_offset=4) for k in keys]
n.agents = validators
n.generate_peers()

for i in range(100000):
    # print 'ticking'
    n.tick()
    if i % 100 == 0:
        print 'Validator heads:', [v.chain.head.header.number if v.chain.head else None for v in validators]
        print 'Total blocks created:', casper.global_block_counter
        print 'Dunkle count:', call_casper(validators[0].chain.state, 'getTotalDunklesIncluded', [])
Example #10
0
# Check pre-balance

pre_balance = state.get_balance(my_account)
pre_dao_tokens = get_dao_balance(state, my_account)
pre_withdrawer_balance = state.get_balance(withdrawer)

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

# Attempt to claim the ETH without approving (should fail)

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

med_balance = state.get_balance(my_account)
med_dao_tokens = get_dao_balance(state, my_account)
med_withdrawer_balance = state.get_balance(withdrawer)

assert med_balance == pre_balance
assert med_dao_tokens == pre_dao_tokens
assert med_withdrawer_balance == pre_withdrawer_balance > 0

print 'ETH claim without approving failed, as expected'

# Approve the withdrawal

tx1 = Transaction(
    state.get_nonce(my_account), 0, 1000000, dao, 0,
Example #11
0
print 'State created'

# Check pre-balance

pre_balance = state.get_balance(my_account)
pre_dao_tokens = get_dao_balance(state, my_account)
pre_withdrawer_balance = state.get_balance(withdrawer)

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

# Attempt to claim the ETH without approving (should fail)

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

med_balance = state.get_balance(my_account)
med_dao_tokens = get_dao_balance(state, my_account)
med_withdrawer_balance = state.get_balance(withdrawer)

assert med_balance == pre_balance
assert med_dao_tokens == pre_dao_tokens
assert med_withdrawer_balance == pre_withdrawer_balance > 0

print 'ETH claim without approving failed, as expected'

# 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)
Example #12
0
#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)
import rlp
s = t.state()
t.languages['viper'] = compiler.Compiler()
t.gas_limit = 9999999

EPOCH_LENGTH = 100

# Install RLP decoder library
s.state.set_balance('0xfe2ec957647679d210034b65e9c7db2452910b0c',
                    9350880000000000)
state_transition.apply_transaction(
    s.state,
    rlp.decode(
        utils.decode_hex(
            'f903bd808506fc23ac008304c1908080b903aa6103988061000e6000396103a65660006101bf5361202059905901600090526101008152602081019050602052600060605261040036018060200159905901600090528181526020810190509050608052600060e0527f0100000000000000000000000000000000000000000000000000000000000000600035046101005260c061010051121561007e57fe5b60f86101005112156100a95760c061010051036001013614151561009e57fe5b6001610120526100ec565b60f761010051036020036101000a600161012051013504610140526101405160f7610100510360010101361415156100dd57fe5b60f76101005103600101610120525b5b366101205112156102ec577f01000000000000000000000000000000000000000000000000000000000000006101205135046101005260e0516060516020026020510152600160605101606052608061010051121561017a57600160e0516080510152600161012051602060e0516080510101376001610120510161012052602160e0510160e0526102da565b60b8610100511215610218576080610100510360e05160805101526080610100510360016101205101602060e05160805101013760816101005114156101ef5760807f010000000000000000000000000000000000000000000000000000000000000060016101205101350412156101ee57fe5b5b600160806101005103016101205101610120526020608061010051030160e0510160e0526102d9565b60c06101005112156102d65760b761010051036020036101000a6001610120510135046101405260007f0100000000000000000000000000000000000000000000000000000000000000600161012051013504141561027357fe5b603861014051121561028157fe5b6101405160e05160805101526101405160b761010051600161012051010103602060e05160805101013761014051600160b7610100510301016101205101610120526020610140510160e0510160e0526102d8565bfe5b5b5b602060605113156102e757fe5b6100ed565b60e051606051602002602051015261082059905901600090526108008152602081019050610160526000610120525b6060516101205113151561035c576020602060605102610120516020026020510151010161012051602002610160510152600161012051016101205261031b565b60e0518060206020606051026101605101018260805160006004600a8705601201f161038457fe5b50602060e051602060605102010161016051f35b6000f31b2d4f'
        ), transactions.Transaction))
assert s.state.get_code('0x0b8178879f97f2ada01fb8d219ee3d0ad74e91e0')

# Install sig hasher

s.state.set_balance('0x6e7406512b244843c1171840dfcd3d7532d979fe',
                    7291200000000000)

state_transition.apply_transaction(
    s.state,
    rlp.decode(
        utils.decode_hex(
            'f902b9808506fc23ac008303b5608080b902a66102948061000e6000396102a2567f01000000000000000000000000000000000000000000000000000000000000006000350460205260c0602051121561003857fe6100a7565b60f8602051121561005657600160405260c0602051036060526100a6565b60f76020510360010160405260007f010000000000000000000000000000000000000000000000000000000000000060013504141561009157fe5b60f7602051036020036101000a600135046060525b5b36606051604051011415156100b857fe5b604051608052600060a0525b3660405112156101c0577f0100000000000000000000000000000000000000000000000000000000000000604051350460c052608060c05112156101165760405160a0526001604051016040526101bb565b60b860c051121561014257608060c0510360605260405160a052600160605101604051016040526101ba565b60c060c05112156101b75760007f01000000000000000000000000000000000000000000000000000000000000006001604051013504141561018057fe5b60b760c051036020036101000a600160405101350460605260405160a052600160b760c051036060510101604051016040526101b9565bfe5b5b5b6100c4565b60805160a0510360e0526103e861010052603860e051121561020f5760e05160c001610100515360e051608051600161010051013760e0516001016101005120610120526020610120f3610293565b60006101405260e051610160525b610160511561024257600161014051016101405261010061016051046101605261021d565b6101405160f7016101005153610140516020036101000a60e05102600161010051015260e0516080516101405160016101005101013760e05161014051600101016101005120610180526020610180f35b5b6000f31b2d4f'
        ), transactions.Transaction))