mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
a069738c20
* update shared/params * update eth2-types deps * update protobufs * update shared/* * fix testutil/state * update beacon-chain/state * update beacon-chain/db * update tests * fix test * update beacon-chain/core * update beacon-chain/blockchain * update beacon-chain/cache * beacon-chain/forkchoice * update beacon-chain/operations * update beacon-chain/p2p * update beacon-chain/rpc * update sync/initial-sync * update deps * update deps * go fmt * update beacon-chain/sync * update endtoend/ * bazel build //beacon-chain - works w/o issues * update slasher code * udpate tools/ * update validator/ * update fastssz * fix build * fix test building * update tests * update ethereumapis deps * fix tests * update state/stategen * fix build * fix test * add FarFutureSlot * go imports * Radek's suggestions * Ivan's suggestions * type conversions * Nishant's suggestions * add more tests to rpc_send_request * fix test * clean up * fix conflicts Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: nisdas <nishdas93@gmail.com>
125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package attestations
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/runutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
|
)
|
|
|
|
func TestPruneExpired_Ticker(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
s, err := NewService(ctx, &Config{
|
|
Pool: NewPool(),
|
|
pruneInterval: 250 * time.Millisecond,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ad1 := testutil.HydrateAttestationData(ðpb.AttestationData{})
|
|
|
|
ad2 := testutil.HydrateAttestationData(ðpb.AttestationData{Slot: 1})
|
|
|
|
atts := []*ethpb.Attestation{
|
|
{Data: ad1, AggregationBits: bitfield.Bitlist{0b1000, 0b1}, Signature: make([]byte, 96)},
|
|
{Data: ad2, AggregationBits: bitfield.Bitlist{0b1000, 0b1}, Signature: make([]byte, 96)},
|
|
}
|
|
require.NoError(t, s.pool.SaveUnaggregatedAttestations(atts))
|
|
require.Equal(t, 2, s.pool.UnaggregatedAttestationCount(), "Unexpected number of attestations")
|
|
atts = []*ethpb.Attestation{
|
|
{Data: ad1, AggregationBits: bitfield.Bitlist{0b1101, 0b1}, Signature: make([]byte, 96)},
|
|
{Data: ad2, AggregationBits: bitfield.Bitlist{0b1101, 0b1}, Signature: make([]byte, 96)},
|
|
}
|
|
require.NoError(t, s.pool.SaveAggregatedAttestations(atts))
|
|
assert.Equal(t, 2, s.pool.AggregatedAttestationCount())
|
|
require.NoError(t, s.pool.SaveBlockAttestations(atts))
|
|
|
|
// Rewind back one epoch worth of time.
|
|
s.genesisTime = uint64(timeutils.Now().Unix()) - uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
|
|
|
go s.pruneAttsPool()
|
|
|
|
done := make(chan struct{}, 1)
|
|
runutil.RunEvery(ctx, 500*time.Millisecond, func() {
|
|
atts, err := s.pool.UnaggregatedAttestations()
|
|
require.NoError(t, err)
|
|
for _, attestation := range atts {
|
|
if attestation.Data.Slot == 0 {
|
|
return
|
|
}
|
|
}
|
|
for _, attestation := range s.pool.AggregatedAttestations() {
|
|
if attestation.Data.Slot == 0 {
|
|
return
|
|
}
|
|
}
|
|
for _, attestation := range s.pool.BlockAttestations() {
|
|
if attestation.Data.Slot == 0 {
|
|
return
|
|
}
|
|
}
|
|
if s.pool.UnaggregatedAttestationCount() != 1 || s.pool.AggregatedAttestationCount() != 1 {
|
|
return
|
|
}
|
|
done <- struct{}{}
|
|
})
|
|
select {
|
|
case <-done:
|
|
// All checks are passed.
|
|
case <-ctx.Done():
|
|
t.Error("Test case takes too long to complete")
|
|
}
|
|
}
|
|
|
|
func TestPruneExpired_PruneExpiredAtts(t *testing.T) {
|
|
s, err := NewService(context.Background(), &Config{Pool: NewPool()})
|
|
require.NoError(t, err)
|
|
|
|
ad1 := testutil.HydrateAttestationData(ðpb.AttestationData{})
|
|
|
|
ad2 := testutil.HydrateAttestationData(ðpb.AttestationData{})
|
|
|
|
att1 := ðpb.Attestation{Data: ad1, AggregationBits: bitfield.Bitlist{0b1101}}
|
|
att2 := ðpb.Attestation{Data: ad1, AggregationBits: bitfield.Bitlist{0b1111}}
|
|
att3 := ðpb.Attestation{Data: ad2, AggregationBits: bitfield.Bitlist{0b1101}}
|
|
att4 := ðpb.Attestation{Data: ad2, AggregationBits: bitfield.Bitlist{0b1110}}
|
|
atts := []*ethpb.Attestation{att1, att2, att3, att4}
|
|
require.NoError(t, s.pool.SaveAggregatedAttestations(atts))
|
|
require.NoError(t, s.pool.SaveBlockAttestations(atts))
|
|
|
|
// Rewind back one epoch worth of time.
|
|
s.genesisTime = uint64(timeutils.Now().Unix()) - uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
|
|
|
s.pruneExpiredAtts()
|
|
// All the attestations on slot 0 should be pruned.
|
|
for _, attestation := range s.pool.AggregatedAttestations() {
|
|
if attestation.Data.Slot == 0 {
|
|
t.Error("Should be pruned")
|
|
}
|
|
}
|
|
for _, attestation := range s.pool.BlockAttestations() {
|
|
if attestation.Data.Slot == 0 {
|
|
t.Error("Should be pruned")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPruneExpired_Expired(t *testing.T) {
|
|
s, err := NewService(context.Background(), &Config{Pool: NewPool()})
|
|
require.NoError(t, err)
|
|
|
|
// Rewind back one epoch worth of time.
|
|
s.genesisTime = uint64(timeutils.Now().Unix()) - uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot))
|
|
assert.Equal(t, true, s.expired(0), "Should be expired")
|
|
assert.Equal(t, false, s.expired(1), "Should not be expired")
|
|
}
|