---
# System prepended metadata

title: <center><i class="fa fa-edit"></i> 3.5 The Bitcoin Network (P2P)</center>
tags: [Blockchain]

---

# <center><i class="fa fa-edit"></i> 3.5 The Bitcoin Network (P2P)</center>
###### tags: `Blockchain`
The following notes are taken from [Coursera](https://www.coursera.org/learn/cryptocurrency/home/week/2)

---
- Ad-hoc protocol (runs on TCP plrt 8333)
    - Ad-hoc: neccessary, 特设
- Ad-hoc network with random topology
- Alll nodes are equal 
- New nodes can join at any time
- Forget non-responding nodes after 3 hrs 

### Joining Bitcoin Network
1. Find seed node and send msg to all other nodes the seed node knows about using `getaddr()`
2. Repeat for all other nodes

Result in random set of nodes connected to 

### Transaction Propagation (Flooding)
1. A pays B, ends up in their transaction pools
2. Speads and tells every node to memory pool (analogous to gossip)
3. Stops because eventually all other nodes already heard about the transaction 

### Should I Relay A Proposed Transaction?
- Transaction valid with current blockain 
- (default) script matches whitelist
    - Avoid unusual scripts*
- Haven't seen before 
    - Avoid infinite loops*
- Doesn't conflict with other's I've related 
    - Avoid double-spends*

*sanity checks; some nodes may ignore them

### Nodes May Differ On Transaction Pool
- Divided state with nodes divided on different transactions 

### Race Conditions 
Transaction or blocks *may conflict*
- Default behavior: accept what you hear first
- Network position matters
- Miners may implement other logic

### Block Propagation Nearly Identical 
Relay a new block when you hear it if:
- Block meets hash targer
- Block has all valid transactions 
    - Run ALL scripts, even if you wouldn't relay*
- Block builds on current longest chain
    - Avoid forks*

Takes over 30 sec, not for efficiency
*sanity checks; some nodes may ignore them

### How Big is the Network
- Impossible to measure precisely
- ~ 1M IP addresses per month
- Only ~ 5-10K "full nodes"
    - Permanently connected
    - Fully-validate
- Numbers may be lower

### Fully-Validating Nodes
- Permanently connected
- Sotre entire block chain
- Hear and forward every node per transaction

### Lightweight Nodes (Contrast to Fully-Validating Nodes)
- AKA thin clients or simple payment verification clients
- Don't attempt to store entire blockchain
- Only store pieces that they need to verify some specifc transactions 
- Store block headers only
- Request transactions as needed
    - To verify incoming payment
- Trust full-validating nodes 
- 1000x cost saving (20 GB -> 25 MB)

### Tracking the UTXO Set
- Unspent transaction output
    - Everything else can be stored on disk
- Currently ~12M UTXOs
    - Out of 44M transactions proposed
- Can easily fit into RAM


