mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-03 00:27:38 +00:00
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
//go:build !fuzz
|
|
|
|
package cache
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"math/big"
|
|
"testing"
|
|
|
|
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
)
|
|
|
|
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,
|
|
}
|
|
st, err := state_native.InitializeFromProtoPhase0(raw)
|
|
require.NoError(t, err)
|
|
|
|
cache := NewEffectiveBalanceCache()
|
|
_, err = cache.Get(st)
|
|
require.ErrorContains(t, ErrNotFound.Error(), err)
|
|
|
|
b := big.NewInt(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 = big.NewInt(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 = big.NewInt(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,
|
|
}
|
|
st, err := state_native.InitializeFromProtoPhase0(raw)
|
|
require.NoError(t, err)
|
|
require.NoError(t, st.SetSlot(primitives.Slot(math.MaxUint64)))
|
|
|
|
_, err = balanceCacheKey(st)
|
|
require.NoError(t, err)
|
|
}
|