---
# System prepended metadata

title: Blockchain Security

---

# Blockchain Security

https://devansh.xyz/

https://github.com/crytic/awesome-ethereum-security

https://capturetheether.com/challenges/

https://www.lesswrong.com/posts/YdfpDyRpNyypivgdu/aalwa-ask-any-lesswronger-anything?commentId=TLvSTxuypiHBuoCLM

https://www.youtube.com/watch?v=bBC-nXj3Ng4


https://www.youtube.com/watch?v=f9EbD6iY9zI

http://karpathy.github.io/2021/06/21/blockchain/


# Introduction

Adam Back
Wei Dai
Cypherpunks


http://www.weidai.com/

![](https://i.imgur.com/5HpAVq2.png)

http://www.cypherspace.org/adam/
https://twitter.com/adam3us


# Who is Satoshi Nakamoto
https://www.youtube.com/watch?v=XfcvX0P1b5g

https://www.youtube.com/watch?v=BoxDiCGjFA8



# Genesis
A Genesis Block is the name given to the first block a cryptocurrency.

Bitcoin,
https://www.blockchain.com/btc/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f


https://www.investopedia.com/terms/g/genesis-block.asp
![](https://i.imgur.com/2AALy5w.png)

-> first 50 btc was sent to this address,
https://www.blockchain.com/btc/address/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?page=636


Ethereum,
https://etherscan.io/block/0



![](https://i.imgur.com/1dUwqYk.png)

https://explorer.avax.network/blockchain/25i636VUB5wvghNqumGRPGawyK5MSpmVTQiyy5s8SY9ya4CtRR


![](https://i.imgur.com/HAiIlHb.png)


# why bitcoin

- double spending problem
- decentralized


# double spending problem

%51 attacks

https://www.youtube.com/watch?v=cOc7V64HUDQ

https://gist.github.com/DrSammyD/097f84b49e02fda4ab434515346f6039
https://www.youtube.com/watch?v=UxyGt58EPa4
https://www.coindesk.com/tech/2021/01/21/the-bitcoin-double-spend-that-never-happened/


from bitcoin's whitepaper,
![](https://i.imgur.com/nw7LfXx.png)





# Transactions in bitcoin

Each owner transfers the coin to the next by digitally signing a hash of the previous transaction and the public key of the next owner and adding these to the end of the coin.


For the next tx, sign(with your private key) a hash of previous tx, public key of the next owner and add it at the end of the coin.




# hashcash

https://gist.github.com/bellbind/8554970

http://www.hashcash.org/


```
❯ echo -n 1:20:040806:foo::65f460d0726f420d:13a6b8 | shasum

00000f91d51a9c213f9b7420c35c62b5e818c23e  -
```

```
melihkaanyildiz ➜ hashcash-1.22/ λ time ./hashcash -mb16 foo
hashcash stamp: 1:16:220103:foo::xVBLy/YVPSp2Sg7w:000000000000000XPU
./hashcash -mb16 foo  0.02s user 0.00s system 81% cpu 0.024 total

[Mon 3 | 23:27]
melihkaanyildiz ➜ hashcash-1.22/ λ time ./hashcash -mb24 foo
hashcash stamp: 1:24:220103:foo::vG4/1rS0Nhotnaay:00000000000000Pk+4
./hashcash -mb24 foo  0.60s user 0.01s system 93% cpu 0.661 total

[Mon 3 | 23:27]
melihkaanyildiz ➜ hashcash-1.22/ λ time ./hashcash -mb28 foo
hashcash stamp: 1:28:220103:foo::j1CfCapp8+qbbkzd:0000000000000R0sJx
./hashcash -mb28 foo  40.53s user 0.26s system 99% cpu 41.199 total
```


```
melihkaanyildiz ➜ hashcash-1.22/ λ ./hashcash -cdb16 -r foo 1:16:220103:foo::xVBLy/YVPSp2Sg7w:000000000000000XPU
matched stamp: 1:16:220103:foo::xVBLy/YVPSp2Sg7w:000000000000000XPU
check: ok

melihkaanyildiz ➜ hashcash-1.22/ λ ./hashcash -cdb28 -r foo 1:28:220103:foo::j1CfCapp8+qbbkzd:0000000000000R0sJx
matched stamp: 1:28:220103:foo::j1CfCapp8+qbbkzd:0000000000000R0sJx
check: ok
```


```
melihkaanyildiz ➜ hashcash-1.22/ λ echo -ne '1:16:220103:foo::xVBLy/YVPSp2Sg7w:000000000000000XPU' | shasum
000022867a22cfbf455ffe8e7eb437d450407b19  -

[Mon 3 | 23:30]
melihkaanyildiz ➜ hashcash-1.22/ λ echo -ne '1:28:220103:foo::j1CfCapp8+qbbkzd:0000000000000R0sJx' | shasum
00000009c92edd7a59b487c9f7252d0a25d989f9  -
```

0000000 -> 7 zeros, 28 bit

# mining 


![](https://i.imgur.com/6RprNV0.png)


https://www.freecodecamp.org/news/how-bitcoin-mining-really-works-38563ec38c87/


`The nonce value in a block header is stored as a 32-bit number. This means that the highest nonce anybody is able to achieve is 2³² (approximately 4 billion). After 4 billion iterations, the nonce is exhausted, and if a solution is not found, miners are once again stuck.`

`If all 4 billion possible values of the nonce are exhausted, the extraNonce is added and incremented to the coinbase. A new merkle root and subsequently new block header are calculated, and the nonce is iterated over once again. This process is repeated until a sufficient hash is found.`





https://medium.com/all-things-ledger/decoding-the-enigma-of-bitcoin-mining-f8b2697bc4e2





![](https://i.imgur.com/GINRG5Y.png)


![](https://i.imgur.com/XHYMJEM.png)



https://www.tutorialspoint.com/python_blockchain/python_blockchain_creating_miners.htm


https://github.com/jgarzik/pyminer/blob/master/pyminer.py



# mining pools


https://www.investopedia.com/tech/how-does-bitcoin-mining-work/,
![](https://i.imgur.com/46XQ1oK.png)



https://en.wikipedia.org/wiki/Mining_pool,
![](https://i.imgur.com/Bd3TCY8.png)


![](https://i.imgur.com/1MYRJNs.png)


![](https://i.imgur.com/Soe1k7k.png)



![](https://i.imgur.com/g3HKeAs.png)


https://www.buybitcoinworldwide.com/mining/pools/


![](https://i.imgur.com/eJPnGFQ.png)


1) Antpool - China - %17.3
2) F2Pool - China - %13.7
3) ViaBTC - China - %12.2
4) Poolin - China - %10.7
5) Binance Pool - Malta - %9.3
6) Foundry - USA - %9.3
7) BTC.com - China - %8.9
8) Slush(First Mining pool) - Czech Repub - %4.7
9) Huobi - China - %1.7
10) SBI - Japan %1.5


Top10

%64.5 China
%9.3 USA
%6.2 Unknown
%4.7 Czech

~85

Overall,

~70 China
~12 USA
~10 Unknown
~8 Other countries


![](https://i.imgur.com/uME55pB.png)

# Blocks

https://www.blockchain.com/btc/blocks?page=14342


https://decrypt.co/35373/how-long-does-it-take-to-mine-a-bitcoin

`This is currently set at 6.25 BTC per block, but halves approximately every four years (210,000 blocks)`

![](https://i.imgur.com/mmPe17G.png)


![](https://i.imgur.com/ztUyZh8.png)


https://www.blockchain.com/btc/blocks?page=1


https://www.youtube.com/watch?v=CVzRqN_f29s



https://btc.com/stats/diff


1 block requires 24 terahashes persecond.


https://www.coinwarz.com/mining/bitcoin/difficulty-chart



![](https://i.imgur.com/DSeV7Tn.png)


# 10 Minute Block Interval
![](https://i.imgur.com/zbOfVj9.png)


https://www.youtube.com/watch?v=rhZowbYpN3Q

On average, mining a new block averages to 10 mins. Bitcoin guarantees that the block will be mined in that amount of time by adjusting the difficulty.



# How does Bitcoin difficulty work

This is calculated in every 2016th block (2 weeks). 

https://blockgeeks.com/guides/bitcoin-mining-difficulty/

https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_calculated.3F_What_is_the_difference_between_bdiff_and_pdiff.3F




![](https://i.imgur.com/cMWUni5.png)


000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f -> 1st block
00000000000000000004d3183655fd0ecfcfcde75aeb599342b27a382c76cc42 -> 717056th block


1st block      -> 10 zeros, difficulty -> 1
717056th block -> 19 zeros, difficulty -> 24,272,331,996,979.97


## Halving


3rd halve, block 630,000

May 11, 2020
https://www.blockchain.com/btc/block/000000000000000000024bead8df69990852c202db0e0097c1a12ea637d7e96d


4th halve will occur at block 840,000
We are currently at 716,918
It's been 601 day from the last halve and we mined 86918 blocks which is ~%41.3 of the total mine. So considering the speed we have now, 4th halve will occur at day 1501 which is 

~ June 20th, 2024



# Merkle root

whole previous chain's hashsum

# Hardfork

https://www.investopedia.com/terms/h/hard-fork.asp
https://corporatefinanceinstitute.com/resources/knowledge/other/hard-fork/



# Ethereum - Istanbul hard fork

https://www.coindesk.com/tech/2019/12/08/ethereums-istanbul-hard-fork-is-now-live/
https://etherscan.io/block/9069000



https://www.blockchain.com/btc/block/000000000000000000024bead8df69990852c202db0e0097c1a12ea637d7e96d
https://www.blockchain.com/btc/block/00000000000000000008b21728a7609c7986b103bc2c654e829dbe154c9ccb30



```python
import hashlib
import sys

MAX_NONCE = 2**32


def mine(message, difficulty=4):
    assert difficulty >= 4
    prefix = "0" * difficulty
    for i in range(MAX_NONCE):
        hash_with_nonce = hashlib.sha256((message +str(i)).encode() ).hexdigest()
        sys.stdout.write(f"hash: {hash_with_nonce}, nonce: {i}\r")

        if hash_with_nonce.startswith(prefix):
            print(f"Found a solution: {hash_with_nonce}, with nonce: {i}")
            return hash_with_nonce, i
    return None, None


if __name__ == "__main__":
    print(f"Mining block with difficulty: {sys.argv[1]}")
    hash_with_nonce, nonce = mine("Hello World",int(sys.argv[1]))

```