Skip to content

Commit b69476b

Browse files
rjl493456442karalabe
authored andcommitted
all: make indexer configurable (#17188)
1 parent c64d72b commit b69476b

25 files changed

Lines changed: 514 additions & 322 deletions

‎core/chain_indexer.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ func (c *ChainIndexer) updateLoop() {
322322
updating = false
323323
c.log.Info("Finished upgrading chain index")
324324
}
325-
326325
c.cascadedHead = c.storedSections*c.sectionSize - 1
327326
for _, child := range c.children {
328327
c.log.Trace("Cascading chain index update", "head", c.cascadedHead)

‎eth/backend.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
136136
gasPrice: config.MinerGasPrice,
137137
etherbase: config.Etherbase,
138138
bloomRequests: make(chan chan *bloombits.Retrieval),
139-
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, bloomConfirms),
139+
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
140140
}
141141

142142
log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId)
@@ -426,7 +426,7 @@ func (s *Ethereum) Protocols() []p2p.Protocol {
426426
// Ethereum protocol implementation.
427427
func (s *Ethereum) Start(srvr *p2p.Server) error {
428428
// Start the bloom bits servicing goroutines
429-
s.startBloomHandlers()
429+
s.startBloomHandlers(params.BloomBitsBlocks)
430430

431431
// Start the RPC service
432432
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())

‎eth/bloombits.go‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ethereum/go-ethereum/core/rawdb"
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/ethdb"
30-
"github.com/ethereum/go-ethereum/params"
3130
)
3231

3332
const (
@@ -50,7 +49,7 @@ const (
5049

5150
// startBloomHandlers starts a batch of goroutines to accept bloom bit database
5251
// retrievals from possibly a range of filters and serving the data to satisfy.
53-
func (eth *Ethereum) startBloomHandlers() {
52+
func (eth *Ethereum) startBloomHandlers(sectionSize uint64) {
5453
for i := 0; i < bloomServiceThreads; i++ {
5554
go func() {
5655
for {
@@ -62,9 +61,9 @@ func (eth *Ethereum) startBloomHandlers() {
6261
task := <-request
6362
task.Bitsets = make([][]byte, len(task.Sections))
6463
for i, section := range task.Sections {
65-
head := rawdb.ReadCanonicalHash(eth.chainDb, (section+1)*params.BloomBitsBlocks-1)
64+
head := rawdb.ReadCanonicalHash(eth.chainDb, (section+1)*sectionSize-1)
6665
if compVector, err := rawdb.ReadBloomBits(eth.chainDb, task.Bit, section, head); err == nil {
67-
if blob, err := bitutil.DecompressBytes(compVector, int(params.BloomBitsBlocks)/8); err == nil {
66+
if blob, err := bitutil.DecompressBytes(compVector, int(sectionSize/8)); err == nil {
6867
task.Bitsets[i] = blob
6968
} else {
7069
task.Error = err
@@ -81,10 +80,6 @@ func (eth *Ethereum) startBloomHandlers() {
8180
}
8281

8382
const (
84-
// bloomConfirms is the number of confirmation blocks before a bloom section is
85-
// considered probably final and its rotated bits are calculated.
86-
bloomConfirms = 256
87-
8883
// bloomThrottling is the time to wait between processing two consecutive index
8984
// sections. It's useful during chain upgrades to prevent disk overload.
9085
bloomThrottling = 100 * time.Millisecond
@@ -102,14 +97,14 @@ type BloomIndexer struct {
10297

10398
// NewBloomIndexer returns a chain indexer that generates bloom bits data for the
10499
// canonical chain for fast logs filtering.
105-
func NewBloomIndexer(db ethdb.Database, size, confReq uint64) *core.ChainIndexer {
100+
func NewBloomIndexer(db ethdb.Database, size, confirms uint64) *core.ChainIndexer {
106101
backend := &BloomIndexer{
107102
db: db,
108103
size: size,
109104
}
110105
table := ethdb.NewTable(db, string(rawdb.BloomBitsIndexPrefix))
111106

112-
return core.NewChainIndexer(db, table, backend, size, confReq, bloomThrottling, "bloombits")
107+
return core.NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits")
113108
}
114109

115110
// Reset implements core.ChainIndexerBackend, starting a new bloombits index

‎les/api_backend.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
192192
return 0, 0
193193
}
194194
sections, _, _ := b.eth.bloomIndexer.Sections()
195-
return light.BloomTrieFrequency, sections
195+
return params.BloomBitsBlocksClient, sections
196196
}
197197

198198
func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {

‎les/backend.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
9595
lesCommons: lesCommons{
9696
chainDb: chainDb,
9797
config: config,
98+
iConfig: light.DefaultClientIndexerConfig,
9899
},
99100
chainConfig: chainConfig,
100101
eventMux: ctx.EventMux,
@@ -105,16 +106,16 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
105106
shutdownChan: make(chan bool),
106107
networkId: config.NetworkId,
107108
bloomRequests: make(chan chan *bloombits.Retrieval),
108-
bloomIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency, light.HelperTrieConfirmations),
109+
bloomIndexer: eth.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations),
109110
}
110111

111112
leth.relay = NewLesTxRelay(peers, leth.reqDist)
112113
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg)
113114
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
114115

115-
leth.odr = NewLesOdr(chainDb, leth.retriever)
116-
leth.chtIndexer = light.NewChtIndexer(chainDb, true, leth.odr)
117-
leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, true, leth.odr)
116+
leth.odr = NewLesOdr(chainDb, light.DefaultClientIndexerConfig, leth.retriever)
117+
leth.chtIndexer = light.NewChtIndexer(chainDb, leth.odr, params.CHTFrequencyClient, params.HelperTrieConfirmations)
118+
leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, leth.odr, params.BloomBitsBlocksClient, params.BloomTrieFrequency)
118119
leth.odr.SetIndexers(leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer)
119120

120121
// Note: NewLightChain adds the trusted checkpoint so it needs an ODR with
@@ -135,7 +136,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
135136
}
136137

137138
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
138-
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, leth.serverPool, quitSync, &leth.wg); err != nil {
139+
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, light.DefaultClientIndexerConfig, true, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, leth.serverPool, quitSync, &leth.wg); err != nil {
139140
return nil, err
140141
}
141142
leth.ApiBackend = &LesApiBackend{leth, nil}
@@ -230,8 +231,8 @@ func (s *LightEthereum) Protocols() []p2p.Protocol {
230231
// Start implements node.Service, starting all internal goroutines needed by the
231232
// Ethereum protocol implementation.
232233
func (s *LightEthereum) Start(srvr *p2p.Server) error {
233-
s.startBloomHandlers()
234234
log.Warn("Light client mode is an experimental feature")
235+
s.startBloomHandlers(params.BloomBitsBlocksClient)
235236
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId)
236237
// clients are searching for the first advertised protocol in the list
237238
protocolVersion := AdvertiseProtocolVersions[0]

‎les/bloombits.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const (
4343

4444
// startBloomHandlers starts a batch of goroutines to accept bloom bit database
4545
// retrievals from possibly a range of filters and serving the data to satisfy.
46-
func (eth *LightEthereum) startBloomHandlers() {
46+
func (eth *LightEthereum) startBloomHandlers(sectionSize uint64) {
4747
for i := 0; i < bloomServiceThreads; i++ {
4848
go func() {
4949
for {
@@ -57,7 +57,7 @@ func (eth *LightEthereum) startBloomHandlers() {
5757
compVectors, err := light.GetBloomBits(task.Context, eth.odr, task.Bit, task.Sections)
5858
if err == nil {
5959
for i := range task.Sections {
60-
if blob, err := bitutil.DecompressBytes(compVectors[i], int(light.BloomTrieFrequency/8)); err == nil {
60+
if blob, err := bitutil.DecompressBytes(compVectors[i], int(sectionSize/8)); err == nil {
6161
task.Bitsets[i] = blob
6262
} else {
6363
task.Error = err

‎les/commons.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
// lesCommons contains fields needed by both server and client.
3434
type lesCommons struct {
3535
config *eth.Config
36+
iConfig *light.IndexerConfig
3637
chainDb ethdb.Database
3738
protocolManager *ProtocolManager
3839
chtIndexer, bloomTrieIndexer *core.ChainIndexer
@@ -81,7 +82,7 @@ func (c *lesCommons) nodeInfo() interface{} {
8182

8283
if !c.protocolManager.lightSync {
8384
// convert to client section size if running in server mode
84-
sections /= light.CHTFrequencyClient / light.CHTFrequencyServer
85+
sections /= c.iConfig.PairChtSize / c.iConfig.ChtSize
8586
}
8687

8788
if sections2 < sections {
@@ -94,7 +95,8 @@ func (c *lesCommons) nodeInfo() interface{} {
9495
if c.protocolManager.lightSync {
9596
chtRoot = light.GetChtRoot(c.chainDb, sectionIndex, sectionHead)
9697
} else {
97-
chtRoot = light.GetChtV2Root(c.chainDb, sectionIndex, sectionHead)
98+
idxV2 := (sectionIndex+1)*c.iConfig.PairChtSize/c.iConfig.ChtSize - 1
99+
chtRoot = light.GetChtRoot(c.chainDb, idxV2, sectionHead)
98100
}
99101
cht = light.TrustedCheckpoint{
100102
SectionIdx: sectionIndex,

‎les/handler.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type ProtocolManager struct {
9494
txrelay *LesTxRelay
9595
networkId uint64
9696
chainConfig *params.ChainConfig
97+
iConfig *light.IndexerConfig
9798
blockchain BlockChain
9899
chainDb ethdb.Database
99100
odr *LesOdr
@@ -123,13 +124,14 @@ type ProtocolManager struct {
123124

124125
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
125126
// with the ethereum network.
126-
func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, serverPool *serverPool, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) {
127+
func NewProtocolManager(chainConfig *params.ChainConfig, indexerConfig *light.IndexerConfig, lightSync bool, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, serverPool *serverPool, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) {
127128
// Create the protocol manager with the base fields
128129
manager := &ProtocolManager{
129130
lightSync: lightSync,
130131
eventMux: mux,
131132
blockchain: blockchain,
132133
chainConfig: chainConfig,
134+
iConfig: indexerConfig,
133135
chainDb: chainDb,
134136
odr: odr,
135137
networkId: networkId,
@@ -882,7 +884,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
882884
trieDb := trie.NewDatabase(ethdb.NewTable(pm.chainDb, light.ChtTablePrefix))
883885
for _, req := range req.Reqs {
884886
if header := pm.blockchain.GetHeaderByNumber(req.BlockNum); header != nil {
885-
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, req.ChtNum*light.CHTFrequencyServer-1)
887+
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, req.ChtNum*pm.iConfig.ChtSize-1)
886888
if root := light.GetChtRoot(pm.chainDb, req.ChtNum-1, sectionHead); root != (common.Hash{}) {
887889
trie, err := trie.New(root, trieDb)
888890
if err != nil {
@@ -1137,10 +1139,11 @@ func (pm *ProtocolManager) getAccount(statedb *state.StateDB, root, hash common.
11371139
func (pm *ProtocolManager) getHelperTrie(id uint, idx uint64) (common.Hash, string) {
11381140
switch id {
11391141
case htCanonical:
1140-
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.CHTFrequencyClient-1)
1141-
return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix
1142+
idxV1 := (idx+1)*(pm.iConfig.PairChtSize/pm.iConfig.ChtSize) - 1
1143+
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idxV1+1)*pm.iConfig.ChtSize-1)
1144+
return light.GetChtRoot(pm.chainDb, idxV1, sectionHead), light.ChtTablePrefix
11421145
case htBloomBits:
1143-
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1)
1146+
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*pm.iConfig.BloomTrieSize-1)
11441147
return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix
11451148
}
11461149
return common.Hash{}, ""

0 commit comments

Comments
 (0)