主页 > 华为手机安装不了imtoken > 一个理解比特币原理的程序

一个理解比特币原理的程序

华为手机安装不了imtoken 2023-01-17 13:42:13

一个理解比特币原理的程序

jopen 8 年前

自从比特币大行其道以来,网络上对比特币的解读可以说是铺天盖地、繁杂。但是对于程序员来说,最直接的方式就是直接看程序代码。比特币代码太复杂也没关系,我找到了一个简洁的代码,更能理解比特币。

以下程序改编自吴昊在知乎上的回答。

 function mine ()  {      while(true)      {          longestChain = getLongestValidChain ()            -- A number that changes every time, so that you don't waste  --               time trying to calculate a valid blockHash with the same          -- input.          nonce = getNewNonce ()            currentTXs = getUnconfirmedTransactionsFromNetwork ()            newBlock = getNewBlock (longestChain, currentTXs, nonce)            -- http://en.wikipedia.org/wiki/SHA-2 -- and this is what all the               "mining machines" are doing.          blockHash = sha256(newBlock)            if (meetReqirements (blockHash))          {              broadcast (newBlock)              -- Now the height the block chain is incremented by 1 --                 (if the new block is accepted by other peers),              -- and all the TXs in the new block are "confirmed"          }      }  }  ////////////////////////////////////////////////////// function sendBTC (amount)  {      sourceTXs = pickConfirmedTransactionsToBeSpent (amount)      tx = generateTX (sourceTXs, targetAddrs, amount, fee)      signedTx = sign (tx, privateKeysOfAllInputAddress)      broadcast (signedTx)  }  ///////////////////////////////////////////////////////////////

这是我的解释:

挖矿过程是不断地从比特币网络getUnconfirmedTransactionsFromNetwork()获取所有未确认的交易,打包成一个区块挂载到最长的区块链getNewBlock(longestChain, currentTXs, nonce),然后计算出该区块的哈希值新区块sha256(newBlock),如果哈希值刚好满足挖矿难度满足要求(blockHash),则挖矿成功。所谓挖矿难度是指所需二进制哈希值末尾的0个数比特币地址生成原理,哈希值是偶然产生的。除了精疲力尽,别无他法。需要的 0 越多,挖矿就越困难。更大。

支付过程是将一些已确认的交易余额作为发送地址pickConfirmedTransactionsToBeSpent(amount)比特币地址生成原理,然后根据目标地址支付一定的交易费用生成新的交易generateTX(sourceTXs, targetAddrs, amount, fee) ,并使用钱包私下将密钥对交易签名签名(tx, privateKeysOfAllInputAddress),然后广播出去。

发件人: