Ejemplo n.º 1
0
def make_head_candidate(chain,
                        txqueue=None,
                        parent=None,
                        timestamp=None,
                        coinbase='\x35' * 20,
                        extra_data='moo ha ha says the laughing cow.',
                        min_gasprice=0):
    log.info('Creating head candidate')
    if parent is None:
        temp_state = State.from_snapshot(
            chain.state.to_snapshot(root_only=True), chain.env)
    else:
        temp_state = chain.mk_poststate_of_blockhash(parent.hash)

    cs = get_consensus_strategy(chain.env.config)
    # Initialize a block with the given parent and variables
    blk = mk_block_from_prevstate(chain, temp_state, timestamp, coinbase,
                                  extra_data)
    # Find and set the uncles
    blk.uncles = cs.get_uncles(chain, temp_state)
    blk.header.uncles_hash = sha3(rlp.encode(blk.uncles))
    # Call the initialize state transition function
    cs.initialize(temp_state, blk)
    # Add transactions
    add_transactions(temp_state, blk, txqueue, min_gasprice)
    # Call the finalize state transition function
    cs.finalize(temp_state, blk)
    # Set state root, receipt root, etc
    set_execution_results(temp_state, blk)
    log.info('Created head candidate successfully')
    return blk, temp_state
Ejemplo n.º 2
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.º 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(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