odr_util.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package light
  17. import (
  18. "bytes"
  19. "context"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. var sha3Nil = crypto.Keccak256Hash(nil)
  27. func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
  28. db := odr.Database()
  29. hash := rawdb.ReadCanonicalHash(db, number)
  30. if (hash != common.Hash{}) {
  31. // if there is a canonical hash, there is a header too
  32. header := rawdb.ReadHeader(db, hash, number)
  33. if header == nil {
  34. panic("Canonical hash present but header not found")
  35. }
  36. return header, nil
  37. }
  38. var (
  39. chtCount, sectionHeadNum uint64
  40. sectionHead common.Hash
  41. )
  42. if odr.ChtIndexer() != nil {
  43. chtCount, sectionHeadNum, sectionHead = odr.ChtIndexer().Sections()
  44. canonicalHash := rawdb.ReadCanonicalHash(db, sectionHeadNum)
  45. // if the CHT was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  46. for chtCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  47. chtCount--
  48. if chtCount > 0 {
  49. sectionHeadNum = chtCount*odr.IndexerConfig().ChtSize - 1
  50. sectionHead = odr.ChtIndexer().SectionHead(chtCount - 1)
  51. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  52. }
  53. }
  54. }
  55. if number >= chtCount*odr.IndexerConfig().ChtSize {
  56. return nil, ErrNoTrustedCht
  57. }
  58. r := &ChtRequest{ChtRoot: GetChtRoot(db, chtCount-1, sectionHead), ChtNum: chtCount - 1, BlockNum: number, Config: odr.IndexerConfig()}
  59. if err := odr.Retrieve(ctx, r); err != nil {
  60. return nil, err
  61. }
  62. return r.Header, nil
  63. }
  64. func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) {
  65. hash := rawdb.ReadCanonicalHash(odr.Database(), number)
  66. if (hash != common.Hash{}) {
  67. return hash, nil
  68. }
  69. header, err := GetHeaderByNumber(ctx, odr, number)
  70. if header != nil {
  71. return header.Hash(), nil
  72. }
  73. return common.Hash{}, err
  74. }
  75. // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  76. func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error) {
  77. if data := rawdb.ReadBodyRLP(odr.Database(), hash, number); data != nil {
  78. return data, nil
  79. }
  80. r := &BlockRequest{Hash: hash, Number: number}
  81. if err := odr.Retrieve(ctx, r); err != nil {
  82. return nil, err
  83. } else {
  84. return r.Rlp, nil
  85. }
  86. }
  87. // GetBody retrieves the block body (transactons, uncles) corresponding to the
  88. // hash.
  89. func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error) {
  90. data, err := GetBodyRLP(ctx, odr, hash, number)
  91. if err != nil {
  92. return nil, err
  93. }
  94. body := new(types.Body)
  95. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  96. return nil, err
  97. }
  98. return body, nil
  99. }
  100. // GetBlock retrieves an entire block corresponding to the hash, assembling it
  101. // back from the stored header and body.
  102. func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error) {
  103. // Retrieve the block header and body contents
  104. header := rawdb.ReadHeader(odr.Database(), hash, number)
  105. if header == nil {
  106. return nil, ErrNoHeader
  107. }
  108. body, err := GetBody(ctx, odr, hash, number)
  109. if err != nil {
  110. return nil, err
  111. }
  112. // Reassemble the block and return
  113. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
  114. }
  115. // GetBlockReceipts retrieves the receipts generated by the transactions included
  116. // in a block given by its hash.
  117. func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) {
  118. // Assume receipts are already stored locally and attempt to retrieve.
  119. receipts := rawdb.ReadRawReceipts(odr.Database(), hash, number)
  120. if receipts == nil {
  121. r := &ReceiptsRequest{Hash: hash, Number: number}
  122. if err := odr.Retrieve(ctx, r); err != nil {
  123. return nil, err
  124. }
  125. receipts = r.Receipts
  126. }
  127. // If the receipts are incomplete, fill the derived fields
  128. if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) {
  129. block, err := GetBlock(ctx, odr, hash, number)
  130. if err != nil {
  131. return nil, err
  132. }
  133. genesis := rawdb.ReadCanonicalHash(odr.Database(), 0)
  134. config := rawdb.ReadChainConfig(odr.Database(), genesis)
  135. if err := rawdb.SetReceiptsData(config, block.Hash(), block.Number(), block.Body(), receipts); err != nil {
  136. return nil, err
  137. }
  138. rawdb.WriteReceipts(odr.Database(), hash, number, receipts)
  139. }
  140. return receipts, nil
  141. }
  142. // GetBlockLogs retrieves the logs generated by the transactions included in a
  143. // block given by its hash.
  144. func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) {
  145. // Retrieve the potentially incomplete receipts from disk or network
  146. receipts, err := GetBlockReceipts(ctx, odr, hash, number)
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Return the logs without deriving any computed fields on the receipts
  151. logs := make([][]*types.Log, len(receipts))
  152. for i, receipt := range receipts {
  153. logs[i] = receipt.Logs
  154. }
  155. return logs, nil
  156. }
  157. // GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes
  158. func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error) {
  159. var (
  160. db = odr.Database()
  161. result = make([][]byte, len(sectionIdxList))
  162. reqList []uint64
  163. reqIdx []int
  164. )
  165. var (
  166. bloomTrieCount, sectionHeadNum uint64
  167. sectionHead common.Hash
  168. )
  169. if odr.BloomTrieIndexer() != nil {
  170. bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
  171. canonicalHash := rawdb.ReadCanonicalHash(db, sectionHeadNum)
  172. // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  173. for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  174. bloomTrieCount--
  175. if bloomTrieCount > 0 {
  176. sectionHeadNum = bloomTrieCount*odr.IndexerConfig().BloomTrieSize - 1
  177. sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
  178. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  179. }
  180. }
  181. }
  182. for i, sectionIdx := range sectionIdxList {
  183. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*odr.IndexerConfig().BloomSize-1)
  184. // if we don't have the canonical hash stored for this section head number, we'll still look for
  185. // an entry with a zero sectionHead (we store it with zero section head too if we don't know it
  186. // at the time of the retrieval)
  187. bloomBits, err := rawdb.ReadBloomBits(db, bitIdx, sectionIdx, sectionHead)
  188. if err == nil {
  189. result[i] = bloomBits
  190. } else {
  191. // TODO(rjl493456442) Convert sectionIndex to BloomTrie relative index
  192. if sectionIdx >= bloomTrieCount {
  193. return nil, ErrNoTrustedBloomTrie
  194. }
  195. reqList = append(reqList, sectionIdx)
  196. reqIdx = append(reqIdx, i)
  197. }
  198. }
  199. if reqList == nil {
  200. return result, nil
  201. }
  202. r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1,
  203. BitIdx: bitIdx, SectionIndexList: reqList, Config: odr.IndexerConfig()}
  204. if err := odr.Retrieve(ctx, r); err != nil {
  205. return nil, err
  206. } else {
  207. for i, idx := range reqIdx {
  208. result[idx] = r.BloomBits[i]
  209. }
  210. return result, nil
  211. }
  212. }