mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
light, les: watch checkpoint event and update in local
This commit is contained in:
parent
2da657de34
commit
88004ccbc2
11 changed files with 258 additions and 79 deletions
|
|
@ -26,11 +26,16 @@ import (
|
|||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
var (
|
||||
MainNetAddr = common.HexToAddress("")
|
||||
TestNetAddr = common.HexToAddress("0x3b934494985d17bcb49557671e1bc8ec32cccdd5") // Rinkeby
|
||||
// registrar contract address for mainnet or test chain.
|
||||
RegistrarAddr = map[common.Hash]common.Address{
|
||||
params.MainnetGenesisHash: common.HexToAddress(""),
|
||||
params.TestnetGenesisHash: common.HexToAddress(""),
|
||||
params.RinkebyGenesisHash: common.HexToAddress("0x3b934494985d17bcb49557671e1bc8ec32cccdd5"),
|
||||
}
|
||||
)
|
||||
|
||||
var errEventNotFound = errors.New("contract event not found")
|
||||
|
|
@ -40,18 +45,6 @@ const (
|
|||
checkpointConfirmation = 10000 // The number of confirmations needed before a checkpoint becoming stable.
|
||||
)
|
||||
|
||||
// Checkpoint 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 Checkpoint struct {
|
||||
SectionIndex uint64
|
||||
SectionHead common.Hash // Block Hash for the last block in the section
|
||||
ChtRoot common.Hash // CHT(Canonical Hash Trie) root associated to the section
|
||||
BloomTrieRoot common.Hash // Bloom Trie root associated to the section
|
||||
}
|
||||
|
||||
type Registrar struct {
|
||||
contract *contract.Contract
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,15 +27,16 @@ import (
|
|||
"github.com/ethereum/go-ethereum/contracts/registrar/contract"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/light"
|
||||
)
|
||||
|
||||
var (
|
||||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
key, _ = crypto.GenerateKey()
|
||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||
emptyHash = [32]byte{}
|
||||
|
||||
trustedCheckpoint = Checkpoint{
|
||||
SectionIndex: 0,
|
||||
trustedCheckpoint = light.TrustedCheckpoint{
|
||||
SectionIdx: 0,
|
||||
SectionHead: common.HexToHash("14c8639dfc32812ed20839f5a11993cd59b22e5226cb2179640ba5c1f0c08f87"),
|
||||
ChtRoot: common.HexToHash("cf92fd2a79464354e8dae4d589ae92acdf90a3a4f8f7d8a3ec5fb9c114ae81cd"),
|
||||
BloomTrieRoot: common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"),
|
||||
|
|
@ -115,10 +116,10 @@ func TestCheckpointRegister(t *testing.T) {
|
|||
contractBackend.Commit()
|
||||
|
||||
// Register an unstable checkpoint
|
||||
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIndex)), trustedCheckpoint.SectionHead,
|
||||
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIdx)), trustedCheckpoint.SectionHead,
|
||||
trustedCheckpoint.ChtRoot, trustedCheckpoint.BloomTrieRoot)
|
||||
contractBackend.Commit()
|
||||
head, chtRoot, bloomTrieRoot, err := contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIndex)))
|
||||
head, chtRoot, bloomTrieRoot, err := contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIdx)))
|
||||
if err != nil {
|
||||
t.Error("fetch checkpoint failed", err)
|
||||
}
|
||||
|
|
@ -128,10 +129,10 @@ func TestCheckpointRegister(t *testing.T) {
|
|||
|
||||
// Register a stable checkpoint
|
||||
contractBackend.ShiftBlocks(sectionSize + checkpointConfirmation)
|
||||
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIndex)), trustedCheckpoint.SectionHead,
|
||||
contract.SetCheckpoint(transactOpts, big.NewInt(int64(trustedCheckpoint.SectionIdx)), trustedCheckpoint.SectionHead,
|
||||
trustedCheckpoint.ChtRoot, trustedCheckpoint.BloomTrieRoot)
|
||||
contractBackend.Commit()
|
||||
head, chtRoot, bloomTrieRoot, err = contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIndex)))
|
||||
head, chtRoot, bloomTrieRoot, err = contract.GetCheckpoint(nil, big.NewInt(int64(trustedCheckpoint.SectionIdx)))
|
||||
if err != nil {
|
||||
t.Error("fetch checkpoint failed", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,7 +89,11 @@ func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Add
|
|||
// SuggestGasPrice implements bind.ContractTransactor retrieving the currently
|
||||
// suggested gas price to allow a timely execution of a transaction.
|
||||
func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
|
||||
return b.eapi.GasPrice(ctx)
|
||||
price, err := b.eapi.GasPrice(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*big.Int)(price), nil
|
||||
}
|
||||
|
||||
// EstimateGasLimit implements bind.ContractTransactor trying to estimate the gas
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import (
|
|||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/contracts/registrar"
|
||||
"github.com/ethereum/go-ethereum/contracts/registrar/contract"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
|
@ -44,8 +45,9 @@ type LesServer struct {
|
|||
privateKey *ecdsa.PrivateKey
|
||||
quitSync chan struct{}
|
||||
|
||||
// Checkpoint contract relative fields
|
||||
// Checkpoint relative fields
|
||||
registrar *registrar.Registrar // Handler for checkpoint contract
|
||||
stableCheckpoint *light.TrustedCheckpoint // The nearest stable checkpoint
|
||||
|
||||
// Indexers
|
||||
chtIndexer *core.ChainIndexer // Indexers for creating cht root for each block section
|
||||
|
|
@ -102,6 +104,13 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
|||
}
|
||||
srv.fcManager = flowcontrol.NewClientManager(uint64(config.LightServ), 10, 1000000000)
|
||||
srv.fcCostStats = newCostStats(eth.ChainDb())
|
||||
if addr, ok := registrar.RegistrarAddr[eth.BlockChain().Genesis().Hash()]; ok {
|
||||
registrar, err := registrar.NewRegistrar(addr, eth.APIBackend, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
srv.registrar = registrar
|
||||
}
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
|
|
@ -126,6 +135,9 @@ func (s *LesServer) Start(srvr *p2p.Server) {
|
|||
}
|
||||
s.privateKey = srvr.PrivateKey
|
||||
s.protocolManager.blockLoop()
|
||||
if s.registrar != nil {
|
||||
s.checkpointLoop()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LesServer) SetBloomBitsIndexer(bloomIndexer *core.ChainIndexer) {
|
||||
|
|
@ -142,6 +154,43 @@ func (s *LesServer) Stop() {
|
|||
<-s.protocolManager.noMorePeers
|
||||
}()
|
||||
s.protocolManager.Stop()
|
||||
close(s.quitSync)
|
||||
}
|
||||
|
||||
// checkpointLoop starts a standalone goroutine to watch new checkpoint event and upgrades local's stable checkpoint.
|
||||
func (s *LesServer) checkpointLoop() (err error) {
|
||||
sink := make(chan *contract.ContractNewCheckpointEvent)
|
||||
sub, err := s.registrar.WatchNewCheckpointEvent(sink)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
sub.Unsubscribe()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case event := <-sink:
|
||||
// New stable checkpoint received
|
||||
// Note several duplicate events can be received due to chain reorg, just track the first arrive one.
|
||||
if event.Index.Uint64() > s.stableCheckpoint.SectionIdx {
|
||||
checkpoint := &light.TrustedCheckpoint{
|
||||
SectionIdx: event.Index.Uint64(),
|
||||
SectionHead: common.Hash(event.SectionHead),
|
||||
ChtRoot: common.Hash(event.ChtRoot),
|
||||
BloomTrieRoot: common.Hash(event.BloomTrieRoot),
|
||||
}
|
||||
light.WriteTrustedCheckpoint(s.protocolManager.chainDb, checkpoint)
|
||||
s.stableCheckpoint = checkpoint
|
||||
log.Info("update checkpoint", "section", checkpoint.SectionIdx, "head", checkpoint.SectionHead.Hex(),
|
||||
"chtRoot", checkpoint.ChtRoot.Hex(), "bloomTrieRoot", checkpoint.BloomTrieRoot.Hex())
|
||||
}
|
||||
|
||||
case <-s.quitSync:
|
||||
// Les server is closed.
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *ProtocolManager) blockLoop() {
|
||||
|
|
|
|||
120
light/checkpoint.go
Normal file
120
light/checkpoint.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// Copyright 2018 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 (
|
||||
"io"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
var (
|
||||
// checkpointKey tracks the latest stable checkpoint.
|
||||
checkpointKey = []byte("Checkpoint")
|
||||
)
|
||||
|
||||
// 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 // Indicator which chain the checkpoint belongs to
|
||||
SectionIdx uint64 // Section index
|
||||
SectionHead common.Hash // Block Hash for the last block in the section
|
||||
ChtRoot common.Hash // CHT(Canonical Hash Trie) root associated to the section
|
||||
BloomTrieRoot common.Hash // Bloom Trie root associated to the section
|
||||
}
|
||||
|
||||
type trustCheckpointRLP struct {
|
||||
SectionIdx uint64
|
||||
SectionHead common.Hash
|
||||
ChtRoot common.Hash
|
||||
BloomTrieRoot common.Hash
|
||||
}
|
||||
|
||||
// EncodeRLP implements rlp.Encoder, and flattens the necessary fields of a checkpoint
|
||||
// into an RLP stream.
|
||||
func (c *TrustedCheckpoint) EncodeRLP(w io.Writer) (err error) {
|
||||
return rlp.Encode(w, &trustCheckpointRLP{c.SectionIdx, c.SectionHead, c.ChtRoot, c.BloomTrieRoot})
|
||||
}
|
||||
|
||||
// DecodeRLP implements rlp.Decoder, and loads the necessary fields of a checkpoint
|
||||
// from an RLP stream.
|
||||
func (c *TrustedCheckpoint) DecodeRLP(s *rlp.Stream) error {
|
||||
var dec trustCheckpointRLP
|
||||
if err := s.Decode(&dec); err != nil {
|
||||
return err
|
||||
}
|
||||
c.SectionIdx, c.SectionHead, c.ChtRoot, c.BloomTrieRoot = dec.SectionIdx, dec.SectionHead, dec.ChtRoot, dec.BloomTrieRoot
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
// Hardcode checkpoint for mainnet and testnet(ropsten). Will be deleted eventually once checkpoint contract
|
||||
// works.
|
||||
mainnetCheckpoint = TrustedCheckpoint{
|
||||
Name: "mainnet",
|
||||
SectionIdx: 179,
|
||||
SectionHead: common.HexToHash("ae778e455492db1183e566fa0c67f954d256fdd08618f6d5a393b0e24576d0ea"),
|
||||
ChtRoot: common.HexToHash("646b338f9ca74d936225338916be53710ec84020b89946004a8605f04c817f16"),
|
||||
BloomTrieRoot: common.HexToHash("d0f978f5dbc86e5bf931d8dd5b2ecbebbda6dc78f8896af6a27b46a3ced0ac25"),
|
||||
}
|
||||
|
||||
ropstenCheckpoint = TrustedCheckpoint{
|
||||
Name: "ropsten",
|
||||
SectionIdx: 107,
|
||||
SectionHead: common.HexToHash("e1988f95399debf45b873e065e5cd61b416ef2e2e5deec5a6f87c3127086e1ce"),
|
||||
ChtRoot: common.HexToHash("15cba18e4de0ab1e95e202625199ba30147aec8b0b70384b66ebea31ba6a18e0"),
|
||||
BloomTrieRoot: common.HexToHash("e00fa6389b2e597d9df52172cd8e936879eed0fca4fa59db99e2c8ed682562f2"),
|
||||
}
|
||||
)
|
||||
|
||||
// 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,
|
||||
}
|
||||
|
||||
// ReadTrustedCheckpoint retrieves the checkpoint from the database.
|
||||
func ReadTrustedCheckpoint(db ethdb.Database) *TrustedCheckpoint {
|
||||
data, err := db.Get(checkpointKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
c := new(TrustedCheckpoint)
|
||||
if err := rlp.DecodeBytes(data, c); err != nil {
|
||||
log.Error("Invalid checkpoint RLP", "err", err)
|
||||
return nil
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// WriteTrustedCheckpoint stores an RLP encoded checkpoint into the database.
|
||||
func WriteTrustedCheckpoint(db ethdb.Putter, checkpoint *TrustedCheckpoint) {
|
||||
data, err := rlp.EncodeToBytes(checkpoint)
|
||||
if err != nil {
|
||||
log.Crit("Failed to RLP encode checkpoint", err)
|
||||
}
|
||||
if err := db.Put(checkpointKey, data); err != nil {
|
||||
log.Crit("Failed to store checkpoint", "err", err)
|
||||
}
|
||||
}
|
||||
45
light/checkpoint_test.go
Normal file
45
light/checkpoint_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2018 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 (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
)
|
||||
|
||||
var testCheckpoint = &TrustedCheckpoint{
|
||||
Name: "test",
|
||||
SectionIdx: 100,
|
||||
SectionHead: common.HexToHash("0xbeef"),
|
||||
ChtRoot: common.HexToHash("0xdead"),
|
||||
BloomTrieRoot: common.HexToHash("0xdeadbeef"),
|
||||
}
|
||||
|
||||
func TestRWCheckpoint(t *testing.T) {
|
||||
mdb := ethdb.NewMemDatabase()
|
||||
WriteTrustedCheckpoint(mdb, testCheckpoint)
|
||||
if !assertCheckpointEqual(testCheckpoint, ReadTrustedCheckpoint(mdb)) {
|
||||
t.Error("the checkpoint retrieved from database is different")
|
||||
}
|
||||
}
|
||||
|
||||
func assertCheckpointEqual(ckp1, ckp2 *TrustedCheckpoint) bool {
|
||||
return ckp1.SectionIdx == ckp2.SectionIdx && ckp1.SectionHead == ckp2.SectionHead && ckp1.ChtRoot == ckp2.ChtRoot &&
|
||||
ckp1.BloomTrieRoot == ckp2.BloomTrieRoot
|
||||
}
|
||||
|
|
@ -98,7 +98,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
|
|||
if bc.genesisBlock == nil {
|
||||
return nil, core.ErrNoGenesis
|
||||
}
|
||||
if cp, ok := trustedCheckpoints[bc.genesisBlock.Hash()]; ok {
|
||||
if cp, ok := TrustedCheckpoints[bc.genesisBlock.Hash()]; ok {
|
||||
bc.addTrustedCheckpoint(cp)
|
||||
}
|
||||
if err := bc.loadLastState(); err != nil {
|
||||
|
|
@ -116,19 +116,19 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
|
|||
}
|
||||
|
||||
// addTrustedCheckpoint adds a trusted checkpoint to the blockchain
|
||||
func (self *LightChain) addTrustedCheckpoint(cp trustedCheckpoint) {
|
||||
func (self *LightChain) addTrustedCheckpoint(cp TrustedCheckpoint) {
|
||||
if self.odr.ChtIndexer() != nil {
|
||||
StoreChtRoot(self.chainDb, cp.sectionIdx, cp.sectionHead, cp.chtRoot)
|
||||
self.odr.ChtIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
|
||||
StoreChtRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.ChtRoot)
|
||||
self.odr.ChtIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
|
||||
}
|
||||
if self.odr.BloomTrieIndexer() != nil {
|
||||
StoreBloomTrieRoot(self.chainDb, cp.sectionIdx, cp.sectionHead, cp.bloomTrieRoot)
|
||||
self.odr.BloomTrieIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
|
||||
StoreBloomTrieRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.BloomTrieRoot)
|
||||
self.odr.BloomTrieIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
|
||||
}
|
||||
if self.odr.BloomIndexer() != nil {
|
||||
self.odr.BloomIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
|
||||
self.odr.BloomIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
|
||||
}
|
||||
log.Info("Added trusted checkpoint", "chain", cp.name, "block", (cp.sectionIdx+1)*CHTFrequencyClient-1, "hash", cp.sectionHead)
|
||||
log.Info("Added trusted checkpoint", "chain", cp.Name, "block", (cp.SectionIdx+1)*CHTFrequencyClient-1, "hash", cp.SectionHead)
|
||||
}
|
||||
|
||||
func (self *LightChain) getProcInterrupt() bool {
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ func (req *BloomRequest) StoreResult(db ethdb.Database) {
|
|||
for i, sectionIdx := range req.SectionIdxList {
|
||||
sectionHead := rawdb.ReadCanonicalHash(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
|
||||
// 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.
|
||||
rawdb.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i])
|
||||
|
|
|
|||
|
|
@ -203,7 +203,7 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi
|
|||
for i, sectionIdx := range sectionIdxList {
|
||||
sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
|
||||
// if we don't have the canonical hash stored for this section head number, we'll still look for
|
||||
// an entry with a zero sectionHead (we store it with zero section head too if we don't know it
|
||||
// an entry with a zero SectionHead (we store it with zero section head too if we don't know it
|
||||
// at the time of the retrieval)
|
||||
bloomBits, err := rawdb.ReadBloomBits(db, bitIdx, sectionIdx, sectionHead)
|
||||
if err == nil {
|
||||
|
|
|
|||
|
|
@ -29,11 +29,18 @@ import (
|
|||
"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"
|
||||
)
|
||||
|
||||
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-"
|
||||
)
|
||||
|
||||
const (
|
||||
// CHTFrequencyClient is the block frequency for creating CHTs on the client side.
|
||||
CHTFrequencyClient = 32768
|
||||
|
|
@ -47,47 +54,6 @@ const (
|
|||
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{
|
||||
name: "mainnet",
|
||||
sectionIdx: 179,
|
||||
sectionHead: common.HexToHash("ae778e455492db1183e566fa0c67f954d256fdd08618f6d5a393b0e24576d0ea"),
|
||||
chtRoot: common.HexToHash("646b338f9ca74d936225338916be53710ec84020b89946004a8605f04c817f16"),
|
||||
bloomTrieRoot: common.HexToHash("d0f978f5dbc86e5bf931d8dd5b2ecbebbda6dc78f8896af6a27b46a3ced0ac25"),
|
||||
}
|
||||
|
||||
ropstenCheckpoint = trustedCheckpoint{
|
||||
name: "ropsten",
|
||||
sectionIdx: 107,
|
||||
sectionHead: common.HexToHash("e1988f95399debf45b873e065e5cd61b416ef2e2e5deec5a6f87c3127086e1ce"),
|
||||
chtRoot: common.HexToHash("15cba18e4de0ab1e95e202625199ba30147aec8b0b70384b66ebea31ba6a18e0"),
|
||||
bloomTrieRoot: common.HexToHash("e00fa6389b2e597d9df52172cd8e936879eed0fca4fa59db99e2c8ed682562f2"),
|
||||
}
|
||||
)
|
||||
|
||||
// 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
|
||||
|
|
@ -95,7 +61,7 @@ type ChtNode struct {
|
|||
}
|
||||
|
||||
// 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
|
||||
// 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)
|
||||
|
|
@ -104,13 +70,13 @@ func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) c
|
|||
}
|
||||
|
||||
// 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
|
||||
// Note that SectionIdx is specified according to LES/2 CHT section size
|
||||
func GetChtV2Root(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
||||
return GetChtRoot(db, (sectionIdx+1)*(CHTFrequencyClient/CHTFrequencyServer)-1, sectionHead)
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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)
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import (
|
|||
var (
|
||||
MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
|
||||
TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
|
||||
RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
|
|||
Loading…
Reference in a new issue