def pull_headers(host, from_block_id, to_block_id): """ Pulls headers for a range of blocks starting with from_block_id until to_block_id :param host: IP:PORT string :param from_block_id: Initial block ID :param to_block_id: Final block ID :return: Array of raw block header contents (byte arrays) for each queried block. """ conn = get_channel(host) ret = [] if conn: stub = node_pb2_grpc.NodeStub(conn) from_id = bytes(bytearray.fromhex(from_block_id.strip())) to_id = bytes(bytearray.fromhex(to_block_id.strip())) _pr1 = {"from": [from_id], "to": to_id} req = node_pb2.PullHeadersRequest(**_pr1) response = stub.PullHeaders(req) for header in response: ret.append(header.content) conn.close() return ret
def upload_block(host, block_str): """ Push a raw block to the remote host in response to a solicit request during a BlockSubscription session. From the original jormungander documentation: // Uploads blocks to the service in response to a `solicit` item // received from the BlockSubscription response stream. :param host: IP:PORT string :param block_str: Raw hex encoded string of block bytes. :return: Empty UploadBlocksResponse """ conn = get_channel(host) jsx = None def block_iter(): """ Simple block iterator. This is just a closure over a single header in args.header. """ for h in [bytes(bytearray.fromhex(block_str.strip())), ]: yield node_pb2.Block(content=h) if conn: stub = node_pb2_grpc.NodeStub(conn) block_iterator = block_iter() response = stub.UploadBlocks(block_iterator) jsx = json_format.MessageToJson(response) conn.close() json_o = None if jsx: json_o = json.loads(jsx) return json_o
def get_peers_from_host(host): """ :param host: IP:PORT string :return: Json { "peers" : [v4 : { ip: , port: }] } on success. None on failure. """ conn = get_channel(host) jsx = None if conn: stub = node_pb2_grpc.NodeStub(conn) response = stub.Peers(node_pb2.PeersRequest()) jsx = json_format.MessageToJson(response) conn.close() json_o = None if jsx: json_o = json.loads(jsx) if "peers" in json_o: for i in range(len(json_o["peers"])): ipnum = int(json_o["peers"][i]["v4"]["ip"]) ipx = ipaddress.ip_address(ipnum) json_o["peers"][i]["v4"]["ip"] = str(ipx) return json_o
def search(args: List[str]) -> None: id_busca: int = 0 # TODO change id_busca to something meaningful node_origin: int = int(args[0]) key: str = args[1] node_origin_port: int = NODE_STARTING_PORT + node_origin with grpc.insecure_channel(f'localhost:{node_origin_port}') as channel: node_stub: node_pb2_grpc.NodeStub = node_pb2_grpc.NodeStub(channel) _ = node_stub.Lookup( node_pb2.LookupRequest(node_origin=node_origin, key=key, client_port=CLIENT_PORT))
def insert(args: List[str]) -> None: node_origin: int = int(args[0]) chave: str = args[1] valor: str = args[2] node_origin_port: int = NODE_STARTING_PORT + node_origin print(f'Insert:\n no: {node_origin}\n chave: {chave}\n valor: {valor}') print(f' porta: {node_origin_port}') with grpc.insecure_channel(f'localhost:{node_origin_port}') as channel: node_stub: node_pb2_grpc.NodeStub = node_pb2_grpc.NodeStub(channel) _ = node_stub.Insert( node_pb2.InsertRequest(node_origin=node_origin, key=chave, value=valor, client_port=CLIENT_PORT))
def fragment_subscription(host, fragment_iterator): """ Subscribe to receive fragment messages from host. Caller must close the connection after the iterator reaches EOF :param host: IP:PORT string :param fragment_iterator: Input fragment iterator :return: Returns an array of [connection, output Fragment iterator]. """ conn = get_channel(host) if conn: stub = node_pb2_grpc.NodeStub(conn) md = get_metadata() return [conn, stub.FragmentSubscription(fragment_iterator, metadata=md)] return None
def block_subscription(host, block_iter): """ Subscribe to receive block messages from host. Caller must close the connection after the iterator reaches EOF :param host: IP:PORT string :param block_iter: Input block iterator. :return: Returns an array of [connection, output Block iterator]. """ conn = get_channel(host) if conn: stub = node_pb2_grpc.NodeStub(conn) md = get_metadata() return [conn, stub.BlockSubscription(block_iter, metadata=md)] return None
def pull_blocks_to_tip_iterator(host, from_id): """ Pull blocks from from_id until tip of chain. :param host: IP:PORT string :param from_id: Pull blocks starting from from_id :return: An array containing the connection and a Block iterator. """ conn = get_channel(host) ret = [] if conn: stub = node_pb2_grpc.NodeStub(conn) from_bid = bytes(bytearray.fromhex(from_id.strip())) _pr1 = {"from": [from_bid]} req = node_pb2.PullBlocksToTipRequest(**_pr1) return [conn, stub.PullBlocksToTip(req)]
def gossip_subscription(host, gossip_iterator): """ Subscribe to receive peer gossip from host. Caller must close the connection after the iterator reaches EOF :param host: IP:PORT string :param gossip_iterator: Input Gossip iterator. :return: Returns an array of [connection, output Gossip iterator]. """ conn = get_channel(host) if conn: stub = node_pb2_grpc.NodeStub(conn) md = get_metadata() ret = stub.GossipSubscription(gossip_iterator, metadata=md) return [conn, ret] return None
def tip(host): """ :param host: IP:PORT string :return: json format reply from Tip message """ conn = get_channel(host) jsx = None if conn: stub = node_pb2_grpc.NodeStub(conn) response = stub.Tip(node_pb2.TipRequest()) jsx = json_format.MessageToJson(response) conn.close() json_o = None if jsx: json_o = json.loads(jsx) return json_o
def get_fragments(host, fragment_ids): """ :param host: IP:PORT string :param fragment_ids: comma separated fragment ID's :return: Array of raw fragment contents (byte arrays) for each queried ID. """ conn = get_channel(host) ret = [] if conn: stub = node_pb2_grpc.NodeStub(conn) bids = [bytes(bytearray.fromhex(b.strip())) for b in fragment_ids.split(",")] req = node_pb2.FragmentIds(ids=bids) response = stub.GetFragments(req) for fragment in response: ret.append(fragment.content) conn.close() return ret
def get_headers(host, block_ids): """ :param host: IP:PORT string :param block_ids: comma separated block ID's :return: Array of raw block header contents (byte arrays) for each queried block. """ conn = get_channel(host) ret = [] if conn: stub = node_pb2_grpc.NodeStub(conn) bids = [bytes(bytearray.fromhex(b.strip())) for b in block_ids.split(",")] req = node_pb2.BlockIds(ids=bids) response = stub.GetHeaders(req) for block in response: ret.append(block.content) conn.close() return ret
def push_headers(host, header_str): """ Pushes raw headers to the remote host when a remote header is reported missing in a BlockSubscription From the original jormungander documentation: // Sends headers of blocks to the service in response to a `missing` // item received from the BlockSubscription response stream. // The headers are streamed the in chronological order of the chain. :param host: IP:PORT string :param header_str: Raw hex encoded string of header bytes. :return: A (likely empty) PushHeaderResponse """ conn = get_channel(host) jsx = None def header_iter(): """ Simple header iterator. This is just a closure over a single header in args.header. Create your own implementation for more advanced usage. """ for h in [bytes(bytearray.fromhex(header_str.strip())) ]: yield node_pb2.Header(content=h) if conn: stub = node_pb2_grpc.NodeStub(conn) header_iterator = header_iter() response = stub.PushHeaders(header_iterator) jsx = json_format.MessageToJson(response) conn.close() json_o = None if jsx: json_o = json.loads(jsx) return json_o
#! /usr/bin/python import grpc import node_pb2_grpc as npg import node_pb2 as np channel = grpc.insecure_channel("localhost:4225") stub = npg.NodeStub(channel) addr = np.Address(ip="78.248.188.120", port=4224) pair = np.Peer(address=addr) stub.ConnectPeer(np.ConnectPeerRequest(peer=pair)) stub.GetBestBlocks(np.GetBestBlocksRequest()) a = stub.GetInfo(np.GetInfoRequest()) print(a) #tx = stub.GetTxByHash(np.GetTxByHashRequest()) genesis = a.best_block_hash print(stub.GetBlockByHash(np.GetBlockByHashRequest(hash=genesis)))