xeroc / python-graphenelib

Python3 library for Graphene-based blockchains
MIT License
74 stars 60 forks source link

Aio: Unable to reconnect another node #164

Closed biobdeveloper closed 4 years ago

biobdeveloper commented 4 years ago

I try to change node in connected aio.bitshares instance, but receive an error:

from bitshares.aio.graphene import BitShares

async def foo():
    bitshares_instance = BitShares(node="wss://node_1")
    await bitshares_instance.connect()
    assert bitshares_instance.is_connected()
    bitshares_instance.rpc.url = "wss://node_2"
    await bitshares_instance.connect()

/graphenecommon/aio/chain.py", line 43, in connect
    await self.rpc.connect()
/grapheneapi/aio/api.py", line 48, in connect
    await self.next()
grapheneapi/aio/api.py", line 56, in next
    await self.connection.disconnect()
AttributeError: 'function' object has no attribute 'disconnect'
bitphage commented 4 years ago
  1. You do it in a wrong way.
  2. The bug you discovered was due to double-call of await bitshres_instance.connect() which I fixed.

Why this is a wrong way:

  1. await bitshares_instance.connect() instantiates an RPC instance (Websocket or Http).
  2. Then you're changing it's url bitshares_instance.rpc.url = ...
  3. but then you're crating a new RPC instance via await bitshares_instance.connect() with initial url, e.g. step 2 is just lost.

Correct way should be

    bitshares_instance = BitShares(node="wss://node_1")
    await bitshares_instance.connect()
    bitshares_instance.rpc.url = "wss://node_2"
    await bitshares_instance.rpc.connect()

But I wonder why you're not using multiple nodes and next() call?

nodes = ['wss://node_1', 'wss://node_2']
bitshares_instance = BitShares(node=nodes)
await bitshares_instance.connect()
await bitshares_instance.rpc.next()  # switch node
biobdeveloper commented 4 years ago

But if I need to switch determined node, what I need to do? Seems like .rpc.next() just iterate over list, without choosing node by user

bitphage commented 4 years ago

Then use "correct way" solution.