Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use HMSET for batch Builder status store #373

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion datastore/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var (
RedisBlockBuilderStatusBlacklisted BlockBuilderStatus = "blacklisted"
)

type PubKeyStatusPair struct {
EliasiOfir marked this conversation as resolved.
Show resolved Hide resolved
PubKey string
Status BlockBuilderStatus
}

func PubkeyHexToLowerStr(pk boostTypes.PubkeyHex) string {
return strings.ToLower(string(pk))
}
Expand Down Expand Up @@ -385,8 +390,17 @@ func (r *RedisCache) GetBidTrace(slot uint64, proposerPubkey, blockHash string)
return resp, err
}

func (r *RedisCache) SetMultiBlockBuilderStatus(pairs []PubKeyStatusPair) (err error) {
values := []string{}
for _, v := range pairs {
values = append(values, v.PubKey, string(v.Status))
}

return r.client.HMSet(context.Background(), r.keyBlockBuilderStatus, values).Err()
}

func (r *RedisCache) SetBlockBuilderStatus(builderPubkey string, status BlockBuilderStatus) (err error) {
return r.client.HSet(context.Background(), r.keyBlockBuilderStatus, builderPubkey, string(status)).Err()
return r.SetMultiBlockBuilderStatus([]PubKeyStatusPair{{Status: status, PubKey: builderPubkey}})
}

func (r *RedisCache) GetBlockBuilderStatus(builderPubkey string) (isHighPrio, isBlacklisted bool, err error) {
Expand Down
72 changes: 72 additions & 0 deletions datastore/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,78 @@ func TestActiveValidators(t *testing.T) {
require.True(t, vals[pk1])
}

func TestBlockBuilderStatus(t *testing.T) {
cache := setupTestRedis(t)

pk1 := "0x1a1d7b8dd64e0aafe7ea7b6c95065c9364cf99d38470c12ee807d55f7de1529ad29ce2c422e0b65e3d5a05c02caca249"
pk2 := "0x2a1d7b8dd64e0aafe7ea7b6c95065c9364cf99d38470c12ee807d55f7de1529ad29ce2c422e0b65e3d5a05c02caca249"
pk3 := "0x8016d3229030424cfeff6c5b813970ea193f8d012cfa767270ca9057d58eddc556e96c14544bf4c038dbed5f24aa8da0"

t.Run("Set and get single block builder status", func(t *testing.T) {
// Set status test
require.NoError(t, cache.SetBlockBuilderStatus(pk1, RedisBlockBuilderStatusHighPrio))

isHighPrio, isBlacklisted, err := cache.GetBlockBuilderStatus(pk1)
require.NoError(t, err)
require.True(t, isHighPrio)
require.False(t, isBlacklisted)

// Update status test
require.NoError(t, cache.SetBlockBuilderStatus(pk1, RedisBlockBuilderStatusBlacklisted))

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk1)
require.NoError(t, err)
require.False(t, isHighPrio)
require.True(t, isBlacklisted)
})

t.Run("Set and get multi block builder statuses", func(t *testing.T) {
pairs := []PubKeyStatusPair{
{PubKey: pk1, Status: RedisBlockBuilderStatusLowPrio},
{PubKey: pk2, Status: RedisBlockBuilderStatusHighPrio},
{PubKey: pk3, Status: RedisBlockBuilderStatusBlacklisted},
}

// Set multi status test
require.NoError(t, cache.SetMultiBlockBuilderStatus(pairs))

isHighPrio, isBlacklisted, err := cache.GetBlockBuilderStatus(pk1)
require.NoError(t, err)
require.False(t, isHighPrio)
require.False(t, isBlacklisted)

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk2)
require.NoError(t, err)
require.True(t, isHighPrio)
require.False(t, isBlacklisted)

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk3)
require.NoError(t, err)
require.False(t, isHighPrio)
require.True(t, isBlacklisted)

// Update multi status test
pairs[0].Status = RedisBlockBuilderStatusBlacklisted

require.NoError(t, cache.SetMultiBlockBuilderStatus(pairs))

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk1)
require.NoError(t, err)
require.False(t, isHighPrio)
require.True(t, isBlacklisted)

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk2)
require.NoError(t, err)
require.True(t, isHighPrio)
require.False(t, isBlacklisted)

isHighPrio, isBlacklisted, err = cache.GetBlockBuilderStatus(pk3)
require.NoError(t, err)
require.False(t, isHighPrio)
require.True(t, isBlacklisted)
})
}

func TestBuilderBids(t *testing.T) {
slot := uint64(2)
parentHash := "0x13e606c7b3d1faad7e83503ce3dedce4c6bb89b0c28ffb240d713c7b110b9747"
Expand Down
11 changes: 7 additions & 4 deletions services/housekeeper/housekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,15 @@ func (hk *Housekeeper) updateBlockBuildersInRedis() {
}

hk.log.Infof("updating %d block builders in Redis...", len(builders))
pairs := []datastore.PubKeyStatusPair{}

for _, builder := range builders {
status := datastore.MakeBlockBuilderStatus(builder.IsHighPrio, builder.IsBlacklisted)
hk.log.Infof("updating block builder in Redis: %s - %s", builder.BuilderPubkey, status)
err = hk.redis.SetBlockBuilderStatus(builder.BuilderPubkey, status)
if err != nil {
hk.log.WithError(err).Error("failed to set block builder status in redis")
}
pairs = append(pairs, datastore.PubKeyStatusPair{PubKey: builder.BuilderPubkey, Status: status})
}
err = hk.redis.SetMultiBlockBuilderStatus(pairs)
if err != nil {
hk.log.WithError(err).Error("failed to set block builders status in redis")
}
}