github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/rawdb/accessors_chain.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package rawdb
    26  
    27  import (
    28  	"bytes"
    29  	"encoding/binary"
    30  	"math/big"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/rlp"
    36  )
    37  
    38  //readCanonicalHash检索分配给规范块号的哈希。
    39  func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash {
    40  	data, _ := db.Get(headerHashKey(number))
    41  	if len(data) == 0 {
    42  		return common.Hash{}
    43  	}
    44  	return common.BytesToHash(data)
    45  }
    46  
    47  //WriteCanonicalHash存储分配给规范块号的哈希。
    48  func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) {
    49  	if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil {
    50  		log.Crit("Failed to store number to hash mapping", "err", err)
    51  	}
    52  }
    53  
    54  //DeleteConnicalHash删除到哈希规范映射的数字。
    55  func DeleteCanonicalHash(db DatabaseDeleter, number uint64) {
    56  	if err := db.Delete(headerHashKey(number)); err != nil {
    57  		log.Crit("Failed to delete number to hash mapping", "err", err)
    58  	}
    59  }
    60  
    61  //readheadernumber返回分配给哈希的头编号。
    62  func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 {
    63  	data, _ := db.Get(headerNumberKey(hash))
    64  	if len(data) != 8 {
    65  		return nil
    66  	}
    67  	number := binary.BigEndian.Uint64(data)
    68  	return &number
    69  }
    70  
    71  //readHeaderHash检索当前规范头的哈希。
    72  func ReadHeadHeaderHash(db DatabaseReader) common.Hash {
    73  	data, _ := db.Get(headHeaderKey)
    74  	if len(data) == 0 {
    75  		return common.Hash{}
    76  	}
    77  	return common.BytesToHash(data)
    78  }
    79  
    80  //WriteHeaderHash存储当前规范头的哈希。
    81  func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) {
    82  	if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
    83  		log.Crit("Failed to store last header's hash", "err", err)
    84  	}
    85  }
    86  
    87  //readheadblockhash检索当前规范头块的哈希。
    88  func ReadHeadBlockHash(db DatabaseReader) common.Hash {
    89  	data, _ := db.Get(headBlockKey)
    90  	if len(data) == 0 {
    91  		return common.Hash{}
    92  	}
    93  	return common.BytesToHash(data)
    94  }
    95  
    96  //WriteHeadBlockHash存储头块的哈希。
    97  func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) {
    98  	if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
    99  		log.Crit("Failed to store last block's hash", "err", err)
   100  	}
   101  }
   102  
   103  //readHeadFastBlockHash检索当前快速同步头块的哈希。
   104  func ReadHeadFastBlockHash(db DatabaseReader) common.Hash {
   105  	data, _ := db.Get(headFastBlockKey)
   106  	if len(data) == 0 {
   107  		return common.Hash{}
   108  	}
   109  	return common.BytesToHash(data)
   110  }
   111  
   112  //WriteHeadFastBlockHash存储当前快速同步头块的哈希。
   113  func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) {
   114  	if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {
   115  		log.Crit("Failed to store last fast block's hash", "err", err)
   116  	}
   117  }
   118  
   119  //readfasttriegrogress检索快速同步以允许的尝试次数节点
   120  //在重新启动时报告正确的数字。
   121  func ReadFastTrieProgress(db DatabaseReader) uint64 {
   122  	data, _ := db.Get(fastTrieProgressKey)
   123  	if len(data) == 0 {
   124  		return 0
   125  	}
   126  	return new(big.Int).SetBytes(data).Uint64()
   127  }
   128  
   129  //WriteFastTrieProgress存储要支持的快速同步Trie进程计数器
   130  //重新启动时检索。
   131  func WriteFastTrieProgress(db DatabaseWriter, count uint64) {
   132  	if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
   133  		log.Crit("Failed to store fast sync trie progress", "err", err)
   134  	}
   135  }
   136  
   137  //readheaderrlp以其原始RLP数据库编码检索块头。
   138  func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
   139  	data, _ := db.Get(headerKey(number, hash))
   140  	return data
   141  }
   142  
   143  //散列头验证与散列对应的块头是否存在。
   144  func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool {
   145  	if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
   146  		return false
   147  	}
   148  	return true
   149  }
   150  
   151  //readheader检索与哈希对应的块头。
   152  func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header {
   153  	data := ReadHeaderRLP(db, hash, number)
   154  	if len(data) == 0 {
   155  		return nil
   156  	}
   157  	header := new(types.Header)
   158  	if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
   159  		log.Error("Invalid block header RLP", "hash", hash, "err", err)
   160  		return nil
   161  	}
   162  	return header
   163  }
   164  
   165  //WriteHeader将一个块头存储到数据库中,还存储哈希-
   166  //到数字映射。
   167  func WriteHeader(db DatabaseWriter, header *types.Header) {
   168  //写入哈希->数字映射
   169  	var (
   170  		hash    = header.Hash()
   171  		number  = header.Number.Uint64()
   172  		encoded = encodeBlockNumber(number)
   173  	)
   174  	key := headerNumberKey(hash)
   175  	if err := db.Put(key, encoded); err != nil {
   176  		log.Crit("Failed to store hash to number mapping", "err", err)
   177  	}
   178  //写入编码的头
   179  	data, err := rlp.EncodeToBytes(header)
   180  	if err != nil {
   181  		log.Crit("Failed to RLP encode header", "err", err)
   182  	}
   183  	key = headerKey(number, hash)
   184  	if err := db.Put(key, data); err != nil {
   185  		log.Crit("Failed to store header", "err", err)
   186  	}
   187  }
   188  
   189  //DeleteHeader删除与哈希关联的所有块头数据。
   190  func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) {
   191  	if err := db.Delete(headerKey(number, hash)); err != nil {
   192  		log.Crit("Failed to delete header", "err", err)
   193  	}
   194  	if err := db.Delete(headerNumberKey(hash)); err != nil {
   195  		log.Crit("Failed to delete hash to number mapping", "err", err)
   196  	}
   197  }
   198  
   199  //readbodyrlp以rlp编码方式检索块体(事务和uncles)。
   200  func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
   201  	data, _ := db.Get(blockBodyKey(number, hash))
   202  	return data
   203  }
   204  
   205  //writebodyrlp将RLP编码的块体存储到数据库中。
   206  func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
   207  	if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
   208  		log.Crit("Failed to store block body", "err", err)
   209  	}
   210  }
   211  
   212  //hasbody验证哈希对应的块体的存在。
   213  func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool {
   214  	if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
   215  		return false
   216  	}
   217  	return true
   218  }
   219  
   220  //readbody检索与哈希相对应的块体。
   221  func ReadBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body {
   222  	data := ReadBodyRLP(db, hash, number)
   223  	if len(data) == 0 {
   224  		return nil
   225  	}
   226  	body := new(types.Body)
   227  	if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
   228  		log.Error("Invalid block body RLP", "hash", hash, "err", err)
   229  		return nil
   230  	}
   231  	return body
   232  }
   233  
   234  //WriteBody将块体存储到数据库中。
   235  func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.Body) {
   236  	data, err := rlp.EncodeToBytes(body)
   237  	if err != nil {
   238  		log.Crit("Failed to RLP encode body", "err", err)
   239  	}
   240  	WriteBodyRLP(db, hash, number, data)
   241  }
   242  
   243  //DeleteBody删除与哈希关联的所有块体数据。
   244  func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) {
   245  	if err := db.Delete(blockBodyKey(number, hash)); err != nil {
   246  		log.Crit("Failed to delete block body", "err", err)
   247  	}
   248  }
   249  
   250  //readtd检索与哈希相对应的块的总难度。
   251  func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int {
   252  	data, _ := db.Get(headerTDKey(number, hash))
   253  	if len(data) == 0 {
   254  		return nil
   255  	}
   256  	td := new(big.Int)
   257  	if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
   258  		log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
   259  		return nil
   260  	}
   261  	return td
   262  }
   263  
   264  //writetd将块的总难度存储到数据库中。
   265  func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) {
   266  	data, err := rlp.EncodeToBytes(td)
   267  	if err != nil {
   268  		log.Crit("Failed to RLP encode block total difficulty", "err", err)
   269  	}
   270  	if err := db.Put(headerTDKey(number, hash), data); err != nil {
   271  		log.Crit("Failed to store block total difficulty", "err", err)
   272  	}
   273  }
   274  
   275  //deletetd删除与哈希关联的所有块总难度数据。
   276  func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
   277  	if err := db.Delete(headerTDKey(number, hash)); err != nil {
   278  		log.Crit("Failed to delete block total difficulty", "err", err)
   279  	}
   280  }
   281  
   282  //readReceipts检索属于块的所有事务收据。
   283  func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts {
   284  //检索扁平收据切片
   285  	data, _ := db.Get(blockReceiptsKey(number, hash))
   286  	if len(data) == 0 {
   287  		return nil
   288  	}
   289  //将revcepts从其存储表单转换为内部表示形式
   290  	storageReceipts := []*types.ReceiptForStorage{}
   291  	if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
   292  		log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
   293  		return nil
   294  	}
   295  	receipts := make(types.Receipts, len(storageReceipts))
   296  	for i, receipt := range storageReceipts {
   297  		receipts[i] = (*types.Receipt)(receipt)
   298  	}
   299  	return receipts
   300  }
   301  
   302  //WriteReceipts存储属于某个块的所有交易记录收据。
   303  func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) {
   304  //将收据转换为其存储表单并将其序列化
   305  	storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
   306  	for i, receipt := range receipts {
   307  		storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
   308  	}
   309  	bytes, err := rlp.EncodeToBytes(storageReceipts)
   310  	if err != nil {
   311  		log.Crit("Failed to encode block receipts", "err", err)
   312  	}
   313  //存储扁平收据切片
   314  	if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
   315  		log.Crit("Failed to store block receipts", "err", err)
   316  	}
   317  }
   318  
   319  //删除收据删除与块哈希关联的所有收据数据。
   320  func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) {
   321  	if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
   322  		log.Crit("Failed to delete block receipts", "err", err)
   323  	}
   324  }
   325  
   326  //readblock检索与哈希相对应的整个块,对其进行组装
   327  //从存储的标题和正文返回。如果标题或正文不能
   328  //收回零。
   329  //
   330  //注意,由于头和块体同时下载,因此头和块体
   331  //规范散列可以存储在数据库中,但主体数据尚未存储。
   332  func ReadBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block {
   333  	header := ReadHeader(db, hash, number)
   334  	if header == nil {
   335  		return nil
   336  	}
   337  	body := ReadBody(db, hash, number)
   338  	if body == nil {
   339  		return nil
   340  	}
   341  	return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
   342  }
   343  
   344  //WriteBlock将块分别序列化到数据库、头和正文中。
   345  func WriteBlock(db DatabaseWriter, block *types.Block) {
   346  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   347  	WriteHeader(db, block.Header())
   348  }
   349  
   350  //删除块删除与哈希关联的所有块数据。
   351  func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) {
   352  	DeleteReceipts(db, hash, number)
   353  	DeleteHeader(db, hash, number)
   354  	DeleteBody(db, hash, number)
   355  	DeleteTd(db, hash, number)
   356  }
   357  
   358  //findcommonancestor返回两个块头的最后一个公共祖先
   359  func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header {
   360  	for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
   361  		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
   362  		if a == nil {
   363  			return nil
   364  		}
   365  	}
   366  	for an := a.Number.Uint64(); an < b.Number.Uint64(); {
   367  		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
   368  		if b == nil {
   369  			return nil
   370  		}
   371  	}
   372  	for a.Hash() != b.Hash() {
   373  		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
   374  		if a == nil {
   375  			return nil
   376  		}
   377  		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
   378  		if b == nil {
   379  			return nil
   380  		}
   381  	}
   382  	return a
   383  }