Bitcoin

Sources: 1

Architecture of the Bitcoin network

Three types of main nodes within the Bitcoin network:

  1. Users with wallets
  2. Miners that compete with each other to add new blocks to the ledger
  3. Exchanges: place where users can buy BTC in exchange for other currencies

Wallets

Software wallets allow user to manage a collection of private keys corresponding to their accounts, and use them to create and side transactions on the Bitcoin network.

Hardware wallets are specialized devices that operate with suitable software to realize the functionality of software wallets.

  • Private keys are generally stored on the hardware and never leave the device.
  • Public keys are exported so that payments can be received
  • The unsigned transaction is sent from the software to the device, verified by the user on the display of the device, and confirmed with a PIN. Then, the transaction is signed by the device and sent back to the software.

Cold wallets such as paper and metal plates are used as backups for the wallets.

Exchanges

Exchanges are usually websites that buy BTC in exchange for other currencies.

Exchanges hold BTC on behalf of users, making it a form of trusted party within the Bitcoin system.

Users can ask the exchange to transfer purchased Bitcoin to an address under their control. Until that point, if the exchange is compromised, users might lose control of their BTC.

Accounts and Transactions

An account in Bitcoin is associated with a cryptographic key pair:

  • The public key is used to create the account address
  • The private key is required to sign transactions originating from the account.

Bitcoin does not track account balance explicitly. Instead, the balance is derived as the sum of unspent transaction outputs that an account has control over. Therefore, the record-keeping model of Bitcoin is referred to as UTXO.

Bitcoin Transaction Processing

Mempool and Orphan Transactions

Incoming transactions are handled by the so-called "mempool".

If the referenced input transactions, called "parents", are as yet unknown, a miner will delay the inclusion of the new transaction. Such a transaction becomes an "orphan".

Orphans might stay in the mempool until their parents arrive. Or, they can be removed after a timeout.

Bitcoin script

The script language of Bitcoin is called "Script":

  • A simple, stack-based languaged
  • Processed from left to right
  • Not Turing complete, meaning it has limited complexity, without looping and complex control flow.
  • Pure functions
  • Cannot poll external servers or import any external state.

Each script is a list of instructions associated with each transaction, which describes how the BTC transffered with the transaction can be spent.

Types of scripts:

  • Locking script is placed on an output, which specifies the conditions that must be met to spend the BTC.
  • Unlocking script is placed in an input, which satisfies the conditions of the locking script. To validate a transaction, the unlocking script and the locking script are combined and executed. If the result is true, the transaction is valid.

Pay-to-PubKey Hash (P2PKH) is the most common case. In this implementation, the locking script specifies which (hashed) public key and corresponding signature are required to unlock the output. It means that only the holder of the public key and the corresponding private key can spend the output.

OP_RETURN

OP_RETURN is a keyword, called "opcode", which is used to mark a transation output as invalid. It has been used as a standard way to embed arbitrary data to the Bitcoin blockchain for other purposes, such as presenting assets.

Mining

Bitcoin uses hashcash proof-of-work function.

Mining process:

  1. Miner removes transactions from the mempool that belong to the latest received block, as these has been processed.
  2. Miner aggregates the remaining valid transactions into a candidate block and reassess their validity
  3. Miner adds coinbase transactions to reward itself
  4. Miner constructs the block header, which includes a previous hash and a Merkle tree.
  5. Miner finds a solution to the proof-of-work function.
  6. The new block is propagated to the rest of the network.
  7. Receivers verify the new block and add it into their replica.

Nakamoto Consensus

In Nakamoto consensus, processing nodes by convention treat the longest history of blocks as the authoritative history, also known as the main chain.

In the case multiple chains exist, it is unclear which chain is the main chain until one chain grows longer than the other one.

Bitcoin protocol presents the parallel forks of the ledger to continue for more than a block or two, unless the network is separated.


  1. Architecture for Blockchain Application