4

I am writing a program (in golang) to mine bitcoins. Supposing I have this dummy transaction, how may I encode it so it can be used in the block im trying to mine?

Also, supposing I have a block header as below:

// binary header data encoded as a hex string:

000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1

// original header data that has been encoded to build the above header:

Version: 01000030

prev_block: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000

Merkle root: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582

Timestamp: 7f476357

nbits: a09b0518

Nonce: ae5ae1c1

Block header: 01000030cd0594ebccfe15b205125165cc3b66986ed5b2a311cd8805000000000000000037022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb5827f476357a09b0518ae5ae1c1

computed_hash: 000000000000000003e6d3647001d1f455d585cdd3c298093902fe52ea4529ba

How may I add our dummy transaction to the given header (suppose the header already have the merkle root in it)? What if I wish to add multiple transactions like our dummy transaction data to our block header ?

1 Answer 1

2

Feb2018: This is a very generic question of an older request, I answer it anyhow, maybe someone will be searching for this info, just like I was doing :-)

The main point is, that the transaction is not part of the block header. The block starts with a blocksize number of 4 bytes, a block header of 80 bytes, and a transaction counter (var_int, 1-9 Bytes). Then follows all the transactions in the block. The block header starts with a 4 bytes version field, followed by the previous block hash (32 bytes) and the merkle tree (32bytes). This is followed by three fields of 4 bytes each: difficulty, timestamp, and nonce.

In your example, the block should be composed this way:

Block size: 00000264 (612 Bytes)

Version: 01000030

prev_block: cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000

Merkle root: 37022f7a3b9a2b199d53fbf1552be32c141892319966c3ef999d348d74cfb582

Timestamp: 7f476357

nbits: a09b0518

Nonce: ae5ae1c1

tx counter: 2

transaction: 010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600

Without the first field, this is a length of 612 bytes, which must be converted into a 4 bytes, which is placed at the beginning of the block. So the full block would be:

<block size>

01000030

cd0594ebccfe15b205125165cc3b66986ed5b2a311cd88050000000000000000

<merkle tree>

7f476357

a09b0518

ae5ae1c1

2

<coinbase ... >

010000000106af2297b4c9a602be8d866ce644f5a03a4485e973309a7d8a0a64fbd985d1c4010000006b483045022100c37ea6fde87034c0dcc5a62f5fdc8c5292b5b2340c327a4c5d5ff6c1f84a748102203a2ed49ecd0a30f10a1ef77704fcd2c853eecf29c3afd9eea9d44e4fd48d39a50121034ef202077f758c66ad3bf6901c844fd88df6e9889be09b963af1a89fda49f4d3feffffff0271c24413000000001976a914c619073e11f2937f344482ed78a791ec93ca8da988acc0ac0d0d000000001976a914efceeab4cc7010eb07421f7351bf2c22570a5ef488ac9c5f0600

Then the merkle tree needs to be re-calculated, and double sha256'd. The length need to be calculated, and put at the very beginning, and the "coinbase tx" is missing (funds calculations!).

The details of the structure can be found here in these two reference pages: https://bitcoin.org/en/developer-guide#block-chain-overview https://bitcoin.org/en/developer-reference#block-chain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.