odr.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. "golang.org/x/net/context"
  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. // OdrBackend is an interface to a backend service that handles ODR retrievals
  33. type OdrBackend interface {
  34. Database() ethdb.Database
  35. Retrieve(ctx context.Context, req OdrRequest) error
  36. }
  37. // OdrRequest is an interface for retrieval requests
  38. type OdrRequest interface {
  39. StoreResult(db ethdb.Database)
  40. }
  41. // TrieID identifies a state or account storage trie
  42. type TrieID struct {
  43. BlockHash, Root common.Hash
  44. BlockNumber uint64
  45. AccKey []byte
  46. }
  47. // StateTrieID returns a TrieID for a state trie belonging to a certain block
  48. // header.
  49. func StateTrieID(header *types.Header) *TrieID {
  50. return &TrieID{
  51. BlockHash: header.Hash(),
  52. BlockNumber: header.Number.Uint64(),
  53. AccKey: nil,
  54. Root: header.Root,
  55. }
  56. }
  57. // StorageTrieID returns a TrieID for a contract storage trie at a given account
  58. // of a given state trie. It also requires the root hash of the trie for
  59. // checking Merkle proofs.
  60. func StorageTrieID(state *TrieID, addr common.Address, root common.Hash) *TrieID {
  61. return &TrieID{
  62. BlockHash: state.BlockHash,
  63. BlockNumber: state.BlockNumber,
  64. AccKey: crypto.Keccak256(addr[:]),
  65. Root: root,
  66. }
  67. }
  68. // TrieRequest is the ODR request type for state/storage trie entries
  69. type TrieRequest struct {
  70. OdrRequest
  71. Id *TrieID
  72. Key []byte
  73. Proof []rlp.RawValue
  74. }
  75. // StoreResult stores the retrieved data in local database
  76. func (req *TrieRequest) StoreResult(db ethdb.Database) {
  77. storeProof(db, req.Proof)
  78. }
  79. // storeProof stores the new trie nodes obtained from a merkle proof in the database
  80. func storeProof(db ethdb.Database, proof []rlp.RawValue) {
  81. for _, buf := range proof {
  82. hash := crypto.Keccak256(buf)
  83. val, _ := db.Get(hash)
  84. if val == nil {
  85. db.Put(hash, buf)
  86. }
  87. }
  88. }
  89. // CodeRequest is the ODR request type for retrieving contract code
  90. type CodeRequest struct {
  91. OdrRequest
  92. Id *TrieID
  93. Hash common.Hash
  94. Data []byte
  95. }
  96. // StoreResult stores the retrieved data in local database
  97. func (req *CodeRequest) StoreResult(db ethdb.Database) {
  98. db.Put(req.Hash[:], req.Data)
  99. }
  100. // BlockRequest is the ODR request type for retrieving block bodies
  101. type BlockRequest struct {
  102. OdrRequest
  103. Hash common.Hash
  104. Number uint64
  105. Rlp []byte
  106. }
  107. // StoreResult stores the retrieved data in local database
  108. func (req *BlockRequest) StoreResult(db ethdb.Database) {
  109. core.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
  110. }
  111. // ReceiptsRequest is the ODR request type for retrieving block bodies
  112. type ReceiptsRequest struct {
  113. OdrRequest
  114. Hash common.Hash
  115. Number uint64
  116. Receipts types.Receipts
  117. }
  118. // StoreResult stores the retrieved data in local database
  119. func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
  120. core.WriteBlockReceipts(db, req.Hash, req.Number, req.Receipts)
  121. }
  122. // TrieRequest is the ODR request type for state/storage trie entries
  123. type ChtRequest struct {
  124. OdrRequest
  125. ChtNum, BlockNum uint64
  126. ChtRoot common.Hash
  127. Header *types.Header
  128. Td *big.Int
  129. Proof []rlp.RawValue
  130. }
  131. // StoreResult stores the retrieved data in local database
  132. func (req *ChtRequest) StoreResult(db ethdb.Database) {
  133. // if there is a canonical hash, there is a header too
  134. core.WriteHeader(db, req.Header)
  135. hash, num := req.Header.Hash(), req.Header.Number.Uint64()
  136. core.WriteTd(db, hash, num, req.Td)
  137. core.WriteCanonicalHash(db, hash, num)
  138. //storeProof(db, req.Proof)
  139. }