odr_util.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. var sha3Nil = crypto.Keccak256Hash(nil)
  28. func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
  29. db := odr.Database()
  30. hash := rawdb.ReadCanonicalHash(db, number)
  31. if (hash != common.Hash{}) {
  32. // if there is a canonical hash, there is a header too
  33. header := rawdb.ReadHeader(db, hash, number)
  34. if header == nil {
  35. panic("Canonical hash present but header not found")
  36. }
  37. return header, nil
  38. }
  39. var (
  40. chtCount, sectionHeadNum uint64
  41. sectionHead common.Hash
  42. )
  43. if odr.ChtIndexer() != nil {
  44. chtCount, sectionHeadNum, sectionHead = odr.ChtIndexer().Sections()
  45. canonicalHash := rawdb.ReadCanonicalHash(db, sectionHeadNum)
  46. // if the CHT was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  47. for chtCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  48. chtCount--
  49. if chtCount > 0 {
  50. sectionHeadNum = chtCount*odr.IndexerConfig().ChtSize - 1
  51. sectionHead = odr.ChtIndexer().SectionHead(chtCount - 1)
  52. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  53. }
  54. }
  55. }
  56. if number >= chtCount*odr.IndexerConfig().ChtSize {
  57. return nil, ErrNoTrustedCht
  58. }
  59. r := &ChtRequest{ChtRoot: GetChtRoot(db, chtCount-1, sectionHead), ChtNum: chtCount - 1, BlockNum: number, Config: odr.IndexerConfig()}
  60. if err := odr.Retrieve(ctx, r); err != nil {
  61. return nil, err
  62. }
  63. return r.Header, nil
  64. }
  65. func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) {
  66. hash := rawdb.ReadCanonicalHash(odr.Database(), number)
  67. if (hash != common.Hash{}) {
  68. return hash, nil
  69. }
  70. header, err := GetHeaderByNumber(ctx, odr, number)
  71. if header != nil {
  72. return header.Hash(), nil
  73. }
  74. return common.Hash{}, err
  75. }
  76. // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  77. func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error) {
  78. if data := rawdb.ReadBodyRLP(odr.Database(), hash, number); data != nil {
  79. return data, nil
  80. }
  81. r := &BlockRequest{Hash: hash, Number: number}
  82. if err := odr.Retrieve(ctx, r); err != nil {
  83. return nil, err
  84. } else {
  85. return r.Rlp, nil
  86. }
  87. }
  88. // GetBody retrieves the block body (transactons, uncles) corresponding to the
  89. // hash.
  90. func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error) {
  91. data, err := GetBodyRLP(ctx, odr, hash, number)
  92. if err != nil {
  93. return nil, err
  94. }
  95. body := new(types.Body)
  96. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  97. return nil, err
  98. }
  99. return body, nil
  100. }
  101. // GetBlock retrieves an entire block corresponding to the hash, assembling it
  102. // back from the stored header and body.
  103. func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error) {
  104. // Retrieve the block header and body contents
  105. header := rawdb.ReadHeader(odr.Database(), hash, number)
  106. if header == nil {
  107. return nil, ErrNoHeader
  108. }
  109. body, err := GetBody(ctx, odr, hash, number)
  110. if err != nil {
  111. return nil, err
  112. }
  113. // Reassemble the block and return
  114. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
  115. }
  116. // GetBlockReceipts retrieves the receipts generated by the transactions included
  117. // in a block given by its hash.
  118. func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) {
  119. // Assume receipts are already stored locally and attempt to retrieve.
  120. receipts := rawdb.ReadRawReceipts(odr.Database(), hash, number)
  121. if receipts == nil {
  122. r := &ReceiptsRequest{Hash: hash, Number: number}
  123. if err := odr.Retrieve(ctx, r); err != nil {
  124. return nil, err
  125. }
  126. receipts = r.Receipts
  127. }
  128. // If the receipts are incomplete, fill the derived fields
  129. if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) {
  130. block, err := GetBlock(ctx, odr, hash, number)
  131. if err != nil {
  132. return nil, err
  133. }
  134. genesis := rawdb.ReadCanonicalHash(odr.Database(), 0)
  135. config := rawdb.ReadChainConfig(odr.Database(), genesis)
  136. if err := receipts.DeriveFields(config, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
  137. return nil, err
  138. }
  139. rawdb.WriteReceipts(odr.Database(), hash, number, receipts)
  140. }
  141. return receipts, nil
  142. }
  143. // GetBlockLogs retrieves the logs generated by the transactions included in a
  144. // block given by its hash.
  145. func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) {
  146. // Retrieve the potentially incomplete receipts from disk or network
  147. receipts, err := GetBlockReceipts(ctx, odr, hash, number)
  148. if err != nil {
  149. return nil, err
  150. }
  151. // Return the logs without deriving any computed fields on the receipts
  152. logs := make([][]*types.Log, len(receipts))
  153. for i, receipt := range receipts {
  154. logs[i] = receipt.Logs
  155. }
  156. return logs, nil
  157. }
  158. // GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes
  159. func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error) {
  160. var (
  161. db = odr.Database()
  162. result = make([][]byte, len(sectionIdxList))
  163. reqList []uint64
  164. reqIdx []int
  165. )
  166. var (
  167. bloomTrieCount, sectionHeadNum uint64
  168. sectionHead common.Hash
  169. )
  170. if odr.BloomTrieIndexer() != nil {
  171. bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
  172. canonicalHash := rawdb.ReadCanonicalHash(db, sectionHeadNum)
  173. // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  174. for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  175. bloomTrieCount--
  176. if bloomTrieCount > 0 {
  177. sectionHeadNum = bloomTrieCount*odr.IndexerConfig().BloomTrieSize - 1
  178. sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
  179. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  180. }
  181. }
  182. }
  183. for i, sectionIdx := range sectionIdxList {
  184. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*odr.IndexerConfig().BloomSize-1)
  185. // if we don't have the canonical hash stored for this section head number, we'll still look for
  186. // an entry with a zero sectionHead (we store it with zero section head too if we don't know it
  187. // at the time of the retrieval)
  188. bloomBits, err := rawdb.ReadBloomBits(db, bitIdx, sectionIdx, sectionHead)
  189. if err == nil {
  190. result[i] = bloomBits
  191. } else {
  192. // TODO(rjl493456442) Convert sectionIndex to BloomTrie relative index
  193. if sectionIdx >= bloomTrieCount {
  194. return nil, ErrNoTrustedBloomTrie
  195. }
  196. reqList = append(reqList, sectionIdx)
  197. reqIdx = append(reqIdx, i)
  198. }
  199. }
  200. if reqList == nil {
  201. return result, nil
  202. }
  203. r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1,
  204. BitIdx: bitIdx, SectionIndexList: reqList, Config: odr.IndexerConfig()}
  205. if err := odr.Retrieve(ctx, r); err != nil {
  206. return nil, err
  207. } else {
  208. for i, idx := range reqIdx {
  209. result[idx] = r.BloomBits[i]
  210. }
  211. return result, nil
  212. }
  213. }
  214. // GetTransaction retrieves a canonical transaction by hash and also returns its position in the chain
  215. func GetTransaction(ctx context.Context, odr OdrBackend, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
  216. r := &TxStatusRequest{Hashes: []common.Hash{txHash}}
  217. if err := odr.Retrieve(ctx, r); err != nil || r.Status[0].Status != core.TxStatusIncluded {
  218. return nil, common.Hash{}, 0, 0, err
  219. } else {
  220. pos := r.Status[0].Lookup
  221. // first ensure that we have the header, otherwise block body retrieval will fail
  222. // also verify if this is a canonical block by getting the header by number and checking its hash
  223. if header, err := GetHeaderByNumber(ctx, odr, pos.BlockIndex); err != nil || header.Hash() != pos.BlockHash {
  224. return nil, common.Hash{}, 0, 0, err
  225. }
  226. if body, err := GetBody(ctx, odr, pos.BlockHash, pos.BlockIndex); err != nil || uint64(len(body.Transactions)) <= pos.Index || body.Transactions[pos.Index].Hash() != txHash {
  227. return nil, common.Hash{}, 0, 0, err
  228. } else {
  229. return body.Transactions[pos.Index], pos.BlockHash, pos.BlockIndex, pos.Index, nil
  230. }
  231. }
  232. }