def __mine(self, mempool: Set[Transaction], chain: Chain, wallet: Wallet) -> Block: c_pool = list(copy.deepcopy(mempool)) mlist = self.__calculate_transactions(c_pool) logger.debug(len(mlist)) block_header = BlockHeader( version=consts.MINER_VERSION, height=chain.length, prev_block_hash=dhash(chain.header_list[-1]), merkle_root=merkle_hash(mlist), timestamp=int(time.time()), signature="", ) sign = wallet.sign(dhash(block_header)) block_header.signature = sign block = Block(header=block_header, transactions=mlist) r = requests.post("http://0.0.0.0:" + str(consts.MINER_SERVER_PORT) + "/newblock", data=compress(block.to_json())) if r.text == "Block Received": vjti_chain_relayer = VJTIChainRelayer(wallet) vjti_chain_relayer.new_block(block) logger.info(f"Miner: Mined Block with {len(mlist)} transaction(s)") return block else: logger.info( f"Miner: Could not mine block with {len(mlist)} transaction(s)" ) return None
def __mine(self, mempool: Set[Transaction], chain: Chain, payout_addr: str) -> Block: c_pool = list(copy.deepcopy(mempool)) mlist, fees = self.__calculate_best_transactions(c_pool) # logger.debug(f"Miner: Will mine {len(mlist)} transactions and get {fees} scoins in fees") coinbase_tx_in = { 0: TxIn(payout=None, sig="Receiving some Money", pub_key="Does it matter?") } coinbase_tx_out = { 0: TxOut(amount=chain.current_block_reward(), address=payout_addr), 1: TxOut(amount=fees, address=payout_addr), } coinbase_tx = Transaction( is_coinbase=True, version=consts.MINER_VERSION, fees=0, timestamp=int(time.time()), locktime=-1, vin=coinbase_tx_in, vout=coinbase_tx_out, ) mlist.insert(0, coinbase_tx) block_header = BlockHeader( version=consts.MINER_VERSION, height=chain.length, prev_block_hash=dhash(chain.header_list[-1]), merkle_root=merkle_hash(mlist), timestamp=int(time.time()), target_difficulty=chain.target_difficulty, nonce=0, ) DONE = False for n in range(2**64): block_header.nonce = n bhash = dhash(block_header) if chain.is_proper_difficulty(bhash): block = Block(header=block_header, transactions=mlist) requests.post("http://0.0.0.0:" + str(consts.MINER_SERVER_PORT) + "/newblock", data=compress(block.to_json())) logger.info( f"Miner: Mined Block with {len(mlist)} transactions, Got {fees} in fees and {chain.current_block_reward()} as reward" ) DONE = True break if not DONE: logger.error( "Miner: Exhausted all 2 ** 64 values without finding proper hash" )
def get_image_block(self, fScale, c_x, c_y, nWidth, nHeight): ''' 在指定坐标和倍镜下,提取切片的图块 :param fScale: 倍镜数 :param c_x: 中心x :param c_y: 中心y :param nWidth: 图块的宽 :param nHeight: 图块的高 :return: 返回一个图块对象 ''' data = self._slide.get_image_block(fScale, c_x, c_y, nWidth, nHeight) newBlock = Block(self.slice_id, c_x, c_y, fScale, 0, nWidth, nHeight) newBlock.set_img(data) return newBlock
def process_new_block(request_data: bytes) -> str: global BLOCKCHAIN block_json = decompress(request_data) if block_json: try: block = Block.from_json(block_json).object() # Check if block already exists if get_block_from_db(dhash(block.header)): logger.info("Server: Received block exists, doing nothing") return "Block already Received Before" if BLOCKCHAIN.add_block(block): logger.info( "Server: Received a New Valid Block, Adding to Chain") logger.debug("Server: Sending new block to peers") # Broadcast block to other peers send_to_all_peers("/newblock", request_data) else: return "Block Received, but was not added, due to some error" except Exception as e: logger.error("Server: New Block: invalid block received " + str(e)) return "Invalid Block Received" # Kill Miner t = Timer(1, miner.stop_mining) t.start() return "Block Received" logger.error("Server: Invalid Block Received") return "Invalid Block"
def render_block_header(hdr): html = "<table>" html += "<tr><th>" + "Height" + "</th>" html += "<td>" + str(hdr.height) + "</td></tr>" html += "<tr><th>" + "Block Hash" + "</th>" html += "<td>" + dhash(hdr) + "</td></tr>" html += "<tr><th>" + "Prev Block Hash" + "</th>" html += "<td>" + str(hdr.prev_block_hash) + "</td></tr>" html += "<tr><th>" + "Merkle Root" + "</th>" html += "<td>" + str(hdr.merkle_root) + "</td></tr>" html += "<tr><th>" + "Timestamp" + "</th>" html += ("<td>" + str( datetime.fromtimestamp(hdr.timestamp).strftime("%d-%m-%Y %H:%M:%S")) + " (" + str(hdr.timestamp) + ")</td></tr>") # get block block = Block.from_json(get_block_from_db(dhash(hdr))).object() html += "<tr><th>" + "Transactions" + "</th>" html += "<td>" + str(len(block.transactions)) + "</td></tr>" # for i, transaction in enumerate(block.transactions): # s = "coinbase: " + str(transaction.is_coinbase) + ", fees: " + str(transaction.fees) # html += "<tr><th>Transaction " + str(i) + "</th><td>" + str(s) + "</td></tr>" html += "</table>" return str(html)
def static_show(self, surf, image): pos = (image.position[0] + 25, image.position[1] + 45) for i, v in enumerate(self.options[self.rotation]): x, y = i % 4, i // 4 if v == '1': tmp_block = Block(self.color, (0, 0)) surf.blit(tmp_block.image, (pos[0] + x * TILE, pos[1] + y * TILE))
def block(blockhash): log_ip(request, inspect.stack()[0][3]) try: block = Block.from_json(get_block_from_db(blockhash)).object() except Exception as e: logger.debug("BLOCK/blockhash: " + str(e)) return template("error.html") return template("block.html", block=block)
def travel(self, player): data = { 'name': str(random.choice(self.playerList[player])), 'level': random.randint(0, int(player.getLevel()) + self.diff) } player.receivePokemon(self.worldChain.mine(Block(data), self.getDiff())) self.updateTier(player)
def rotate(self, turnover): self.rotation = (self.rotation + turnover) % 4 self.blocks = [] for i, v in enumerate(self.options[self.rotation]): x, y = i % 4, i // 4 if v == '1': self.blocks.append( Block(self.color, (self.corner[0] + x, self.corner[1] + y)))
def explorer(): log_ip(request, inspect.stack()[0][3]) prev = int(request.query.prev or 0) if prev < 0: prev = 0 hdr_list = list(reversed(BLOCKCHAIN.active_chain.header_list)) indexes = [i for i in range(prev * 8, (prev + 1) * 8) if i < len(hdr_list)] blocks = [Block.from_json(get_block_from_db(dhash(hdr_list[i]))).object() for i in indexes] transactions = list(BLOCKCHAIN.mempool) return template("explorer.html", blocks=blocks, transactions=transactions, prev=prev)
def sync(peer_list): max_peer = max(peer_list, key=lambda k: k["blockheight"]) r = requests.post(get_peer_url(max_peer) + "/getblockhashes/", data={"myheight": len(ACTIVE_CHAIN)}) hash_list = json.loads(r.text) for hhash in hash_list: peer_url = get_peer_url(random.choice(peer_list)) + "/getblock/" r = requests.post(peer_url, data={"headerhash": hhash}) block = Block.from_json(r.text) if not ACTIVE_CHAIN.add_block(block): raise Exception("WTF")
def transaction(blockhash, txhash): log_ip(request, inspect.stack()[0][3]) try: block = Block.from_json(get_block_from_db(blockhash)).object() tx = None for t in block.transactions: if t.hash() == txhash: tx = t except Exception as e: logger.debug("Transaction/bhash/tx: " + str(e)) return template("error.html") return template("transaction.html", tx=tx, block=block)
def __init__(self, color=None, options=None): self.color = color if color else random.randint(0, 7) self.options = options if options else random.choice(FIGURES) self.rotation = 0 self.state = '__STOP__' self.blocks = [] self.shift = 0 self.corner = (BOARD_SIZE[0] // 2, 0) for i, v in enumerate(self.options[self.rotation]): x, y = i % 4, i // 4 if v == '1': self.blocks.append( Block(self.color, (self.corner[0] + x, self.corner[1] + y)))
def can_rotate(self, field, turnover): rotation = (self.rotation + turnover) % 4 new_blocks = [] for i, v in enumerate(self.options[rotation]): x, y = i % 4, i // 4 if v == '1': new_blocks.append( Block(self.color, (self.corner[0] + x, self.corner[1] + y))) check = all([field.get(block.position) == 0 for block in new_blocks]) if self.state == '__FALLING__': check = (check and all([ field.get(block.position, (0, 1)) == 0 for block in new_blocks ])) return check
def get_cc_co_cp_by_contract_address( contract_address: str) -> Tuple[str, str, str]: global BLOCKCHAIN tx_history = BLOCKCHAIN.active_chain.transaction_history txns = tx_history.get(contract_address) if len(txns) == 0: header_list = read_header_list_from_db() for header in reversed(header_list): block = Block.from_json(get_block_from_db(header)).object() for tx in block.transactions: if tx.get_contract_address() == contract_address: return tx.contract_code, tx.contract_output, tx.contract_priv_key else: for tx in txns: tx = json.loads(tx) cc = tx.get('contract_code', '') if cc != '': return cc, tx.get('contract_output', ''), tx.get('contract_priv_key', '') return '', '', ''
def receive_block_from_peer(peer: Dict[str, Any], header_hash) -> Block: r = requests.post(get_peer_url(peer), data={"header_hash": header_hash}) return Block.from_json(r.text)
def receive_block_from_peer(peer: Dict[str, Any], header_hash) -> Block: r = requests.post(get_peer_url(peer) + "/getblock", data={"headerhash": header_hash}) return Block.from_json(decompress(r.text)).object()
def evaluate_model_based_slice(self, samples_name, batch_size, max_count, slice_count): test_list = "{}/{}".format( self._params.PATCHS_ROOT_PATH[samples_name[0]], samples_name[1]) Xtest, Ytest = read_csv_file( self._params.PATCHS_ROOT_PATH[samples_name[0]], test_list) slice_X = {} slice_Y = {} b = Block() for file_name, true_y in zip(Xtest, Ytest): b.decoding(file_name, 256, 256) if b.slice_number in slice_X.keys(): slice_X[b.slice_number].append(file_name) slice_Y[b.slice_number].append(true_y) else: slice_X[b.slice_number] = [file_name] slice_Y[b.slice_number] = [true_y] result = [] for slice_name in sorted(slice_X.keys()): if "Normal" in slice_name: continue X_data = slice_X[slice_name] Y_data = slice_Y[slice_name] if max_count is not None: X_data, Y_data = X_data[: max_count], Y_data[: max_count] # for debug test_data = Image_Dataset(X_data, Y_data, norm=None) test_loader = Data.DataLoader(dataset=test_data, batch_size=batch_size, shuffle=False, num_workers=self.NUM_WORKERS) data_len = len(test_loader) self.model = self.load_pretrained_model_on_predict() self.construct_shadow_classifier(self.model) self.model.to(self.device) self.model.eval() probability = [] prediction = [] high_dim_features = [] low_dim_features = [] for step, (x, y) in enumerate(test_loader): b_x = Variable(x.to(self.device)) output = self.model(b_x) # model最后不包括一个softmax层 output_softmax = nn.functional.softmax(output, dim=1) probs, preds = torch.max(output_softmax, 1) high_dim_features.extend(self.model.out_feature.cpu().numpy()) low_dim_features.extend(output.detach().cpu().numpy()) probability.extend(probs.detach().cpu().numpy()) prediction.extend(preds.detach().cpu().numpy()) print('predicting => %d / %d ' % (step + 1, data_len)) low_dim_features = np.array(low_dim_features) prediction = np.array(prediction) probability = np.array(probability) if len(prediction) > 6 and np.sum( prediction) > 3: # 至少3个Cancer样本输入 # 对样本进行加权处理 weight = self.correct_sample_weights(low_dim_features, prediction) high_dim_features = np.array(high_dim_features) prediction = np.array(prediction) # 开启弹性调整过程 self.shadow_classifier.train_myself(high_dim_features, prediction, weight, batch_size, 0.1, 10) probability, prediction, low_dim_features = self.shadow_classifier.predict( high_dim_features, batch_size) Ytest = np.array(Y_data) predicted_tags = np.array(prediction) probability = np.array(probability) count = len(Y_data) accu = float(sum(predicted_tags == Ytest)) / count result_str = "{} => accu ={:.4f}, count = {}, mean of prob = {:.6f}".format( slice_name, accu, count, np.mean(probability)) print(result_str) result.append(result_str) if len(result) > slice_count: break # 最后一次性输出全部 for item in result: print(item)
def normalize_dataset(self, source_samples, tagrget_dir, range=None, batch_size=20): self.opcode = 19 # normal = ACDNormalization_tf("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="template_normal") normal = ACDNormalization("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="template_normal") patch_root = self._params.PATCHS_ROOT_PATH[source_samples[0]] sample_filename = source_samples[1] train_list = "{}/{}".format(patch_root, sample_filename) Xtrain, Ytrain = read_csv_file(patch_root, train_list) if range is not None: Xtrain = Xtrain[range[0]:range[1]] Ytrain = Ytrain[range[0]:range[1]] # prepare images = [] for patch_file in Xtrain: img = io.imread(patch_file, as_gray=False) # imgBGR = img[:, :, (2, 1, 0)] # images.append(imgBGR) images.append(img) normal.prepare(images) target_cancer_path = "{}/{}_cancer".format(patch_root, tagrget_dir) target_normal_path = "{}/{}_normal".format(patch_root, tagrget_dir) if (not os.path.exists(target_cancer_path)): os.makedirs(target_cancer_path) if (not os.path.exists(target_normal_path)): os.makedirs(target_normal_path) n = 0 batch_images = [] batch_y = [] batch_blocks = [] for K, (x, y) in enumerate(zip(Xtrain, Ytrain)): new_block = Block() new_block.load_img(x) img = np.array(new_block.get_img()) # imgBGR = img[:, :, (2, 1, 0)] # batch_images.append(imgBGR) batch_images.append(img) batch_y.append(y) batch_blocks.append(new_block) n = n + 1 if n >= batch_size: norm_images = normal.normalize_on_batch(batch_images) for block, norm_img, y in zip(batch_blocks, norm_images, batch_y): # block.set_img(255 * norm_img[:, :, (2, 1, 0)]) block.set_img(255 * norm_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) batch_images = [] batch_y = [] batch_blocks = [] n = 0 if (0 == K % 1000): print("{} normalizing >>> {}".format( time.asctime(time.localtime()), K)) if n > 0: norm_images = normal.normalize_on_batch(batch_images) for block, norm_img, y in zip(batch_blocks, norm_images, batch_y): # block.set_img(255 * norm_img[:, :, (2, 1, 0)]) block.set_img(255 * norm_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) return
), ] for tx in first_block_transactions: tx.sign() first_block_header = BlockHeader( version=1, prev_block_hash=dhash(genesis_block_header), height=1, merkle_root=merkle_hash(first_block_transactions), timestamp=1231006505, target_difficulty=0, nonce=2083236893, ) first_block = Block(header=first_block_header, transactions=first_block_transactions) if __name__ == "__main__": result = ACTIVE_CHAIN.add_block(genesis_block) logger.debug(result) logger.debug(ACTIVE_CHAIN.utxo) result = ACTIVE_CHAIN.add_block(first_block) logger.debug(result) logger.debug(ACTIVE_CHAIN.utxo) # # ORDER # Get list of peers ✓ # Contact peers and get current state of blockchain ✓
def augment_dataset(self, params, source_samples, tagrget_dir, range = None): patch_root = params.PATCHS_ROOT_PATH[source_samples[0]] sample_filename = source_samples[1] train_list = "{}/{}".format(patch_root, sample_filename) Xtrain, Ytrain = read_csv_file(patch_root, train_list) if range is not None: Xtrain = Xtrain[range[0]:range[1]] Ytrain = Ytrain[range[0]:range[1]] target_cancer_path = "{}/{}_cancer".format(patch_root, tagrget_dir) target_normal_path = "{}/{}_noraml".format(patch_root, tagrget_dir) if (not os.path.exists(target_cancer_path)): os.makedirs(target_cancer_path) if (not os.path.exists(target_normal_path)): os.makedirs(target_normal_path) for K, (x, y) in enumerate(zip(Xtrain, Ytrain)): block = Block() block.load_img(x) img = block.get_img() aug_img = self.augment_images(img) * 255 block.set_img(aug_img) block.opcode = self.opcode if y == 0: block.save_img(target_normal_path) else: block.save_img(target_cancer_path) if (0 == K % 1000): print("{} augmenting >>> {}".format(time.asctime(time.localtime()), K))
def __init__(self, nickname): self.name = nickname starters = ['Charmander', 'Bulbasaur', 'Squirtle'] genesisPokemon = {'name': str(random.choice(starters)), 'level': 1} self.receivePokemon(Block(genesisPokemon))