Esempio n. 1
0
def state_from_genesis_declaration(genesis_data, env, block=None, allow_empties=False):
    if block:
        assert isinstance(block, Block)
    else:
        block = block_from_genesis_declaration(genesis_data, env)

    state = State(env=env)
    for addr, data in genesis_data["alloc"].items():
        addr = normalize_address(addr)
        assert len(addr) == 20
        if 'wei' in data:
            state.set_balance(addr, parse_as_int(data['wei']))
        if 'balance' in data:
            state.set_balance(addr, parse_as_int(data['balance']))
        if 'code' in data:
            state.set_code(addr, parse_as_bin(data['code']))
        if 'nonce' in data:
            state.set_nonce(addr, parse_as_int(data['nonce']))
        if 'storage' in data:
            for k, v in data['storage'].items():
                state.set_storage_data(addr, big_endian_to_int(parse_as_bin(k)), big_endian_to_int(parse_as_bin(v)))
    get_consensus_strategy(state.config).initialize(state, block)
    state.commit(allow_empties=allow_empties)
    print('deleting %d' % len(state.deletes))
    rdb = RefcountDB(state.db)
    for delete in state.deletes:
        rdb.delete(delete)
    block.header.state_root = state.trie.root_hash
    state.prev_headers=[block.header]
    return state
Esempio n. 2
0
def init_state(env, pre):
    # Setup env
    state = State(
        env=Env(config=konfig),
        block_prevhash=decode_hex(remove_0x_head(env['previousHash'])),
        prev_headers=[mk_fake_header(i) for i in range(parse_int_or_hex(env['currentNumber']) - 1,
                                                       max(-1, parse_int_or_hex(env['currentNumber']) - 257), -1)],
        block_number=parse_int_or_hex(env['currentNumber']),
        block_coinbase=decode_hex(remove_0x_head(env['currentCoinbase'])),
        block_difficulty=parse_int_or_hex(env['currentDifficulty']),
        gas_limit=parse_int_or_hex(env['currentGasLimit']),
        timestamp=parse_int_or_hex(env['currentTimestamp']))

    # Fill up pre
    for address, h in list(pre.items()):
        assert len(address) in (40, 42)
        address = decode_hex(remove_0x_head(address))
        assert set(h.keys()) == set(['code', 'nonce', 'balance', 'storage'])
        state.set_nonce(address, parse_int_or_hex(h['nonce']))
        state.set_balance(address, parse_int_or_hex(h['balance']))
        state.set_code(address, decode_hex(remove_0x_head(h['code'])))
        for k, v in h['storage'].items():
            state.set_storage_data(address,
                                   big_endian_to_int(decode_hex(k[2:])),
                                   big_endian_to_int(decode_hex(v[2:])))

    state.commit(allow_empties=True)
    # state.commit()
    return state
Esempio n. 3
0
def profile_vm_test(params, _):
    pre = params['pre']
    exek = params['exec']
    env = params['env']
    index = 0

    # setup env
    blkh = block.BlockHeader(prevhash=codecs.decode(env['previousHash'],
                                                    'hex'),
                             number=int(env['currentNumber']),
                             coinbase=env['currentCoinbase'],
                             difficulty=int(env['currentDifficulty']),
                             gas_limit=int(env['currentGasLimit']),
                             timestamp=int(env['currentTimestamp']))
    block.Block(blkh, db=_env)
    state = State(env=_env,
                  block_number=int(env['currentNumber']),
                  block_coinbase=env['currentCoinbase'],
                  block_difficulty=int(env['currentDifficulty']),
                  gas_limit=int(env['currentGasLimit']),
                  timestamp=int(env['currentTimestamp']))

    # setup state
    for address, h in pre.items():
        state.set_nonce(address, int(h['nonce']))
        state.set_balance(address, int(h['balance']))
        state.set_balance("cd1722f3947def4cf144679da39c4c32bdc35681",
                          int(h['balance']))
        state.set_code(address, codecs.decode(h['code'][2:], 'hex'))
        for k, v in h['storage'].items():
            state.set_storage_data(
                address, u.big_endian_to_int(codecs.decode(k[2:], 'hex')),
                u.big_endian_to_int(codecs.decode(v[2:], 'hex')))

    for curr_row in range(1, num_tx + 1, 1):
        state = State(env=_env,
                      block_number=int(env['currentNumber']),
                      block_coinbase=env['currentCoinbase'],
                      block_difficulty=int(env['currentDifficulty']),
                      gas_limit=int(env['currentGasLimit']),
                      timestamp=int(env['currentTimestamp']))

        nonce = 0
        conBench(state, exek, curr_row, nonce, index)
        nonce = nonce + 1
        index += 1

    df = pd.DataFrame(result)
    df.columns = [
        'TX', 'Type', 'Used gas', 'minTime', 'maxTime', 'avgTime', 'medTime',
        'stdevTime', 'CI(95%)'
    ]
    writer = pd.ExcelWriter('BenchmarkResults8U.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Set8')
    writer.save()
Esempio n. 4
0
def mk_basic_state(alloc, header=None, env=None, executing_on_head=False):
    env = env or Env()
    state = State(env=env, executing_on_head=executing_on_head)
    if not header:
        header = {
            "number": 0,
            "gas_limit": env.config['BLOCK_GAS_LIMIT'],
            "gas_used": 0,
            "timestamp": 1467446877,
            "difficulty": 1,
            "uncles_hash": '0x' + encode_hex(BLANK_UNCLES_HASH)
        }
    h = BlockHeader(number=parse_as_int(header['number']),
                    timestamp=parse_as_int(header['timestamp']),
                    difficulty=parse_as_int(header['difficulty']),
                    gas_limit=parse_as_int(header['gas_limit']),
                    uncles_hash=parse_as_bin(header['uncles_hash']))
    state.prev_headers = [h]

    for addr, data in alloc.items():
        addr = normalize_address(addr)
        assert len(addr) == 20
        if 'wei' in data:
            state.set_balance(addr, parse_as_int(data['wei']))
        if 'balance' in data:
            state.set_balance(addr, parse_as_int(data['balance']))
        if 'code' in data:
            state.set_code(addr, parse_as_bin(data['code']))
        if 'nonce' in data:
            state.set_nonce(addr, parse_as_int(data['nonce']))
        if 'storage' in data:
            for k, v in data['storage'].items():
                state.set_storage_data(addr, parse_as_bin(k), parse_as_bin(v))

    state.block_number = header["number"]
    state.gas_limit = header["gas_limit"]
    state.timestamp = header["timestamp"]
    state.block_difficulty = header["difficulty"]
    state.commit()
    return state