Пример #1
0
def test_farm_block_one_spendbundle():
    REWARD = 10000
    unspent_db = RAM_DB()
    chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db)

    pos = ProofOfSpace(get_pool_public_key(), get_plot_public_key())

    puzzle_hash = puzzle_hash_for_index(1)

    empty_spend_bundle = SpendBundle.aggregate([])
    header, header_signature, body = farm_block(GENESIS_BLOCK,
                                                Signature.zero(), 1, pos,
                                                empty_spend_bundle,
                                                puzzle_hash, REWARD)
    coinbase_coin = body.coinbase_coin

    conditions = standard_conditions()
    spend_bundle = spend_coin(coin=coinbase_coin,
                              conditions=conditions,
                              index=1)

    header, header_signature, body = farm_block(GENESIS_BLOCK,
                                                Signature.zero(), 1, pos,
                                                spend_bundle, puzzle_hash,
                                                REWARD)
    removals = removals_for_body(body)
    assert len(removals) == 1
    assert removals[0] == list(spend_bundle.coin_solutions)[0].coin.name()

    run = asyncio.get_event_loop().run_until_complete
    additions, removals = run(
        chain_view.accept_new_block(header, unspent_db, REWARD, 0))
    assert len(additions) == 4
    assert len(removals) == 1
Пример #2
0
def test_farm_two_blocks():
    """
    In this test, we farm two blocks: one empty block,
    then one block which spends the coinbase transaction from the empty block.
    """
    REWARD = 10000
    unspent_db = RAM_DB()
    chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db)

    assert chain_view.genesis_hash == GENESIS_BLOCK
    assert chain_view.tip_hash == HeaderHash(GENESIS_BLOCK)
    assert chain_view.tip_index == 0
    assert chain_view.unspent_db == unspent_db

    pos_1 = ProofOfSpace(get_pool_public_key(), get_plot_public_key())

    puzzle_hash = puzzle_hash_for_index(1)

    empty_spend_bundle = SpendBundle.aggregate([])
    header, header_signature, body = farm_block(GENESIS_BLOCK,
                                                Signature.zero(), 1, pos_1,
                                                empty_spend_bundle,
                                                puzzle_hash, REWARD)

    run = asyncio.get_event_loop().run_until_complete
    additions, removals = run(
        chain_view.accept_new_block(header, unspent_db, REWARD, 0))
    assert len(additions) == 2
    assert len(removals) == 0
    # TODO: check additions
    assert additions[1].puzzle_hash == body.fees_coin.puzzle_hash
    assert additions[1].amount == 0

    chain_view = run(
        chain_view.augment_chain_view(header, header_signature, unspent_db,
                                      unspent_db, REWARD, 0))

    assert chain_view.genesis_hash == GENESIS_BLOCK
    assert chain_view.tip_hash == HeaderHash(header)
    assert chain_view.tip_index == 1
    assert chain_view.unspent_db == unspent_db

    conditions = standard_conditions()
    spend_bundle_2 = spend_coin(coin=additions[0],
                                conditions=conditions,
                                index=1)

    assert validate_spend_bundle_signature(spend_bundle_2)

    pos_2 = ProofOfSpace(get_pool_public_key(1), get_plot_public_key())

    header_2, header_signature_2, body_2 = farm_block(header, header_signature,
                                                      2, pos_2, spend_bundle_2,
                                                      puzzle_hash, REWARD)
    print(header_2)
    print(header_signature_2)

    removals = removals_for_body(body_2)
    assert len(removals) == 1
    assert removals[0] == list(spend_bundle_2.coin_solutions)[0].coin.name()
Пример #3
0
 async def check_tip_signature(self, storage):
     if self.tip_hash == self.genesis_hash:
         if self.tip_signature != Signature.zero():
             raise ConsensusError(Err.BAD_GENESIS_SIGNATURE,
                                  self.tip_signature)
     else:
         await check_header_signature(self.tip_hash, self.tip_signature,
                                      storage)
Пример #4
0
async def check_header_signature(header_hash: HeaderHash,
                                 header_signature: Signature,
                                 storage: Storage):

    # fetch header for header_hash

    header = await header_hash.obj(storage)
    if header is None:
        raise ConsensusError(Err.MISSING_FROM_STORAGE, header_hash)

    # get proof of space

    pos = await header.proof_of_space_hash.obj(storage)
    if pos is None:
        raise ConsensusError(Err.MISSING_FROM_STORAGE,
                             header.proof_of_space_hash)

    # verify header signature

    hkp = header_signature.aggsig_pair(pos.plot_public_key, HeaderHash(header))
    if not header_signature.validate([hkp]):
        raise ConsensusError(Err.BAD_HEADER_SIGNATURE, header_signature)
Пример #5
0
def test_farm_block_empty():
    REWARD = 10000
    unspent_db = RAM_DB()
    chain_view = ChainView.for_genesis_hash(GENESIS_BLOCK, unspent_db)

    pos = ProofOfSpace(get_pool_public_key(), get_plot_public_key())

    puzzle_hash = puzzle_hash_for_index(1)

    spend_bundle = SpendBundle.aggregate([])

    header, header_signature, body = farm_block(GENESIS_BLOCK,
                                                Signature.zero(), 1, pos,
                                                spend_bundle, puzzle_hash,
                                                REWARD)
    removals = removals_for_body(body)
    assert len(removals) == 0

    run = asyncio.get_event_loop().run_until_complete
    additions, removals = run(
        chain_view.accept_new_block(header, unspent_db, REWARD, 0))
    assert len(additions) == 2
    assert len(removals) == 0
Пример #6
0
 def for_genesis_hash(cls, genesis_hash: HeaderHash, unspent_db: UnspentDB):
     return cls(genesis_hash, genesis_hash, Signature.zero(), 0, unspent_db)