2018-02-14 12:49:11 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-10-24 13:19:09 +00:00
|
|
|
// 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 (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/bitutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-10-24 13:19:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-02-11 12:57:46 +00:00
|
|
|
// CHTFrequencyClient is the block frequency for creating CHTs on the client side.
|
|
|
|
CHTFrequencyClient = 32768
|
|
|
|
|
|
|
|
// CHTFrequencyServer is the block frequency for creating CHTs on the server side.
|
|
|
|
// Eventually this can be merged back with the client version, but that requires a
|
|
|
|
// full database upgrade, so that should be left for a suitable moment.
|
|
|
|
CHTFrequencyServer = 4096
|
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
HelperTrieConfirmations = 2048 // number of confirmations before a server is expected to have the given HelperTrie available
|
|
|
|
HelperTrieProcessConfirmations = 256 // number of confirmations before a HelperTrie is generated
|
|
|
|
)
|
|
|
|
|
|
|
|
// trustedCheckpoint represents a set of post-processed trie roots (CHT and BloomTrie) associated with
|
|
|
|
// the appropriate section index and head hash. It is used to start light syncing from this checkpoint
|
|
|
|
// and avoid downloading the entire header chain while still being able to securely access old headers/logs.
|
|
|
|
type trustedCheckpoint struct {
|
|
|
|
name string
|
|
|
|
sectionIdx uint64
|
|
|
|
sectionHead, chtRoot, bloomTrieRoot common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mainnetCheckpoint = trustedCheckpoint{
|
2018-02-08 05:49:23 +00:00
|
|
|
name: "mainnet",
|
2018-06-11 07:38:05 +00:00
|
|
|
sectionIdx: 174,
|
|
|
|
sectionHead: common.HexToHash("a3ef48cd8f1c3a08419f0237fc7763491fe89497b3144b17adf87c1c43664613"),
|
|
|
|
chtRoot: common.HexToHash("dcbeed9f4dea1b3cb75601bb27c51b9960c28e5850275402ac49a150a667296e"),
|
|
|
|
bloomTrieRoot: common.HexToHash("6b7497a4a03e33870a2383cb6f5e70570f12b1bf5699063baf8c71d02ca90b02"),
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ropstenCheckpoint = trustedCheckpoint{
|
2018-02-08 05:49:23 +00:00
|
|
|
name: "ropsten",
|
2018-06-11 07:38:05 +00:00
|
|
|
sectionIdx: 102,
|
|
|
|
sectionHead: common.HexToHash("9017ab08465cb2b2dee035ee5b817bbd7fa28e2c8d2cd903e0aed1cccb249e89"),
|
|
|
|
chtRoot: common.HexToHash("f61c10a7a787a5ef15f0ae1ae6c13c64331e57e79d0466d2bd9b0c06833fe956"),
|
|
|
|
bloomTrieRoot: common.HexToHash("69f2ad19aa46d5213a90137b3d2c9bff8a7c9483f7170f0125096ff450c9a873"),
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// trustedCheckpoints associates each known checkpoint with the genesis hash of the chain it belongs to
|
|
|
|
var trustedCheckpoints = map[common.Hash]trustedCheckpoint{
|
|
|
|
params.MainnetGenesisHash: mainnetCheckpoint,
|
|
|
|
params.TestnetGenesisHash: ropstenCheckpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoTrustedCht = errors.New("No trusted canonical hash trie")
|
|
|
|
ErrNoTrustedBloomTrie = errors.New("No trusted bloom trie")
|
|
|
|
ErrNoHeader = errors.New("Header not found")
|
|
|
|
chtPrefix = []byte("chtRoot-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash
|
|
|
|
ChtTablePrefix = "cht-"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChtNode structures are stored in the Canonical Hash Trie in an RLP encoded format
|
|
|
|
type ChtNode struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Td *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChtRoot reads the CHT root assoctiated to the given section from the database
|
|
|
|
// Note that sectionIdx is specified according to LES/1 CHT section size
|
|
|
|
func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
|
|
|
data, _ := db.Get(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...))
|
|
|
|
return common.BytesToHash(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChtV2Root reads the CHT root assoctiated to the given section from the database
|
|
|
|
// Note that sectionIdx is specified according to LES/2 CHT section size
|
|
|
|
func GetChtV2Root(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
2018-02-11 12:57:46 +00:00
|
|
|
return GetChtRoot(db, (sectionIdx+1)*(CHTFrequencyClient/CHTFrequencyServer)-1, sectionHead)
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StoreChtRoot writes the CHT root assoctiated to the given section into the database
|
|
|
|
// Note that sectionIdx is specified according to LES/1 CHT section size
|
|
|
|
func StoreChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
|
|
|
db.Put(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChtIndexerBackend implements core.ChainIndexerBackend
|
|
|
|
type ChtIndexerBackend struct {
|
2018-02-05 16:40:32 +00:00
|
|
|
diskdb ethdb.Database
|
|
|
|
triedb *trie.Database
|
2017-10-24 13:19:09 +00:00
|
|
|
section, sectionSize uint64
|
|
|
|
lastHash common.Hash
|
|
|
|
trie *trie.Trie
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBloomTrieIndexer creates a BloomTrie chain indexer
|
|
|
|
func NewChtIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer {
|
|
|
|
var sectionSize, confirmReq uint64
|
|
|
|
if clientMode {
|
2018-02-11 12:57:46 +00:00
|
|
|
sectionSize = CHTFrequencyClient
|
2017-10-24 13:19:09 +00:00
|
|
|
confirmReq = HelperTrieConfirmations
|
|
|
|
} else {
|
2018-02-11 12:57:46 +00:00
|
|
|
sectionSize = CHTFrequencyServer
|
2017-10-24 13:19:09 +00:00
|
|
|
confirmReq = HelperTrieProcessConfirmations
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
idb := ethdb.NewTable(db, "chtIndex-")
|
|
|
|
backend := &ChtIndexerBackend{
|
|
|
|
diskdb: db,
|
|
|
|
triedb: trie.NewDatabase(ethdb.NewTable(db, ChtTablePrefix)),
|
|
|
|
sectionSize: sectionSize,
|
|
|
|
}
|
|
|
|
return core.NewChainIndexer(db, idb, backend, sectionSize, confirmReq, time.Millisecond*100, "cht")
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset implements core.ChainIndexerBackend
|
|
|
|
func (c *ChtIndexerBackend) Reset(section uint64, lastSectionHead common.Hash) error {
|
|
|
|
var root common.Hash
|
|
|
|
if section > 0 {
|
2018-02-05 16:40:32 +00:00
|
|
|
root = GetChtRoot(c.diskdb, section-1, lastSectionHead)
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
var err error
|
2018-02-05 16:40:32 +00:00
|
|
|
c.trie, err = trie.New(root, c.triedb)
|
2017-10-24 13:19:09 +00:00
|
|
|
c.section = section
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements core.ChainIndexerBackend
|
|
|
|
func (c *ChtIndexerBackend) Process(header *types.Header) {
|
|
|
|
hash, num := header.Hash(), header.Number.Uint64()
|
|
|
|
c.lastHash = hash
|
|
|
|
|
2018-05-07 11:35:06 +00:00
|
|
|
td := rawdb.ReadTd(c.diskdb, hash, num)
|
2017-10-24 13:19:09 +00:00
|
|
|
if td == nil {
|
|
|
|
panic(nil)
|
|
|
|
}
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], num)
|
|
|
|
data, _ := rlp.EncodeToBytes(ChtNode{hash, td})
|
|
|
|
c.trie.Update(encNumber[:], data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements core.ChainIndexerBackend
|
|
|
|
func (c *ChtIndexerBackend) Commit() error {
|
2018-02-05 16:40:32 +00:00
|
|
|
root, err := c.trie.Commit(nil)
|
2017-10-24 13:19:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
c.triedb.Commit(root, false)
|
|
|
|
|
2018-02-11 12:57:46 +00:00
|
|
|
if ((c.section+1)*c.sectionSize)%CHTFrequencyClient == 0 {
|
|
|
|
log.Info("Storing CHT", "section", c.section*c.sectionSize/CHTFrequencyClient, "head", c.lastHash, "root", root)
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
|
|
|
StoreChtRoot(c.diskdb, c.section, c.lastHash, root)
|
2017-10-24 13:19:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
BloomTrieFrequency = 32768
|
|
|
|
ethBloomBitsSection = 4096
|
|
|
|
ethBloomBitsConfirmations = 256
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
|
|
|
|
BloomTrieTablePrefix = "blt-"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetBloomTrieRoot reads the BloomTrie root assoctiated to the given section from the database
|
|
|
|
func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
|
|
|
data, _ := db.Get(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...))
|
|
|
|
return common.BytesToHash(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreBloomTrieRoot writes the BloomTrie root assoctiated to the given section into the database
|
|
|
|
func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
|
|
|
db.Put(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// BloomTrieIndexerBackend implements core.ChainIndexerBackend
|
|
|
|
type BloomTrieIndexerBackend struct {
|
2018-02-05 16:40:32 +00:00
|
|
|
diskdb ethdb.Database
|
|
|
|
triedb *trie.Database
|
2017-10-24 13:19:09 +00:00
|
|
|
section, parentSectionSize, bloomTrieRatio uint64
|
|
|
|
trie *trie.Trie
|
|
|
|
sectionHeads []common.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBloomTrieIndexer creates a BloomTrie chain indexer
|
|
|
|
func NewBloomTrieIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer {
|
2018-02-05 16:40:32 +00:00
|
|
|
backend := &BloomTrieIndexerBackend{
|
|
|
|
diskdb: db,
|
|
|
|
triedb: trie.NewDatabase(ethdb.NewTable(db, BloomTrieTablePrefix)),
|
|
|
|
}
|
2017-10-24 13:19:09 +00:00
|
|
|
idb := ethdb.NewTable(db, "bltIndex-")
|
2018-02-05 16:40:32 +00:00
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
var confirmReq uint64
|
|
|
|
if clientMode {
|
|
|
|
backend.parentSectionSize = BloomTrieFrequency
|
|
|
|
confirmReq = HelperTrieConfirmations
|
|
|
|
} else {
|
|
|
|
backend.parentSectionSize = ethBloomBitsSection
|
|
|
|
confirmReq = HelperTrieProcessConfirmations
|
|
|
|
}
|
|
|
|
backend.bloomTrieRatio = BloomTrieFrequency / backend.parentSectionSize
|
|
|
|
backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio)
|
|
|
|
return core.NewChainIndexer(db, idb, backend, BloomTrieFrequency, confirmReq-ethBloomBitsConfirmations, time.Millisecond*100, "bloomtrie")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset implements core.ChainIndexerBackend
|
|
|
|
func (b *BloomTrieIndexerBackend) Reset(section uint64, lastSectionHead common.Hash) error {
|
|
|
|
var root common.Hash
|
|
|
|
if section > 0 {
|
2018-02-05 16:40:32 +00:00
|
|
|
root = GetBloomTrieRoot(b.diskdb, section-1, lastSectionHead)
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
|
|
|
var err error
|
2018-02-05 16:40:32 +00:00
|
|
|
b.trie, err = trie.New(root, b.triedb)
|
2017-10-24 13:19:09 +00:00
|
|
|
b.section = section
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements core.ChainIndexerBackend
|
|
|
|
func (b *BloomTrieIndexerBackend) Process(header *types.Header) {
|
|
|
|
num := header.Number.Uint64() - b.section*BloomTrieFrequency
|
|
|
|
if (num+1)%b.parentSectionSize == 0 {
|
|
|
|
b.sectionHeads[num/b.parentSectionSize] = header.Hash()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements core.ChainIndexerBackend
|
|
|
|
func (b *BloomTrieIndexerBackend) Commit() error {
|
|
|
|
var compSize, decompSize uint64
|
|
|
|
|
|
|
|
for i := uint(0); i < types.BloomBitLength; i++ {
|
|
|
|
var encKey [10]byte
|
|
|
|
binary.BigEndian.PutUint16(encKey[0:2], uint16(i))
|
|
|
|
binary.BigEndian.PutUint64(encKey[2:10], b.section)
|
|
|
|
var decomp []byte
|
|
|
|
for j := uint64(0); j < b.bloomTrieRatio; j++ {
|
2018-05-07 11:35:06 +00:00
|
|
|
data, err := rawdb.ReadBloomBits(b.diskdb, i, b.section*b.bloomTrieRatio+j, b.sectionHeads[j])
|
2017-10-24 13:19:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
decompData, err2 := bitutil.DecompressBytes(data, int(b.parentSectionSize/8))
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
decomp = append(decomp, decompData...)
|
|
|
|
}
|
|
|
|
comp := bitutil.CompressBytes(decomp)
|
|
|
|
|
|
|
|
decompSize += uint64(len(decomp))
|
|
|
|
compSize += uint64(len(comp))
|
|
|
|
if len(comp) > 0 {
|
|
|
|
b.trie.Update(encKey[:], comp)
|
|
|
|
} else {
|
|
|
|
b.trie.Delete(encKey[:])
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
root, err := b.trie.Commit(nil)
|
2017-10-24 13:19:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
b.triedb.Commit(root, false)
|
|
|
|
|
|
|
|
sectionHead := b.sectionHeads[b.bloomTrieRatio-1]
|
2018-02-11 12:57:46 +00:00
|
|
|
log.Info("Storing bloom trie", "section", b.section, "head", sectionHead, "root", root, "compression", float64(compSize)/float64(decompSize))
|
2018-02-05 16:40:32 +00:00
|
|
|
StoreBloomTrieRoot(b.diskdb, b.section, sectionHead, root)
|
2017-10-24 13:19:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|