jamesob / tinychain

A pocket-sized implementation of Bitcoin
MIT License
1.45k stars 123 forks source link

AssertionError: assert len(string) == curve.baselen, (len(string), curve.baselen) #4

Open ztnark opened 7 years ago

ztnark commented 7 years ago

I get the following error after running:

./bin/sync_wallets

Traceback (most recent call last):
  File "./client.py", line 137, in <module>
    main(docopt(__doc__, version='tinychain client 0.1'))
  File "./client.py", line 37, in main
    t.init_wallet(args.get('--wallet')))
  File "/Users/jeffkrantz/Apps/sandbox/tinychain/tinychain.py", line 1068, in init_wallet
    f.read(), curve=ecdsa.SECP256k1)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ecdsa/keys.py", line 149, in from_string
    assert len(string) == curve.baselen, (len(string), curve.baselen)
AssertionError: (33, 32)
jamesob commented 7 years ago

Yeah, I've actually seen this sporadically in local development too. Given our pretty direct usage of ecdsa, I wonder if it's a problem there? The extent of what we do with the key bytes is a file(..., 'wb').write(), so I'm not sure where things could be going awry.

jamesob commented 7 years ago

At any rate, the easy workaround is to restart the containers which causes new wallet generation (as you've probably figured out). Would like to get to the bottom of this, though.

jamesob commented 7 years ago

Unable to reproduce locally, so I'm betting this has something to do with container -> host issues.

In [6]: def check_key():
   ...:     with open('/tmp/foo', 'wb') as f:
   ...:         f.write(ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1).to_string())
   ...:     with open('/tmp/foo', 'rb') as f: 
   ...:         assert len(f.read()) == 32
   ...:         
   ...:         

In [7]: for i in range(10000):
   ...:     check_key()
   ...:     
   ...:     
jamesob commented 7 years ago

We could probably use binascii to encode/decode the on-disk private key to avoid this.

ghost commented 7 years ago

i have the same issue running on osx. renaming wallet1.dat and wallet2.dat and restarting the containers doesn't seem to help. it generates new wallets but spits out the same assert error.

DerekRay commented 5 years ago

We could use docker cp Node2ID:/wallet.dat ./wallet2.dat
to replace docker-compose exec node2 cat wallet.dat > wallet2.dat This error may be caused by docker.

DerekRay commented 5 years ago

Just like this: vim ./bin/sync_wallets

#!/bin/bash

docker-compose exec node1 cat wallet.dat > wallet1.dat
echo "Synced node1's wallet:"
./client.py balance --wallet wallet1.dat
echo

node2id=$(docker ps | grep 'node2' | awk '{print $1}')
docker cp ${node2id}:/wallet.dat ./wallet2.dat
echo "Synced node2's wallet:"
./client.py balance --wallet wallet2.dat