Sunday, July 12, 2015

Querying the Census blockchain for wallets




The local instance of Census keeps the state of the coinbase (database of who owns what) itself in a local database. When new system is brought up – it ends up 'replaying' most of the transactions of the blockchain to get to a current state of the network. Most of the churn as your system downloads the blockchain is a complete reaudit of all the transactions – it doesn't need to challenge the older transactions with a large number of confirmations, but it still does top level validations and block signature checks just to make sure that the node isn't being fed fake data from the peer.

It still would be possible to create a malicious node that periodically sends bad transactions out on the network – but these will not poison the other systems since they'll eventually be purged. It's expected that bad data will get onto the network – stuff happens and the protocol is made to recover. There are work items for the core dev team to eventually enable the concept of 'fame' – so that a user or organization on the network can be vouched for by other well known users and issues such as data errors and poor quality items can be logged permanently to the network. For now though, if you use the normal distribution you'll be connected to the Foundation node or to another random well known node by default.

Okay, that was a lot of preamble – so now to create a quick sample program to query the local coinbase.

__author__ = 'lpreimesberger'
import censusprotocollib
import json
print "Hello world!  I'm trying to query the blockchain server on this host!"
wallet_address = raw_input( "which wallet to look for? : ")
print censusprotocollib.get_wallet("localhost", wallet_address )
Yeah, I'm not a Python guru, but that's good enough. All it does is ask for a wallet from the node on the local system. That's pretty cool – now to ask for random stuff from it:

$ python testwalletquery.py
Hello world! I'm trying to query the blockchain server on this host!
which wallet to look for? : random_wallet
Warning - server has responded with error code 400
{"result":"abend-malformed-address","warning":"Server saw: random_wallet"}

Hmm – we're not asking for the right format obviously. The Census Protocol uses Bitcoin formatted addresses – originally it tried to make an obviously 'new' format, but the current ECDSA public key to address format is pretty damn good and is hard to improve on. The format is shown really well on this diagram from https://en.bitcoin.it/wiki/File:PubKeyToAddr.png.




Altcoins use this same format and just change the network ID byte to some random, screwy number. The data is so different in Census it didn't seem there was any need to choose something new.

Okay, let's look for the sample address from the Bitcoin wiki...

$ python testwalletquery.py
Hello world! I'm trying to query the blockchain server on this host!
which wallet to look for? : 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy
Warning - server has responded with error code 400
{"result":"abend-invalid-to"}

Well, that's better at least. We can tell it wasn't a good call from the HTTP response code of 400, and the optional return data says why. Looking at the response, it's the defined result code for 'not found'. Unless by some miracle somebody has managed to reserve that address – which is nearly impossible (stay tuned for more on this later). Fortunately for debug installs – there's a defined 'fake' Foundation account of 1Goo8Lf1sVEa12npNziCncVGDEoDuw1yEL (as of current builds at least – check the docs). Let's try that:

$ python testwalletquery.py
Hello world! I'm trying to query the blockchain server on this host!
which wallet to look for? : 1Goo8Lf1sVEa12npNziCncVGDEoDuw1yEL
[{"_id":"556e670c311f7416b2d6fc58","item_type":"wallet","txid":"0","version":1,"source":"1Goo8Lf1sVEa12npNziCncVGDEoDuw1yEL","balance":4000000000025,"created":"0","signature":"","block_in":"0","rsa_keys":[]}]


That wallet is missing a lot of the optional and more useful data real wallets have, but we'll see that next.

No comments:

Post a Comment