Code: Select all
First of all, you need (of course) to run a Namecoin client. I suggest
you to try Namecoin Core [1] instead of the legacy client [2]. While it
is definitely still "experimental" software, it is based on Bitcoin 0.10
instead of ancient code and it has been in production use by a major
pool since December without any problems.
[1] https://github.com/namecoin/namecore/
[2] https://github.com/namecoin/namecoin/
With the client running, the merge-mining procedure looks (on a high
level) like this:
1) You call "namecoind getauxblock" via the RPC interface. This returns
an object like this:
{
"hash" : HASH,
"chainid" : 1,
...
"bits" : "184927b0",
"_target" :
"000000000000000000000000000000000000000000b027490000000000000000"
}
The important information is the block hash, the chain id (always 1 for
Namecoin) and the difficulty bits / target. (Note that _target has
reversed byte order due to legacy usage.)
2) You mine on your Bitcoin block as usual, but the coinbase input
includes a special data string. Every now and then (especially if a new
Namecoin block arrives!) you have to repeat step 1 in order to update
the Namecoin block you mine for. This implies updating the Bitcoin
block's coinbase input as well.
3) When you find a Bitcoin block header that matches Namecoin's
difficulty as returned by "getauxblock" (even if the block doesn't match
Bitcoin's difficulty), you've found an auxpow.
4) You call "namecoind getauxblock HASH AUXPOW" to submit the block to
the Namecoin network. HASH is the HASH returned by "getauxblock" in
step 1 and AUXPOW is a hex string that encodes the auxpow (see below).
Now, two important steps remain open:
A) How the Bitcoin coinbase should look like.
B) How the auxpow hex string is constructed.
Piece A is easy if you do not intend to merge-mine any other coins (it
is also not too hard if you do, but for a beginning you can try
without). For instance, Namecoin block
2a130cf885ee077f329434c1206a0307ced1a926f7311146c47036d63feede75 was
merge-mined with Bitcoin's
00000000000000000cfe6c72e9ee1f8bfff1dcd80ce788d59ff9834a3cbb17db.
If you look in the Bitcoin block's coinbase input, it is (with added
line breaks):
0372530504001a900849124d696e656420627920425443204775696c642c
fabe6d6d2a130cf885ee077f329434c1206a0307ced1a926f7311146c47036d63feede75
01000000000000000800207fbbf6ce0000
The second line contains the so-called "merge-mining header" fabe6d6d
followed by the Namecoin block hash. The next eight bytes are also
required (in particular the 4-byte integer 1 is important, I think the
following 4-byte integer 0 could be anything in this case).
If you want to take a look at the code that checks this, look at
auxpow.cpp in the Namecoin Core code (linked above).
Finally, regarding the auxpow hex string construction: Look at auxpow.h
to see, roughly, how a CAuxPow object is serialised. You need to
construct such a serialisation in hex. If you follow the advice above
(not merge-mining other coins for now), your "chain Merkle branch" will
always be empty, and your "chain index" will always be 0. The parent
block is the 80-byte block header of your mined Bitcoin block. The
initial part of the auxpow is a serialisation of the Bitcoin coinbase tx
including its Merkle branch.
You can also take a look at src/tests/auxpow_tests.cpp and
qa/rpc-tests/auxpow.py. There is code in there that constructs and
mines (with regtest difficulty) an auxpow.