Exemplo n.º 1
0
    def persist_header_to_db(self, header):
        """
        :returns: iterable of headers newly on the canonical chain
        """
        if header.parent_hash != GENESIS_PARENT_HASH and not self.header_exists(header.parent_hash):
            raise ParentNotFound(
                "Cannot persist block header ({}) with unknown parent ({})".format(
                    encode_hex(header.hash), encode_hex(header.parent_hash)))

        self.db.set(
            header.hash,
            rlp.encode(header),
        )

        if header.parent_hash == GENESIS_PARENT_HASH:
            score = header.difficulty
        else:
            score = self.get_score(header.parent_hash) + header.difficulty
        self.db.set(
            make_block_hash_to_score_lookup_key(header.hash),
            rlp.encode(score, sedes=rlp.sedes.big_endian_int))

        try:
            head_score = self.get_score(self.get_canonical_head().hash)
        except CanonicalHeadNotFound:
            new_headers = self._set_as_canonical_chain_head(header)
        else:
            if score > head_score:
                new_headers = self._set_as_canonical_chain_head(header)
            else:
                new_headers = []

        return new_headers
Exemplo n.º 2
0
def test_get_score(chaindb):
    genesis = BlockHeader(difficulty=1, block_number=0, gas_limit=0)
    chaindb.persist_header_to_db(genesis)

    genesis_score_key = make_block_hash_to_score_lookup_key(genesis.hash)
    genesis_score = rlp.decode(chaindb.db.get(genesis_score_key), sedes=rlp.sedes.big_endian_int)
    assert genesis_score == 1
    assert chaindb.get_score(genesis.hash) == 1

    block1 = BlockHeader(difficulty=10, block_number=1, gas_limit=0, parent_hash=genesis.hash)
    chaindb.persist_header_to_db(block1)

    block1_score_key = make_block_hash_to_score_lookup_key(block1.hash)
    block1_score = rlp.decode(chaindb.db.get(block1_score_key), sedes=rlp.sedes.big_endian_int)
    assert block1_score == 11
    assert chaindb.get_score(block1.hash) == 11
Exemplo n.º 3
0
def test_persist_header_to_db(chaindb, header):
    with pytest.raises(BlockNotFound):
        chaindb.get_block_header_by_hash(header.hash)
    number_to_hash_key = make_block_hash_to_score_lookup_key(header.hash)
    assert not chaindb.exists(number_to_hash_key)

    chaindb.persist_header_to_db(header)

    assert chaindb.get_block_header_by_hash(header.hash) == header
    assert chaindb.exists(number_to_hash_key)
Exemplo n.º 4
0
def test_persist_header(chaindb, header):
    with pytest.raises(BlockNotFound):
        chaindb.get_block_header_by_hash(header.hash)
    number_to_hash_key = make_block_hash_to_score_lookup_key(header.hash)
    assert not chaindb.exists(number_to_hash_key)

    chaindb.persist_header(header)

    assert chaindb.get_block_header_by_hash(header.hash) == header
    assert chaindb.exists(number_to_hash_key)
Exemplo n.º 5
0
def test_get_score(chaindb):
    genesis = BlockHeader(difficulty=1, block_number=0, gas_limit=0)
    chaindb.persist_header(genesis)

    genesis_score_key = make_block_hash_to_score_lookup_key(genesis.hash)
    genesis_score = rlp.decode(chaindb.db.get(genesis_score_key),
                               sedes=rlp.sedes.big_endian_int)
    assert genesis_score == 1
    assert chaindb.get_score(genesis.hash) == 1

    block1 = BlockHeader(difficulty=10,
                         block_number=1,
                         gas_limit=0,
                         parent_hash=genesis.hash)
    chaindb.persist_header(block1)

    block1_score_key = make_block_hash_to_score_lookup_key(block1.hash)
    block1_score = rlp.decode(chaindb.db.get(block1_score_key),
                              sedes=rlp.sedes.big_endian_int)
    assert block1_score == 11
    assert chaindb.get_score(block1.hash) == 11
Exemplo n.º 6
0
 def get_score(self, block_hash):
     return rlp.decode(
         self.db.get(make_block_hash_to_score_lookup_key(block_hash)),
         sedes=rlp.sedes.big_endian_int)
Exemplo n.º 7
0
def test_persist_block(chaindb, block):
    set_empty_root(chaindb, block.header)
    block_to_hash_key = make_block_hash_to_score_lookup_key(block.hash)
    assert not chaindb.exists(block_to_hash_key)
    chaindb.persist_block(block)
    assert chaindb.exists(block_to_hash_key)
Exemplo n.º 8
0
 def get_score(self, block_hash):
     return rlp.decode(
         self.db.get(make_block_hash_to_score_lookup_key(block_hash)),
         sedes=rlp.sedes.big_endian_int)
Exemplo n.º 9
0
def test_persist_block_to_db(chaindb, block):
    block_to_hash_key = make_block_hash_to_score_lookup_key(block.hash)
    assert not chaindb.exists(block_to_hash_key)
    chaindb.persist_block_to_db(block)
    assert chaindb.exists(block_to_hash_key)
Exemplo n.º 10
0
def test_persist_block_to_db(chaindb, block):
    block_to_hash_key = make_block_hash_to_score_lookup_key(block.hash)
    assert not chaindb.exists(block_to_hash_key)
    chaindb.persist_block_to_db(block)
    assert chaindb.exists(block_to_hash_key)
Exemplo n.º 11
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
Exemplo n.º 12
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)