Mastering Bitcoin 2nd Edition Chapter 2: How Bitcoin Works

Mastering Bitcoin 2nd Edition Chapter 2: How Bitcoin Works

Bitcoin transactions

Terms

Transaction
Move value from transaction inputs to transaction outputs

将多个 bitcoin value 转移给多个outputs地址

Input: reference to a previous transaction’s output, showing where the value is coming from

Wallet里面的bitcoin都来自于之前的 transaction 的 output,使用的时候就是引用之前 outputs的地址

Output: directs a specific value to new owner’s bitcoin address, and can include a change output back to the original owner

Bitcoin value发送到新的拥有者的地址,同时一部分找零回到原有者的地址

Transaction as double-entry bookkeeping

Change
找零

与物理货币使用类似,当作为input的bitcoin value大于实际所需要的output的值,那么由于不能把一个bitcoin value拆开,因此output除了原来要求的值,还有一个新的bitcoin value返回给原有的账户,均使用新的地址

Transaction fee
The slight difference between input and outputs

It is collected by the miner as a fee for validating and including the transaction in a block to be recorded on the blockchain

Transaction output 后会扣除一定的 transcation fee,用于作为miner验证和将transaction加入block的奖励

“Spending”
sign a transaction that transfers value from a previous transaction over to a new owner identified by a bitcoin address 使用用户的 key 来确认一次transaction
Transaction chains
所有当前拥有者所拥有的bitcoin value 均来自于上一次 transaction的 output,这些 bitcoin value将会作为下一次 transaction 的 input

Transaction forms

  • Most common transaction

    最普通的 transaction,input的value 大于output,因此一部分转交给新的owner,一部分找零返回原来的地址

  • Transaction aggregating funds

    当有很多“零钱”的时候,凑齐(aggregate)很多的inputs转交给新的owner

  • Transaction distributing funds

    一份或多份inputs需要转交给多个owners,例如发工资(payroll)

Constructing a Transaction

Getting the Right Inputs:

Most wallets keep track of all the available outputs belonging to addresses in the wallet

Wallet application queries the bitcoin network to retrieve this information

For example, use HTTP GET command to a specific URL to return all the unspent transaction outputs for an address

Wallet application can construct transactions even if it is completely offline

关于Transaction的工作完全是由用户选择的wallet application来完成的,那么在获取正确的inputs的时候

如果用户选择的wallet是一个full node,那么wallet就会直接去查询bitcoin network 中的内容;

如果用户选择的是一个lightweight 的wallet,那么它只会追踪用户个人的unspent outputs

然后进行转账,这时候不需要网络也可以进行,就像在家里写了一张支票,可以稍后再交给银行一样,这里在本地离线写好了transaction的inputs和outputs,随后再交给bitcoin network 也可以

Creating outputs

“A transaction output is created in the form of a script that creates an encumbrance on the value and can only be redeemed by the introduction of a solution to the script.”

“Alice’s transaction output will contain a script that says something like, “This output is payable to whoever can present a signature from the key corresponding to Bob’s public address.” Because only Bob has the wallet with the keys corresponding to that address, only Bob’s wallet can present such a signature to redeem this output. Alice will therefore “encumber” the output value with a demand for a signature from Bob.”

创建outputs:

Alice 创建一个脚本,这个脚本会实现:将一部分的inputs抵押在bitcoin network 上,随后等待Bob 来赎回。那么赎回就需要Bob 提供他对应的key来进行签名,只要签名正确就可以赎回这部分的 bitcoin value

同时transaction 会产生transaction fee 用来支付 miner 对transaction进行验证和记账的奖励,这个 trasaction fee 通常会在找零 change 里扣除

Bitcoin Mining

记账

转发交易:最终的目的是将每一次交易都记录在block chain 上面

所有参与到bitcoin network 中的终端都叫做bitcoin node 他们都通过bitcoin protocol 来进行交流

当Alice的wallet 将一个transaction 发送到另一个bitcoin node 的时候,每个节点都会验证这个transaction 是否属于自己,因为他们都可以用自己的key来验证,直到传递到Bob

当一个bitcoin node 收到了之前没有见过的transaction 的时候,他们就会做flooding 来确保大部分的节点都能收到这个transaction

这里有一个常见的误区就是,Bob 需要等待10min或者60min的时间,等待transaction被记账之后,才能完成咖啡的交付,实际上只需要几秒钟的时间,Bob 所拥有的 bitcoin node 就会得知有一个属于他的待赎回的outputs, 这已经是十分可靠的了

挖矿

矿工负担高性能的机器以及运行所需要的电费来进行挖矿,主要的工作是验证 Proof-of-Work

“The algorithm for Proof-of-Work involves repeatedly hashing the header of the block and a random number with the SHA256 cryptographic algorithm until a solution matching a predetermined pattern emerges.”

矿工之前都可以互相合作,通过 mining pool

每次当bitcoin network 中有bitcoin node 提交一个新的transaction ,这些transaction就会被放到pool of unverified transactions 当中。

每个矿工开始挖矿的目的都是为了创造一个属于自己的block, 然后将这个block 添加到blockchain 上面去

因此他们会首先创建一个 candidate block, 然后开始验证所有当前存在于 pool of unverified 的 transactions, 同时他们会添加一条新的transaction which 指向他们的地址,这个transaction 所包含的bitcoin value 就是一个新的block 的奖励以及所有的transaction fee ,相当于是银行印了新的钱还有所有转账的手续费,当他们谁第一个完成了 proof of work algorithm的计算,就可以将他们所创造的 candidate block 添加到block chain 的末尾去成为新的block,其他人验证了其正确性之后,就会转而创建新的candidate block来计算新的block

一个很好的比喻就是proof of work 就是一个大型的数独游戏,通过增加行和列的大小可以很容易的增加难度,但是验证起来却十分容易

同时除了验证pool of unverified transactions 之外,还需要验证以前的transactions ,也就是说计算量会越来越大,之前的transaction 也会变得越来越可靠,一般认为存在于6个blocks的 transaction就已经是不可逆的了

comments powered by Disqus
Cogito, ergo sum
Built with Hugo
Theme Stack designed by Jimmy