2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-17 06:11:24 +00:00
|
|
|
"bytes"
|
2019-03-17 02:56:05 +00:00
|
|
|
"context"
|
2019-02-15 19:27:45 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-12-22 20:30:59 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-10-05 17:14:50 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-02-18 23:34:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2018-10-05 17:14:50 +00:00
|
|
|
)
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestSaveAndRetrieveAttestation_OK(t *testing.T) {
|
2018-10-17 06:11:24 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-12-22 20:30:59 +00:00
|
|
|
a := &pb.Attestation{
|
|
|
|
Data: &pb.AttestationData{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 0,
|
|
|
|
},
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
if err := db.SaveAttestation(context.Background(), a); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to save attestation: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 23:34:49 +00:00
|
|
|
aHash, err := hashutil.HashProto(a)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to hash Attestation: %v", err)
|
|
|
|
}
|
2019-01-21 09:34:11 +00:00
|
|
|
aPrime, err := db.Attestation(aHash)
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2019-01-21 09:34:11 +00:00
|
|
|
t.Fatalf("Failed to call Attestation: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 20:30:59 +00:00
|
|
|
aEnc, err := proto.Marshal(a)
|
2018-10-17 06:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to encode: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-12-22 20:30:59 +00:00
|
|
|
aPrimeEnc, err := proto.Marshal(aPrime)
|
2018-10-17 06:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to encode: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(aEnc, aPrimeEnc) {
|
|
|
|
t.Fatalf("Saved attestation and retrieved attestation are not equal: %#x and %#x", aEnc, aPrimeEnc)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRetrieveAttestations_OK(t *testing.T) {
|
2019-02-15 19:27:45 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
// Generate 100 unique attestations to save in DB.
|
|
|
|
attestations := make([]*pb.Attestation, 100)
|
|
|
|
for i := 0; i < len(attestations); i++ {
|
|
|
|
attestations[i] = &pb.Attestation{
|
|
|
|
Data: &pb.AttestationData{
|
|
|
|
Slot: uint64(i),
|
|
|
|
Shard: uint64(i),
|
|
|
|
},
|
|
|
|
}
|
2019-03-17 02:56:05 +00:00
|
|
|
if err := db.SaveAttestation(context.Background(), attestations[i]); err != nil {
|
2019-02-15 19:27:45 +00:00
|
|
|
t.Fatalf("Failed to save attestation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retrievedAttestations, err := db.Attestations()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve attestations: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the retrieved attestations based on slot ordering for comparison.
|
|
|
|
sort.Slice(retrievedAttestations, func(i, j int) bool {
|
|
|
|
return retrievedAttestations[i].Data.Slot < retrievedAttestations[j].Data.Slot
|
|
|
|
})
|
|
|
|
if !reflect.DeepEqual(retrievedAttestations, attestations) {
|
|
|
|
t.Log("Retrieved attestations did not match generated attestations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestDeleteAttestation_OK(t *testing.T) {
|
2019-02-15 19:27:45 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
a := &pb.Attestation{
|
|
|
|
Data: &pb.AttestationData{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
if err := db.SaveAttestation(context.Background(), a); err != nil {
|
2019-02-15 19:27:45 +00:00
|
|
|
t.Fatalf("Could not save attestation: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:34:49 +00:00
|
|
|
aHash, err := hashutil.HashProto(a)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to hash Attestation: %v", err)
|
|
|
|
}
|
2019-02-15 19:27:45 +00:00
|
|
|
aPrime, err := db.Attestation(aHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not call Attestation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(aPrime, a) {
|
|
|
|
t.Errorf("Saved attestation and retrieved attestation are not equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.DeleteAttestation(a); err != nil {
|
|
|
|
t.Fatalf("Could not delete attestation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.HasAttestation(aHash) {
|
|
|
|
t.Error("Deleted attestation still there")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestNilAttestation_OK(t *testing.T) {
|
2018-10-17 06:11:24 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
nilHash := [32]byte{}
|
2019-01-21 09:34:11 +00:00
|
|
|
a, err := db.Attestation(nilHash)
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to retrieve nilHash: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
if a != nil {
|
|
|
|
t.Fatal("Expected nilHash to return no attestation")
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestHasAttestation_OK(t *testing.T) {
|
2018-10-17 06:11:24 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-12-22 20:30:59 +00:00
|
|
|
a := &pb.Attestation{
|
|
|
|
Data: &pb.AttestationData{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-02-18 23:34:49 +00:00
|
|
|
aHash, err := hashutil.HashProto(a)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to hash Attestation: %v", err)
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2019-02-18 23:34:49 +00:00
|
|
|
if db.HasAttestation(aHash) {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatal("Expected HasAttestation to return false")
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
if err := db.SaveAttestation(context.Background(), a); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to save attestation: %v", err)
|
|
|
|
}
|
2019-02-18 23:34:49 +00:00
|
|
|
if !db.HasAttestation(aHash) {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatal("Expected HasAttestation to return true")
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
}
|