2022-04-06 23:36:52 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2022-04-06 23:36:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidatorPayloadIDsCache_GetAndSaveValidatorPayloadIDs(t *testing.T) {
|
2023-12-22 18:47:51 +00:00
|
|
|
cache := NewPayloadIDCache()
|
2022-12-22 09:20:10 +00:00
|
|
|
var r [32]byte
|
2023-12-22 18:47:51 +00:00
|
|
|
p, ok := cache.PayloadID(0, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, false, ok)
|
2023-12-22 18:47:51 +00:00
|
|
|
require.Equal(t, primitives.PayloadID{}, p)
|
2022-04-06 23:36:52 +00:00
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
slot := primitives.Slot(1234)
|
2023-12-22 18:47:51 +00:00
|
|
|
pid := primitives.PayloadID{1, 2, 3, 3, 7, 8, 7, 8}
|
2022-08-11 14:55:26 +00:00
|
|
|
r = [32]byte{1, 2, 3}
|
2023-12-22 18:47:51 +00:00
|
|
|
cache.Set(slot, r, pid)
|
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, pid, p)
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
slot = primitives.Slot(9456456)
|
2022-08-11 14:55:26 +00:00
|
|
|
r = [32]byte{4, 5, 6}
|
2023-12-22 18:47:51 +00:00
|
|
|
cache.Set(slot, r, primitives.PayloadID{})
|
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, true, ok)
|
2023-12-22 18:47:51 +00:00
|
|
|
require.Equal(t, primitives.PayloadID{}, p)
|
2022-04-06 23:36:52 +00:00
|
|
|
|
|
|
|
// reset cache without pid
|
2023-01-26 14:40:12 +00:00
|
|
|
slot = primitives.Slot(9456456)
|
2022-08-11 14:55:26 +00:00
|
|
|
r = [32]byte{7, 8, 9}
|
2022-04-06 23:36:52 +00:00
|
|
|
pid = [8]byte{3, 2, 3, 33, 72, 8, 7, 8}
|
2023-12-22 18:47:51 +00:00
|
|
|
cache.Set(slot, r, pid)
|
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, pid, p)
|
|
|
|
|
2022-08-11 14:55:26 +00:00
|
|
|
// Forked chain
|
|
|
|
r = [32]byte{1, 2, 3}
|
2023-12-22 18:47:51 +00:00
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-08-11 14:55:26 +00:00
|
|
|
require.Equal(t, false, ok)
|
2023-12-22 18:47:51 +00:00
|
|
|
require.Equal(t, primitives.PayloadID{}, p)
|
2022-08-11 14:55:26 +00:00
|
|
|
|
2023-12-22 18:47:51 +00:00
|
|
|
// existing pid - change the cache
|
2023-01-26 14:40:12 +00:00
|
|
|
slot = primitives.Slot(9456456)
|
2022-08-11 14:55:26 +00:00
|
|
|
r = [32]byte{7, 8, 9}
|
2023-12-22 18:47:51 +00:00
|
|
|
newPid := primitives.PayloadID{1, 2, 3, 33, 72, 8, 7, 1}
|
|
|
|
cache.Set(slot, r, newPid)
|
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, true, ok)
|
2023-12-22 18:47:51 +00:00
|
|
|
require.Equal(t, newPid, p)
|
2022-04-06 23:36:52 +00:00
|
|
|
|
|
|
|
// remove cache entry
|
2023-12-22 18:47:51 +00:00
|
|
|
cache.prune(slot + 1)
|
|
|
|
p, ok = cache.PayloadID(slot, r)
|
2022-04-06 23:36:52 +00:00
|
|
|
require.Equal(t, false, ok)
|
2023-12-22 18:47:51 +00:00
|
|
|
require.Equal(t, primitives.PayloadID{}, p)
|
2022-04-06 23:36:52 +00:00
|
|
|
}
|