- Who selects the transactions to go into this block? If not a person, where in the Core is this included
Miner select them. More specifically, node software that miners run select the transactions. Most miners use Bitcoin Core or some variation of it.
- How do they then get bundled as a block?
- How do they get passed on then to the miners?
When the node software selects transactions, it creates a hexadecimal string for each transaction that represents each of the fields. That hex string follows the same format used for serializing transactions for sending over the network, just as the hex representation of the bytes instead of the bytes themselves. This is then packaged together with some other information into a JSON object in the getblocktemplate format and provided over a JSON-RPC interface to mining software.
The mining software then takes the information provided by getblocktemplate, adds their own additional information (e.g. the nonce) and constructs a serialized block. Serialized blocks are byte arrays containing all of the information in a block and are the same format as sent over the network. If the block is valid, the miner sends the serialized block back to the node which then sends it to the rest of the Bitcoin network.
Here is a step by step example of how a miner Alice mines a block.
- Alice runs Bitcoin Core which constantly receives, validates, and relays transactions and other blocks.
- Alice runs a mining software (such as cgminer) and connects here hashing machines (e.g. an Antminer S9) to that.
- Alice also provides to her instance of cgminer the credentials to the JSON-RPC interface for her Bitcoin Core node.
- cgminer will use getblocktemplate and send a request for a block template to Bitcoin Core.
- Bitcoin Core receives the getblocktemplate request and chooses transactions from its pool of valid and unconfirmed transactions that it has stored in memory. The transactions are typically selected by their fee rates, highest fee rate being selected first.
- Bitcoin Core constructs a block template using the selected transactions and adds any additional information required.
- Bitcoin Core responds back to cgminer over the JSON-RPC interface with the completed block template.
- cgminer receives the block template and uses it to construct a serialized block header in a form that is acceptable to the hashing hardware
- cgminer sends whatever data it has serialized to the hashing hardware and the hardware computes SHA256 hashes on it while incrementing some data in the data.
- The hashing hardware (or cgminer) checks whether a hash has a valid proof of work. If it does, skip to step 14.
- Once the hardware exhausts all possible combinations it can try using the data given and responds back to cgminer
- cgminer, when the hashing hardware can no longer continue with its data, sends another getblocktemplate request to Bitcoin Core for a new set of transactions and block data to try.
- Repeat steps 4 through 12 until the hashing hardware finds a hash with a valid proof of work in setp 10.
- Once a hash with a valid proof of work has been found, cgminer takes the data that was hashed for that valid proof of work and combines it with the serialized transactions.
- The serialized block is converted into a hexadecimal string and sent to Bitcoin Core using the submitblock RPC command.
- Bitcoin Core takes the hexadecimal string representing the block, converts it into a byte array, checks that the block is valid, and if it is, sends the block to the nodes that it is connected to.