示例#1
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
示例#2
0
 def init_from_rlp(cls, block_data, newblock_timestamp=0):
     header = BlockHeader.deserialize(block_data[0])
     transactions = rlp.sedes.CountableList(Transaction).deserialize(
         block_data[1])
     uncles = rlp.sedes.CountableList(BlockHeader).deserialize(
         block_data[2])
     return cls(header, transactions, uncles, newblock_timestamp)
示例#3
0
def build_dao_header(config):
    return BlockHeader(
        prevhash=decode_hex(
            'a218e2c611f21232d857e3c8cecdcdf1f65f25a4477f98f6f47e4063807f2308'
        ),
        uncles_hash=decode_hex(
            '1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
        ),
        coinbase=decode_hex('bcdfc35b86bedf72f0cda046a3c16829a2ef41d1'),
        state_root=decode_hex(
            'c5e389416116e3696cce82ec4533cce33efccb24ce245ae9546a4b8f0d5e9a75'
        ),
        tx_list_root=decode_hex(
            '7701df8e07169452554d14aadd7bfa256d4a1d0355c1d174ab373e3e2d0a3743'
        ),
        receipts_root=decode_hex(
            '26cf9d9422e9dd95aedc7914db690b92bab6902f5221d62694a2fa5d065f534b'
        ),
        bloom=int256.deserialize(
            decode_hex(
                '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
            ), ),
        difficulty=big_endian_to_int(decode_hex('38c3bf2616aa')),
        number=config['DAO_FORK_BLKNUM'],
        gas_limit=big_endian_to_int(decode_hex('47e7c0')),
        gas_used=big_endian_to_int(decode_hex('014820')),
        timestamp=big_endian_to_int(decode_hex('578f7aa8')),
        extra_data=config['DAO_FORK_BLKEXTRA'],
        mixhash=decode_hex(
            '5b5acbf4bf305f948bd7be176047b20623e1417f75597341a059729165b92397'
        ),
        nonce=decode_hex('bede87201de42426'))
示例#4
0
 def __init__(self, app):
     super(ChainServiceMock, self).__init__(app)
     self.on_new_head_cbs = []
     self.transaction_queue = TransactionQueue()
     self.is_syncing = False
     self.mined_block = None
     self.block_mined_event = Event()
     self.head_candidate = Block(BlockHeader(difficulty=DIFFICULTY),
                                 db=DB())
示例#5
0
def block_from_genesis_declaration(genesis_data, env):
    h = BlockHeader(nonce=parse_as_bin(genesis_data["nonce"]),
                    difficulty=parse_as_int(genesis_data["difficulty"]),
                    mixhash=parse_as_bin(genesis_data.get("mixhash", genesis_data.get("mixHash", "0"*64))),
                    coinbase=parse_as_bin(genesis_data["coinbase"]),
                    bloom=parse_as_int(genesis_data.get("bloom", "0")),
                    timestamp=parse_as_int(genesis_data["timestamp"]),
                    prevhash=parse_as_bin(genesis_data["parentHash"]),
                    extra_data=parse_as_bin(genesis_data["extraData"]),
                    gas_used=parse_as_int(genesis_data.get("gasUsed", "0")),
                    gas_limit=parse_as_int(genesis_data["gasLimit"]))
    return Block(h, [], [])
示例#6
0
def to_blockheader(block_attr_dict):
    return BlockHeader(bytes(block_attr_dict.parentHash),
                       bytes(block_attr_dict.sha3Uncles),
                       normalize_address(block_attr_dict.miner),
                       bytes(block_attr_dict.stateRoot),
                       bytes(block_attr_dict.transactionsRoot),
                       bytes(block_attr_dict.receiptsRoot),
                       bytes_to_int(bytes(block_attr_dict.logsBloom)),
                       block_attr_dict.difficulty, block_attr_dict.number,
                       block_attr_dict.gasLimit, block_attr_dict.gasUsed,
                       block_attr_dict.timestamp,
                       bytes(block_attr_dict.extraData),
                       bytes(block_attr_dict.mixHash),
                       bytes(block_attr_dict.nonce))
示例#7
0
 def run(self):
     """Import all the blocks in /blocks/"""
     print('\nImport all the blocks in /blocks/')
     blocks = sorted(os.listdir('/blocks'), key=str)
     for block in blocks:
         data = open('/blocks/' + block, 'r').read()
         block_data = rlp.decode_lazy(data)
         header = BlockHeader.deserialize(block_data[0])
         transactions = rlp.sedes.CountableList(Transaction).deserialize(
             block_data[1])
         uncles = rlp.sedes.CountableList(BlockHeader).deserialize(
             block_data[2])
         transient_block = TransientBlock(header, transactions, uncles, 0)
         print '\nIMPORTING BLOCK %s (%s)' % (
             block, transient_block.header.hex_hash)
         self.eth.chain.add_block(transient_block.to_block())
示例#8
0
def mk_block_from_prevstate(chain, state=None, timestamp=None,
                            coinbase=b'\x35' * 20, extra_data='moo ha ha says the laughing cow.'):
    state = state or chain.state
    blk = Block(BlockHeader())
    now = timestamp or chain.time()
    blk.header.number = state.prev_headers[0].number + 1
    blk.header.difficulty = calc_difficulty(
        state.prev_headers[0], now, chain.config)
    blk.header.gas_limit = calc_gaslimit(state.prev_headers[0], chain.config)
    blk.header.timestamp = max(now, state.prev_headers[0].timestamp + 1)
    blk.header.prevhash = state.prev_headers[0].hash
    blk.header.coinbase = coinbase
    blk.header.extra_data = extra_data
    blk.header.bloom = 0
    blk.transactions = []
    return blk
示例#9
0
def mk_block_from_prevstate(chain,
                            state=None,
                            timestamp=None,
                            coinbase=b'\x35' * 20,
                            extra_data=b'moo ha ha says the laughing cow.'):
    state = state or chain.state
    now = timestamp or chain.time()

    blk = Block(header=BlockHeader(
        number=state.prev_headers[0].number + 1,
        difficulty=calc_difficulty(state.prev_headers[0], now, chain.config),
        gas_limit=calc_gaslimit(state.prev_headers[0], chain.config),
        timestamp=max(now, state.prev_headers[0].timestamp + 1),
        prevhash=state.prev_headers[0].hash,
        coinbase=coinbase,
        extra_data=extra_data,
        bloom=0),
                transactions=[])
    return blk
示例#10
0
def test_export():
    # requires a chain with at least 5 blocks
    assert subprocess.call('pyethapp export', shell=True) != 0
    assert subprocess.call('pyethapp export --from -1 -', shell=True) != 0
    assert subprocess.call('pyethapp export --to -3 -', shell=True) != 0
    assert subprocess.call('pyethapp export --from 4 --to 2 -',
                           shell=True) != 0

    result = subprocess.Popen('pyethapp export --from 2 --to 4 -',
                              shell=True,
                              stdout=subprocess.PIPE)
    result.wait()
    assert result.returncode == 0
    s = result.stdout.read()

    headers = []
    end = 0
    while end < len(s):
        item, end = rlp.codec.consume_item(s, end)
        headers.append(BlockHeader.deserialize(item[0]))
    assert [header.number for header in headers] == [2, 3, 4]
示例#11
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
from ethereum.block import BlockHeader
import rlp
try:
    data = json.load(open('progress.json'))
    blknum = data['blknum']
    pos = data['pos']
except:
    blknum, pos = 0, 0
f = open('geth-2283415.dump')
outdata = []
while 1:
    f.seek(pos)
    prefix = f.read(10)
    _typ, _len, _pos = rlp.codec.consume_length_prefix(prefix, 0)
    blkdata = prefix + f.read(_pos + _len - 10)
    header = rlp.decode(rlp.descend(blkdata, 0), BlockHeader)
    txcount = len(rlp.decode(rlp.descend(blkdata, 1)))
    uncles = [
        BlockHeader.deserialize(x) for x in rlp.decode(rlp.descend(blkdata, 2))
    ]
    outdata.append([
        header.number,
        len(uncles),
        sum([4.375 - 0.625 * (header.number - u.number) for u in uncles]),
        sum([u.gas_used for u in uncles]), txcount, header.gas_used,
        _len + _pos,
        blkdata.count('\x00')
    ])
    print outdata[-1]
    pos += _pos + _len
import json
from ethereum.block import BlockHeader
import rlp
try:
    data = json.load(open('progress.json'))
    blknum = data['blknum']
    pos = data['pos']
except:
    blknum, pos = 0, 0
f = open('geth-2283415.dump')
outdata = []
while 1:
    f.seek(pos)
    prefix = f.read(10)
    _typ, _len, _pos = rlp.codec.consume_length_prefix(prefix, 0)
    blkdata = prefix + f.read(_pos + _len - 10)
    header = rlp.decode(rlp.descend(blkdata, 0), BlockHeader)
    txcount = len(rlp.decode(rlp.descend(blkdata, 1)))
    uncles = [BlockHeader.deserialize(x) for x in rlp.decode(rlp.descend(blkdata, 2))]
    outdata.append([header.number, len(uncles), sum([4.375 - 0.625 * (header.number - u.number) for u in uncles]), sum([u.gas_used for u in uncles]), txcount, header.gas_used, _len + _pos, blkdata.count('\x00')])
    print outdata[-1]
    pos += _pos + _len