github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/rawdb/schema.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:35</date> 10 //</624450079624794112> 11 12 13 //包RAWDB包含低级别数据库访问器的集合。 14 package rawdb 15 16 import ( 17 "encoding/binary" 18 19 "github.com/ethereum/go-ethereum/common" 20 "github.com/ethereum/go-ethereum/metrics" 21 ) 22 23 //下面的字段定义低级数据库模式前缀。 24 var ( 25 //databaseverisionkey跟踪当前数据库版本。 26 databaseVerisionKey = []byte("DatabaseVersion") 27 28 //HeadHeaderKey跟踪最新的已知头散列。 29 headHeaderKey = []byte("LastHeader") 30 31 //headblockkey跟踪最新的已知完整块哈希。 32 headBlockKey = []byte("LastBlock") 33 34 //HeadFastBlockKey在快速同步期间跟踪最新的已知不完整块的哈希。 35 headFastBlockKey = []byte("LastFast") 36 37 //FastTrieProgressKey跟踪在快速同步期间导入的Trie条目数。 38 fastTrieProgressKey = []byte("TrieSync") 39 40 //数据项前缀(使用单字节避免混合数据类型,避免使用“i”,用于索引)。 41 headerPrefix = []byte("h") //headerPrefix+num(uint64 big endian)+hash->header 42 headerTDSuffix = []byte("t") //headerPrefix+num(uint64 big endian)+hash+headerTsuffix->td 43 headerHashSuffix = []byte("n") //headerPrefix+num(uint64 big endian)+headerHashSuffix->hash 44 headerNumberPrefix = []byte("H") //headerNumberPrefix+hash->num(uint64 big endian) 45 46 blockBodyPrefix = []byte("b") //blockbodyprefix+num(uint64 big endian)+hash->block body 47 blockReceiptsPrefix = []byte("r") //blockReceiptsPrefix+num(uint64 big endian)+hash->block receipts 48 49 txLookupPrefix = []byte("l") //txlookupprefix+hash->交易/收据查找元数据 50 bloomBitsPrefix = []byte("B") //bloombitsprefix+bit(uint16 big endian)+section(uint64 big endian)+hash->bloom位 51 52 preimagePrefix = []byte("secure-key-") //preimagePrefix + hash -> preimage 53 configPrefix = []byte("ethereum-config-") //数据库的配置前缀 54 55 //链索引前缀(使用'i`+单字节以避免混合数据类型)。 56 BloomBitsIndexPrefix = []byte("iB") //BloomBitsIndexPrefix是跟踪其进展的链表索引器的数据表。 57 58 preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) 59 preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) 60 ) 61 62 //txLookupEntry是一个位置元数据,用于帮助查找 63 //只给出散列值的交易或收据。 64 type TxLookupEntry struct { 65 BlockHash common.Hash 66 BlockIndex uint64 67 Index uint64 68 } 69 70 //encodeBlockNumber将块编号编码为big endian uint64 71 func encodeBlockNumber(number uint64) []byte { 72 enc := make([]byte, 8) 73 binary.BigEndian.PutUint64(enc, number) 74 return enc 75 } 76 77 //headerkey=headerprefix+num(uint64 big endian)+哈希 78 func headerKey(number uint64, hash common.Hash) []byte { 79 return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 80 } 81 82 //headertdkey=headerprefix+num(uint64 big endian)+hash+headertdsuffix 83 func headerTDKey(number uint64, hash common.Hash) []byte { 84 return append(headerKey(number, hash), headerTDSuffix...) 85 } 86 87 //headerhashkey=headerprefix+num(uint64 big endian)+headerhashsuffix 88 func headerHashKey(number uint64) []byte { 89 return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) 90 } 91 92 //headerNumberKey=headerNumberPrefix+hash 93 func headerNumberKey(hash common.Hash) []byte { 94 return append(headerNumberPrefix, hash.Bytes()...) 95 } 96 97 //blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash 98 func blockBodyKey(number uint64, hash common.Hash) []byte { 99 return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 100 } 101 102 //blockReceiptskey=blockReceiptsPrefix+num(uint64 big endian)+哈希 103 func blockReceiptsKey(number uint64, hash common.Hash) []byte { 104 return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 105 } 106 107 //txLookupKey=txLookupPrefix+哈希 108 func txLookupKey(hash common.Hash) []byte { 109 return append(txLookupPrefix, hash.Bytes()...) 110 } 111 112 //bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash 113 func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { 114 key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) 115 116 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 117 binary.BigEndian.PutUint64(key[3:], section) 118 119 return key 120 } 121 122 //preImageKey=preImagePrefix+哈希 123 func preimageKey(hash common.Hash) []byte { 124 return append(preimagePrefix, hash.Bytes()...) 125 } 126 127 //configkey=configPrefix+哈希 128 func configKey(hash common.Hash) []byte { 129 return append(configPrefix, hash.Bytes()...) 130 } 131