odr.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2015 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 implements on-demand retrieval capable state and chain objects
  17. // for the Ethereum Light Client.
  18. package light
  19. import (
  20. "context"
  21. "errors"
  22. "math/big"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. )
  29. // NoOdr is the default context passed to an ODR capable function when the ODR
  30. // service is not required.
  31. var NoOdr = context.Background()
  32. // ErrNoPeers is returned if no peers capable of serving a queued request are available
  33. var ErrNoPeers = errors.New("no suitable peers available")
  34. // OdrBackend is an interface to a backend service that handles ODR retrievals type
  35. type OdrBackend interface {
  36. Database() ethdb.Database
  37. ChtIndexer() *core.ChainIndexer
  38. BloomTrieIndexer() *core.ChainIndexer
  39. BloomIndexer() *core.ChainIndexer
  40. Retrieve(ctx context.Context, req OdrRequest) error
  41. IndexerConfig() *IndexerConfig
  42. }
  43. // OdrRequest is an interface for retrieval requests
  44. type OdrRequest interface {
  45. StoreResult(db ethdb.Database)
  46. }
  47. // TrieID identifies a state or account storage trie
  48. type TrieID struct {
  49. BlockHash, Root common.Hash
  50. BlockNumber uint64
  51. AccKey []byte
  52. }
  53. // StateTrieID returns a TrieID for a state trie belonging to a certain block
  54. // header.
  55. func StateTrieID(header *types.Header) *TrieID {
  56. return &TrieID{
  57. BlockHash: header.Hash(),
  58. BlockNumber: header.Number.Uint64(),
  59. AccKey: nil,
  60. Root: header.Root,
  61. }
  62. }
  63. // StorageTrieID returns a TrieID for a contract storage trie at a given account
  64. // of a given state trie. It also requires the root hash of the trie for
  65. // checking Merkle proofs.
  66. func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID {
  67. return &TrieID{
  68. BlockHash: state.BlockHash,
  69. BlockNumber: state.BlockNumber,
  70. AccKey: addrHash[:],
  71. Root: root,
  72. }
  73. }
  74. // TrieRequest is the ODR request type for state/storage trie entries
  75. type TrieRequest struct {
  76. OdrRequest
  77. Id *TrieID
  78. Key []byte
  79. Proof *NodeSet
  80. }
  81. // StoreResult stores the retrieved data in local database
  82. func (req *TrieRequest) StoreResult(db ethdb.Database) {
  83. req.Proof.Store(db)
  84. }
  85. // CodeRequest is the ODR request type for retrieving contract code
  86. type CodeRequest struct {
  87. OdrRequest
  88. Id *TrieID // references storage trie of the account
  89. Hash common.Hash
  90. Data []byte
  91. }
  92. // StoreResult stores the retrieved data in local database
  93. func (req *CodeRequest) StoreResult(db ethdb.Database) {
  94. db.Put(req.Hash[:], req.Data)
  95. }
  96. // BlockRequest is the ODR request type for retrieving block bodies
  97. type BlockRequest struct {
  98. OdrRequest
  99. Hash common.Hash
  100. Number uint64
  101. Rlp []byte
  102. }
  103. // StoreResult stores the retrieved data in local database
  104. func (req *BlockRequest) StoreResult(db ethdb.Database) {
  105. rawdb.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
  106. }
  107. // ReceiptsRequest is the ODR request type for retrieving block bodies
  108. type ReceiptsRequest struct {
  109. OdrRequest
  110. Hash common.Hash
  111. Number uint64
  112. Receipts types.Receipts
  113. }
  114. // StoreResult stores the retrieved data in local database
  115. func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
  116. rawdb.WriteReceipts(db, req.Hash, req.Number, req.Receipts)
  117. }
  118. // ChtRequest is the ODR request type for state/storage trie entries
  119. type ChtRequest struct {
  120. OdrRequest
  121. Config *IndexerConfig
  122. ChtNum, BlockNum uint64
  123. ChtRoot common.Hash
  124. Header *types.Header
  125. Td *big.Int
  126. Proof *NodeSet
  127. }
  128. // StoreResult stores the retrieved data in local database
  129. func (req *ChtRequest) StoreResult(db ethdb.Database) {
  130. hash, num := req.Header.Hash(), req.Header.Number.Uint64()
  131. rawdb.WriteHeader(db, req.Header)
  132. rawdb.WriteTd(db, hash, num, req.Td)
  133. rawdb.WriteCanonicalHash(db, hash, num)
  134. }
  135. // BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
  136. type BloomRequest struct {
  137. OdrRequest
  138. Config *IndexerConfig
  139. BloomTrieNum uint64
  140. BitIdx uint
  141. SectionIndexList []uint64
  142. BloomTrieRoot common.Hash
  143. BloomBits [][]byte
  144. Proofs *NodeSet
  145. }
  146. // StoreResult stores the retrieved data in local database
  147. func (req *BloomRequest) StoreResult(db ethdb.Database) {
  148. for i, sectionIdx := range req.SectionIndexList {
  149. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*req.Config.BloomTrieSize-1)
  150. // if we don't have the canonical hash stored for this section head number, we'll still store it under
  151. // a key with a zero sectionHead. GetBloomBits will look there too if we still don't have the canonical
  152. // hash. In the unlikely case we've retrieved the section head hash since then, we'll just retrieve the
  153. // bit vector again from the network.
  154. rawdb.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i])
  155. }
  156. }