2022-05-04 06:15:09 +00:00
|
|
|
//go:build !fuzz
|
|
|
|
|
2021-08-25 16:39:12 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
state_native "github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2021-08-25 16:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBalanceCache_AddGetBalance(t *testing.T) {
|
|
|
|
blockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(b, uint64(i))
|
|
|
|
blockRoots[i] = b
|
|
|
|
}
|
|
|
|
raw := ðpb.BeaconState{
|
|
|
|
BlockRoots: blockRoots,
|
|
|
|
}
|
2022-09-16 22:17:46 +00:00
|
|
|
st, err := state_native.InitializeFromProtoPhase0(raw)
|
2021-08-25 16:39:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cache := NewEffectiveBalanceCache()
|
|
|
|
_, err = cache.Get(st)
|
|
|
|
require.ErrorContains(t, ErrNotFound.Error(), err)
|
|
|
|
|
|
|
|
b := uint64(100)
|
|
|
|
require.NoError(t, cache.AddTotalEffectiveBalance(st, b))
|
|
|
|
cachedB, err := cache.Get(st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b, cachedB)
|
|
|
|
|
|
|
|
require.NoError(t, st.SetSlot(1000))
|
|
|
|
_, err = cache.Get(st)
|
|
|
|
require.ErrorContains(t, ErrNotFound.Error(), err)
|
|
|
|
|
|
|
|
b = uint64(200)
|
|
|
|
require.NoError(t, cache.AddTotalEffectiveBalance(st, b))
|
|
|
|
cachedB, err = cache.Get(st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b, cachedB)
|
|
|
|
|
|
|
|
require.NoError(t, st.SetSlot(1000+params.BeaconConfig().SlotsPerHistoricalRoot))
|
|
|
|
_, err = cache.Get(st)
|
|
|
|
require.ErrorContains(t, ErrNotFound.Error(), err)
|
|
|
|
|
|
|
|
b = uint64(300)
|
|
|
|
require.NoError(t, cache.AddTotalEffectiveBalance(st, b))
|
|
|
|
cachedB, err = cache.Get(st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b, cachedB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalanceCache_BalanceKey(t *testing.T) {
|
|
|
|
blockRoots := make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
|
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(b, uint64(i))
|
|
|
|
blockRoots[i] = b
|
|
|
|
}
|
|
|
|
raw := ðpb.BeaconState{
|
|
|
|
BlockRoots: blockRoots,
|
|
|
|
}
|
2022-09-16 22:17:46 +00:00
|
|
|
st, err := state_native.InitializeFromProtoPhase0(raw)
|
2021-08-25 16:39:12 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-26 14:40:12 +00:00
|
|
|
require.NoError(t, st.SetSlot(primitives.Slot(math.MaxUint64)))
|
2021-08-25 16:39:12 +00:00
|
|
|
|
|
|
|
_, err = balanceCacheKey(st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|