odr.go 6.1 KB

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