mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 21:27:19 +00:00
74df479bd3
* Use active balance cache * Feature flag and test * Gazelle * Fix fuzz * Fix test * Mix in validator count * Update test to reflect validator count * Add eth1 data block hash * Fix assign * Use eth1 data root * Update active_balance_test.go * Remove eth1 data root * Remove eth1 data usage in tests Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package cache
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"testing"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
state "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestBalanceCache_AddGetBalance(t *testing.T) {
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
|
|
EnableActiveBalanceCache: true,
|
|
})
|
|
defer resetCfg()
|
|
|
|
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.InitializeFromProto(raw)
|
|
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,
|
|
}
|
|
st, err := state.InitializeFromProto(raw)
|
|
require.NoError(t, err)
|
|
require.NoError(t, st.SetSlot(types.Slot(math.MaxUint64)))
|
|
|
|
_, err = balanceCacheKey(st)
|
|
require.NoError(t, err)
|
|
}
|