Mastering Bitcoin(掌握比特币)读书笔记

Mastering Bitcoin

目录

介绍

比特币如何工作

  1. miner(矿工)约每10分钟产生一个block,confirm所有事务(?),同时获得25BTC奖励

比特币Client

  1. $ bitcoin-cli getaddressesbyaccount ""
  2. ... after confirmation, the txid is immutable and authoritative
  3. $ bitcoin-cli decoderawtransaction ... //vin/vout?
    1. asm字段是什么意思?
  4. block的height:genesis块为0
  5. scriptSig:转账事务的签名(by signrawtransaction)
    1. 自己给自己转账?
  6. sendrawtransaction:向比特币网络提交事务,返回一个txid(这个id又是谁生成的?)
  7. 其他的Clients、Libs and Toolkits
    1. libbitcoin与sx工具
    2. pycoin
    3. btcd(Go语言实现,不包含wallet,后者由btcwallet/btcgui提供)

密钥、地址、钱包

  1. 密钥:256位,~= 10^77(对比:可见宇宙包含10^80原子)
    1. dumpprivkey:Base58 checksum-encoded WIF
  2. 公钥:K = k * G,ECC,G:generator point
    1. ECC:secp256k1标准?(这里居然解释了一下ECC,晕)
  3. 比特币地址:“1”开始
    1. 公钥 --> SHA256 --> RIPEMD160 --> Base58Check?
      1. Base58:Base64,without 0,O,l,I,+,/
      2. Base58Check:checksum = SHA256(SHA256(prefix+data)),取前4个字节
    2. 关于不同类型地址的‘前缀’:
      1. bitcoin:1
      2. Pay-to-Script Hash:3
      3. Bitcoin Testnet:m/n
      4. Private Key WIF:5/K/L
      5. BIP38加密的私钥:6P
      6. BIP32扩展的公钥:xpub
  4. 压缩的公钥
    1. 如果知道x,则可以通过y^2 mod p = (x^3 +7) mod p解出y
    2. 没有压缩的前缀是04,压缩的02或03(y有正负2个取值,在模p有限域上有奇偶2个取值)
    3. 靠,还不是所有的客户端都支持?这说明当初的设计也太粗糙了点
    4. Ironically,WIF-压缩的私钥比无压缩的版本多一个字节,这是因为它添加了01后缀
  5. 钱包:私钥的容器
    1. 第一版 Type-0 非确定式(私钥是随机生成的,——因此理论上可能有冲突?)
      1. 缺点:必须经常备份?
    2. 确定式(seeded)
      1. 我怎么感觉这种更容易被追踪?因为假如seed可以被定位的话
  6. Mnemonic Code Words:代表一随机数种子,#see BIP0039
    1. 只是个草稿建议,不是规范
  7. 层次式的确定式钱包(BIP0032/0044)
    1. 可以映射到一个组织机构???
    2. root seed --> HMAC-SHA512 --> master private key(m) and master chain code
      1. 导出Private child key(CKD)
      2. 我个人觉得HD钱包其实并不安全,它的安全性建立在别人不知道parent key以及一路哈希计算的不可逆上
    3. Extended keys:可导出children key
    4. 从Public parent key直接导出public child key,不需要私钥(?扩展的public key包含了chain code)
    5. 风险:
      1. 一个泄漏的child private key可以导出全部其他的child private keys
      2. 一个泄漏的child private key再加上parent chain code可以导出parent private key
    6. ==> Hardend CKD
      1. 使用parent private key导出child chain code,而不是从parent public key(利用一路hash的不可逆性)
      2. 缺点?
      3. 最佳实践:第1级children都通过加固的CKD导出?
    7. HD path表示(略)
  8. Advanced Keys and Addresses
  9. Pay-to-Script Hash(P2SH)与Multi-Sig地址(BIP0016)
    1. A P2SH地址从一个transaction script创建,定义了谁可以spend a transaction output(有主的钱包?)
      1. 注意,向P2SH地址支付需要多方参与!
    2. 多签名地址与P2SH:M-of-N
  10. Vanity地址(包含了人类可读的信息)
    1. 由于SHA256,找到这么个地址并不容易(取决于子模式的长度)
  11. Paper Wallets:印在纸上的私钥?
    1. BIP0038增强
    2. 一旦收到支付后,只能一次性用光,或者转移到另一个新的Paper Wallet(这让我想到了现在的礼品卡了)

事务(交易)

  1. 由funds的拥有者签名
  2. 不需要认证发送者的身份(直接转发即可,直到被miner放到blockchain中)
  3. 数据结构
    1. 版本?
    2. input、ouput
    3. locktime?
  4. UTXO(没有balance的概念,有点类似于数据库里的‘物化视图’的概念)
    1. 不是实物,不可切割;input等于output;“change”
    2. Transactions consume UTXO by unlocking it with the signature of the current owner,
      and create UTXO by locking it to the bitcoin address of the new owner.
  5. coinbase事务:block中的第一个transaction,由挖矿竞争胜利的矿工创建,支付给他自己
  6. Transaction outputs(UTXO):
    1. An amount of bitcoin, denominated in satoshis(1/100000000 BTC)
    2. A locking script, 指定谁才能接受这个UTXO
    3. ... requests.get('https://blockchain.info/unspent?active=%s' % address)
  7. Transaction Inputs
    1. In simple terms, transaction inputs are pointers to UTXO.
    2. unlocking script:通常是拥有者对其比特币地址的签名
  8. 事务费用
    1. Transaction fees are calculated based on the size of the transaction in kilobytes, not the value of the transaction in bitcoin.
    2. 隐含的
  9. 事务链结与孤儿事务
    1. orphan transaction pool:临时存放引用了parent,但parent还未知的child事务
  10. Transaction Scripts
    1. 类似与Forth?
      1. DUP CHECKSIG OP_CHECKMULTISIG ...
    2. locking:scriptPubKey;unlock:scriptSig
      1. 原来是顺序执行的,有安全漏洞,...
      2. 现在,executed separately with the stack transferred between the two executions
    3. the Script:
      1. Turing Incompleteness:无循环,复杂的flow control(只有条件分支)
      2. 无状态的(?)
  11. 5种标准事务:
    1. pay-to-public-key-hash (P2PKH),
    2. public-key,
    3. multi-signature (limited to 15 keys),
    4. pay-to-script-hash (P2SH),
      1. p132 2 <Mohammed's Public Key> <Partner1 Public Key> <Partner2 Public Key> <Partner3 Public Key> <Attorney Public Key> 5 OP_CHECKMULTISIG
      2. With P2SH payments, the complex locking script is replaced with its digital fingerprint, a cryptographic hash.
      3. ==> redeem script(为了节省blockchain的存储!)
        1. 即使redeem script有可能是invalid,P2SH本身仍然会被接受(???)
      4. 备注:Script Hash有点类似于我设想的‘兴趣路由’
    5. data output (OP_RETURN):用于证明某样东西在指定日期起已经存在?滥用??
      1. 这个增加了其他存储blockchain的client的负担。。。(不过,可以用于私有链?)
      2. 更糟糕的是,使用20byte的虚假目标地址来存储信息,导致unspent UTXO,溢出节点的临时内存池...
      3. OP_RETURN:创建了一个provably unspendable的output,这增加了块链的大小,但不会bloat内存池(compromise?版本0.9+)
        1. 40bytes:32 byte的SHA256,加上一个DOCPROOF前缀?

比特币网络

  1. Stratum网关代理?
  2. 网络发现
    1. TCP 8333端口?
    2. BestHeight:节点当前的块链高度
  3. seed nodes
  4. 消息:inventory, getblocks,getdata
  5. SPV节点(瘦客户端)
    1. SPV nodes download only the block headers and do not download the transactions included in each block.
      1. 需要用到时再向peer请求?
    2. The SPV node establishes the existence of a transaction in a block by requesting a merkle path proof and by validating the proof of work in the chain of blocks.
      1. 检查事务所在的block是否被6个以上的新block所引用,且其中事务unspent?
        1. 注意这里的事务一般是SPV节点钱包中已知的比特币地址
        2. ==> 隐私风险!#see 'Bloom Filters'
    3. 可以证明存在,但是不能verify它的unspent状态 ==> double-spending attack
      1. 随机连接到多个其他nodes?期望其中至少有一个honest节点
        1. ==> SPV nodes also are vulnerable to network partitioning attacks or Sybil attacks
    4. getheaders消息
  6. Bloom Filters(这让我想起了密码学里的零知识证明)
    1. offer an efficient way to express a search pattern(搜索模式) while protecting privacy.
    2. filterload消息
      1. 交互式的filteradd
  7. Transaction Pools
  8. Alert Messages

块链

  1. linked-back list
    1. “previous block hash”(块链概念的精髓所在)
  2. metadata in LevelDB
  3. Although a block has just one parent, it can temporarily have multiple children. (Fork)
    1. when different blocks are discovered almost simultaneously by different miners
  4. 80个字节的Block Header
    1. The second set of metadata, namely the difficulty, timestamp, and nonce,(挖矿竞争)
    2. Merkle树根
  5. Block Identifiers: Block Header Hash(2次SHA256) and Block Height
    1. 例如,第一个块头部的hash:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
  6. The Genesis Block
  7. Linking Blocks in the Blockchain
    1. 检查新block的previousblockhash域
  8. Merkle Trees
    1. double-SHA256
    2. 技术细节:如果奇数个事务,最后一个重复!
    3. ?证明某个transaction在指定block中(仅有根hash怎么做?by retrieving a small merkle path?)
    4. 与SPV
      1. peer的merkleblock消息(包含了tranaction所在的block头部,以及merkle path)——哦,需要‘计算’来验证!

挖矿与Consensus

  1. miner:构造一个candidate block(候选块)
  2. 事务优先级:age,fees(size)
    1. 高优先级:>57600000
    2. 初始的50KB事务空间,regardless of fee?(注意,矿工本身挖到矿有25BTC奖励)
  3. 注意:这里的blockchain确认过程与数据库的WAL日志有点类似之处
  4. generation/coinbase事务:不消耗UTXO,creates bitcoin from nothing
    1. out value = 25BTC + 块内所有事务的手续费
      1. reward计算:初始50BTC,每210000块减半,当前是25BTC,但2016年某个时候就会变成12.5BTC
    2. input:32B Transaction Hash=0(不代表UTXO引用), 4B Output Index=-1, 2~100B Coinbase Data(可被miner随意使用), 4B Sequence Number=-1
      1. BIP0034,version-2 blocks MUST contain the height index as a script "push"
        1. 以#277316为例,03(push)-443b04(小端编码的height index)-03858402062(extra nonce)-2f503253482f(‘/P2SH/’,#BIP0016)
          1. BIP0017:‘p2sh/CHV’
  5. 构造块头部
    1. 4B Version
    2. 32B Prev Block Hash
    3. 32B Merkle Root
    4. 4B Timestamp
    5. 4B Diffiulty Target(mantissa指数编码?)
      1. 以#277316为例,0x1903a30c,19是指数,3a30c是系数
      2. target = coefficient * 2 ^ (8 * (exponent-3)),大约相当于前缀60bit=0
    6. 4B Nonce(用于Proof-of-Work算法的计数器)
  6. 头部构造完之后,可以开始‘挖矿’:寻找一个Nonce值(最多尝试2^32次),使得头部Hash<difficulty target(?)
  7. Proof-of-Work算法
    1. 先解释了一下SHA256的特性...
    2. 降低target(这意味着要求SHA256输出结果有更多的前缀0),则难度增加
    3. 备注:所谓的比特币挖矿专用ASIC其实就是SHA256的硬件实现?
    4. ... 幸运的是,整个网络的处理能力是100 petahashes per second(PH/sec)
  8. Difficulty retargeting
    1. 网络必须保证每10分钟产生一个block,不多也不少?
    2. 自动地,每2016个块:
      1. 度量最近的2016个块的产生时间,与20160分钟比较,比率用来调整Difficulty
  9. 啊,太神奇啦
  10. Validating a New Block
    1. CheckBlock(对比之前针对Transaction的CheckInputs)
  11. main chain:具有最高的累积困难度,在大多数情况下也是拥有最多块的链
    1. branch chain可以临时保留,如果它将来的累积困难度超过了main chain
    2. orphan block
    3. 最终的network-wide consensus
    4. ?Mining nodes “vote” with their power by choosing which chain to extend(感觉这里可能是个漏洞?因为涉及到分布式系统的一致性选举...)
  12. Blockchain Forks
    1. Forks are almost always resolved within one block.
    2. (理论上可能)一个Fork扩展到2个blocks,——然而发生的可能性很小!
  13. Extra Nonce
    1. 遍历所有Nonce都没有找到?增加Timestamp!==>使用Coinbase事务的额外空间
  14. Mining Pools:这在技术上意味着需要拆分Nonce的遍历空间?
    1. set an easier target?
    2. Managed Pools
    3. P2Pool:share chain(一个困难度小于比特币的块链)?
    4. 51% attack问题
  15. Consensus Attacks
    1. 只能影响最近的块,导致针对future blocks创建的DoS破坏
    2. A 51% attack允许double-spend in the new chain...
      1. To prevent,大笔支付必须等待至少6个确认
    3. 对于特定事务的DoS攻击(只需忽略这些事务即可)

其他链、币与应用

  1. Meta Coins:implemented on top of bitcoin
    1. Colored Coins:同时也作为其他资产(股份?)的标记
    2. Mastercoin:?
      1. “exodus”address (1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P)
    3. Counterparty:都使用OP_RETURN来编码metadata?
  2. Alt Coins:从比特币源代码基础上定制修改的
    1. IXCoin:降低了难度,并把奖励提高到96BTC?
    2. Tenebrix:另一个PoW算法scrypt,内存密集,用于抵抗GPU/ASIC挖矿?Litecoin的基础
    3. Litecoin:更快的块生成时间:10m -> 2.5m
      1. touted as “silver to bitcoin's gold”
      2. 囧:Creating an alt coin is easy, which is why there are now more than 500 of them
    4. Evaluating an Alt Coin
    5. Monetary Parameter Alternatives: Litecoin, Dogecoin, Freicoin
      1. Litecoin
        1. Consensus algorithm: Scrypt proof of work
      2. Dogecoin:Litecoin的修改版
        1. 块生成时间:60s?靠
      3. Freicoin:demurrage currency;鼓励消费,不鼓励守财奴?通货膨胀??
    6. Consensus Innovation: Peercoin, Myriad, Blackcoin, Vericoin, NXT
      1. Proof of stake is a system by which existing owners of a currency can “stake” currency as interest-bearing collateral. (存钱能收利息???囧)
      2. Peercoin
      3. Myriad:同时使用5个不同的PoW:SHA256d, Scrypt, Qubit, Skein, or Myriad-Groestl(疯了,有这个必要吗)
        1. 抵御ASIC挖矿???
      4. Blackcoin:introducing “multipools”
      5. VeriCoin:可变利率???
      6. NXT:单独的实现,非比特币fork,2.0 cryptocurrency?
    7. Dual-Purpose Mining Innovation: Primecoin, Curecoin, Gridcoin
      1. 比特币的PoW被批评为‘wasteful’??
      2. Primecoin
        1. PoW:computing Cunningham and bi-twin prime chains?
      3. Curecoin
        1. PoW:protein-folding research through the [email protected] project
      4. Gridcoin:Proof-of-work with BOINC grid computing subsidy
    8. Anonymity-Focused Alt Coins:
      1. Zerocash/Zerocoin:理论研究,not released yet
      2. CryptoNote
        1. has a built-in periodic reset mechanism that makes it unusable as a currency itself ?
      3. Bytecoin(BCN)
      4. Monero
      5. Darkcoin
        1. 使用11轮不同的hash函数:blake, bmw, groestl, jh, keccak, skein, luffa, cubehash, shavite, simd, echo
    9. Noncurrency Alt Chains
      1. Namecoin
        1. 名字空间:d/用于.bit域名,id/用于PGP,u/
          1. Namecoin registrations need to be updated every 36,000 blocks,更新没有费用?
      2. Bitmessage:a server-less encrypted email system
      3. Ethereum
        1. a Turing-complete contract processing and execution platform based on a blockchain ledger
          1. ‘图灵完全’的合同契约,什么鬼
        2. 完全独立的设计/实现?
        3. contract:运行在每一个node上,acting as decentralized autonomous software agents
        4. Namecoin可用Ethereum来实现?

安全

  1. decentralization
  2. 用户安全最佳实践
    1. ?Holding bitcoin on a computer serves to focus the user's mind on the need for improved computer security.
    2. Physical Bitcoin Storage
    3. Hardware Wallets:bitcoin key的加密存储太复杂,而最终又丢失了master key,.... 蠢货
    4. Diversifying Risk
    5. Multi-sig and Governance
    6. Survivability

A Transaction Script Language Operators, Constants, and Symbols

B BIP

C pycoin, ku, and tx

D Available Commands with sx Tools

阅读更多

更多精彩内容