Skip to content

Commit

Permalink
Merge pull request #98 from uprendis/feature/increase-api-caches
Browse files Browse the repository at this point in the history
More extensive caching
  • Loading branch information
uprendis authored May 1, 2021
2 parents b7e6223 + 8048d5f commit 0817dd0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
8 changes: 8 additions & 0 deletions evmcore/dummy_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions gossip/c_block_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions gossip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),

Expand Down Expand Up @@ -233,7 +230,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),
Expand Down
10 changes: 9 additions & 1 deletion gossip/evm_state_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
15 changes: 12 additions & 3 deletions gossip/evmstore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,10 +31,12 @@ 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),
EvmBlocksNum: scale.I(5000),
EvmBlocksSize: scale.U(6 * opt.MiB),
},
}
}
Expand All @@ -41,6 +48,8 @@ func LiteStoreConfig() StoreConfig {
ReceiptsSize: 3 * 1024,
ReceiptsBlocks: 100,
TxPositions: 500,
EvmBlocksNum: 100,
EvmBlocksSize: 3 * 1024,
},
}
}
6 changes: 5 additions & 1 deletion gossip/evmstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions gossip/evmstore/store_block_cache.go
Original file line number Diff line number Diff line change
@@ -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()))
}
4 changes: 2 additions & 2 deletions gossip/evmstore/store_tx_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions gossip/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0817dd0

Please sign in to comment.