Kaynağa Gözat

add option to print the connected peers to the wallet

Malte Kraus 8 yıl önce
ebeveyn
işleme
ff0a23e251
2 değiştirilmiş dosya ile 13 ekleme ve 2 silme
  1. 3 0
      miner.py
  2. 10 2
      wallet.py

+ 3 - 0
miner.py

@@ -26,6 +26,9 @@ def parse_addr_port(val):
     return (url.hostname, url.port)
 
 def rpc_server(port, chainbuilder):
+    @app.route("/network-info", methods=['GET'])
+    def get_network_info():
+        return json.dumps([list(peer.peer_addr)[:2] for peer in chainbuilder.protocol.peers if peer.is_connected])
 
     @app.route("/transactions", methods=['POST'])
     def get_transactions_for_key():

+ 10 - 2
wallet.py

@@ -14,8 +14,10 @@ def send_transaction(sess, url, transaction):
     resp = sess.put(url + 'new-transaction', data=transaction.to_json_compatible())
     resp.raise_for_status()
 
-def network_state(sess, url):
-    pass
+def network_info(sess, url):
+    resp = sess.get(url + 'network-info')
+    resp.raise_for_status()
+    return resp.json()
 
 def get_transactions(sess, url, pubkey):
     resp = sess.post(url + 'transactions', data=pubkey.as_bytes())
@@ -28,6 +30,8 @@ def main():
                         help="The RPC port of the miner to connect to.")
     parser.add_argument("--show-transactions", type=argparse.FileType("rb"), default=[], action="append",
                         help="Shows all transactions involving the public key stored in the specified file.")
+    parser.add_argument("--show-network", action="store_true", default=False,
+                        help="Prints networking information about the miner.")
     args = parser.parse_args()
 
     url = "http://localhost:{}/".format(args.miner_port)
@@ -38,5 +42,9 @@ def main():
             print(trans.to_json_compatible())
         key.close()
 
+    if args.show_network:
+        for [k, v] in network_info(s, url):
+            print("{}\t{}".format(k, v))
+
 if __name__ == '__main__':
     main()