def mine_new_block(self): previous_hash = self.target_in_string if len(self.chain) == 0 else hash(self.get_latest_block()) block_data = to_json_string(self.mempool) block = Block(block_data, len(self.chain)+1, previous_hash) nonce, timestamp = self.find_nonce(block) block.seal(nonce, timestamp) self.chain.append(block)
def add_block(self, block_id, pos_list, label_dict): self.__block_list.append(Block(block_id, pos_list, label_dict)) for pos, label in label_dict.items(): if label > 0: if label not in self.__terminals_dict: self.__terminals_dict[label] = list() self.__terminals_dict[label].append((block_id, pos))
def __create_block(self, blocknode): """\brief Creates a block \param blocknode (\c xml.dom.Node) The block in xml form """ block_name = blocknode.attributes['id'].value block_type = blocknode.attributes['type'].value active = False tpool_id = "" if blocknode.attributes.has_key('sched_type'): active = True if (blocknode.attributes['sched_type'].value == 'active') else False if active: tpool_id = blocknode.attributes['threadpool'].value params = blocknode.getElementsByTagName('params') params = params[0].toxml() tpool = None if (active): tpool = self.__thread_pools[str(tpool_id)] self.__blocks[block_name] = Block(block_name,\ block_type,\ self.__comp_id,\ active,\ params,\ tpool_id,\ tpool,\ self.__blockmon,\ self.__logger)
def make_genesis(self) -> HashedBlock: b = Block( 0, # block num None, # parent hash BlockConfig(DEFAULT_DIFFICULTY), []) hb = HashedBlock(b) while not hb.hash_meets_difficulty(): hb.replace_mining_entropy(os.urandom(32)) return hb
def generateNextBlock(previousBlock: Block, transactions: List[Transaction]) -> Block: """ Attempts to generate the next block in given new data """ nextIndex = previousBlock.index + 1 nextTimestamp = time.time() noonce = 0 while True: hash = hashBlock(nextIndex, nextTimestamp, transactions, noonce, previousBlock.hash) if hasProofOfWork(hash): return Block(index=nextIndex, timestamp=nextTimestamp, transactions=transactions, noonce=noonce, previousHash=previousBlock.hash) noonce += 1 return None
def mine_on(self, parent: HashedBlock, difficulty: int) -> Optional[HashedBlock]: reward = self.make_reward() config = BlockConfig(difficulty) txns = self.transaction_storage.get_all_transactions() txns.append(reward) self.l.debug("Mining on {} txns".format(len(txns))) block = Block(parent.block_num() + 1, parent.mining_hash(), config, txns) hb = HashedBlock(block) start = time.time() while time.time() - start < 1.0: hb.replace_mining_entropy(os.urandom(32)) if hb.hash_meets_difficulty(): return hb return None
def generate(cls, user: User, magicnumber: str = None) -> 'Chain': """ Generate new chain. New chain has 2 block; first element is closed, and second element is not closed. >>> chain = Chain.generate(User.generate()) >>> len(chain) 2 >>> chain[0].is_closed() True >>> chain[1].is_closed() False >>> chain[0].verify() True """ root = Block.make_root(user, magicnumber) return cls([root, Block(root)])
def init_genesis_block(self): genesis_data = "Some random data for genesis block!" genesis_block = Block(genesis_data, 1, self.target_in_string) nonce, timestamp = self.find_nonce(genesis_block) genesis_block.seal(nonce, timestamp) return genesis_block
def __init__(self): self.out = 0 self.blck = Block()