vbuterin / pybitcointools

SImple, common-sense Bitcoin-themed Python ECC library
1.28k stars 856 forks source link

Avoid infinite loop #159

Open NicosKaralis opened 7 years ago

NicosKaralis commented 7 years ago

I'm not a python developer but we are using your lib to make payments with bitcoin. Currently the script we made works fine with one problem.

The payments are collected from a redis server in a private network and processed on a machine that sometimes goes offline. When the payment machine goes offline the scripts does not halt execution or raises an exception. It just prints <urlopen error [Errno 8] nodename nor servname provided, or not known> and keeps on loop for ever until the internet is back online.

Is there a way to make the script fail fast? So I could add to the redis server the fail reason being Internet down and log that event and reschedule that payment

I know I can make a fork and add the functionality that I need, but I was hoping for a already built solution

Below is an output example of my script

Addr: [pub Address] <urlopen error [Errno 8] nodename nor servname provided, or not known><urlopen error [Errno 8] nodename nor servname provided, or not known><urlopen error [Errno 8] nodename nor servname provided, or not known><urlopen error [Errno 8] nodename nor servname provided, or not known><urlopen error [Errno 8] nodename nor servname provided, or not known> ... ... on and on until the internet is back online, then the process resumes ('History:', [{'output': u'dce983f657f195127847dd81c10a8516405b22fd115f29a62974bfe4e440b380:1', 'block_height': 469894, 'spend': u'547fcb16f08c523e3757013924de467f9d2973c9f9731b5356561cfd3f25508b:0', 'value': 100000, 'address': u'1M4qthLLio3G2fnJKkJoEAigwYH6bQai1S'}, {'output': u'62f114e1f5a4ac86a023c43928572061406a78a73dc99674e138b1e31aaa67a2:0', 'block_height': 469899, 'spend': u'547fcb16f08c523e3757013924de467f9d2973c9f9731b5356561cfd3f25508b:1', 'value': 500000, 'address': u'1M4qthLLio3G2fnJKkJoEAigwYH6bQai1S'}])

reiven commented 7 years ago

Hi @NicosKaralis looks like the problem is not the library itself but the script you run to use it. Probablye you (or your organization) is importing this lib into your script to do 'bitcoin stuff' but taking care of the internet connection is not part of this lib, but your script should do it. I suggest you to review your script and add the conection check inside the script itself

Regards

NicosKaralis commented 7 years ago

Hi @reiven , thanks for the fast reply

My problem is not taking care of the connection.

To clarify

  1. The machine that will create the transactions is running headless on a private network and has access to the internet
  2. Other machines on the private network will push to redis the transaction parameters
  3. My script gets the params from redis, get the history, get the unspents, builds the transaction and pushes the transaction
    1. If the transaction was pushed send to redis success
    2. if the transaction failed send to redis the reason

The problem is that on 3., the command history defined at bitcoin/bci.py#L210 has a while 1: that makes a infinite loop and does not break untill the blockchain server is reached.

Because network instability my script can push to the redis client the status of the network, but only if the loop is stopped, which doesn't happen until internet is back

Do you understand the problem now? Everything works fine, IF the network cooperates. IF the network does not cooperate then this lib enters an infinite loop and there is no way to warn the other peers about the connectivity issues

Is there a python way to check if the history command did hang?

If not, that's ok, I'll make a fork of this and solve my problem. But I would like if this could be done without forking

reiven commented 7 years ago

@NicosKaralis what i mean is that you can check the internet conection before calling the history function so you can catch the problem and not call the history function until the internet connection goes back. From my point of view that would be the correct way to handle the situation you describe. Another option, which also IMHO is the best, is running you own bitcoin node and instead of using a third party API like blockchain.info , so you can make RPC calls to your node and being sure you can access the correct information to a thusted valid bitcoin node.

Regards