Post
Topic
Board Development & Technical Discussion
Re: python-bitcoinlib, creating CBlock with getblocktemplate data
by
yg10
on 08/07/2017, 08:41:32 UTC
I'm trying to learn about bitcoin by writing a python bitcoin miner using python-bitcoinlib (https://github.com/petertodd/python-bitcoinlib).
I'm having problems creating a complete CBlock with data from a getblocktemplate request. I get all parameters right except from the transactions, the vtx() in the function argument. CBlock() accepts an array of CTransaction()-objects. Is this the correct parameter? If so, how do I populate add data to the CTransaction()-objects from the getblocktemplate data? I've tried using the template transaction-data, but I only get errors of the type:  AttributeError: 'unicode' object has no attribute 'read'. I tried formatting the data as string or int aswell. What kind of object should be passed to CTransaction()? I'm reading the source code, but I can't figure it out.

No error:
Code:
foo = [bitcoin.core.CTransaction() for i in range(5)]
mineBlock = bitcoin.core.CBlock(2, bitcoin.core.x(block['previousblockhash']),bitcoin.core.x(mHash),block['curtime'],int(block['bits'],16),0x00,foo)

Error:
Code:
foo[0].stream_deserialize(block['transactions'][0]['data'])
gives
Code:
Traceback (most recent call last):

  File "mining.py", line 165, in
    foo[0].stream_deserialize(block['transactions'][0]['data'])
  File "/home/hadoque/.local/lib/python2.7/site-packages/bitcoin/core/__init__.py", line 328, in stream_deserialize
    nVersion = struct.unpack(b"  File "/home/hadoque/.local/lib/python2.7/site-packages/bitcoin/core/serialize.py", line 79, in ser_read
    r = f.read(n)
AttributeError: 'unicode' object has no attribute 'read'


So, am I submitting the right data but in wrong format (how do I make data a serializable object?) or is the data wrong all together?



It looks like you are passing stream data or stream name instead of the stream itself.

This library has zero documentation, but you can take look at file bitcoin/core/serialize.py