ビットコインが炎上して以来、インターネットはビットコインに関する複雑な説明で溢れています。しかしプログラマーにとって、コードを見ること以上にビットコインを理解する方法はありません。コードが気に入らなくても大丈夫です。ビットコインを理解するのに最適な、短くてシンプルなコードを見つけました。
この手順は以下から転載したものです。
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://..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(blockHandle)を満たすのに十分である場合。その後、新しいブロックのハッシュ値sha256(newBlock)を計算し、ハッシュ値が正確に採掘難易度meetReqirements(blockHash)を満たしている場合は、マイニング成功。いわゆる採掘難易度は、必要なバイナリハッシュ値の末尾に0の数を指し、ハッシュ値は、より多くの0を必要とする他の方法の枯渇に加えて、偶然に生成されます採掘の難易度が高いほど。
決済プロセスは、残高のある確認済みのトランザクションをいくつか受け取り、それを送信アドレスpickConfirmedTransactionsToBeSpent(amount)として使用し、ターゲットアドレスに基づいて一定の取引手数料を支払って新しいトランザクションを生成generateTX(sourceTXs, targetAddrs, amountfee)、そしてトランザクションに署名するためにウォレットの秘密鍵で sign(tx, privateKeysOfAllInputAddress)、そしてそれをブロードキャストします。