mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-11 20:20:05 +00:00
6ec9d7e6e2
* begin indices approach * use shard bucket * continue the indices approach * eliminate the filter checkers in favor of the single loop of root lookups * elim extraneous println statement * continue the indices approach * intersection for multiple filter types works, but is complex, verbose, and nearly unreadable * remove unused code * table drive tests for byte slice intersections * include all table driven tests * gazelle imports * better abstractions * better comments * variadic approach working * transform to variadic * comments * comments * separate bucket for indices for faster range scans * attestation key as hash tree root of data and different indices buckets * test pass * default behavior without filter * appropriate filter criterion errors if criterion does not apply to type * better abstractions and prune keys on deletion * better naming * fix build * fix build * rem extraneous code
151 lines
3.3 KiB
Go
151 lines
3.3 KiB
Go
package kv
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
)
|
|
|
|
func TestStore_AttestationCRUD(t *testing.T) {
|
|
db := setupDB(t)
|
|
defer teardownDB(t, db)
|
|
att := ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 5,
|
|
ParentRoot: []byte("parent"),
|
|
StartEpoch: 1,
|
|
EndEpoch: 2,
|
|
},
|
|
},
|
|
}
|
|
ctx := context.Background()
|
|
if err := db.SaveAttestation(ctx, att); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
attDataRoot, err := ssz.HashTreeRoot(att.Data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !db.HasAttestation(ctx, attDataRoot) {
|
|
t.Error("Expected attestation to exist in the db")
|
|
}
|
|
retrievedAtt, err := db.Attestation(ctx, attDataRoot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(att, retrievedAtt) {
|
|
t.Errorf("Wanted %v, received %v", att, retrievedAtt)
|
|
}
|
|
if err := db.DeleteAttestation(ctx, attDataRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if db.HasAttestation(ctx, attDataRoot) {
|
|
t.Error("Expected attestation to have been deleted from the db")
|
|
}
|
|
}
|
|
|
|
func TestStore_Attestations_FiltersCorrectly(t *testing.T) {
|
|
db := setupDB(t)
|
|
defer teardownDB(t, db)
|
|
sameParentRoot := [32]byte{1, 2, 3}
|
|
otherParentRoot := [32]byte{4, 5, 6}
|
|
atts := []*ethpb.Attestation{
|
|
{
|
|
Data: ðpb.AttestationData{
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 5,
|
|
ParentRoot: sameParentRoot[:],
|
|
StartEpoch: 1,
|
|
EndEpoch: 2,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Data: ðpb.AttestationData{
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 5,
|
|
ParentRoot: sameParentRoot[:],
|
|
StartEpoch: 10,
|
|
EndEpoch: 11,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Data: ðpb.AttestationData{
|
|
Crosslink: ðpb.Crosslink{
|
|
Shard: 5,
|
|
ParentRoot: otherParentRoot[:],
|
|
StartEpoch: 1,
|
|
EndEpoch: 20,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
ctx := context.Background()
|
|
if err := db.SaveAttestations(ctx, atts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tests := []struct {
|
|
filter *filters.QueryFilter
|
|
expectedNumAtt int
|
|
}{
|
|
{
|
|
filter: filters.NewFilter().
|
|
SetShard(5),
|
|
expectedNumAtt: 3,
|
|
},
|
|
{
|
|
filter: filters.NewFilter().
|
|
SetShard(5).
|
|
SetParentRoot(otherParentRoot[:]),
|
|
expectedNumAtt: 1,
|
|
},
|
|
{
|
|
filter: filters.NewFilter().
|
|
SetShard(5).
|
|
SetParentRoot(sameParentRoot[:]),
|
|
expectedNumAtt: 2,
|
|
},
|
|
{
|
|
filter: filters.NewFilter().SetStartEpoch(1),
|
|
expectedNumAtt: 2,
|
|
},
|
|
{
|
|
filter: filters.NewFilter().
|
|
SetParentRoot(otherParentRoot[:]),
|
|
expectedNumAtt: 1,
|
|
},
|
|
{
|
|
// Only two attestation in the list meet the composite filter criteria above.
|
|
filter: filters.NewFilter().SetShard(5).SetStartEpoch(1),
|
|
expectedNumAtt: 2,
|
|
},
|
|
{
|
|
// No specified filter should return all attestations.
|
|
filter: nil,
|
|
expectedNumAtt: 3,
|
|
},
|
|
{
|
|
// No attestation meets the criteria below.
|
|
filter: filters.NewFilter().
|
|
SetShard(1000),
|
|
expectedNumAtt: 0,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
retrievedAtts, err := db.Attestations(ctx, tt.filter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(retrievedAtts) != tt.expectedNumAtt {
|
|
t.Errorf("Expected %d attestations, received %d", tt.expectedNumAtt, len(retrievedAtts))
|
|
}
|
|
}
|
|
}
|