Skip to content

Commit

Permalink
remove unused hash in the bloom bits and return empty bitsets if bloo…
Browse files Browse the repository at this point in the history
…m bits are pruned
  • Loading branch information
beer-1 committed Jan 28, 2025
1 parent 3551b6d commit f3c193b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
14 changes: 5 additions & 9 deletions indexer/bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"cosmossdk.io/collections"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/bloombits"
coretypes "github.com/ethereum/go-ethereum/core/types"

Expand Down Expand Up @@ -45,7 +44,6 @@ func (e *EVMIndexerImpl) bloomIndexing(ctx context.Context, height uint64) error
return err
}

lastHead := common.Hash{}
for i := uint64(0); i < evmconfig.SectionSize; i++ {
header, err := e.BlockHeaderByNumber(ctx, section*evmconfig.SectionSize+i)
if err != nil {
Expand All @@ -55,8 +53,6 @@ func (e *EVMIndexerImpl) bloomIndexing(ctx context.Context, height uint64) error
if err := gen.AddBloom(uint(header.Number.Uint64()-section*evmconfig.SectionSize), header.Bloom); err != nil {
return err
}

lastHead = header.Hash()
}

// write the bloom bits to the store
Expand All @@ -66,7 +62,7 @@ func (e *EVMIndexerImpl) bloomIndexing(ctx context.Context, height uint64) error
return err
}

if err := e.RecordBloomBits(ctx, section, uint32(i), lastHead, bits); err != nil {
if err := e.RecordBloomBits(ctx, section, uint32(i), bits); err != nil {
return err
}
}
Expand All @@ -80,8 +76,8 @@ func (e *EVMIndexerImpl) bloomIndexing(ctx context.Context, height uint64) error
}

// ReadBloomBits reads the bloom bits for the given index, section and hash.
func (e *EVMIndexerImpl) ReadBloomBits(ctx context.Context, section uint64, index uint32, hash common.Hash) ([]byte, error) {
bloomBits, err := e.BloomBits.Get(ctx, collections.Join3(section, index, hash.Bytes()))
func (e *EVMIndexerImpl) ReadBloomBits(ctx context.Context, section uint64, index uint32) ([]byte, error) {
bloomBits, err := e.BloomBits.Get(ctx, collections.Join(section, index))
if err != nil {
return nil, err
}
Expand All @@ -90,8 +86,8 @@ func (e *EVMIndexerImpl) ReadBloomBits(ctx context.Context, section uint64, inde
}

// RecordBloomBits records the bloom bits for the given index, section and hash.
func (e *EVMIndexerImpl) RecordBloomBits(ctx context.Context, section uint64, index uint32, hash common.Hash, bloomBits []byte) error {
return e.BloomBits.Set(ctx, collections.Join3(section, index, hash.Bytes()), bloomBits)
func (e *EVMIndexerImpl) RecordBloomBits(ctx context.Context, section uint64, index uint32, bloomBits []byte) error {
return e.BloomBits.Set(ctx, collections.Join(section, index), bloomBits)
}

// NextBloomBitsSection increments the section number.
Expand Down
6 changes: 3 additions & 3 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type EVMIndexer interface {
TxInMempool(hash common.Hash) *rpctypes.RPCTransaction

// bloom
ReadBloomBits(ctx context.Context, section uint64, index uint32, bloom common.Hash) ([]byte, error)
ReadBloomBits(ctx context.Context, section uint64, index uint32) ([]byte, error)
PeekBloomBitsNextSection(ctx context.Context) (uint64, error)
IsBloomIndexingRunning() bool
}
Expand Down Expand Up @@ -84,7 +84,7 @@ type EVMIndexerImpl struct {
CosmosTxHashToTxHash collections.Map[[]byte, []byte]

// bloom
BloomBits collections.Map[collections.Triple[uint64, uint32, []byte], []byte]
BloomBits collections.Map[collections.Pair[uint64, uint32], []byte]
BloomBitsNextSection collections.Sequence

blockChans []chan *coretypes.Header
Expand Down Expand Up @@ -134,7 +134,7 @@ func NewEVMIndexer(
BlockHashToNumberMap: collections.NewMap(sb, prefixBlockHashToNumber, "block_hash_to_number", collections.BytesKey, collections.Uint64Value),
TxHashToCosmosTxHash: collections.NewMap(sb, prefixTxHashToCosmosTxHash, "tx_hash_to_cosmos_tx_hash", collections.BytesKey, collections.BytesValue),
CosmosTxHashToTxHash: collections.NewMap(sb, prefixCosmosTxHashToTxHash, "cosmos_tx_hash_to_tx_hash", collections.BytesKey, collections.BytesValue),
BloomBits: collections.NewMap(sb, prefixBloomBits, "bloom_bits", collections.TripleKeyCodec(collections.Uint64Key, collections.Uint32Key, collections.BytesKey), collections.BytesValue),
BloomBits: collections.NewMap(sb, prefixBloomBits, "bloom_bits", collections.PairKeyCodec(collections.Uint64Key, collections.Uint32Key), collections.BytesValue),
BloomBitsNextSection: collections.NewSequence(sb, prefixBloomBitsNextSection, "bloom_bits_next_section"),

blockChans: nil,
Expand Down
4 changes: 2 additions & 2 deletions indexer/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ var (
prefixCosmosTxHashToTxHash = collections.Prefix([]byte{0, 0, 3, 2})

// bloom filter indexes
prefixBloomBits = collections.Prefix([]byte{0, 0, 4, 1})
prefixBloomBitsNextSection = collections.Prefix([]byte{0, 0, 4, 2})
prefixBloomBits = collections.Prefix([]byte{0, 0, 5, 1})
prefixBloomBitsNextSection = collections.Prefix([]byte{0, 0, 5, 2})
)
17 changes: 10 additions & 7 deletions jsonrpc/backend/filters.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package backend

import (
"errors"
"time"

"cosmossdk.io/collections"
"github.com/ethereum/go-ethereum/common/bitutil"
"github.com/ethereum/go-ethereum/core/bloombits"
coretypes "github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -115,21 +117,22 @@ func (b *JSONRPCBackend) startBloomHandlers(sectionSize uint64) {
}

for i, section := range task.Sections {
header, err := b.app.EVMIndexer().BlockHeaderByNumber(queryCtx, (section+1)*sectionSize-1)
if err != nil {
task.Error = err
break
}
compVector, err := b.app.EVMIndexer().ReadBloomBits(queryCtx, section, uint32(task.Bit), header.Hash())
if err != nil {
compVector, err := b.app.EVMIndexer().ReadBloomBits(queryCtx, section, uint32(task.Bit))
if errors.Is(err, collections.ErrNotFound) {
// pruned section, return empty bitset
task.Bitsets[i] = make([]byte, evmconfig.SectionSize/8)
continue
} else if err != nil {
task.Error = err
break
}

blob, err := bitutil.DecompressBytes(compVector, int(sectionSize/8))
if err != nil {
task.Error = err
break
}

task.Bitsets[i] = blob
}
request <- task
Expand Down

0 comments on commit f3c193b

Please sign in to comment.