odr_util.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/types"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. var sha3_nil = crypto.Keccak256Hash(nil)
  27. func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
  28. db := odr.Database()
  29. hash := core.GetCanonicalHash(db, number)
  30. if (hash != common.Hash{}) {
  31. // if there is a canonical hash, there is a header too
  32. header := core.GetHeader(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 := core.GetCanonicalHash(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*CHTFrequencyClient - 1
  50. sectionHead = odr.ChtIndexer().SectionHead(chtCount - 1)
  51. canonicalHash = core.GetCanonicalHash(db, sectionHeadNum)
  52. }
  53. }
  54. }
  55. if number >= chtCount*CHTFrequencyClient {
  56. return nil, ErrNoTrustedCht
  57. }
  58. r := &ChtRequest{ChtRoot: GetChtRoot(db, chtCount-1, sectionHead), ChtNum: chtCount - 1, BlockNum: number}
  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 := core.GetCanonicalHash(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 := core.GetBodyRLP(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 := core.GetHeader(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. // Retrieve the potentially incomplete receipts from disk or network
  119. receipts := core.GetBlockReceipts(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 := core.GetCanonicalHash(odr.Database(), 0)
  134. config, _ := core.GetChainConfig(odr.Database(), genesis)
  135. core.SetReceiptsData(config, block, receipts)
  136. core.WriteBlockReceipts(odr.Database(), hash, number, receipts)
  137. }
  138. return receipts, nil
  139. }
  140. // GetBlockLogs retrieves the logs generated by the transactions included in a
  141. // block given by its hash.
  142. func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) {
  143. // Retrieve the potentially incomplete receipts from disk or network
  144. receipts := core.GetBlockReceipts(odr.Database(), hash, number)
  145. if receipts == nil {
  146. r := &ReceiptsRequest{Hash: hash, Number: number}
  147. if err := odr.Retrieve(ctx, r); err != nil {
  148. return nil, err
  149. }
  150. receipts = r.Receipts
  151. }
  152. // Return the logs without deriving any computed fields on the receipts
  153. logs := make([][]*types.Log, len(receipts))
  154. for i, receipt := range receipts {
  155. logs[i] = receipt.Logs
  156. }
  157. return logs, nil
  158. }
  159. // GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes
  160. func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error) {
  161. db := odr.Database()
  162. result := make([][]byte, len(sectionIdxList))
  163. var (
  164. reqList []uint64
  165. reqIdx []int
  166. )
  167. var (
  168. bloomTrieCount, sectionHeadNum uint64
  169. sectionHead common.Hash
  170. )
  171. if odr.BloomTrieIndexer() != nil {
  172. bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
  173. canonicalHash := core.GetCanonicalHash(db, sectionHeadNum)
  174. // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  175. for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  176. bloomTrieCount--
  177. if bloomTrieCount > 0 {
  178. sectionHeadNum = bloomTrieCount*BloomTrieFrequency - 1
  179. sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
  180. canonicalHash = core.GetCanonicalHash(db, sectionHeadNum)
  181. }
  182. }
  183. }
  184. for i, sectionIdx := range sectionIdxList {
  185. sectionHead := core.GetCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
  186. // if we don't have the canonical hash stored for this section head number, we'll still look for
  187. // an entry with a zero sectionHead (we store it with zero section head too if we don't know it
  188. // at the time of the retrieval)
  189. bloomBits, err := core.GetBloomBits(db, bitIdx, sectionIdx, sectionHead)
  190. if err == nil {
  191. result[i] = bloomBits
  192. } else {
  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, BitIdx: bitIdx, SectionIdxList: reqList}
  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. }