mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 10:41:19 +00:00
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package kv
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
func TestKV_Unaggregated_AlreadyAggregated(t *testing.T) {
|
|
cache := NewAttCaches()
|
|
|
|
att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b111}}
|
|
|
|
wanted := "attestation is aggregated"
|
|
if err := cache.SaveUnaggregatedAttestation(att); !strings.Contains(err.Error(), wanted) {
|
|
t.Error("Did not received wanted error")
|
|
}
|
|
}
|
|
|
|
func TestKV_Unaggregated_CanSaveRetrieve(t *testing.T) {
|
|
cache := NewAttCaches()
|
|
|
|
data := ðpb.AttestationData{Slot: 100, CommitteeIndex: 99}
|
|
att1 := ðpb.Attestation{Data: ðpb.AttestationData{}, AggregationBits: bitfield.Bitlist{0b101}}
|
|
att2 := ðpb.Attestation{Data: data, Signature: []byte{0, 0}, AggregationBits: bitfield.Bitlist{0b110}}
|
|
att3 := ðpb.Attestation{Data: data, Signature: []byte{0, 1}, AggregationBits: bitfield.Bitlist{0b110}}
|
|
atts := []*ethpb.Attestation{att1, att2, att3}
|
|
|
|
for _, att := range atts {
|
|
if err := cache.SaveUnaggregatedAttestation(att); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
returned := cache.UnaggregatedAttestation(data.Slot, data.CommitteeIndex)
|
|
sort.Slice(returned, func(i, j int) bool {
|
|
return binary.BigEndian.Uint16(returned[i].Signature) < binary.BigEndian.Uint16(returned[j].Signature)
|
|
})
|
|
|
|
wanted := []*ethpb.Attestation{att2, att3}
|
|
if !reflect.DeepEqual(wanted, returned) {
|
|
t.Error("Did not receive correct unaggregated atts")
|
|
}
|
|
}
|
|
|
|
func TestKV_Unaggregated_CanDelete(t *testing.T) {
|
|
cache := NewAttCaches()
|
|
|
|
att1 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b101}}
|
|
att2 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b110}}
|
|
att3 := ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b110}}
|
|
atts := []*ethpb.Attestation{att1, att2, att3}
|
|
|
|
for _, att := range atts {
|
|
if err := cache.SaveUnaggregatedAttestation(att); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if err := cache.DeleteUnaggregatedAttestation(att2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
returned := cache.UnaggregatedAttestation(2, 0)
|
|
|
|
if !reflect.DeepEqual([]*ethpb.Attestation{}, returned) {
|
|
t.Error("Did not receive correct aggregated atts")
|
|
}
|
|
}
|
|
|
|
func TestKV_Unaggregated_CheckExpTime(t *testing.T) {
|
|
cache := NewAttCaches()
|
|
|
|
att := ðpb.Attestation{AggregationBits: bitfield.Bitlist{0b11}}
|
|
r, _ := ssz.HashTreeRoot(att)
|
|
|
|
if err := cache.SaveUnaggregatedAttestation(att); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
item, exp, exists := cache.unAggregatedAtt.GetWithExpiration(string(r[:]))
|
|
if !exists {
|
|
t.Error("Saved att does not exist")
|
|
}
|
|
|
|
receivedAtt := item.(*ethpb.Attestation)
|
|
if !proto.Equal(att, receivedAtt) {
|
|
t.Error("Did not receive correct unaggregated att")
|
|
}
|
|
|
|
wanted := float64(params.BeaconConfig().SlotsPerEpoch * params.BeaconConfig().SecondsPerSlot)
|
|
if math.RoundToEven(exp.Sub(time.Now()).Seconds()) != wanted {
|
|
t.Errorf("Did not receive correct exp time. Wanted: %f, got: %f", wanted,
|
|
math.RoundToEven(exp.Sub(time.Now()).Seconds()))
|
|
}
|
|
}
|