diff options
author | Zsolt Felfoldi <zsfelfoldi@gmail.com> | 2017-08-19 03:52:20 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-09-06 16:13:13 +0800 |
commit | 4ea4d2dc3473afd9d2eda6ef6b359accce1f0946 (patch) | |
tree | e651cfc2e3aa36083b333bf34dc3cccef2623f26 /eth/db_upgrade.go | |
parent | 1e67378df879b1ce566f17dd95a3b126056254b5 (diff) | |
download | dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar.gz dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar.bz2 dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar.lz dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar.xz dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.tar.zst dexon-4ea4d2dc3473afd9d2eda6ef6b359accce1f0946.zip |
core, eth: add bloombit indexer, filter based on it
Diffstat (limited to 'eth/db_upgrade.go')
-rw-r--r-- | eth/db_upgrade.go | 70 |
1 files changed, 32 insertions, 38 deletions
diff --git a/eth/db_upgrade.go b/eth/db_upgrade.go index 90111b2b3..ce8ce699a 100644 --- a/eth/db_upgrade.go +++ b/eth/db_upgrade.go @@ -19,11 +19,13 @@ package eth import ( "bytes" - "fmt" "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/bitutil" "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/bloombits" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" @@ -135,45 +137,37 @@ func upgradeDeduplicateData(db ethdb.Database) func() error { } } -func addMipmapBloomBins(db ethdb.Database) (err error) { - const mipmapVersion uint = 2 - - // check if the version is set. We ignore data for now since there's - // only one version so we can easily ignore it for now - var data []byte - data, _ = db.Get([]byte("setting-mipmap-version")) - if len(data) > 0 { - var version uint - if err := rlp.DecodeBytes(data, &version); err == nil && version == mipmapVersion { - return nil - } - } +// BloomBitsIndex implements ChainIndex +type BloomBitsIndex struct { + db ethdb.Database + bc *bloombits.BloomBitsCreator + section, sectionSize uint64 + sectionHead common.Hash +} - defer func() { - if err == nil { - var val []byte - val, err = rlp.EncodeToBytes(mipmapVersion) - if err == nil { - err = db.Put([]byte("setting-mipmap-version"), val) - } - return - } - }() - latestHash := core.GetHeadBlockHash(db) - latestBlock := core.GetBlock(db, latestHash, core.GetBlockNumber(db, latestHash)) - if latestBlock == nil { // clean database - return - } +// number of confirmation blocks before a section is considered probably final and its bloom bits are calculated +const bloomBitsConfirmations = 256 - tstart := time.Now() - log.Warn("Upgrading db log bloom bins") - for i := uint64(0); i <= latestBlock.NumberU64(); i++ { - hash := core.GetCanonicalHash(db, i) - if (hash == common.Hash{}) { - return fmt.Errorf("chain db corrupted. Could not find block %d.", i) - } - core.WriteMipmapBloom(db, i, core.GetBlockReceipts(db, hash, i)) +// NewBloomBitsProcessor returns a chain processor that generates bloom bits data for the canonical chain +func NewBloomBitsProcessor(db ethdb.Database, sectionSize uint64) *core.ChainIndexer { + backend := &BloomBitsIndex{db: db, sectionSize: sectionSize} + return core.NewChainIndexer(db, ethdb.NewTable(db, "bbIndex-"), backend, sectionSize, bloomBitsConfirmations, time.Millisecond*100, "bloombits") +} + +func (b *BloomBitsIndex) Reset(section uint64, lastSectionHead common.Hash) { + b.bc = bloombits.NewBloomBitsCreator(b.sectionSize) + b.section = section +} + +func (b *BloomBitsIndex) Process(header *types.Header) { + b.bc.AddHeaderBloom(header.Bloom) + b.sectionHead = header.Hash() +} + +func (b *BloomBitsIndex) Commit() error { + for i := 0; i < bloombits.BloomLength; i++ { + compVector := bitutil.CompressBytes(b.bc.GetBitVector(uint(i))) + core.StoreBloomBits(b.db, uint64(i), b.section, b.sectionHead, compVector) } - log.Info("Bloom-bin upgrade completed", "elapsed", common.PrettyDuration(time.Since(tstart))) return nil } |