odr.go 4.5 KB

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