novitski / bitcoinj

Automatically exported from code.google.com/p/bitcoinj
Apache License 2.0
0 stars 0 forks source link

Ability to create coinbase transactions #312

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I know this is likely to be a strange one, but I figure it doesn't hurt to ask.

I would like bitcoinj to support creating a the coinbase transaction for 
stratum mining:
http://mining.bitcoin.cz/stratum-mining

This means a transaction with the coinbase input and ability to replace some 
bytes (in the example, 8 bytes) with extra nonce data from the miners.

Use case: I am building a java stratum mining pool implementation.  The mining 
pool will of course call into a bitcoind to get the transactions and other data 
but right now there seems to be no easy way to make the coinbase transaction.  
I'm currently using bitcoinj in this project for address validation and I plan 
to use it to build the blocks (gathering transactions learned from bitcoind 
getblocktemplate).  Right now, I'm looking at probably execing some python to 
make the actual coinbase transaction but I'd rather not do it that way.

Original issue reported on code.google.com by fireduck@gmail.com on 11 Feb 2013 at 9:49

GoogleCodeExporter commented 9 years ago
You can just create a Transaction object and then use addInput and addOutput as 
needed. I'm not sure what else is required - bitcoinj can and does create 
coinbase transactions, see the unit tests for examples of doing that.

Is there something specific missing?

Original comment by hearn@google.com on 11 Feb 2013 at 9:55

GoogleCodeExporter commented 9 years ago
The missing thing is probably understanding on my part.  When I looked, it 
seemed there was no way to create a TransactionInput that had a zero hash 
output transaction and -1 output index.  Maybe I was looking at an old version, 
I think I had 0.6.1.

I am also really unclear on where and how to substitute in the extra nonce 
bytes needed for stratum mining.

Original comment by fireduck@gmail.com on 11 Feb 2013 at 10:20

GoogleCodeExporter commented 9 years ago
Look at the implementation of (package private) method 
Block.addCoinbaseTransaction

        Transaction coinbase = new Transaction(params);
        coinbase.addInput(new TransactionInput(params, coinbase, new byte[]{(byte) txCounter++, (byte) 1}));
        coinbase.addOutput(new TransactionOutput(params, coinbase, value, Script.createOutputScript(pubKeyTo)));

In this case it's just stuffing a counter inside the coinbase scriptSig, but of 
course, you can use the methods on the Script object to create your own 
scriptSig with the right things in it.

The TransactionInput c'tor used above creates an input with an outpoint of 
Sha256Hash.ZERO so it is suitable for a coinbase input.

Original comment by hearn@google.com on 11 Feb 2013 at 10:39

GoogleCodeExporter commented 9 years ago
Sweet, this helps a lot.  Let me make sure I understand:

The script bytes for the input of the coinbase transaction can be literally 
whatever I want (limited to 100 bytes)?  

I was thinking it had to be a valid script that terminated in such a way that 
code evaluating the script knew to not bother evaluating the rest to allow some 
other data to be crammed in.

Original comment by fireduck@gmail.com on 11 Feb 2013 at 10:58

GoogleCodeExporter commented 9 years ago
Look at the format of existing Stratum mined scriptSigs and match them.

Original comment by hearn@google.com on 11 Feb 2013 at 11:11

GoogleCodeExporter commented 9 years ago
Request: make constructor public

TransactionInput(NetworkParameters params, Transaction parentTransaction, 
byte[] scriptBytes)

Original comment by fireduck@gmail.com on 11 Feb 2013 at 11:26

GoogleCodeExporter commented 9 years ago
OK, done (on master).

Original comment by hearn@google.com on 14 Feb 2013 at 2:53