def parse(cls, stream): num_headers = read_varint(stream) blocks = [] for _ in range(num_headers): blocks.append(Block.parse(stream)) num_txs = read_varint(stream) if num_txs != 0: raise RuntimeError("number of txs not 0") return cls(blocks)
def generate_block(self, prev_hash, transaction_list, nonce_start): block_hash, nonce, try_and_error = proof_of_work( prev_hash, difficulty, nonce_start, lambda: self.found) if try_and_error == -1: return None block_header = BlockHeader(prev_hash, block_hash, nonce, difficulty, self.user.public_key) block = Block(block_header, transaction_list) return block
def read(self, recursive=False): self._init_name_hash() self.entries = [] # create initial list with blk_num/hash_index for dir scan blocks = [] for i in xrange(self.block.hash_size): blk_num = self.block.hash_table[i] if blk_num != 0: blocks.append((blk_num,i)) for blk_num,hash_idx in blocks: # read anonymous block blk = Block(self.blkdev, blk_num) blk.read() if not blk.valid: self.valid = False return # create file/dir node hash_chain,node = self._read_add_node(blk, recursive) # store node in entries self.entries.append(node) # store node in name_hash self.name_hash[hash_idx].append(node) # follow hash chain if hash_chain != 0: blocks.append((hash_chain,hash_idx)) # dircaches available? if self.volume.is_dircache: self.dcache_blks = [] dcb_num = self.block.extension while dcb_num != 0: dcb = DirCacheBlock(self.blkdev, dcb_num) dcb.read() if not dcb.valid: self.valid = False return self.dcache_blks.append(dcb) dcb_num = dcb.next_cache
def read(self, recursive=False): self._init_name_hash() self.entries = [] # create initial list with blk_num/hash_index for dir scan blocks = [] for i in xrange(self.block.hash_size): blk_num = self.block.hash_table[i] if blk_num != 0: blocks.append((blk_num,i)) for blk_num,hash_idx in blocks: # read anonymous block blk = Block(self.blkdev, blk_num) blk.read() # create file/dir node hash_chain,node = self._read_add_node(blk, recursive) # store node in entries self.entries.append(node) # store node in name_hash self.name_hash[hash_idx].append(node) # follow hash chain if hash_chain != 0: blocks.append((hash_chain,hash_idx))
def parse_message(message): message_type = MessageType.STRING data = message try: message = json.loads(message) message_type = MessageType[message['type']] data = message['data'] if message_type == MessageType.NEIGHBOR_LIST: data = [Neighbor(total_dict=neighbor) for neighbor in data] elif message_type in (MessageType.NEIGHBOR, MessageType.LOGOUT): data = Neighbor(total_dict=data) elif message_type == MessageType.TRANSACTION: data = Transaction().load_dict(data) elif message_type == MessageType.BLOCK: data = Block().load_dict(data) except Exception as e: logging.error(e) return message_type, data