Bitcoin Forum
June 22, 2024, 01:15:20 PM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: python-bitcoinlib, creating CBlock with getblocktemplate data  (Read 1012 times)
arch_stanton (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 1


View Profile
July 06, 2017, 08:56:59 PM
 #1

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 <module>
    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"<i", ser_read(f,4))[0]
  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?

arch_stanton (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 1


View Profile
July 07, 2017, 07:56:21 AM
 #2

BTW is this the correct subforum for this post? I can't really find one that seems to fit the question, but i read that this subforum is not for support inquiries.
yg10
Newbie
*
Offline Offline

Activity: 88
Merit: 0


View Profile
July 08, 2017, 08:41:32 AM
 #3

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 <module>
    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"<i", ser_read(f,4))[0]
  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

arch_stanton (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 1


View Profile
July 13, 2017, 07:09:09 PM
 #4

Well, there is some documentation, but it doesn't answer my question.. https://media.readthedocs.org/pdf/python-bitcoinlib/latest/python-bitcoinlib.pdf . I was rather hoping that one of the developers would read this forum. Im reading the source code, but its kind of difficult to follow with the high level of abstraction (at least with my experiencie in mostly simple python code).
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1078


I may write code in exchange for bitcoins.


View Profile
July 13, 2017, 07:42:31 PM
Last edit: July 13, 2017, 07:58:34 PM by tspacepilot
 #5

I don't have experience with this lib.  But I took a look at the documentation you linked to as well as your example code.  I see that you create a list of CTransaction objects and pass that as the last argument to CBlock.  The docs show the last argument as a tuple of vtx. So, here are a couple of ideas.


EDIT: removed wrong ideas after looking a bit deeper.

Can you follow the call stack from that CBlock construction all the way to line 328 in stream_deserialize?  I can't see enough of your code to tell how you got there.

Sorry!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!