def get_blocks(self, offset, limit): if not self.connected: logging.error("no connection") return self.request = proto.GetBlocks(offset, limit) self.sock.sendall(self.request.buffer.raw) req_term = proto.TerminatingBlock() req_term.pack() self.sock.sendall(req_term.buffer.raw) self.response = proto.Header() self.sock.recv_into(self.response.buffer, self.response.structure.size) self.response.unpack() if not self.response.check_cmd_num('SendBlocks'): return blocks = [] if self.response.size == 0: return blocks block_size = proto.calcsize(proto.F_HASH) if self.response.size % block_size > 0: return blocks blocks_count = int(self.response.size / block_size) for b in range(0, blocks_count): block_hash = proto.BlockHash() self.sock.recv_into(block_hash.buffer, block_hash.structure.size) block_hash.unpack() block = Block() block.set_hash(block_hash.get_hash()) blocks.append(block) return blocks
def get_block_size(self, block_hash): if not self.is_connected(): return self.request = proto.GetBlockSize(block_hash) self.sock.sendall(self.request.buffer.raw) req_term = proto.TerminatingBlock() req_term.pack() self.sock.sendall(req_term.buffer.raw) self.response = proto.Header() self.sock.recv_into(self.response.buffer, self.response.structure.size) self.response.unpack() if not self.response.check_cmd_num('SendBlockSize'): return r_block_hash = proto.BlockHash() self.sock.recv_into(r_block_hash.buffer, r_block_hash.structure.size) r_block_hash.unpack() block_size = proto.BlockSize() self.sock.recv_into(block_size.buffer, block_size.structure.size) block_size.unpack() resp_term = proto.TerminatingBlock() self.sock.recv_into(resp_term.buffer, resp_term.structure.size) resp_term.unpack() block_size = block_size.values[0] return block_size
def get_last_hash(self): if not self.is_connected(): return self.request = proto.GetLastHash() self.sock.sendall(self.request.buffer.raw) req_term = proto.TerminatingBlock() req_term.pack() self.sock.sendall(req_term.buffer.raw) self.response = proto.Header() self.sock.recv_into(self.response.buffer, self.response.structure.size) self.response.unpack() if not self.response.check_cmd_num('SendLastHash'): return r_block_hash = proto.BlockHash() self.sock.recv_into(r_block_hash.buffer, r_block_hash.structure.size) r_block_hash.unpack() resp_term = proto.TerminatingBlock() self.sock.recv_into(resp_term.buffer, resp_term.structure.size) resp_term.unpack() block = Block() block.set_hash(r_block_hash.get_hash()) return block
def get_transaction(self, b_hash, t_hash): if not self.is_connected(): return self.request = proto.GetTransaction(b_hash, t_hash) self.sock.sendall(self.request.buffer.raw) req_term = proto.TerminatingBlock() req_term.pack() self.sock.sendall(req_term.buffer.raw) self.response = proto.Header() self.sock.recv_into(self.response.buffer, self.response.structure.size) self.response.unpack() if not self.response.check_cmd_num('SendTransaction'): return block_hash = proto.BlockHash() self.sock.recv_into(block_hash.buffer, block_hash.structure.size) block_hash.unpack() signature = proto.Signature() self.sock.recv_into(signature.buffer, signature.structure.size) signature.unpack() pub_key = proto.PublicKey() self.sock.recv_into(pub_key.buffer, pub_key.structure.size) pub_key.unpack() tx_data = proto.TransactionData() self.sock.recv_into(tx_data.buffer, tx_data.structure.size) tx_data.unpack() pprint(tx_data.buffer.raw) pprint(pub_key.buffer.raw) pprint(signature.buffer.raw) from racrypt import RaCryptLib import racrypt from os import path lib = racrypt.RaCryptLib() pprint(path.dirname(racrypt.__file__)) lib.load(path.dirname(racrypt.__file__)) res = lib.verify(data=tx_data.buffer.raw, pub_key=pub_key.buffer.raw, signature=signature.buffer.raw) pprint(res) # transaction = proto.Transaction() # self.sock.recv_into(transaction.buffer, transaction.structure.size) # transaction.unpack() # pprint(transaction.values) resp_term = proto.TerminatingBlock() self.sock.recv_into(resp_term.buffer, resp_term.structure.size) resp_term.unpack() t = Transaction() # t.parse(transaction.values) return t
def get_transactions(self, block_hash, offset, limit): if not self.is_connected(): return self.request = proto.GetTransactions(block_hash, offset, limit) self.sock.sendall(self.request.buffer.raw) req_term = proto.TerminatingBlock() req_term.pack() self.sock.sendall(req_term.buffer.raw) self.response = proto.Header() self.sock.recv_into(self.response.buffer, self.response.structure.size) self.response.unpack() txs = [] if not self.response.check_cmd_num('SendTransactions'): return txs if not self.response.check(): return txs tx_size = proto.calcsize('=%s' % proto.F_TRANSACTION) block_size = proto.calcsize('=%s' % proto.F_HASH) txs_size = self.response.size - block_size r_block_hash = proto.BlockHash() self.sock.recv_into(r_block_hash.buffer, r_block_hash.structure.size) r_block_hash.unpack() if txs_size % tx_size > 0: return txs txs_count = int(txs_size / tx_size) for i in range(0, txs_count): tx = proto.Transaction() self.sock.recv_into(tx.buffer, tx.structure.size) tx.unpack() # pprint(tx.values) t = Transaction() t.parse(tx.values) txs.append(t) return txs