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])
def mine_on_chain(chain, parent=None, transactions=[], coinbase=None, timestamp=None): """Mine the next block on a chain. The newly mined block will be considered to be the head of the chain, regardless of its total difficulty. :param parent: the parent of the block to mine, or `None` to use the current chain head :param transactions: a list of transactions to include in the new block :param coinbase: optional coinbase to replace ``chain.coinbase`` """ txqueue = TransactionQueue() for t in transactions: txqueue.add_transaction(t) parent_timestamp = parent.timestamp if parent else chain.state.timestamp hc, _ = meta.make_head_candidate(chain, txqueue, parent, timestamp or parent_timestamp + 1, coinbase or '\x00' * 20) assert hc.difficulty == 1 m = ethpow.Miner(hc) rounds = 100 nonce = 0 while True: b = m.mine(rounds=rounds, start_nonce=nonce) if b: break nonce += rounds assert chain.add_block(b) return b
def head_candidate(self): if self._head_candidate_needs_updating: self._head_candidate_needs_updating = False # Make a copy of self.transaction_queue because # make_head_candidate modifies it. txqueue = copy.deepcopy(self.transaction_queue) self._head_candidate, self._head_candidate_state = make_head_candidate( self.chain, txqueue, timestamp=int(time.time())) return self._head_candidate
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
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)
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