odr.go 5.3 KB

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