Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit eec9205

Browse files
authored
Merge pull request #79 from binance-chain/release/v0.30.1-binance.4
prepare for releasing v0.30.1 binance.4
2 parents 685fb83 + 3a4d805 commit eec9205

12 files changed

Lines changed: 198 additions & 36 deletions

File tree

config/config.go

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"time"
88

99
"github.com/pkg/errors"
10+
"github.com/syndtr/goleveldb/leveldb/filter"
11+
optPkg "github.com/syndtr/goleveldb/leveldb/opt"
1012
)
1113

1214
const (
@@ -63,6 +65,7 @@ type Config struct {
6365
// Options for services
6466
RPC *RPCConfig `mapstructure:"rpc"`
6567
P2P *P2PConfig `mapstructure:"p2p"`
68+
DBCache *DBCacheConfig `mapstructure:"dbcache"`
6669
Mempool *MempoolConfig `mapstructure:"mempool"`
6770
Consensus *ConsensusConfig `mapstructure:"consensus"`
6871
TxIndex *TxIndexConfig `mapstructure:"tx_index"`
@@ -75,6 +78,7 @@ func DefaultConfig() *Config {
7578
BaseConfig: DefaultBaseConfig(),
7679
RPC: DefaultRPCConfig(),
7780
P2P: DefaultP2PConfig(),
81+
DBCache: DefaultDBCacheConfig(),
7882
Mempool: DefaultMempoolConfig(),
7983
Consensus: DefaultConsensusConfig(),
8084
TxIndex: DefaultTxIndexConfig(),
@@ -88,6 +92,7 @@ func TestConfig() *Config {
8892
BaseConfig: TestBaseConfig(),
8993
RPC: TestRPCConfig(),
9094
P2P: TestP2PConfig(),
95+
DBCache: TestDBCacheConfig(),
9196
Mempool: TestMempoolConfig(),
9297
Consensus: TestConsensusConfig(),
9398
TxIndex: TestTxIndexConfig(),
@@ -117,6 +122,9 @@ func (cfg *Config) ValidateBasic() error {
117122
if err := cfg.P2P.ValidateBasic(); err != nil {
118123
return errors.Wrap(err, "Error in [p2p] section")
119124
}
125+
if err := cfg.DBCache.ValidateBasic(); err != nil {
126+
return errors.Wrap(err, "Error in [dbcache] section")
127+
}
120128
if err := cfg.Mempool.ValidateBasic(); err != nil {
121129
return errors.Wrap(err, "Error in [mempool] section")
122130
}
@@ -477,11 +485,11 @@ func DefaultP2PConfig() *P2PConfig {
477485
MaxNumOutboundPeers: 10,
478486
FlushThrottleTimeout: 10 * time.Millisecond,
479487
MaxPacketMsgPayloadSize: 1024 * 1024, // 1 MB
480-
KeysPerRequest: 2500, // would be around 250K for account node
488+
KeysPerRequest: 2500, // would be around 250K for account node
481489
SendRate: 50 * 1024 * 1024, // 50 MB/s
482490
RecvRate: 50 * 1024 * 1024, // 50 MB/s
483-
PingInterval: 60 * time.Second,
484-
PongTimeout: 45 * time.Second,
491+
PingInterval: 60 * time.Second,
492+
PongTimeout: 45 * time.Second,
485493
PexReactor: true,
486494
SeedMode: false,
487495
AllowDuplicateIP: false,
@@ -551,6 +559,73 @@ func DefaultFuzzConnConfig() *FuzzConnConfig {
551559
}
552560
}
553561

562+
//-----------------------------------------------------------------------------
563+
// DBCacheConfig defines the cache related configuration (should not impact back compatibility) options of goleveldb
564+
type DBCacheConfig struct {
565+
// OpenFilesCacheCapacity defines the capacity of the open files caching.
566+
OpenFilesCacheCapacity int `mapstructure:"open_files_cache_capacity"`
567+
568+
// BlockCacheCapacity defines the capacity of the 'sorted table' block caching.
569+
BlockCacheCapacity int `mapstructure:"block_cache_capacity"`
570+
571+
// WriteBuffer defines maximum size of a 'memdb' before flushed to
572+
// 'sorted table'.
573+
WriteBuffer int `mapstructure:"write_buffer"`
574+
575+
// Filter defines an 'effective filter' to use. An 'effective filter'
576+
// if defined will be used to generate per-table filter block.
577+
// Greater than 0 would creates a new initialized bloom filter for given bitsPerKey.
578+
BitsPerKey int `mapstructure:"bits_per_key"`
579+
}
580+
581+
// DefaultDBCacheConfig returns a default configuration for goleveldb config
582+
func DefaultDBCacheConfig() *DBCacheConfig {
583+
return &DBCacheConfig{
584+
OpenFilesCacheCapacity: 1024,
585+
BlockCacheCapacity: 8 * optPkg.MiB,
586+
WriteBuffer: 4 * optPkg.MiB,
587+
BitsPerKey: 10,
588+
}
589+
}
590+
591+
// TestDBCacheConfig returns a configuration for testing
592+
func TestDBCacheConfig() *DBCacheConfig {
593+
cfg := DefaultDBCacheConfig()
594+
cfg.OpenFilesCacheCapacity = 500
595+
cfg.BlockCacheCapacity = 8 * optPkg.MiB
596+
cfg.WriteBuffer = 4 * optPkg.MiB
597+
cfg.BitsPerKey = 0
598+
return cfg
599+
}
600+
601+
// ValidateBasic performs basic validation (checking param bounds, etc.) and
602+
// returns an error if any check fails.
603+
func (cfg *DBCacheConfig) ValidateBasic() error {
604+
if cfg.OpenFilesCacheCapacity < 0 {
605+
return errors.New("open_files_cache_capacity can't be negative")
606+
}
607+
if cfg.BlockCacheCapacity < 0 {
608+
return errors.New("block_cache_capacity can't be negative")
609+
}
610+
if cfg.WriteBuffer < 0 {
611+
return errors.New("write_buffer can't be negative")
612+
}
613+
return nil
614+
}
615+
616+
func (cfg *DBCacheConfig) ToGolevelDBOpt() *optPkg.Options {
617+
var fltr filter.Filter
618+
if cfg.BitsPerKey > 0 {
619+
fltr = filter.NewBloomFilter(cfg.BitsPerKey)
620+
}
621+
return &optPkg.Options{
622+
OpenFilesCacheCapacity: cfg.OpenFilesCacheCapacity,
623+
BlockCacheCapacity: cfg.BlockCacheCapacity,
624+
WriteBuffer: cfg.WriteBuffer,
625+
Filter: fltr,
626+
}
627+
}
628+
554629
//-----------------------------------------------------------------------------
555630
// MempoolConfig
556631

config/toml.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ allow_duplicate_ip = {{ .P2P.AllowDuplicateIP }}
251251
handshake_timeout = "{{ .P2P.HandshakeTimeout }}"
252252
dial_timeout = "{{ .P2P.DialTimeout }}"
253253
254+
##### dbcache configuration options #####
255+
[dbcache]
256+
# OpenFilesCacheCapacity defines the capacity of the open files caching.
257+
open_files_cache_capacity = {{ .DBCache.OpenFilesCacheCapacity }}
258+
259+
# BlockCacheCapacity defines the capacity of the 'sorted table' block caching.
260+
block_cache_capacity = {{ .DBCache.BlockCacheCapacity }}
261+
262+
# WriteBuffer defines maximum size of a 'memdb' before flushed to 'sorted table'.
263+
write_buffer = {{ .DBCache.WriteBuffer }}
264+
265+
# Filter defines an 'effective filter' to use. An 'effective filter'
266+
# if defined will be used to generate per-table filter block.
267+
# Greater than 0 would creates a new initialized bloom filter for given bitsPerKey.
268+
bits_per_key = {{ .DBCache.BitsPerKey }}
269+
254270
##### mempool configuration options #####
255271
[mempool]
256272

libs/db/backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestBackendsGetSetDelete(t *testing.T) {
6060
func withDB(t *testing.T, creator dbCreator, fn func(DB)) {
6161
name := fmt.Sprintf("test_%x", cmn.RandStr(12))
6262
dir := os.TempDir()
63-
db, err := creator(name, dir)
63+
db, err := creator(name, dir, nil)
6464
require.Nil(t, err)
6565
defer cleanupDBDir(dir, name)
6666
fn(db)

libs/db/c_level_db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func init() {
14-
dbCreator := func(name string, dir string) (DB, error) {
14+
dbCreator := func(name string, dir string, opt interface{}) (DB, error) {
1515
return NewCLevelDB(name, dir)
1616
}
1717
registerDBCreator(LevelDBBackend, dbCreator, true)

libs/db/db.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
FSDBBackend DBBackendType = "fsdb" // using the filesystem naively
1919
)
2020

21-
type dbCreator func(name string, dir string) (DB, error)
21+
type dbCreator func(name string, dir string, opt interface{}) (DB, error)
2222

2323
var backends = map[DBBackendType]dbCreator{}
2424

@@ -35,6 +35,15 @@ func registerDBCreator(backend DBBackendType, creator dbCreator, force bool) {
3535
// - backend is unknown (not registered)
3636
// - creator function, provided during registration, returns error
3737
func NewDB(name string, backend DBBackendType, dir string) DB {
38+
return NewDBWithOpt(name, backend, dir, nil)
39+
}
40+
41+
42+
// NewDB creates a new database of type backend with the given name.
43+
// NOTE: function panics if:
44+
// - backend is unknown (not registered)
45+
// - creator function, provided during registration, returns error
46+
func NewDBWithOpt(name string, backend DBBackendType, dir string, opt interface{}) DB {
3847
dbCreator, ok := backends[backend]
3948
if !ok {
4049
keys := make([]string, len(backends))
@@ -46,7 +55,7 @@ func NewDB(name string, backend DBBackendType, dir string) DB {
4655
panic(fmt.Sprintf("Unknown db_backend %s, expected either %s", backend, strings.Join(keys, " or ")))
4756
}
4857

49-
db, err := dbCreator(name, dir)
58+
db, err := dbCreator(name, dir, opt)
5059
if err != nil {
5160
panic(fmt.Sprintf("Error initializing DB: %v", err))
5261
}

libs/db/fsdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
)
2121

2222
func init() {
23-
registerDBCreator(FSDBBackend, func(name string, dir string) (DB, error) {
23+
registerDBCreator(FSDBBackend, func(name string, dir string, opt interface{}) (DB, error) {
2424
dbPath := filepath.Join(dir, name+".db")
2525
return NewFSDB(dbPath), nil
2626
}, false)

libs/db/go_level_db.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import (
77

88
"github.com/syndtr/goleveldb/leveldb"
99
"github.com/syndtr/goleveldb/leveldb/errors"
10-
"github.com/syndtr/goleveldb/leveldb/filter"
1110
"github.com/syndtr/goleveldb/leveldb/iterator"
12-
"github.com/syndtr/goleveldb/leveldb/opt"
11+
optPkg "github.com/syndtr/goleveldb/leveldb/opt"
1312

1413
cmn "github.com/tendermint/tendermint/libs/common"
1514
)
1615

1716
func init() {
18-
dbCreator := func(name string, dir string) (DB, error) {
19-
return NewGoLevelDB(name, dir)
17+
dbCreator := func(name string, dir string, opt interface{}) (DB, error) {
18+
if o, ok := opt.(*optPkg.Options); ok {
19+
return NewGoLevelDBWithOpts(name, dir, o)
20+
} else {
21+
return NewGoLevelDB(name, dir)
22+
}
2023
}
2124
registerDBCreator(LevelDBBackend, dbCreator, false)
2225
registerDBCreator(GoLevelDBBackend, dbCreator, false)
@@ -29,14 +32,10 @@ type GoLevelDB struct {
2932
}
3033

3134
func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) {
32-
var defaultOptions = &opt.Options{OpenFilesCacheCapacity: 1024,
33-
BlockCacheCapacity: 2 * opt.GiB,
34-
WriteBuffer: 768 / 4 * opt.MiB, // Two of these are used internally
35-
Filter: filter.NewBloomFilter(10)}
36-
return NewGoLevelDBWithOpts(name, dir, defaultOptions)
35+
return NewGoLevelDBWithOpts(name, dir, nil)
3736
}
3837

39-
func NewGoLevelDBWithOpts(name string, dir string, o *opt.Options) (*GoLevelDB, error) {
38+
func NewGoLevelDBWithOpts(name string, dir string, o *optPkg.Options) (*GoLevelDB, error) {
4039
dbPath := filepath.Join(dir, name+".db")
4140
db, err := leveldb.OpenFile(dbPath, o)
4241
if err != nil {
@@ -80,7 +79,7 @@ func (db *GoLevelDB) Set(key []byte, value []byte) {
8079
func (db *GoLevelDB) SetSync(key []byte, value []byte) {
8180
key = nonNilBytes(key)
8281
value = nonNilBytes(value)
83-
err := db.db.Put(key, value, &opt.WriteOptions{Sync: true})
82+
err := db.db.Put(key, value, &optPkg.WriteOptions{Sync: true})
8483
if err != nil {
8584
cmn.PanicCrisis(err)
8685
}
@@ -98,7 +97,7 @@ func (db *GoLevelDB) Delete(key []byte) {
9897
// Implements DB.
9998
func (db *GoLevelDB) DeleteSync(key []byte) {
10099
key = nonNilBytes(key)
101-
err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
100+
err := db.db.Delete(key, &optPkg.WriteOptions{Sync: true})
102101
if err != nil {
103102
cmn.PanicCrisis(err)
104103
}
@@ -175,15 +174,15 @@ func (mBatch *goLevelDBBatch) Delete(key []byte) {
175174

176175
// Implements Batch.
177176
func (mBatch *goLevelDBBatch) Write() {
178-
err := mBatch.db.db.Write(mBatch.batch, &opt.WriteOptions{Sync: false})
177+
err := mBatch.db.db.Write(mBatch.batch, &optPkg.WriteOptions{Sync: false})
179178
if err != nil {
180179
panic(err)
181180
}
182181
}
183182

184183
// Implements Batch.
185184
func (mBatch *goLevelDBBatch) WriteSync() {
186-
err := mBatch.db.db.Write(mBatch.batch, &opt.WriteOptions{Sync: true})
185+
err := mBatch.db.db.Write(mBatch.batch, &optPkg.WriteOptions{Sync: true})
187186
if err != nil {
188187
panic(err)
189188
}

libs/db/mem_db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func init() {
10-
registerDBCreator(MemDBBackend, func(name string, dir string) (DB, error) {
10+
registerDBCreator(MemDBBackend, func(name string, dir string, opt interface{}) (DB, error) {
1111
return NewMemDB(), nil
1212
}, false)
1313
}

node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type DBProvider func(*DBContext) (dbm.DB, error)
5959
// specified in the ctx.Config.
6060
func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
6161
dbType := dbm.DBBackendType(ctx.Config.DBBackend)
62-
return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir()), nil
62+
return dbm.NewDBWithOpt(ctx.ID, dbType, ctx.Config.DBDir(), ctx.Config.DBCache.ToGolevelDBOpt()), nil
6363
}
6464

6565
// GenesisDocProvider returns a GenesisDoc.

p2p/pex/pex_reactor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ func (r *PEXReactor) Receive(chID byte, src Peer, msgBytes []byte) {
245245
} else {
246246
// Check we're not receiving requests too frequently.
247247
if err := r.receiveRequest(src); err != nil {
248-
r.Switch.StopPeerForError(src, err)
248+
// If the peer send pexrequest too quick, just ignore it. No need to stop the peer
249+
r.Logger.Error(err.Error())
249250
return
250251
}
251252
r.SendAddrs(src, r.book.GetSelection())

0 commit comments

Comments
 (0)