odr_util.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 sha3_nil = 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*CHTFrequencyClient - 1
  51. sectionHead = odr.ChtIndexer().SectionHead(chtCount - 1)
  52. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  53. }
  54. }
  55. }
  56. if number >= chtCount*CHTFrequencyClient {
  57. return nil, ErrNoTrustedCht
  58. }
  59. r := &ChtRequest{ChtRoot: GetChtRoot(db, chtCount-1, sectionHead), ChtNum: chtCount - 1, BlockNum: number}
  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. // Retrieve the potentially incomplete receipts from disk or network
  120. receipts := rawdb.ReadReceipts(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 := core.SetReceiptsData(config, block, receipts); 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 := rawdb.ReadReceipts(odr.Database(), hash, number)
  148. if receipts == nil {
  149. r := &ReceiptsRequest{Hash: hash, Number: number}
  150. if err := odr.Retrieve(ctx, r); err != nil {
  151. return nil, err
  152. }
  153. receipts = r.Receipts
  154. }
  155. // Return the logs without deriving any computed fields on the receipts
  156. logs := make([][]*types.Log, len(receipts))
  157. for i, receipt := range receipts {
  158. logs[i] = receipt.Logs
  159. }
  160. return logs, nil
  161. }
  162. // GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes
  163. func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error) {
  164. db := odr.Database()
  165. result := make([][]byte, len(sectionIdxList))
  166. var (
  167. reqList []uint64
  168. reqIdx []int
  169. )
  170. var (
  171. bloomTrieCount, sectionHeadNum uint64
  172. sectionHead common.Hash
  173. )
  174. if odr.BloomTrieIndexer() != nil {
  175. bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
  176. canonicalHash := rawdb.ReadCanonicalHash(db, sectionHeadNum)
  177. // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  178. for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  179. bloomTrieCount--
  180. if bloomTrieCount > 0 {
  181. sectionHeadNum = bloomTrieCount*BloomTrieFrequency - 1
  182. sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
  183. canonicalHash = rawdb.ReadCanonicalHash(db, sectionHeadNum)
  184. }
  185. }
  186. }
  187. for i, sectionIdx := range sectionIdxList {
  188. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
  189. // if we don't have the canonical hash stored for this section head number, we'll still look for
  190. // an entry with a zero sectionHead (we store it with zero section head too if we don't know it
  191. // at the time of the retrieval)
  192. bloomBits, err := rawdb.ReadBloomBits(db, bitIdx, sectionIdx, sectionHead)
  193. if err == nil {
  194. result[i] = bloomBits
  195. } else {
  196. if sectionIdx >= bloomTrieCount {
  197. return nil, ErrNoTrustedBloomTrie
  198. }
  199. reqList = append(reqList, sectionIdx)
  200. reqIdx = append(reqIdx, i)
  201. }
  202. }
  203. if reqList == nil {
  204. return result, nil
  205. }
  206. r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1, BitIdx: bitIdx, SectionIdxList: reqList}
  207. if err := odr.Retrieve(ctx, r); err != nil {
  208. return nil, err
  209. } else {
  210. for i, idx := range reqIdx {
  211. result[idx] = r.BloomBits[i]
  212. }
  213. return result, nil
  214. }
  215. }