Пример #1
0
    def import_block(self, block):
        """
        Import a complete block.
        """
        if block.number > self.header.block_number:
            raise ValidationError(
                "Attempt to import block #{0}.  Cannot import block with number "
                "greater than current block #{1}.".format(
                    block.number,
                    self.header.block_number,
                )
            )

        parent_chain = self.get_parent_chain(block)
        imported_block = parent_chain.get_vm().import_block(block)
        # It feels wrong to call validate_block() on self here, but we do that
        # because we want to look up the recent uncles starting from the
        # current canonical chain head.
        self.validate_block(imported_block)

        persist_block_to_db(self.db, imported_block)
        if self.should_be_canonical_chain_head(imported_block):
            self.add_to_canonical_chain_head(imported_block)

        return imported_block
Пример #2
0
    def from_genesis(cls, db, genesis_params, genesis_state=None):
        """
        Initialize the EVM from a genesis state.
        """
        state_db = State(db)

        if genesis_state is None:
            genesis_state = {}

        for account, account_data in genesis_state.items():
            state_db.set_balance(account, account_data['balance'])
            state_db.set_nonce(account, account_data['nonce'])
            state_db.set_code(account, account_data['code'])

            for slot, value in account_data['storage'].items():
                state_db.set_storage(account, slot, value)

        genesis_header = BlockHeader(**genesis_params)
        if genesis_header.state_root != state_db.root_hash:
            raise ValidationError(
                "The provided genesis state root does not match the computed "
                "genesis state root.  Got {0}.  Expected {1}".format(
                    state_db.root_hash,
                    genesis_header.state_root,
                ))

        evm = cls(db, genesis_header)
        persist_block_to_db(evm.db, evm.get_block())

        # XXX: It doesn't feel right to overwrite evm.header here given that
        # it is set by EVM.__init__, which we called above.
        evm.header = evm.create_header_from_parent(genesis_header)
        return evm
Пример #3
0
    def from_genesis(cls,
                     db,
                     genesis_params,
                     genesis_state=None):
        """
        Initialize the Chain from a genesis state.
        """
        state_db = State(db)

        if genesis_state is None:
            genesis_state = {}

        for account, account_data in genesis_state.items():
            state_db.set_balance(account, account_data['balance'])
            state_db.set_nonce(account, account_data['nonce'])
            state_db.set_code(account, account_data['code'])

            for slot, value in account_data['storage'].items():
                state_db.set_storage(account, slot, value)

        genesis_header = BlockHeader(**genesis_params)
        if genesis_header.state_root != state_db.root_hash:
            raise ValidationError(
                "The provided genesis state root does not match the computed "
                "genesis state root.  Got {0}.  Expected {1}".format(
                    state_db.root_hash,
                    genesis_header.state_root,
                )
            )

        genesis_chain = cls(db, genesis_header)
        persist_block_to_db(db, genesis_chain.get_block())
        add_block_number_to_hash_lookup(db, genesis_chain.get_block())

        return cls(db, genesis_chain.create_header_from_parent(genesis_header))
Пример #4
0
    def from_genesis(cls, genesis_params, genesis_state=None):
        """
        Initialize the EVM from a genesis state.
        """
        if cls.db is None:
            raise ValueError("MetaEVM class must have a db")

        state_db = State(cls.db)

        if genesis_state is None:
            genesis_state = {}

        for account, account_data in genesis_state.items():
            state_db.set_balance(account, account_data['balance'])
            state_db.set_nonce(account, account_data['nonce'])
            state_db.set_code(account, account_data['code'])

            for slot, value in account_data['storage']:
                state_db.set_storage(account, slot, value)

        genesis_header = BlockHeader(**genesis_params)
        if genesis_header.state_root != state_db.root_hash:
            raise ValidationError(
                "The provided genesis state root does not match the computed "
                "genesis state root.  Got {0}.  Expected {1}".format(
                    state_db.root_hash,
                    genesis_header.state_root,
                ))

        meta_evm = cls(header=genesis_header)
        evm = meta_evm.get_evm()
        persist_block_to_db(meta_evm.db, evm.block)

        meta_evm.header = evm.create_header_from_parent(genesis_header)
        return meta_evm
Пример #5
0
    def mine_block(self, **mine_params):
        """
        Mine the current block, applying
        """
        vm = self.get_vm()

        block = vm.mine_block(**mine_params)
        persist_block_to_db(self.db, block)

        self.header = vm.create_header_from_parent(block.header)

        return block
Пример #6
0
def test_get_block_header_by_hash(db, block, header):
    persist_block_to_db(db, block)
    block_header = get_block_header_by_hash(db, block.hash)
    assert_rlp_equal(block_header, header)
Пример #7
0
def test_get_score(db, block):
    persist_block_to_db(db, block)
    block_to_hash_key = make_block_hash_to_score_lookup_key(block.hash)
    score = rlp.decode(db.get(block_to_hash_key), sedes=rlp.sedes.big_endian_int)
    assert get_score(db, block.hash) == score
Пример #8
0
def test_perist_block_to_db(db, block):
    block_to_hash_key = make_block_hash_to_score_lookup_key(block.hash)
    assert not db.exists(block_to_hash_key)
    persist_block_to_db(db, block)
    assert db.exists(block_to_hash_key)