electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

block_headers.py (1034B)


      1 #!/usr/bin/env python3
      2 
      3 # A simple script that connects to a server and displays block headers
      4 
      5 import time
      6 import asyncio
      7 
      8 from electrum.network import Network
      9 from electrum.util import print_msg, json_encode, create_and_start_event_loop, log_exceptions
     10 from electrum.simple_config import SimpleConfig
     11 
     12 config = SimpleConfig()
     13 
     14 # start network
     15 loop, stopping_fut, loop_thread = create_and_start_event_loop()
     16 network = Network(config)
     17 network.start()
     18 
     19 # wait until connected
     20 while not network.is_connected():
     21     time.sleep(1)
     22     print_msg("waiting for network to get connected...")
     23 
     24 header_queue = asyncio.Queue()
     25 
     26 @log_exceptions
     27 async def f():
     28     try:
     29         await network.interface.session.subscribe('blockchain.headers.subscribe', [], header_queue)
     30         # 3. wait for results
     31         while network.is_connected():
     32             header = await header_queue.get()
     33             print_msg(json_encode(header))
     34     finally:
     35         stopping_fut.set_result(1)
     36 
     37 # 2. send the subscription
     38 asyncio.run_coroutine_threadsafe(f(), loop)