Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
Expand Down Expand Up @@ -89,6 +90,8 @@ type BlockChainAPI struct {
indexingResumedHeight uint64
rateLimiter RateLimiter
collector metrics.Collector

blockByNumberCache *lru.Cache[uint64, *ethTypes.Block]
}

func NewBlockChainAPI(
Expand All @@ -101,6 +104,7 @@ func NewBlockChainAPI(
rateLimiter RateLimiter,
collector metrics.Collector,
indexingResumedHeight uint64,
blockByNumberCacheSize int,
) *BlockChainAPI {
return &BlockChainAPI{
logger: logger,
Expand All @@ -112,6 +116,7 @@ func NewBlockChainAPI(
indexingResumedHeight: indexingResumedHeight,
rateLimiter: rateLimiter,
collector: collector,
blockByNumberCache: lru.NewCache[uint64, *ethTypes.Block](blockByNumberCacheSize),
}
}

Expand Down Expand Up @@ -407,6 +412,10 @@ func (b *BlockChainAPI) GetBlockByNumber(
return handleError[*ethTypes.Block](err, l, b.collector)
}

if block, ok := b.blockByNumberCache.Get(height); ok {
return block, nil
}

block, err := b.blocks.GetByHeight(height)

if err != nil {
Expand All @@ -418,6 +427,8 @@ func (b *BlockChainAPI) GetBlockByNumber(
return handleError[*ethTypes.Block](err, l, b.collector)
}

_ = b.blockByNumberCache.Add(height, apiBlock)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that GetBlockByHash, does pretty much the exact same DB calls, we could possibly improve that one too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea. now that we're not in a rush to get something out, I think we should take a more wholistic approach to caching based on our findings from the load test


return apiBlock, nil
}

Expand Down
1 change: 1 addition & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
rateLimiter,
b.collector,
indexingResumedHeight,
b.config.BlockByNumberCacheSize,
)

streamAPI := api.NewStreamAPI(
Expand Down
1 change: 1 addition & 0 deletions cmd/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func init() {
Cmd.Flags().BoolVar(&experimentalSealingVerificationEnabled, "experimental-sealing-verification-enabled", true, "Sets whether the gateway should use the experimental soft finality sealing verification feature. WARNING: This may result in indexing halts if events do not match. Use only if you know what you are doing.")
Cmd.Flags().DurationVar(&cfg.EOAActivityCacheTTL, "eoa-activity-cache-ttl", time.Second*10, "Time interval used to track EOA activity. Tx send more frequently than this interval will be batched. Useful only when batch transaction submission is enabled.")
Cmd.Flags().DurationVar(&cfg.RpcRequestTimeout, "rpc-request-timeout", time.Second*120, "Sets the maximum duration at which JSON-RPC requests should generate a response, before they timeout. The default is 120 seconds.")
Cmd.Flags().IntVar(&cfg.BlockByNumberCacheSize, "block-by-number-cache-size", 1000, "Number of block cache for getBlockByNumber API.")

err := Cmd.Flags().MarkDeprecated("init-cadence-height", "This flag is no longer necessary and will be removed in future version. The initial Cadence height is known for testnet/mainnet and this was only required for fresh deployments of EVM Gateway. Once the DB has been initialized, the latest index Cadence height will be used upon start-up.")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ type Config struct {
// RpcRequestTimeout is the maximum duration at which JSON-RPC requests should generate
// a response, before they timeout.
RpcRequestTimeout time.Duration
// BlockByNumberCacheSize is the size of the block by number cache.
BlockByNumberCacheSize int
}