odr.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. "golang.org/x/net/context"
  25. )
  26. // OdrBackend is an interface to a backend service that handles odr retrievals
  27. type OdrBackend interface {
  28. Database() ethdb.Database
  29. Retrieve(ctx context.Context, req OdrRequest) error
  30. }
  31. // OdrRequest is an interface for retrieval requests
  32. type OdrRequest interface {
  33. StoreResult(db ethdb.Database)
  34. }
  35. // TrieRequest is the ODR request type for state/storage trie entries
  36. type TrieRequest struct {
  37. OdrRequest
  38. root common.Hash
  39. key []byte
  40. proof []rlp.RawValue
  41. }
  42. // StoreResult stores the retrieved data in local database
  43. func (req *TrieRequest) StoreResult(db ethdb.Database) {
  44. storeProof(db, req.proof)
  45. }
  46. // storeProof stores the new trie nodes obtained from a merkle proof in the database
  47. func storeProof(db ethdb.Database, proof []rlp.RawValue) {
  48. for _, buf := range proof {
  49. hash := crypto.Sha3(buf)
  50. val, _ := db.Get(hash)
  51. if val == nil {
  52. db.Put(hash, buf)
  53. }
  54. }
  55. }
  56. // NodeDataRequest is the ODR request type for node data (used for retrieving contract code)
  57. type NodeDataRequest struct {
  58. OdrRequest
  59. hash common.Hash
  60. data []byte
  61. }
  62. // GetData returns the retrieved node data after a successful request
  63. func (req *NodeDataRequest) GetData() []byte {
  64. return req.data
  65. }
  66. // StoreResult stores the retrieved data in local database
  67. func (req *NodeDataRequest) StoreResult(db ethdb.Database) {
  68. db.Put(req.hash[:], req.GetData())
  69. }
  70. var sha3_nil = crypto.Sha3Hash(nil)
  71. // retrieveNodeData tries to retrieve node data with the given hash from the network
  72. func retrieveNodeData(ctx context.Context, odr OdrBackend, hash common.Hash) ([]byte, error) {
  73. if hash == sha3_nil {
  74. return nil, nil
  75. }
  76. res, _ := odr.Database().Get(hash[:])
  77. if res != nil {
  78. return res, nil
  79. }
  80. r := &NodeDataRequest{hash: hash}
  81. if err := odr.Retrieve(ctx, r); err != nil {
  82. return nil, err
  83. } else {
  84. return r.GetData(), nil
  85. }
  86. }