密碼學/區塊鏈筆記。四

in #hk7 years ago (edited)

Mining in Detail

做礦工係有回報既。
起最初最初既時候,礦工搵到x such that SHA256("x,A pay B 1BTC,B pay C 1BTC") < target之後
會獲得50個bitcoin作為獎賞
而且呢個獎賞會寫入個block度
每個block入面既第一條交易其實係屬於礦工既,所以block既內容應該係咁
"x,miner gets 50BTC,A pay B 1BTC,B pay C 1BTC"

擺埋礦工獎賞入去同一個block係個好緊要既誘因令到礦工唔會出古惑
因為礦工發佈新block出去之後,其他miner會幫手驗証呢個礦工有無計錯數,同埋睇下D交易有無問題
例如礦工會唔會借D e比多幾個bitcoin自己
如果新block包含住一D唔合法既交易
咁其他miner就會無視呢個block, 當呢個block無出現過,繼續跟據上個block既結果既mine
而且其他miner 搵到新block之後係會extend on 上個block, 而唔係呢個出cheat既block
咁呢位出cheat 礦工就戇居喇,因為只有寫起longest blockchain上面既交易先算有效
佢係就係起某一個block入面得到獎賞,但係唔起longest chain上,出面無人承認
所以就算mine到都係曬電費,真係賠了夫人又折兵

講番個獎賞,呢個獎賞係會隨時間而減半,準確D講係每掘到210000個block就會減半
之前講過mining既難度係會自動調較,盡量做到每10分鐘出現一個新block
即係每210000*10分鐘=2100000分鐘=35000小時=1458.333日=3.995年
即係每大約四年,掘礦獎勵就會減少一半

創世block係起2009年1月出現
Block #210000係起2012年11月出現,由呢個block開始獎勵由50調低到25
Block #420000係起2016年7月出現,由呢個block開始獎勵由25調低到12.5

學過中學數學應該識得計sum of geometric sequence
S(n)=a/1-r
starting term = 50
common ratio = 1/2
sum to infinity = 50 / (1 - 1/2) = 100
如果每個block就獎賞減半,咁總發行量就係100
但係佢係每210000個block先調低一次
所以bitcoin既總發行量 = 210000 * 100 = 21,000,000
二千一百萬個發行量就係咁樣計出黎

起bitcoin既世界係每0.00000001BTC做單位,又叫做1 Satoshi
下面個表就係每一次降低後既獎賞,以satoshi為單位

1st period: 5000000000 (50 BTC)
2nd period: 2500000000 (25 BTC)
3rd period: 1250000000 (12.5 BTC)
4th period: 625000000 (6.25 BTC)
5th period: 312500000
6th period: 156250000
7th period: 78125000
8th period: 39062500
9th period: 19531250
10th period: 9765625
11th period: 4882812.5
12th period: 2441406.25
13th period: 1220703.125
14th period: 610351.5625
15th period: 305175.78125
16th period: 152587.890625
17th period: 76293.9453125
18th period: 38146.97265625
19th period: 19073.486328125
20th period: 9536.7431640625
21st period: 4768.37158203125
22nd period: 2384.185791015625
23rd period: 1192.0928955078125
24th period: 596.0464477539062
25th period: 298.0232238769531
26th period: 149.01161193847656
27th period: 74.50580596923828
28th period: 37.25290298461914
29th period: 18.62645149230957
30th period: 9.313225746154785
31st period: 4.656612873077393
32nd period: 2.3283064365386963
33rd period: 1.1641532182693481

再除落去就會細過1 satoshi, 所以去到33th period之後就唔會再有新bitcoin出現
以每個period 4年黎計,33 period * 4 =132年
所以起我同你既有生之年都唔會見到呢一日

132年之後所有礦工都唔會由掘礦賺到新既bitcoin

問題:如果bitcoin有幸去到132年之後都未收皮,無新bitcoin賺仲邊會有人做礦工?
答案:靠大家比既transaction fee

上個chapter講過,理論上礦工處理邊個交易先,係先到先得咁決定
咁係因為掘礦初期淨係靠獎賞都夠食
但係隨住獎賞不斷下降,就要交發起交易既人比transaction fee先維持到生計
(起碼都幫補下人地既電費丫...)
所以礦工會唔會及時處理你既交易,就好視乎你肯比幾多tips
你比既tips係會直接袋落個礦工袋到
所以,就算你個交易係好遲send出黎,如果你肯比夠多tips既話,全世界既礦工會爭住擺你既交易入下一個block
就算你既交易係遲過人地發出去,分分鐘可以比其他交易更快得到confirmation

掉番轉咁講,如果你平時都唔比錢聽歌/比錢睇AV
斷估你用bitcoin比錢既時候都唔會比tips
咁你個交易既priority就會不斷比其他肯比tips既交易壓低落去
結果好可能係,明明你6pm就send左個交易出去,等到7pm都未有礦工confirm你


上面提到,mining既難度係會調節到每10分鐘就出現一個新block
呢個調節係每2016個block發生一次
如果每一次既調節都做得好既話,即係每2016 * 10分鐘 =20160分鐘=336小時=14日
所以大約每兩個禮拜就會調節一次

起講點樣調節之前,要define幾個parameter
Difficulty = 2^224 / current target
difficult 最低係1, when current target = 2^224
所以2^224 又叫做"Target when difficult = 1"

將2^224表達番做16進制
0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
即係話頭8個位都係0
呢個target其實真係唔難, 你識寫programme既話,求其寫個for loop, loop夠一晚
應該會搵到一個input既hash value細過2^224

頭先講過difficult 1係最易
咁而家既difficult係幾多呢?860,221,984,436.2223
所以current target = 2^224 / 860221984436.2223
大約係0x000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
(18個0字頭)
即係話你個block個hash value要細過0x000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
先合格

如果difficult 係 1
咁平圴要hash幾多次先會到個合格既block呢?
因為SHA256既output係256個bit, total 有2^256個outcome
difficulty = 1既時候, 只要outcome < 2^224都bingo
所以expected value係2^256 / 2^224 = 2^32次
(想象下擲骰仔有6個outcome, 擲到細過3就合格,咁你要擲到合格既expected次數就=6/3=2次)

如果我想每10分鐘=600秒搵到一個新既block
咁成個bitcoin network既total hash power就要有2^32/600 per second

當difficulty 唔係1既時候
hash到合格既expected次數就係difficulty * 2^256 / 2^224 = D * 2^32
成個bitcoin network既total hash power就要有D * 2^32/600 per second

用番而家做例子
而家個difficulty = 860,221,984,436.2223
所以而家既total hash power就係860,221,984,436.2223 * 2^32/600=6.1510^18 / second
即係而家全世界礦工夾埋,一秒計到6.15
10^18次SHA256


秒秒鐘計緊6.1510^18次SHA256
有幾犀利?
根據https://en.bitcoin.it/wiki/Non-specialized_hardware_comparison
Core i7 950 5.8MHash/s , 即係一秒鐘5800000次
GPU可以上到1000MHash/s = 1
10^9 Hash /s
同上面既10^18比仲係爭9條街

GPU可以快過CPU咁多係因為parallel computing
GeForce 10 閒閒地有1000個core,每個core可以各自計SHA256
但係CPU去到盡,就算server grade, 都唔過100 cores

bitcoin面世頭果幾年都仲可以用CPU/GPU 掘到金
後黎比班EE佬/CE佬玩寸左個場
佢地用FPGA黎寫programme計SHA256
FPGA同CPU唔同在於,你起電腦上面寫programme, low level極都要寫C
C要compile做assembly language, 分分鐘一行x=x+1而家用左十行assembly code
但係FPGA就係programme on CPU level,你可以寫hardware description language直接操控點砌d logic gates

越寫low level language, 就越能夠improve performance
後黎D人覺得FPGA都唔夠玩,因為FPGA仲有可programme既功能
馮親可以改既野,即係未優化到盡

所以最終level係ASIC (Application-specific integrated circuit)
即係塊電路版係焊死左,淨係識得計SHA256,做唔到其他野
平平地1000蚊港紙買到一部Antminer S5, 1.16 TH/s
1.16TH/s = 1160GH/s = 1160000MH/s
閒閒地快過GPU成千倍都有,呢部ASIC仲要係cheap野

所以礦工生態由以往各有各掘,發展到而家要form mining pool黎掘
靠自己一個掘既話真係掘到天荒地老都唔會掘到一個

Sort:  

Interesting
I will follow you to see your future posts!

Congratulations @westerpants! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.24
TRX 0.21
JST 0.037
BTC 98048.48
ETH 3406.24
USDT 1.00
SBD 3.35