class: center, middle, inverse, title-slide # 区块链与加密数字货币 ## 简介 ### 吴燕丰 ### 江西财大, 金融学院 ### 2021/03/31 --- ### 比特币(Bitcoin, BTC)₿ 最近一个月价格走势  --- ### 比特币(Bitcoin, BTC)₿ 最近一年价格走势  --- ### 比特币与区块链 **比特币**是一种去中心化的数字货币,具有如下创新: - 一个去中心化的点对点网络(比特币协议) - 一本公开的交易总账(**区块链**) - 一套可独立校验交易和发行货币的规则(共识规则) - 一种通过全球去中心化对有效的区块链达成共识的机制(工作量证明算法) .footnote[ 比特币网站(中文版):[https://bitcoin.org/zh_CN/](https://bitcoin.org/zh_CN/) ] --- ### 情景--Alice向朋友Joe兑换₿0.1 .pull-left[ Alice --> Joe : $10  ] .pull-right[ Alice <-- Joe: ₿0.1  ] --- ### 情景--Alice从Bob店里购买一杯咖啡 .pull-left[ Bob 咖啡店  总计: - $1.5 - ₿0.015 Bob要求Alice支付1.5美元或者15毫比特币 ] .pull-right[ **比特币**收款二维码(₿)  <br> Alice通过扫描支付给Bob₿0.015 Alice --> Bob : ₿0.015 ] --- ### 情景--交易链  --- ### 情景--交易链细节  --- ### 情景--交易链细节  --- ### 公钥密码学  - 一把**私钥**对应一把**公钥** - 私钥**自己持有**,公钥**提供给他人** - 私钥用来**生成**签名(交易不可撤销) - 公钥用来**验证**签名(交易可被验证) --- ### 分布式记账(区块链 = 账簿) .center[] --- ### 挖矿(Mining) **Mining** is the mechanism that underpins the decentralized clearinghouse(清算所), by which transactions are validated and cleared. **Miners** receive two types of rewards: - **new coins** created with each new block, also known as a block reward or coinbase reward, - **transaction fees** from all the transactions included in the block. To earn the reward, miners compete to **solve a difficult mathematical problem** based on a **cryptographic hash algorithm(密码学哈希算法)**. The solution to the problem, called the **Proof-of-Work**(工作证明), is included in the new block and acts as proof that the miner expended significant computing effort. The competition to solve the Proof-of-Work algorithm to earn the reward and the right to record transactions on the blockchain is the basis for bitcoin’s security model. --- ### 挖矿(续) **Mining** is the invention that makes bitcoin special, a decentralized security mechanism that is the basis for P2P digital cash. >The purpose of mining is not the creation of new bitcoin. That’s the incentive system. Mining is the mechanism by which bitcoin’s security is decentralized. A new block, containing transactions that occurred since the last block, is "mined" every 10 minutes on average, thereby adding those transactions to the blockchain. 比特币发现速度: - started at 50 bitcoin per block in January of 2009 - halved to 25 bitcoin per block in November of 2012 - halved to 12.5 bitcoin in July 2016 - again to 6.25 bitcoin in May 2020 - After 2140, no new bitcoin will be issued --- ### 挖矿(续) .center[  ] --- ### 情景--交易被添加到区块 .center[] --- ### 区块链--内容结构 .center[] --- ### Merkle Tree The next step is to summarize all the transactions with a merkle tree, in order to add the merkle root to the block header. .center[ <img width=400 src='./images/merkle_tree.png'> ] The root of the merkle tree summarizes all the transactions into a single 32-byte value, such as in block 277,316, >c91c008c26e50763e9f548bb8b2fc323735f73577effbc55502c51eb4cc7cf2e --- ### 区块生成时间(Unix "epoch" timestamp) The mining node will then add a **4-byte timestamp**, encoded as a Unix "epoch" timestamp, which is based on the number of seconds elapsed since midnight UTC, Thursday, January 1, 1970. .footnote[ Unix "epoch"(Unix纪元): midnight UTC, Thursday, January 1, 1970. Such as, the time 1388185914 is equal to Friday, December 27, 2013, 23:11:54 UTC. ] --- ### 生成难度(Proof-of-Work target) In fact, the **Proof-of-Work target** is a dynamic parameter that is periodically adjusted to meet a 10-minute block interval goal. In simple terms, the target is set so that the current mining power will result in a 10-minute block interval. The node then fills in the **target**(生成难度), which defines the required Proof-of-Work to make this a valid block. The target is stored in the block as a "target bits" metric, which is a mantissa-exponent encoding of the target. In block 277,316, for example, the target bits value is 0x1903a30c: - The first part 0x19 is a hexadecimal(十六进制) **exponent**, - while the next part, 0x03a30c, is the **coefficient**. $$ \text{target} = \text{coefficient}\times 2^{[8\times (\text{exponent}-3)]} $$ .footnote[ mantissa: 尾数,见浮点数表示法,1字节(byte)for exponent,3字节(byte)for mantissa ] --- ### byte and bit (字节和位) 1 byte = 8 bits - 1 bit,可以表示两种状态,比如用0和1表示 - 2 bits,有四种状态组合,可表示数字0,1,2,3 - 3 bits,有8种状态组合,可表示数字0,1,2,3,4,5,6,7(八进制数) - 4 bits,有16种状态组合,可表示 `\(0,1,\cdots,9,a,b,c,d,e,f\)`(十六进制数) 所以,1 byte 可表示2位的十六进制数。用文本表示十六进制数时,前面会加上‘0x’, - 0x19 = 十六进制19 = `\(1\times16^1+9\times16^0=(\text{十进制})25\)` --- ### 生成难度(Proof-of-Work target) Retargeting the Proof-of-Work Retargeting to Adjust Difficulty ``` New Target = Old Target * (Actual Time of Last 2016 Blocks / 20160 minutes) ``` --- ### nouce and Mining the Block The final field is the nonce, which is initialized to zero. With all the other fields filled, the block header is now complete and the process of mining can begin. The goal is now to **find a value for the nonce that results in a block header hash that is equal to or less than the target.** The mining node will need to test billions or trillions of nonce values before a nonce is found that satisfies the requirement. The hash function **SHA256** is the function used in bitcoin’s mining process. In the simplest terms, mining is the process of hashing the block header repeatedly, changing one parameter, until the resulting hash matches a specific target. --- ### Proof-of-Work Algorithm A hash algorithm - takes an arbitrary-length data input and - produces a fixed-length deterministic result, a digital fingerprint of the input. For any specific input, the resulting hash - will always be the same and - can be easily calculated and - verified by anyone implementing the same hash algorithm. The key characteristic of a cryptographic hash algorithm is that it is computationally infeasible to find two different inputs that produce the same fingerprint (known as a collision(碰撞)). It is also virtually impossible to select an input in such a way as to produce a desired fingerprint, other than trying random inputs. --- ### SHA256 ```python import hashlib hashlib.sha256(b"I am Satoshi Nakamoto").hexdigest() ``` ``` ## '5d7c7ba21cbbcd75d14800b100252d5b428e5b1213d27c385bc141ca6b47989e' ``` This **256-bit** number is the hash or digest of the phrase and depends on every part of the phrase. Adding a single letter, punctuation mark, or any other character will produce a different hash. `\(2^{256}\)` --- ### SHA256 Iterating on a nonce: ```python hashlib.sha256(b"I am Satoshi Nakamoto01").hexdigest() ``` ``` ## 'c400964fa9aa5e7fb958eaf17a62adbbc823eb5d631844adab0e7cbd1655d054' ``` ```python hashlib.sha256(b"I am Satoshi Nakamoto02").hexdigest() ``` ``` ## 'b89b6b364213b7818978e412cc63765e71204ef4b4f6b2725c1e858b136fd987' ``` ```python hashlib.sha256(b"I am Satoshi Nakamoto03").hexdigest() ``` ``` ## '9354214dfadc734b9c58af0289c40111a29481fd177878561ce70c72ae9949d8' ``` ```python hashlib.sha256(b"I am Satoshi Nakamoto04").hexdigest() ``` ``` ## 'c1d79cabcae83441a41667234b9a4a333a9b0c0174bf17ff3410f106d1cffe0c' ``` By incrementing the number(**nonce**), we can get different hashes. -- Each phrase produces a completely different hash result. They seem completely random, but you can reproduce the exact results in this example on any computer with Python and see the same exact hashes. --- ### Nonce and Proof-of-Work The number used as a variable in such a scenario is called a nonce. The nonce is used to vary the output of a cryptographic function, in this case to vary the SHA256 fingerprint of the phrase. **Proof-of-Work** The Proof-of-Work must produce a hash that is equal to or less than the target. - A higher target means it is less difficult to find a hash that is equal to or below the target. - A lower target means it is more difficult to find a hash equal to or below the target. The target and difficulty are inversely related. --- ### 比特币交易--复式记账簿式交易 .center[] ??? .center[] --- ### 比特币网络(一种P2P网络) .pull-left[ - 标准客服端节点  ] .pull-right[ - 钱包(简易)节点  ] - 全节点:包含全部区块链数据,作为路由节点 - 独立矿工:包含挖矿程序 + 全部区块链数据 - 矿池协议服务器、挖矿节点、轻Stratum钱包 --- 比特币网络(一种P2P网络) .center[] ??? #### 比特币网络(一种P2P网络) .center[] --- ### 比特币网络特征 比特币网络,是一种P2P(Peer-to-Peer)网络,网络网络节点特征: - 具有本地账本,也即完整的区块链 - 自动与相连接的点,同步**交易**以及**新的区块** - 独立验证交易,以及区块 - 基于自利的原则挖矿,将新的交易打包形成区块,添加到区块链上 --- ### 区块链(账簿)的同步--挖矿 挖矿: - 验证每笔新的交易,打包成**区块**,添加至总账簿(**区块链**) - 平均每10分钟有一个新的区块被挖掘出来 - 矿工之间竞争挖矿,谁先挖到,谁得**新币**和**交易费用** - 耗时耗电,没有捷径,难以造假 --- #### 区块链的分叉与收敛同步 .center[] --- #### 两个区块被同时发掘时,账本的同步过程 .center[] --- #### 区块链两种分支(两个版本账簿) .center[] --- #### 新发掘的区块延长了其中一个分支 .center[] --- #### P2P网络收敛到同一个最长的区块链 .center[] --- ### 区块链(账簿)同步的好处--去中心化 保证去中心化的同时,交易被信任,即使交易双方彼此可能不信任。 --- ### 拓展介绍 - [以太坊Ethereum](../Ethereum/Ethereum.html) - [区块链应用案例](../Ethereum/Ethereum.html#8)