| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | // Copyright 2015 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package light | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-03-22 18:20:33 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-08-15 22:25:46 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	"math/big" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core" | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core/rawdb" | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethdb" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // NoOdr is the default context passed to an ODR capable function when the ODR | 
					
						
							|  |  |  | // service is not required. | 
					
						
							|  |  |  | var NoOdr = context.Background() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 22:25:46 +02:00
										 |  |  | // ErrNoPeers is returned if no peers capable of serving a queued request are available | 
					
						
							|  |  |  | var ErrNoPeers = errors.New("no suitable peers available") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-27 15:57:06 +02:00
										 |  |  | // OdrBackend is an interface to a backend service that handles ODR retrievals type | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | type OdrBackend interface { | 
					
						
							|  |  |  | 	Database() ethdb.Database | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 	ChtIndexer() *core.ChainIndexer | 
					
						
							|  |  |  | 	BloomTrieIndexer() *core.ChainIndexer | 
					
						
							|  |  |  | 	BloomIndexer() *core.ChainIndexer | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | 	Retrieve(ctx context.Context, req OdrRequest) error | 
					
						
							| 
									
										
										
										
											2018-08-28 15:08:16 +08:00
										 |  |  | 	IndexerConfig() *IndexerConfig | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // OdrRequest is an interface for retrieval requests | 
					
						
							|  |  |  | type OdrRequest interface { | 
					
						
							|  |  |  | 	StoreResult(db ethdb.Database) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // TrieID identifies a state or account storage trie | 
					
						
							|  |  |  | type TrieID struct { | 
					
						
							|  |  |  | 	BlockHash, Root common.Hash | 
					
						
							| 
									
										
										
										
											2016-12-10 06:50:36 +01:00
										 |  |  | 	BlockNumber     uint64 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	AccKey          []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StateTrieID returns a TrieID for a state trie belonging to a certain block | 
					
						
							|  |  |  | // header. | 
					
						
							|  |  |  | func StateTrieID(header *types.Header) *TrieID { | 
					
						
							|  |  |  | 	return &TrieID{ | 
					
						
							| 
									
										
										
										
											2016-12-10 06:50:36 +01:00
										 |  |  | 		BlockHash:   header.Hash(), | 
					
						
							|  |  |  | 		BlockNumber: header.Number.Uint64(), | 
					
						
							|  |  |  | 		AccKey:      nil, | 
					
						
							|  |  |  | 		Root:        header.Root, | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StorageTrieID returns a TrieID for a contract storage trie at a given account | 
					
						
							|  |  |  | // of a given state trie. It also requires the root hash of the trie for | 
					
						
							|  |  |  | // checking Merkle proofs. | 
					
						
							| 
									
										
										
										
											2017-06-27 15:57:06 +02:00
										 |  |  | func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID { | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	return &TrieID{ | 
					
						
							| 
									
										
										
										
											2016-12-10 06:50:36 +01:00
										 |  |  | 		BlockHash:   state.BlockHash, | 
					
						
							|  |  |  | 		BlockNumber: state.BlockNumber, | 
					
						
							| 
									
										
										
										
											2017-06-27 15:57:06 +02:00
										 |  |  | 		AccKey:      addrHash[:], | 
					
						
							| 
									
										
										
										
											2016-12-10 06:50:36 +01:00
										 |  |  | 		Root:        root, | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | // TrieRequest is the ODR request type for state/storage trie entries | 
					
						
							|  |  |  | type TrieRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	Id    *TrieID | 
					
						
							|  |  |  | 	Key   []byte | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 	Proof *NodeSet | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *TrieRequest) StoreResult(db ethdb.Database) { | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 	req.Proof.Store(db) | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // CodeRequest is the ODR request type for retrieving contract code | 
					
						
							|  |  |  | type CodeRequest struct { | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | 	OdrRequest | 
					
						
							| 
									
										
										
										
											2017-06-27 15:57:06 +02:00
										 |  |  | 	Id   *TrieID // references storage trie of the account | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	Hash common.Hash | 
					
						
							|  |  |  | 	Data []byte | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *CodeRequest) StoreResult(db ethdb.Database) { | 
					
						
							|  |  |  | 	db.Put(req.Hash[:], req.Data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BlockRequest is the ODR request type for retrieving block bodies | 
					
						
							|  |  |  | type BlockRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							|  |  |  | 	Hash   common.Hash | 
					
						
							|  |  |  | 	Number uint64 | 
					
						
							|  |  |  | 	Rlp    []byte | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | func (req *BlockRequest) StoreResult(db ethdb.Database) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	rawdb.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp) | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // ReceiptsRequest is the ODR request type for retrieving block bodies | 
					
						
							|  |  |  | type ReceiptsRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							| 
									
										
											  
											
												all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
											
										 
											2019-06-28 15:34:02 +08:00
										 |  |  | 	Untrusted bool // Indicator whether the result retrieved is trusted or not | 
					
						
							|  |  |  | 	Hash      common.Hash | 
					
						
							|  |  |  | 	Number    uint64 | 
					
						
							|  |  |  | 	Header    *types.Header | 
					
						
							|  |  |  | 	Receipts  types.Receipts | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { | 
					
						
							| 
									
										
											  
											
												all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
											
										 
											2019-06-28 15:34:02 +08:00
										 |  |  | 	if !req.Untrusted { | 
					
						
							|  |  |  | 		rawdb.WriteReceipts(db, req.Hash, req.Number, req.Receipts) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | // ChtRequest is the ODR request type for state/storage trie entries | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | type ChtRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							| 
									
										
											  
											
												all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
											
										 
											2019-06-28 15:34:02 +08:00
										 |  |  | 	Untrusted        bool   // Indicator whether the result retrieved is trusted or not | 
					
						
							|  |  |  | 	PeerId           string // The specified peer id from which to retrieve data. | 
					
						
							| 
									
										
										
										
											2018-08-28 15:08:16 +08:00
										 |  |  | 	Config           *IndexerConfig | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | 	ChtNum, BlockNum uint64 | 
					
						
							|  |  |  | 	ChtRoot          common.Hash | 
					
						
							|  |  |  | 	Header           *types.Header | 
					
						
							|  |  |  | 	Td               *big.Int | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 	Proof            *NodeSet | 
					
						
							| 
									
										
										
										
											2016-10-14 05:47:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *ChtRequest) StoreResult(db ethdb.Database) { | 
					
						
							|  |  |  | 	hash, num := req.Header.Hash(), req.Header.Number.Uint64() | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
											
										 
											2019-06-28 15:34:02 +08:00
										 |  |  | 	if !req.Untrusted { | 
					
						
							|  |  |  | 		rawdb.WriteHeader(db, req.Header) | 
					
						
							|  |  |  | 		rawdb.WriteTd(db, hash, num, req.Td) | 
					
						
							|  |  |  | 		rawdb.WriteCanonicalHash(db, hash, num) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure | 
					
						
							|  |  |  | type BloomRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							| 
									
										
										
										
											2018-09-20 14:11:14 +03:00
										 |  |  | 	Config           *IndexerConfig | 
					
						
							|  |  |  | 	BloomTrieNum     uint64 | 
					
						
							|  |  |  | 	BitIdx           uint | 
					
						
							|  |  |  | 	SectionIndexList []uint64 | 
					
						
							|  |  |  | 	BloomTrieRoot    common.Hash | 
					
						
							|  |  |  | 	BloomBits        [][]byte | 
					
						
							|  |  |  | 	Proofs           *NodeSet | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *BloomRequest) StoreResult(db ethdb.Database) { | 
					
						
							| 
									
										
										
										
											2018-09-20 14:11:14 +03:00
										 |  |  | 	for i, sectionIdx := range req.SectionIndexList { | 
					
						
							| 
									
										
										
										
											2018-08-28 15:08:16 +08:00
										 |  |  | 		sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*req.Config.BloomTrieSize-1) | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 		// if we don't have the canonical hash stored for this section head number, we'll still store it under | 
					
						
							|  |  |  | 		// a key with a zero sectionHead. GetBloomBits will look there too if we still don't have the canonical | 
					
						
							|  |  |  | 		// hash. In the unlikely case we've retrieved the section head hash since then, we'll just retrieve the | 
					
						
							|  |  |  | 		// bit vector again from the network. | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		rawdb.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i]) | 
					
						
							| 
									
										
										
										
											2017-10-24 15:19:09 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-30 13:34:19 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-13 13:41:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TxStatus describes the status of a transaction | 
					
						
							|  |  |  | type TxStatus struct { | 
					
						
							|  |  |  | 	Status core.TxStatus | 
					
						
							|  |  |  | 	Lookup *rawdb.LegacyTxLookupEntry `rlp:"nil"` | 
					
						
							|  |  |  | 	Error  string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TxStatusRequest is the ODR request type for retrieving transaction status | 
					
						
							|  |  |  | type TxStatusRequest struct { | 
					
						
							|  |  |  | 	OdrRequest | 
					
						
							|  |  |  | 	Hashes []common.Hash | 
					
						
							|  |  |  | 	Status []TxStatus | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // StoreResult stores the retrieved data in local database | 
					
						
							|  |  |  | func (req *TxStatusRequest) StoreResult(db ethdb.Database) {} |