主页 > imtoken苹果下载官网 > 首页 > 下载imtoken > 了解比特币原理的程序
首页 > 下载imtoken > 了解比特币原理的程序
imtoken苹果下载官网 2023-08-18 05:12:02
自比特币走红以来,网络上对比特币的解读可以说是纷繁复杂。 但是对于程序员来说,比特币的原理是什么,最直接的方法就是直接看程序代码。 比特币代码再复杂也没关系,我找到了简洁的代码,也能更好地理解比特币。
以下节目改编自吴昊在知乎上的回答。
- 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)比特币 原理,如果哈希值刚好满足挖矿难度meetReqirements(blockHash),则挖矿成功。 所谓挖矿难度是指所需二进制哈希值末尾零的个数比特币 原理,是偶然产生的。 别无他法,只能让自己精疲力竭地研究比特币到底是什么。 需要的零越多,挖矿就越难。 大。
支付过程是取出一些已确认的交易余额作为发送地址pickConfirmedTransactionsToBeSpent(amount),然后根据目标地址支付一定的交易手续费生成新的交易generateTX(sourceTXs, targetAddrs, amount, fee ),并使用钱包私传密钥对交易签名sign(tx, privateKeysOfAllInputAddress),然后广播。
原文链接: