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