L4 The Mempool

The Mempool

Miners need a way to get new, unmined transactions from other peers. You’ll implement a Pool module that keeps track of transactions (and what UTXOs they spent), similar to Bitcoin’s.

 

  1. When you make a new transaction, you add it to your pool and send it to your peers.
  2. When you receive a transaction from a peer, you validate it against your UTXO set (minus the UTXOs already used by pooled transactions) and add it to your pool.
  3. When a new block comes in, you delete its transactions from the pool.

 

  1. What if a mined transaction conflicts with a pooled transaction? Someone could potentially broadcast a transaction A using UTXOs X and Y. They could then mine a transaction B that uses UTXOs Y and Z. Alternatively, they might be trying to double-spend by mining a transaction that outputs to themselves instead of someone else.

 

  1. Blockchain transactions take precedence over anything that was in the pool. In the above example, mined transaction B means you must remove transaction A and its used UTXOs, X and Y, from the pool.

main.js

Sending a transaction now just adds it to the pool and broadcasts it to all peers. Mining your own transactions is now not necessary, but, if you do mine, it includes the pooled transactions.

 

Making a new transaction now ensures that you don’t try to re-use UTXOs that are already in the pool’s list.

pool.js

See the extensive comments for details about Minicoin’s simplified mempool. Unlike Bitcoin, Minicoin simplified algorithm doesn’t implement spending unconfirmed outputs from pooled transactions.

 

addTx(tx, UTXOs) validates and adds a transaction to the pool. It checks the inputs against the known-mined UTXOs passed in and the Pool module’s internal list of pool-spent UTXOs. If accepted, the transaction’s spent UTXOs are added to the Pool’s list.

 

removeBlockTxs(block) takes a newly-mined block and clears out anything it uses from the pool.

p2p.js

onMessage(data) now needs to handle a TRANSACTION message type. You’ll need to add transactions from other peers to the pool.

 

The LATESTBLOCK and BLOCKCHAIN now clean up the pool when they’re done.

 

broadcastTx(tx, skippedPeer) sends a transaction to your other peers.

 

The circle is now complete. Minicoin can have specialized mining nodes and regular user nodes.