To help me to remember the details of how block chains work I am simulating some core concepts.
The directory bitcoin
aims to simulate concepts from the Bitcoin blockchain such as Wallet
s, Transaction
s, the Transaction Pool and the Blockchain itself. A simulation of events is then run in the script bitcoin/simulate.js
. I hope to increase the complexity over time of the simulation to make it a more accurate representation of the actual blockchain technology.
Core concepts included so far are:
- A Wallet is produced containing a
privateKey
,publicKey
andpublicAddress
. - The
Wallet
itself has no intrinsicbalance
but rather to calculate aWallet
s balance the blockchain is inspected for all prior transactons as a necessary precurser to creating aTransaction
.
- A
Transaction
is created using a Wallets prior transactions to calculate available funds. - A
Transaction
creates a hash value of thefromAddress
,toAddress
,amount
andgas
(gas is static right now, this feature could be fleshed out later) - A Signature is produced on each
Transaction
using the wallet holders private key
- The transaction pool is represented crudely to begin with as a simple queue (no gas fees incorporated yet)
- A simple indiscriminate setInterval runs to check the pool periodically.
- Contains a list of transactions
- Calculates the MerkleTree of the transactions and stores the MerkleRoot Hash value (hex)
- Stores the hash of the previos block
- Contains important information on the mining protocol such as:
bits
(the number of leading zeros needed for a valid nonse)blockReward
(how much newly minted bitcoin does a miner receive for completing a block)
- Represents a full node on the bitcoin network
- Has its own copy of the blockchain and the transaction pool
blockchain
andtxPool
are both array's, this may change in the future- The class
Miner
extends the classNode
to have these attrbutes - The class
Network
extends the classNode
to have these attrbutes
-
All Miners have their own transaction pool and blockchain (extended from
Node
class) -
A miner will pick up transactions from it's
txPool
and attempt to create blocks -
The miner will:
- Verify the signature of each transaction on the block
- Search for a valid nonse per the
bits
specified in theBlock
-
A miner runs a simple subprocess (using
child_process
) to find thenonse
utilizing a single CPU (no gpus here) -
A miner will be rewarded with newly minted bitcoin (tx fees not incorporated)
The single CPU should work fine for simulation so long as the parameter bits
on the Block
object is kept lower than on the actual blockchain
- The
Network
class also extends theNode
class to represent the consensusblockchain
of the network - The instantiation of the
Network
initializes theMiner
workers and handles all incoming transactions to the network
Presently the app is running in Node JS with a simple terminal stdout display. It would also be useful to produce a front end that vizualizes the various components of the blockchain.
-
fix transactions - so that prior transactions are fed in as inputs to a transaction. Right now I'm checking the whole blockchain.
-
fix transactions - After each transaction there should be "change" returned to the senders address
-
Change blockchain data structure from JS Array to "back linked list"
-
mining / proof of work. DONE (for single minor)
-
Tx fees/Gas
-
Miners should validate blocks produced by other miners and add to their copy of the blockchain or reject if invalid. DONE
-
Mining Strategy - empty blocks. Empty blocks are valid on the blockchain network. So if a miner doesnt have txs to process it could be submitting empty blocks. This still increases security of the network so it does provide a function (in reality this is done during the period that a miner is scrutinising their tx pool for complete transactions from an incoming block)
Things to include in view:
- the blockchain
- miners working
- people sending transactions
Blockchain operation info like :
- Avg time to produce a block
- Then we can see if and when the bit rate will change and maybe even keep a log