From 207b44cdad37b1fdf4da0bbb7314090984cf39b1 Mon Sep 17 00:00:00 2001 From: Egor Lysenko Date: Fri, 30 Apr 2021 16:42:05 +0700 Subject: [PATCH 1/2] increase default cache sizes for API data --- gossip/config.go | 2 +- gossip/evmstore/config.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gossip/config.go b/gossip/config.go index 384a96567..362f5bcce 100644 --- a/gossip/config.go +++ b/gossip/config.go @@ -233,7 +233,7 @@ func DefaultStoreConfig(scale cachescale.Func) StoreConfig { Cache: StoreCacheConfig{ EventsNum: scale.I(5000), EventsSize: scale.U(6 * opt.MiB), - BlocksNum: scale.I(1000), + BlocksNum: scale.I(5000), BlocksSize: scale.U(512 * opt.KiB), }, EVM: evmstore.DefaultStoreConfig(scale), diff --git a/gossip/evmstore/config.go b/gossip/evmstore/config.go index 865ad9c46..7ecb0832c 100644 --- a/gossip/evmstore/config.go +++ b/gossip/evmstore/config.go @@ -26,9 +26,9 @@ type ( func DefaultStoreConfig(scale cachescale.Func) StoreConfig { return StoreConfig{ StoreCacheConfig{ - ReceiptsSize: scale.U(64 * 1024), - ReceiptsBlocks: scale.I(1000), - TxPositions: scale.I(5000), + ReceiptsSize: scale.U(4 * opt.MiB), + ReceiptsBlocks: scale.I(4000), + TxPositions: scale.I(20000), EvmDatabase: scale.I(16 * opt.MiB), }, } From 8048d5f3a12683feb6be563e59383de25591c1fe Mon Sep 17 00:00:00 2001 From: Egor Lysenko Date: Fri, 30 Apr 2021 17:12:28 +0700 Subject: [PATCH 2/2] add EVM blocks cache --- evmcore/dummy_block.go | 8 ++++++++ gossip/c_block_callbacks.go | 1 + gossip/config.go | 7 ++----- gossip/evm_state_reader.go | 10 +++++++++- gossip/evmstore/config.go | 9 +++++++++ gossip/evmstore/store.go | 6 +++++- gossip/evmstore/store_block_cache.go | 19 +++++++++++++++++++ gossip/evmstore/store_tx_position.go | 4 ++-- gossip/store.go | 1 + 9 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 gossip/evmstore/store_block_cache.go diff --git a/evmcore/dummy_block.go b/evmcore/dummy_block.go index dd344dba0..d5e1b0373 100644 --- a/evmcore/dummy_block.go +++ b/evmcore/dummy_block.go @@ -120,3 +120,11 @@ func (b *EvmBlock) EthBlock() *types.Block { } return types.NewBlock(b.EvmHeader.EthHeader(), b.Transactions, nil, nil, new(trie.Trie)) } + +func (b *EvmBlock) EstimateSize() int { + est := 0 + for _, tx := range b.Transactions { + est += len(tx.Data()) + } + return est + b.Transactions.Len()*256 +} diff --git a/gossip/c_block_callbacks.go b/gossip/c_block_callbacks.go index 45252e6b7..b8106501a 100644 --- a/gossip/c_block_callbacks.go +++ b/gossip/c_block_callbacks.go @@ -266,6 +266,7 @@ func consensusCallbackBeginBlockFn( store.SetBlockIndex(block.Atropos, blockCtx.Idx) bs.LastBlock = blockCtx store.SetBlockEpochState(bs, es) + store.EvmStore().SetCachedEvmBlock(blockCtx.Idx, evmBlock) // Notify about new block and txs if feed != nil { diff --git a/gossip/config.go b/gossip/config.go index 362f5bcce..ab36b175b 100644 --- a/gossip/config.go +++ b/gossip/config.go @@ -55,9 +55,7 @@ type ( Emitter emitter.Config TxPool evmcore.TxPoolConfig - TxIndex bool // Whether to enable indexing transactions and receipts or not - DecisiveEventsIndex bool // Whether to enable indexing events which decide blocks or not - EventLocalTimeIndex bool // Whether to enable indexing arrival time of events or not + TxIndex bool // Whether to enable indexing transactions and receipts or not // Protocol options Protocol ProtocolConfig @@ -124,8 +122,7 @@ func DefaultConfig(scale cachescale.Func) Config { Emitter: emitter.DefaultConfig(), TxPool: evmcore.DefaultTxPoolConfig(), - TxIndex: true, - DecisiveEventsIndex: false, + TxIndex: true, HeavyCheck: heavycheck.DefaultConfig(), diff --git a/gossip/evm_state_reader.go b/gossip/evm_state_reader.go index f33b33f9c..e9df3b0a4 100644 --- a/gossip/evm_state_reader.go +++ b/gossip/evm_state_reader.go @@ -71,6 +71,11 @@ func (r *EvmStateReader) getBlock(h hash.Event, n idx.Block, readTxs bool) *evmc if (h != hash.Event{}) && (h != block.Atropos) { return nil } + if readTxs { + if cached := r.store.EvmStore().GetCachedEvmBlock(n); cached != nil { + return cached + } + } var transactions types.Transactions if readTxs { @@ -98,7 +103,6 @@ func (r *EvmStateReader) getBlock(h hash.Event, n idx.Block, readTxs bool) *evmc continue } transactions = append(transactions, e.Txs()...) - } transactions = inter.FilterSkippedTxs(transactions, block.SkippedTxs) @@ -124,6 +128,10 @@ func (r *EvmStateReader) getBlock(h hash.Event, n idx.Block, readTxs bool) *evmc EvmHeader: *evmHeader, Transactions: transactions, } + + if readTxs { + r.store.EvmStore().SetCachedEvmBlock(n, evmBlock) + } return evmBlock } diff --git a/gossip/evmstore/config.go b/gossip/evmstore/config.go index 7ecb0832c..26dbd46b7 100644 --- a/gossip/evmstore/config.go +++ b/gossip/evmstore/config.go @@ -14,7 +14,12 @@ type ( ReceiptsBlocks int // Cache size for TxPositions. TxPositions int + // Cache size for EVM database. EvmDatabase int + // Cache size for EvmBlock (number of blocks). + EvmBlocksNum int + // Cache size for EvmBlock (size in bytes). + EvmBlocksSize uint } // StoreConfig is a config for store db. StoreConfig struct { @@ -30,6 +35,8 @@ func DefaultStoreConfig(scale cachescale.Func) StoreConfig { ReceiptsBlocks: scale.I(4000), TxPositions: scale.I(20000), EvmDatabase: scale.I(16 * opt.MiB), + EvmBlocksNum: scale.I(5000), + EvmBlocksSize: scale.U(6 * opt.MiB), }, } } @@ -41,6 +48,8 @@ func LiteStoreConfig() StoreConfig { ReceiptsSize: 3 * 1024, ReceiptsBlocks: 100, TxPositions: 500, + EvmBlocksNum: 100, + EvmBlocksSize: 3 * 1024, }, } } diff --git a/gossip/evmstore/store.go b/gossip/evmstore/store.go index a90cf3f3f..0c7956924 100644 --- a/gossip/evmstore/store.go +++ b/gossip/evmstore/store.go @@ -21,6 +21,8 @@ import ( "github.com/Fantom-foundation/go-opera/utils/rlpstore" ) +const nominalSize uint = 1 + // Store is a node persistent storage working over physical key-value database. type Store struct { cfg StoreConfig @@ -40,6 +42,7 @@ type Store struct { cache struct { TxPositions *wlru.Cache `cache:"-"` // store by pointer Receipts *wlru.Cache `cache:"-"` // store by value + EvmBlocks *wlru.Cache `cache:"-"` // store by pointer } mutex struct { @@ -74,7 +77,8 @@ func NewStore(mainDB kvdb.Store, cfg StoreConfig) *Store { func (s *Store) initCache() { s.cache.Receipts = s.makeCache(s.cfg.Cache.ReceiptsSize, s.cfg.Cache.ReceiptsBlocks) - s.cache.TxPositions = s.makeCache(uint(s.cfg.Cache.TxPositions), s.cfg.Cache.TxPositions) + s.cache.TxPositions = s.makeCache(nominalSize*uint(s.cfg.Cache.TxPositions), s.cfg.Cache.TxPositions) + s.cache.EvmBlocks = s.makeCache(s.cfg.Cache.EvmBlocksSize, s.cfg.Cache.EvmBlocksNum) } // Commit changes. diff --git a/gossip/evmstore/store_block_cache.go b/gossip/evmstore/store_block_cache.go new file mode 100644 index 000000000..943988e78 --- /dev/null +++ b/gossip/evmstore/store_block_cache.go @@ -0,0 +1,19 @@ +package evmstore + +import ( + "github.com/Fantom-foundation/lachesis-base/inter/idx" + + "github.com/Fantom-foundation/go-opera/evmcore" +) + +func (s *Store) GetCachedEvmBlock(n idx.Block) *evmcore.EvmBlock { + c, ok := s.cache.EvmBlocks.Get(n) + if !ok { + return nil + } + return c.(*evmcore.EvmBlock) +} + +func (s *Store) SetCachedEvmBlock(n idx.Block, b *evmcore.EvmBlock) { + s.cache.EvmBlocks.Add(n, b, uint(b.EstimateSize())) +} diff --git a/gossip/evmstore/store_tx_position.go b/gossip/evmstore/store_tx_position.go index 076e61be2..ee4fcc482 100644 --- a/gossip/evmstore/store_tx_position.go +++ b/gossip/evmstore/store_tx_position.go @@ -22,7 +22,7 @@ func (s *Store) SetTxPosition(txid common.Hash, position TxPosition) { s.rlp.Set(s.table.TxPositions, txid.Bytes(), &position) // Add to LRU cache. - s.cache.TxPositions.Add(txid.String(), &position, 1) + s.cache.TxPositions.Add(txid.String(), &position, nominalSize) } // GetTxPosition returns stored transaction block and position. @@ -38,7 +38,7 @@ func (s *Store) GetTxPosition(txid common.Hash) *TxPosition { // Add to LRU cache. if txPosition != nil { - s.cache.TxPositions.Add(txid.String(), txPosition, 1) + s.cache.TxPositions.Add(txid.String(), txPosition, nominalSize) } return txPosition diff --git a/gossip/store.go b/gossip/store.go index 4e597cfa5..4bc257bd3 100644 --- a/gossip/store.go +++ b/gossip/store.go @@ -57,6 +57,7 @@ type Store struct { EventsHeaders *wlru.Cache `cache:"-"` // store by pointer Blocks *wlru.Cache `cache:"-"` // store by pointer BlockHashes *wlru.Cache `cache:"-"` // store by pointer + EvmBlocks *wlru.Cache `cache:"-"` // store by pointer BlockEpochState atomic.Value // store by value HighestLamport atomic.Value // store by value }