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
23 changes: 11 additions & 12 deletions cache/redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cache
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -35,12 +34,12 @@ func InitRedisCache(ctx context.Context, redisAddress string, keyPrefix string)
}

func (cache *RedisCache) SetString(ctx context.Context, key, value string, expiration time.Duration) error {
return cache.redisRemoteCache.Set(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key), value, expiration).Err()
return cache.redisRemoteCache.Set(ctx, cache.keyPrefix+key, value, expiration).Err()
}

func (cache *RedisCache) GetString(ctx context.Context, key string) (string, error) {

value, err := cache.redisRemoteCache.Get(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key)).Result()
value, err := cache.redisRemoteCache.Get(ctx, cache.keyPrefix+key).Result()
if err != nil {
return "", err
}
Expand All @@ -49,12 +48,12 @@ func (cache *RedisCache) GetString(ctx context.Context, key string) (string, err
}

func (cache *RedisCache) SetUint64(ctx context.Context, key string, value uint64, expiration time.Duration) error {
return cache.redisRemoteCache.Set(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key), fmt.Sprintf("%d", value), expiration).Err()
return cache.redisRemoteCache.Set(ctx, cache.keyPrefix+key, strconv.FormatUint(value, 10), expiration).Err()
}

func (cache *RedisCache) GetUint64(ctx context.Context, key string) (uint64, error) {

value, err := cache.redisRemoteCache.Get(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key)).Result()
value, err := cache.redisRemoteCache.Get(ctx, cache.keyPrefix+key).Result()
if err != nil {
return 0, err
}
Expand All @@ -67,12 +66,12 @@ func (cache *RedisCache) GetUint64(ctx context.Context, key string) (uint64, err
}

func (cache *RedisCache) SetBool(ctx context.Context, key string, value bool, expiration time.Duration) error {
return cache.redisRemoteCache.Set(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key), fmt.Sprintf("%t", value), expiration).Err()
return cache.redisRemoteCache.Set(ctx, cache.keyPrefix+key, strconv.FormatBool(value), expiration).Err()
}

func (cache *RedisCache) GetBool(ctx context.Context, key string) (bool, error) {

value, err := cache.redisRemoteCache.Get(ctx, key).Result()
value, err := cache.redisRemoteCache.Get(ctx, cache.keyPrefix+key).Result()
if err != nil {
return false, err
}
Expand All @@ -85,11 +84,11 @@ func (cache *RedisCache) GetBool(ctx context.Context, key string) (bool, error)
}

func (cache *RedisCache) SetBytes(ctx context.Context, key string, value []byte, expiration time.Duration) error {
return cache.redisRemoteCache.Set(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key), value, expiration).Err()
return cache.redisRemoteCache.Set(ctx, cache.keyPrefix+key, value, expiration).Err()
}

func (cache *RedisCache) GetBytes(ctx context.Context, key string) ([]byte, error) {
value, err := cache.redisRemoteCache.Get(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key)).Result()
value, err := cache.redisRemoteCache.Get(ctx, cache.keyPrefix+key).Result()
if err != nil {
return nil, err
}
Expand All @@ -101,18 +100,18 @@ func (cache *RedisCache) Set(ctx context.Context, key string, value interface{},
if err != nil {
return err
}
return cache.redisRemoteCache.Set(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key), valueMarshal, expiration).Err()
return cache.redisRemoteCache.Set(ctx, cache.keyPrefix+key, valueMarshal, expiration).Err()
}

func (cache *RedisCache) Get(ctx context.Context, key string, returnValue interface{}) (interface{}, error) {
value, err := cache.redisRemoteCache.Get(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key)).Result()
value, err := cache.redisRemoteCache.Get(ctx, cache.keyPrefix+key).Result()
if err != nil {
return nil, err
}

err = json.Unmarshal([]byte(value), returnValue)
if err != nil {
cache.redisRemoteCache.Del(ctx, fmt.Sprintf("%s%s", cache.keyPrefix, key)).Err()
cache.redisRemoteCache.Del(ctx, cache.keyPrefix+key).Err()
utils.LogError(err, "error unmarshalling data for key", 0, map[string]interface{}{"key": key})
return nil, err
}
Expand Down
20 changes: 16 additions & 4 deletions cache/tiered_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func NewTieredCache(cacheSize int, redisAddress string, redisPrefix string, logg
}, nil
}

// pageKeyPrefix extracts the page type from a cache key by returning
// the portion before the first ':' separator (e.g. "slots" from "slots:123:456").
func pageKeyPrefix(key string) string {
if before, _, ok := strings.Cut(key, ":"); ok {
return before
}
return key
}

func (cache *TieredCache) isSszCompatible(value interface{}, key string) bool {
if utils.Config.KillSwitch.DisableSSZPageCache {
return false
Expand All @@ -96,13 +105,17 @@ func (cache *TieredCache) isSszCompatible(value interface{}, key string) bool {
cache.sszCompatMutex.Lock()
defer cache.sszCompatMutex.Unlock()

// Re-check after acquiring write lock to avoid redundant validation.
if compat, ok = cache.sszCompatMap[valType]; ok {
return compat
}

err := modelDynSsz.ValidateType(valType)
compat = err == nil
cache.sszCompatMap[valType] = compat

if err != nil {
keySplit := strings.Split(key, ":")
cache.logger.WithError(err).Warnf("page model not ssz compatible: %v (%v)", keySplit[0], valType.Name())
cache.logger.WithError(err).Warnf("page model not ssz compatible: %v (%v)", pageKeyPrefix(key), valType.Name())
}

return compat
Expand Down Expand Up @@ -166,8 +179,7 @@ func (cache *TieredCache) Set(key string, value interface{}, expiration time.Dur
cache.sszCompatMutex.Lock()
cache.sszCompatMap[cacheType] = false
cache.sszCompatMutex.Unlock()
keySplit := strings.Split(key, ":")
cache.logger.WithError(err).Warnf("page model not ssz compatible: %v (%v): %v", keySplit[0], cacheType.Name(), err)
cache.logger.WithError(err).Warnf("page model not ssz compatible: %v (%v)", pageKeyPrefix(key), cacheType.Name())
return err
}

Expand Down
9 changes: 5 additions & 4 deletions handlers/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -286,7 +287,7 @@ func buildBlocksPageData(ctx context.Context, firstSlot uint64, pageSize uint64,
}

if pageData.DisplayMevBlock && dbSlot.EthBlockHash != nil {
if mevBlock, exists := mevBlocksMap[fmt.Sprintf("%x", dbSlot.EthBlockHash)]; exists {
if mevBlock, exists := mevBlocksMap[hex.EncodeToString(dbSlot.EthBlockHash)]; exists {
slotData.IsMevBlock = true

var relays []string
Expand Down Expand Up @@ -375,11 +376,11 @@ func buildBlocksPageData(ctx context.Context, firstSlot uint64, pageSize uint64,
}

func getClientTypeName(clientType uint8) string {
if clientType > 0 {
return execution.ClientType(clientType).String()
if clientType == 0 {
return "Unknown(0)"
}

return fmt.Sprintf("Unknown(%d)", clientType)
return execution.ClientType(clientType).String()
}

func buildBlocksPageSlotGraph(ctx context.Context, pageData *models.BlocksPageData, slotData *models.BlocksPageDataSlot, maxOpenFork *int, openForks map[int][]byte, isFirstPage bool) {
Expand Down
3 changes: 2 additions & 1 deletion handlers/blocks_filtered.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"encoding/hex"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -395,7 +396,7 @@ func buildFilteredBlocksPageData(ctx context.Context, pageIdx uint64, pageSize u
mevBlockRelays := ""

if dbBlock.Block.EthBlockHash != nil {
if mevBlock, exists := mevBlocksMap[fmt.Sprintf("%x", dbBlock.Block.EthBlockHash)]; exists {
if mevBlock, exists := mevBlocksMap[hex.EncodeToString(dbBlock.Block.EthBlockHash)]; exists {
isMevBlock = true

var relays []string
Expand Down
11 changes: 6 additions & 5 deletions handlers/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"context"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -48,7 +49,7 @@ func BuilderDetail(w http.ResponseWriter, r *http.Request) {
var superseded bool

vars := mux.Vars(r)
idxOrPubKey := strings.Replace(vars["idxOrPubKey"], "0x", "", -1)
idxOrPubKey := strings.TrimPrefix(vars["idxOrPubKey"], "0x")
builderPubKey, err := hex.DecodeString(idxOrPubKey)
if err != nil || len(builderPubKey) != 48 {
// search by index
Expand Down Expand Up @@ -384,7 +385,7 @@ func buildBuilderRecentBlocks(ctx context.Context, builderIndex uint64, chainSta
copy(parentRoot[:], slot.ParentRoot)
bids := indexer.GetBlockBids(parentRoot)
for _, bid := range bids {
if bid.BuilderIndex == builderIndexInt64 && fmt.Sprintf("%x", bid.BlockHash) == fmt.Sprintf("%x", slot.EthBlockHash) {
if bid.BuilderIndex == builderIndexInt64 && bytes.Equal(bid.BlockHash, slot.EthBlockHash) {
block.Value = bid.Value
block.ElPayment = bid.ElPayment
break
Expand All @@ -407,7 +408,7 @@ func buildBuilderRecentBids(ctx context.Context, builderIndex uint64, chainState
bidBlockHashes := make(map[string]bool, len(bids))
var minSlot, maxSlot uint64
for i, bid := range bids {
bidBlockHashes[fmt.Sprintf("%x", bid.BlockHash)] = true
bidBlockHashes[hex.EncodeToString(bid.BlockHash)] = true
if i == 0 || bid.Slot > maxSlot {
maxSlot = bid.Slot
}
Expand All @@ -431,7 +432,7 @@ func buildBuilderRecentBids(ctx context.Context, builderIndex uint64, chainState
if assignedSlot.Block == nil {
continue
}
hashKey := fmt.Sprintf("%x", assignedSlot.Block.EthBlockHash)
hashKey := hex.EncodeToString(assignedSlot.Block.EthBlockHash)
if bidBlockHashes[hashKey] && assignedSlot.Block.PayloadStatus == dbtypes.PayloadStatusCanonical {
canonicalBlockHashes[hashKey] = true
}
Expand All @@ -449,7 +450,7 @@ func buildBuilderRecentBids(ctx context.Context, builderIndex uint64, chainState
GasLimit: bid.GasLimit,
Value: bid.Value,
ElPayment: bid.ElPayment,
IsWinning: canonicalBlockHashes[fmt.Sprintf("%x", bid.BlockHash)],
IsWinning: canonicalBlockHashes[hex.EncodeToString(bid.BlockHash)],
}

result = append(result, bidData)
Expand Down
4 changes: 2 additions & 2 deletions handlers/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func buildBuildersPageData(ctx context.Context, pageNumber uint64, pageSize uint
if filterPubKey != "" {
pageData.FilterPubKey = filterPubKey
filterArgs.Add("f.pubkey", filterPubKey)
filterPubKeyVal, _ := hex.DecodeString(strings.Replace(filterPubKey, "0x", "", -1))
filterPubKeyVal, _ := hex.DecodeString(strings.TrimPrefix(filterPubKey, "0x"))
builderFilter.PubKey = filterPubKeyVal
}
if filterIndex != "" {
Expand All @@ -141,7 +141,7 @@ func buildBuildersPageData(ctx context.Context, pageNumber uint64, pageSize uint
if filterExecutionAddr != "" {
pageData.FilterExecutionAddr = filterExecutionAddr
filterArgs.Add("f.execution_addr", filterExecutionAddr)
filterExecutionAddrVal, _ := hex.DecodeString(strings.Replace(filterExecutionAddr, "0x", "", -1))
filterExecutionAddrVal, _ := hex.DecodeString(strings.TrimPrefix(filterExecutionAddr, "0x"))
builderFilter.ExecutionAddress = filterExecutionAddrVal
}
if filterStatus != "" {
Expand Down
6 changes: 2 additions & 4 deletions handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ func buildSearchResolverResult(ctx context.Context, searchQuery string) (searchR
}
}

hashQuery := strings.Replace(searchQuery, "0x", "", -1)
hashQuery = strings.Replace(hashQuery, "0X", "", -1)
hashQuery := strings.TrimPrefix(strings.TrimPrefix(searchQuery, "0x"), "0X")
if len(hashQuery) == 96 {
validatorPubkey, err := hex.DecodeString(hashQuery)
if err == nil && len(validatorPubkey) == 48 {
Expand Down Expand Up @@ -199,8 +198,7 @@ func SearchAhead(w http.ResponseWriter, r *http.Request) {
searchType := vars["type"]
urlArgs := r.URL.Query()
search := strings.Trim(urlArgs.Get("q"), " \t")
search = strings.Replace(search, "0x", "", -1)
search = strings.Replace(search, "0X", "", -1)
search = strings.TrimPrefix(strings.TrimPrefix(search, "0x"), "0X")

// 404 before cache so we don't cache disabled/unknown types
allowedTypes := map[string]bool{
Expand Down
4 changes: 2 additions & 2 deletions handlers/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Slot(w http.ResponseWriter, r *http.Request) {
)

vars := mux.Vars(r)
slotOrHash := strings.Replace(vars["slotOrHash"], "0x", "", -1)
slotOrHash := strings.TrimPrefix(vars["slotOrHash"], "0x")
blockSlot := int64(-1)
blockRootHash, err := hex.DecodeString(slotOrHash)
if err != nil || len(slotOrHash) != 64 {
Expand Down Expand Up @@ -144,7 +144,7 @@ func SlotBlob(w http.ResponseWriter, r *http.Request) {
return
}

blockRoot, err := hex.DecodeString(strings.Replace(vars["root"], "0x", "", -1))
blockRoot, err := hex.DecodeString(strings.TrimPrefix(vars["root"], "0x"))
if err != nil || len(blockRoot) != 32 {
http.Error(w, "Invalid block root", http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion handlers/slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -295,7 +296,7 @@ func buildSlotsPageData(ctx context.Context, firstSlot uint64, pageSize uint64,
}

if pageData.DisplayMevBlock && dbSlot.EthBlockHash != nil {
if mevBlock, exists := mevBlocksMap[fmt.Sprintf("%x", dbSlot.EthBlockHash)]; exists {
if mevBlock, exists := mevBlocksMap[hex.EncodeToString(dbSlot.EthBlockHash)]; exists {
slotData.IsMevBlock = true

var relays []string
Expand Down
3 changes: 2 additions & 1 deletion handlers/slots_filtered.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"encoding/hex"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -544,7 +545,7 @@ func buildFilteredSlotsPageData(ctx context.Context, pageIdx uint64, pageSize ui
slotData.PayloadStatus = uint8(payloadStatus)

if pageData.DisplayMevBlock && dbBlock.Block.EthBlockHash != nil {
if mevBlock, exists := mevBlocksMap[fmt.Sprintf("%x", dbBlock.Block.EthBlockHash)]; exists {
if mevBlock, exists := mevBlocksMap[hex.EncodeToString(dbBlock.Block.EthBlockHash)]; exists {
slotData.IsMevBlock = true

var relays []string
Expand Down
2 changes: 1 addition & 1 deletion handlers/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Validator(w http.ResponseWriter, r *http.Request) {
var validator *v1.Validator

vars := mux.Vars(r)
idxOrPubKey := strings.Replace(vars["idxOrPubKey"], "0x", "", -1)
idxOrPubKey := strings.TrimPrefix(vars["idxOrPubKey"], "0x")
validatorPubKey, err := hex.DecodeString(idxOrPubKey)
if err != nil || len(validatorPubKey) != 48 {
// search by index^
Expand Down
2 changes: 1 addition & 1 deletion handlers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func buildValidatorsPageData(ctx context.Context, pageNumber uint64, pageSize ui
if filterPubKey != "" {
pageData.FilterPubKey = filterPubKey
filterArgs.Add("f.pubkey", filterPubKey)
filterPubKeyVal, _ := hex.DecodeString(strings.Replace(filterPubKey, "0x", "", -1))
filterPubKeyVal, _ := hex.DecodeString(strings.TrimPrefix(filterPubKey, "0x"))
validatorFilter.PubKey = filterPubKeyVal
}
if filterIndex != "" {
Expand Down