-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* clear code fix bug fix publish bug add unit test fix fix fix fix bloom add chain event fix bug & add forking settings impl uninstallFilter fix bug fix bug add timeoutLoop fix fix fix add eth event clear code clear code update add subscribe move save block bloom move save block bloom fmt code get block logs fmt code init index store fmt code impl get filters fix annotation fix add api interface to impl get bloom status fix close database add bloombits store * fmt code * add license * add license * add license * fmt code * fmt code * fmt code * fix bug * fmt code * fmt code * clean up * Update http/ethrpc/filters/filter_system.go * Update http/ethrpc/filters/filter_system.go * tmp commit * fix bug * fix * fix * fix * break import cycle * fix * add max search span * rm filter height * fix * fix * delete unused channel * add bloom cache * fix * add annotation * add lock * fix * fix * fix * fix bug * fix bug * fix bug * fix bug * fix bug * fix bug * fix * fix * fix bug * delete unused code * fix * use block db to store bloom index * refactor code * remove filter start field * fix * fix * fix * fix * add annotation Co-authored-by: laizy <aochyi@126.com>
- Loading branch information
Showing
24 changed files
with
2,001 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright (C) 2018 The ontology Authors | ||
* This file is part of The ontology library. | ||
* | ||
* The ontology 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 ontology 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 ontology. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package ledgerstore | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common/bitutil" | ||
"github.com/ethereum/go-ethereum/core/bloombits" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
common2 "github.com/ontio/ontology/common" | ||
scom "github.com/ontio/ontology/core/store/common" | ||
"github.com/ontio/ontology/core/store/leveldbstore" | ||
) | ||
|
||
const ( | ||
// bloomServiceThreads is the number of goroutines used globally by an Ethereum | ||
// instance to service bloombits lookups for all running filters. | ||
BloomServiceThreads = 16 | ||
|
||
// bloomFilterThreads is the number of goroutines used locally per filter to | ||
// multiplex requests onto the global servicing goroutines. | ||
BloomFilterThreads = 3 | ||
|
||
// bloomRetrievalBatch is the maximum number of bloom bit retrievals to service | ||
// in a single batch. | ||
BloomRetrievalBatch = 16 | ||
|
||
// bloomRetrievalWait is the maximum time to wait for enough bloom bit requests | ||
// to accumulate request an entire batch (avoiding hysteresis). | ||
BloomRetrievalWait = time.Duration(0) | ||
|
||
// BloomBitsBlocks is the number of blocks a single bloom bit section vector | ||
// contains on the server side. | ||
BloomBitsBlocks uint32 = 4096 | ||
) | ||
|
||
var ( | ||
bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint32 big endian) + hash -> bloom bits | ||
) | ||
|
||
// bloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index | ||
// for the Ethereum header bloom filters, permitting blazing fast filtering. | ||
type bloomIndexer struct { | ||
store *leveldbstore.LevelDBStore // database instance to write index data and metadata into | ||
gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index | ||
section uint32 // Section is the section number being processed currently | ||
} | ||
|
||
func NewBloomIndexer(store *leveldbstore.LevelDBStore, section uint32) bloomIndexer { | ||
gen, err := bloombits.NewGenerator(uint(BloomBitsBlocks)) | ||
if err != nil { | ||
panic(err) // never fired since BloomBitsBlocks is multiple of 8 | ||
} | ||
b := bloomIndexer{ | ||
store: store, | ||
} | ||
b.gen, b.section = gen, section | ||
return b | ||
} | ||
|
||
// Process implements core.ChainIndexerBackend, adding a new header's bloom into | ||
// the index. | ||
func (b *bloomIndexer) Process(height uint32, bloom types.Bloom) { | ||
b.gen.AddBloom(uint(height-b.section*BloomBitsBlocks), bloom) | ||
} | ||
|
||
// BatchPut implements core.ChainIndexerBackend, finalizing the bloom section and | ||
// writing it out into the database. | ||
func (b *bloomIndexer) BatchPut() { | ||
for i := 0; i < types.BloomBitLength; i++ { | ||
bits, err := b.gen.Bitset(uint(i)) | ||
if err != nil { | ||
panic(err) // never fired since idx is always less than 8 and section should be right | ||
} | ||
value := bitutil.CompressBytes(bits) | ||
b.store.BatchPut(bloomBitsKey(uint(i), b.section), value) | ||
} | ||
b.section++ | ||
} | ||
|
||
func (this *bloomIndexer) PutFilterStart(height uint32) error { | ||
key := genFilterStartKey() | ||
sink := common2.NewZeroCopySink(nil) | ||
sink.WriteUint32(height) | ||
return this.store.Put(key, sink.Bytes()) | ||
} | ||
|
||
func (this *bloomIndexer) GetFilterStart() (uint32, error) { | ||
key := genFilterStartKey() | ||
data, err := this.store.Get(key) | ||
if err != nil { | ||
return 0, err | ||
} | ||
height, eof := common2.NewZeroCopySource(data).NextUint32() | ||
if eof { | ||
return 0, io.ErrUnexpectedEOF | ||
} | ||
return height, nil | ||
} | ||
|
||
func genFilterStartKey() []byte { | ||
return []byte{byte(scom.ST_ETH_FILTER_START)} | ||
} | ||
|
||
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint32 big endian) | ||
func bloomBitsKey(bit uint, section uint32) []byte { | ||
key := append(bloomBitsPrefix, make([]byte, 6)...) | ||
|
||
binary.BigEndian.PutUint16(key[1:], uint16(bit)) | ||
binary.BigEndian.PutUint32(key[3:], section) | ||
|
||
return key | ||
} | ||
|
||
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given | ||
// section and bit index from the. | ||
func ReadBloomBits(db *leveldbstore.LevelDBStore, bit uint, section uint32) ([]byte, error) { | ||
return db.Get(bloomBitsKey(bit, section)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.