Sorry for another stupid question, but how is the coinbase transaction created? Do I create that first, use the TXID for the merkleroot and hash the block header hoping I solve the block?
The coinbase transaction (AKA Generation Transaction) is created similarly to how any other transaction is created. The creator of the transaction chooses the outputs, and a single input. This input has a "coinbase" parameter instead of a scriptSig. The data in "coinbase" can be anything; it isn't used. Bitcoin puts the current compact-format target and the arbitrary-precision "extraNonce" number there, which increments every time the Nonce field in the block header overflows.
A miner (or mining pool) assembles a set of transactions in an arbitrary order and calculates the merkle root to be used in the block header when attempting to solve the block. If the "set" of transactions contains ONLY the generation transaction, then the merkle root will be the hash of the generation transaction.
The rest of the block header fields are populated with the necessary data and then the miner can begin attempting to solve the block. The block header is repeatedly hashed, while incrementing the nonce value after each attempt until one of the following:
- The hash of the header is found to be lower in value than the current target. Hooray, you've solved a block. Quickly broadcast it to as many other miners and pools as you possibly can before someone else solves a block and yours is orphaned!
- You receive a solved block from a peer that is using the same "prev_block" as you are using. The blockchain has extended, you need to build a new block header. You may also need to build a new set of transactions (and therefore a new merkle root) if the previous block has effected the set of transactions that you had been using.
- You have exhausted all nonce values and still haven't found a blockheader hash value that is lower than the current target. You'll need to modify something else in the block header, and then start your search through all nonce values over again. Some commonly modified values are the timestamp and the merkle root. The merkle root can be modified by changing the input value of the generation transaction (extraNonce) or by updating the set of transactions being used