odr_util.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. receipts := core.GetBlockReceipts(odr.Database(), hash, number)
  119. if receipts != nil {
  120. return receipts, nil
  121. }
  122. r := &ReceiptsRequest{Hash: hash, Number: number}
  123. if err := odr.Retrieve(ctx, r); err != nil {
  124. return nil, err
  125. }
  126. return r.Receipts, nil
  127. }
  128. // GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to the given bit index and section indexes
  129. func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxList []uint64) ([][]byte, error) {
  130. db := odr.Database()
  131. result := make([][]byte, len(sectionIdxList))
  132. var (
  133. reqList []uint64
  134. reqIdx []int
  135. )
  136. var (
  137. bloomTrieCount, sectionHeadNum uint64
  138. sectionHead common.Hash
  139. )
  140. if odr.BloomTrieIndexer() != nil {
  141. bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
  142. canonicalHash := core.GetCanonicalHash(db, sectionHeadNum)
  143. // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
  144. for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
  145. bloomTrieCount--
  146. if bloomTrieCount > 0 {
  147. sectionHeadNum = bloomTrieCount*BloomTrieFrequency - 1
  148. sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
  149. canonicalHash = core.GetCanonicalHash(db, sectionHeadNum)
  150. }
  151. }
  152. }
  153. for i, sectionIdx := range sectionIdxList {
  154. sectionHead := core.GetCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
  155. // if we don't have the canonical hash stored for this section head number, we'll still look for
  156. // an entry with a zero sectionHead (we store it with zero section head too if we don't know it
  157. // at the time of the retrieval)
  158. bloomBits, err := core.GetBloomBits(db, bitIdx, sectionIdx, sectionHead)
  159. if err == nil {
  160. result[i] = bloomBits
  161. } else {
  162. if sectionIdx >= bloomTrieCount {
  163. return nil, ErrNoTrustedBloomTrie
  164. }
  165. reqList = append(reqList, sectionIdx)
  166. reqIdx = append(reqIdx, i)
  167. }
  168. }
  169. if reqList == nil {
  170. return result, nil
  171. }
  172. r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1, BitIdx: bitIdx, SectionIdxList: reqList}
  173. if err := odr.Retrieve(ctx, r); err != nil {
  174. return nil, err
  175. } else {
  176. for i, idx := range reqIdx {
  177. result[idx] = r.BloomBits[i]
  178. }
  179. return result, nil
  180. }
  181. }