偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一段程序看懂比特幣原理

開(kāi)發(fā)
自從比特幣火起來(lái)以后,網(wǎng)上對(duì)比特幣的解釋可謂汗牛充棟,紛繁復(fù)雜。但對(duì)于程序員來(lái)說(shuō),最直接的方式莫過(guò)于直接看程序代碼了。嫌比特幣代碼龐雜沒(méi)關(guān)系,我找到一段簡(jiǎn)明扼要的代碼,用來(lái)理解比特幣再好不過(guò)了。

自從比特幣火起來(lái)以后,網(wǎng)上對(duì)比特幣的解釋可謂汗牛充棟,紛繁復(fù)雜。但對(duì)于程序員來(lái)說(shuō),最直接的方式莫過(guò)于直接看程序代碼了。嫌比特幣代碼龐雜沒(méi)關(guān)系,我找到一段簡(jiǎn)明扼要的代碼,用來(lái)理解比特幣再好不過(guò)了。

以下這段程序轉(zhuǎn)自知乎上Wu Hao的回答。

  1. function mine()  
  2. {  
  3.     while(true)  
  4.     {  
  5.         longestChain = getLongestValidChain()  
  6.  
  7.         -- A number that changes every time, so that you don't waste   
  8.         -- time trying to calculate a valid blockHash with the same  
  9.         -- input.  
  10.         nonce = getNewNonce()  
  11.  
  12.         currentTXs = getUnconfirmedTransactionsFromNetwork()  
  13.  
  14.         newBlock = getNewBlock(longestChain, currentTXs, nonce)  
  15.  
  16.         -- http://en.wikipedia.org/wiki/SHA-2  
  17.         -- and this is what all the "mining machines" are doing.  
  18.         blockHash = sha256(newBlock)  
  19.  
  20.         if (meetReqirements(blockHash))  
  21.         {  
  22.             broadcast(newBlock)  
  23.             -- Now the height the block chain is incremented by 1 
  24.             -- (if the new block is accepted by other peers),  
  25.             -- and all the TXs in the new block are "confirmed" 
  26.         }  
  27.     }  
  28. }  
  29. ////////////////////////////////////////////////////////////////  
  30. function sendBTC(amount)  
  31. {  
  32.     sourceTXs = pickConfirmedTransactionsToBeSpent(amount)  
  33.     tx = generateTX(sourceTXs, targetAddrs, amount, fee)  
  34.     signedTx = sign(tx, privateKeysOfAllInputAddress)  
  35.     broadcast(signedTx)  
  36. }  
  37. //////////////////////////////////////////////////////////////// 

下面是我的解釋:

挖礦過(guò)程就是不斷從比特幣網(wǎng)絡(luò)中獲取所有未確認(rèn)交易getUnconfirmedTransactionsFromNetwork(),把它們打包成一個(gè)區(qū)塊并掛載目前最長(zhǎng)的區(qū)塊鏈上getNewBlock(longestChain, currentTXs, nonce),然后計(jì)算新的區(qū)塊的散列值sha256(newBlock),如果散列值正好滿足挖礦難度了meetReqirements(blockHash),那么就挖礦成功了。所謂挖礦難度,指的是要求的二進(jìn)制散列值末尾0的個(gè)數(shù),而散列值是碰運(yùn)氣生成的,除了窮舉沒(méi)有別的辦法,要求的0個(gè)數(shù)越多挖礦的難度就越大。

付款過(guò)程就是把一些有余額的已確認(rèn)交易拿出來(lái)作為發(fā)送地址pickConfirmedTransactionsToBeSpent(amount),然后根據(jù)目標(biāo)地址支付一定交易費(fèi)生成新的交易generateTX(sourceTXs, targetAddrs, amount, fee),并用錢(qián)包私鑰對(duì)交易簽名sign(tx, privateKeysOfAllInputAddress),然后廣播出去。

原文鏈接:https://www.byvoid.com/zhs/blog/bitcoin-principle-program

責(zé)任編輯:林師授 來(lái)源: byvoid.com
相關(guān)推薦

2017-04-06 12:20:16

2013-12-09 16:35:25

2018-11-02 16:16:41

程序硬盤(pán)存儲(chǔ)

2021-10-28 19:35:02

代碼main方法

2020-04-03 10:14:57

內(nèi)存蠕蟲(chóng)代碼web安全

2015-10-09 11:44:06

2012-03-01 14:13:36

Java

2014-07-08 09:21:10

死代碼創(chuàng)意歌曲

2022-06-21 12:27:12

JavaScript前端

2018-06-19 08:02:00

統(tǒng)計(jì)程序微信

2020-12-31 10:14:42

防注入代碼繞過(guò)

2021-01-18 08:09:20

Java程序JIT

2021-01-04 05:58:57

WindowsXP微軟操作系統(tǒng)

2014-09-09 14:47:20

2021-02-04 07:55:28

代碼離職互聯(lián)網(wǎng)

2021-04-26 10:13:13

區(qū)塊鏈Blockchain物聯(lián)網(wǎng)

2023-12-18 10:45:31

2021-06-02 05:43:36

比特幣虛擬貨幣區(qū)塊鏈

2022-02-08 09:00:00

智能自動(dòng)化人工智能RPA

2019-10-18 09:39:44

爬蟲(chóng)消息大數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)