2016-06-20 14:47:10 +00: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 implements on-demand retrieval capable state and chain objects
|
|
|
|
// for the Ethereum Light Client.
|
|
|
|
package light
|
|
|
|
|
|
|
|
import (
|
2017-05-01 11:09:48 +00:00
|
|
|
"context"
|
2016-07-26 02:54:35 +00:00
|
|
|
"math/big"
|
|
|
|
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NoOdr is the default context passed to an ODR capable function when the ODR
|
|
|
|
// service is not required.
|
|
|
|
var NoOdr = context.Background()
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
// OdrBackend is an interface to a backend service that handles ODR retrievals type
|
2016-06-20 14:47:10 +00:00
|
|
|
type OdrBackend interface {
|
|
|
|
Database() ethdb.Database
|
2018-01-15 20:26:41 +00:00
|
|
|
ChtIndexer() *core.ChainIndexer
|
|
|
|
BloomTrieIndexer() *core.ChainIndexer
|
|
|
|
BloomIndexer() *core.ChainIndexer
|
2016-06-20 14:47:10 +00:00
|
|
|
Retrieve(ctx context.Context, req OdrRequest) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// OdrRequest is an interface for retrieval requests
|
|
|
|
type OdrRequest interface {
|
|
|
|
StoreResult(db ethdb.Database)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrieID identifies a state or account storage trie
|
|
|
|
type TrieID struct {
|
|
|
|
BlockHash, Root common.Hash
|
2017-05-01 11:09:48 +00:00
|
|
|
BlockNumber uint64
|
2016-06-20 14:47:10 +00: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{
|
2017-05-01 11:09:48 +00:00
|
|
|
BlockHash: header.Hash(),
|
|
|
|
BlockNumber: header.Number.Uint64(),
|
|
|
|
AccKey: nil,
|
|
|
|
Root: header.Root,
|
2016-06-20 14:47:10 +00: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-08-04 16:14:17 +00:00
|
|
|
func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID {
|
2016-06-20 14:47:10 +00:00
|
|
|
return &TrieID{
|
2017-05-01 11:09:48 +00:00
|
|
|
BlockHash: state.BlockHash,
|
|
|
|
BlockNumber: state.BlockNumber,
|
2017-08-04 16:14:17 +00:00
|
|
|
AccKey: addrHash[:],
|
2017-05-01 11:09:48 +00:00
|
|
|
Root: root,
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrieRequest is the ODR request type for state/storage trie entries
|
|
|
|
type TrieRequest struct {
|
|
|
|
OdrRequest
|
|
|
|
Id *TrieID
|
|
|
|
Key []byte
|
2018-01-15 20:26:41 +00:00
|
|
|
Proof *NodeSet
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreResult stores the retrieved data in local database
|
|
|
|
func (req *TrieRequest) StoreResult(db ethdb.Database) {
|
2018-01-15 20:26:41 +00:00
|
|
|
req.Proof.Store(db)
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CodeRequest is the ODR request type for retrieving contract code
|
|
|
|
type CodeRequest struct {
|
|
|
|
OdrRequest
|
2017-08-04 16:14:17 +00:00
|
|
|
Id *TrieID // references storage trie of the account
|
2016-06-20 14:47:10 +00:00
|
|
|
Hash common.Hash
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-07-26 02:54:35 +00:00
|
|
|
Hash common.Hash
|
2016-07-03 19:44:31 +00:00
|
|
|
Number uint64
|
2016-07-26 02:54:35 +00:00
|
|
|
Rlp []byte
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreResult stores the retrieved data in local database
|
|
|
|
func (req *BlockRequest) StoreResult(db ethdb.Database) {
|
2016-07-03 19:44:31 +00:00
|
|
|
core.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiptsRequest is the ODR request type for retrieving block bodies
|
|
|
|
type ReceiptsRequest struct {
|
|
|
|
OdrRequest
|
|
|
|
Hash common.Hash
|
2016-07-26 02:54:35 +00:00
|
|
|
Number uint64
|
2016-06-20 14:47:10 +00:00
|
|
|
Receipts types.Receipts
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreResult stores the retrieved data in local database
|
|
|
|
func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
|
2016-07-03 19:44:31 +00:00
|
|
|
core.WriteBlockReceipts(db, req.Hash, req.Number, req.Receipts)
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
// ChtRequest is the ODR request type for state/storage trie entries
|
2016-07-26 02:54:35 +00:00
|
|
|
type ChtRequest struct {
|
|
|
|
OdrRequest
|
|
|
|
ChtNum, BlockNum uint64
|
|
|
|
ChtRoot common.Hash
|
|
|
|
Header *types.Header
|
|
|
|
Td *big.Int
|
2018-01-15 20:26:41 +00:00
|
|
|
Proof *NodeSet
|
2016-07-26 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreResult stores the retrieved data in local database
|
|
|
|
func (req *ChtRequest) StoreResult(db ethdb.Database) {
|
|
|
|
// if there is a canonical hash, there is a header too
|
|
|
|
core.WriteHeader(db, req.Header)
|
|
|
|
hash, num := req.Header.Hash(), req.Header.Number.Uint64()
|
|
|
|
core.WriteTd(db, hash, num, req.Td)
|
|
|
|
core.WriteCanonicalHash(db, hash, num)
|
2018-01-15 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
|
|
|
|
type BloomRequest struct {
|
|
|
|
OdrRequest
|
|
|
|
BloomTrieNum uint64
|
|
|
|
BitIdx uint
|
|
|
|
SectionIdxList []uint64
|
|
|
|
BloomTrieRoot common.Hash
|
|
|
|
BloomBits [][]byte
|
|
|
|
Proofs *NodeSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreResult stores the retrieved data in local database
|
|
|
|
func (req *BloomRequest) StoreResult(db ethdb.Database) {
|
|
|
|
for i, sectionIdx := range req.SectionIdxList {
|
|
|
|
sectionHead := core.GetCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
|
|
|
|
// 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.
|
|
|
|
core.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i])
|
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
}
|