Ejemplo n.º 1
0
 def mine(self, number_of_blocks=1, timestamp=14, coinbase=a0):
     self.cs.finalize(self.head_state, self.block)
     set_execution_results(self.head_state, self.block)
     self.block = Miner(self.block).mine(rounds=100, start_nonce=0)
     assert self.chain.add_block(self.block)
     b = self.block
     for i in range(1, number_of_blocks):
         b, _ = make_head_candidate(
             self.chain, parent=b, timestamp=self.chain.state.timestamp + timestamp, coinbase=coinbase)
         b = Miner(b).mine(rounds=100, start_nonce=0)
         assert self.chain.add_block(b)
     self.change_head(b.header.hash, coinbase)
     return b
Ejemplo n.º 2
0
def test_validate_epochs_skipping_one(db):
    """"
    Create 3 validators, validate an epoch, then skip one, and then mine the next one. Make sure the correct info was submitted
    """
    # Enable validator logging
    t, casper, validators = init_multi_validator_casper(3)
    validators[0].mining = True
    # Mine enough epochs to log everyone in, and then mine one more where everyone is able to prepare & commit
    mine_epochs(validators[0], 3)
    # Mine the fourth epoch without including any Casper transactions
    validators[0].mining = False
    for i in range(EPOCH_LENGTH):
        head_candidate, head_candidate_state = make_head_candidate(
            validators[0].chain,
            timestamp=validators[0].chain.state.timestamp + 14)
        block = Miner(head_candidate).mine(rounds=100, start_nonce=0)
        validators[0].broadcast_newblock(block)
    # Make sure our prev_commit_epoch is for epoch 3
    for v in validators:
        assert v.prev_commit_epoch == 3
    # Now mine the 5th epoch
    validators[0].mining = True
    mine_epochs(validators[0], 1)
    # Check that the prev_commit_epoch is 5
    for v in validators:
        assert v.prev_commit_epoch == 5
    # Make sure the hash for the epoch is justified and the epoch is committed
    cp_hash = validators[0].chain.get_block_by_number(5 * EPOCH_LENGTH).hash
    assert validators[0].chain.get_checkpoint_score(cp_hash) >= 0.6
    # Make sure epoch 4 was not committed
    cp_4_hash = validators[0].chain.get_block_by_number(4 * EPOCH_LENGTH).hash
    assert validators[0].chain.get_checkpoint_score(cp_4_hash) == 0
    # TODO: Check that the ancestry hash is correct
    # Log the first validator's chain
    log_chain(validators[0])
Ejemplo n.º 3
0
 def mine(self, number_of_blocks=1, coinbase=a0):
     self.cs.finalize(self.head_state, self.block)
     set_execution_results(self.head_state, self.block)
     self.block = Miner(self.block).mine(rounds=100, start_nonce=0)
     assert self.chain.add_block(self.block)
     assert self.head_state.trie.root_hash == self.chain.state.trie.root_hash
     for i in range(1, number_of_blocks):
         b, _ = make_head_candidate(self.chain,
                                    timestamp=self.chain.state.timestamp +
                                    14)
         b = Miner(b).mine(rounds=100, start_nonce=0)
         assert self.chain.add_block(b)
     self.block = mk_block_from_prevstate(
         self.chain, timestamp=self.chain.state.timestamp + 14)
     self.head_state = self.chain.state.ephemeral_clone()
     self.cs.initialize(self.head_state, self.block)
Ejemplo n.º 4
0
 def mine_and_broadcast_blocks(self, number_of_blocks=1):
     for i in range(number_of_blocks):
         self._head_candidate_needs_updating = True
         block = Miner(self.head_candidate).mine(rounds=100, start_nonce=0)
         self.transaction_queue = self.transaction_queue.diff(
             block.transactions)
         self.broadcast_newblock(block)
Ejemplo n.º 5
0
    def mine(self, number_of_blocks=1, coinbase=a0):
        self.cs.finalize(self.head_state, self.block)
        set_execution_results(self.head_state, self.block)
        self.block = Miner(self.block).mine(rounds=100, start_nonce=0)
        assert self.chain.add_block(self.block)
        b = self.block

        # Reorganize head collation
        collation = None
        # Check add_header_logs
        for item in self.add_header_logs:
            # [num, num, bytes32, bytes32, bytes32, address, bytes32, bytes32, bytes]
            # use sedes to prevent integer 0 from being decoded as b''
            sedes = List([
                utils.big_endian_int, utils.big_endian_int, utils.hash32,
                utils.hash32, utils.hash32, utils.address, utils.hash32,
                utils.hash32, binary
            ])
            values = rlp.decode(item, sedes)
            shard_id = values[0]
            if shard_id in self.chain.shard_id_list:
                collation_hash = sha3(item)
                collation = self.chain.shards[shard_id].get_collation(
                    collation_hash)
        self.chain.reorganize_head_collation(b, collation)
        # Clear logs
        self.add_header_logs = []

        for i in range(1, number_of_blocks):
            b, _ = make_head_candidate(self.chain,
                                       parent=b,
                                       timestamp=self.chain.state.timestamp +
                                       14,
                                       coinbase=coinbase)
            b = Miner(b).mine(rounds=100, start_nonce=0)
            assert self.chain.add_block(b)
            self.chain.reorganize_head_collation(b, None)

        self.change_head(b.header.hash, coinbase)
        return b