prysm-pulse/beacon-chain/db/attestation_test.go
Preston Van Loon 612bb38077 Cross p2p spans, more spans, synchronous attestations, minor fixes (#2009)
* Fix assignments bug where validators don't retry for assignments on failure

* synch only please

* trying to fix state issues

* trying random stuff

* do not explode

* use ctx

* working build, failing tests

* broadcast local addrs as well as relay addrs

* fixed p2p tests, more tests to fix still

* another test fixed, log warning instead of throw error

* Fix last tests

* godoc

* add test for broadcast in apply fork choiec

* remove unneeded code

* remove tracer adapter, not needed

* remove extra stuff

* remove any

* revert addr_factory

* revert addr_factory

* Revert "revert addr_factory"

This reverts commit e93fb706494a1070158b8db31e67146d6b0648ad.

* Revert "revert addr_factory"

This reverts commit dedaa405559cc818698870c4e4570953367f1e3c.

* revert removal of this code

* unused param
2019-03-17 10:56:05 +08:00

161 lines
3.7 KiB
Go

package db
import (
"bytes"
"context"
"reflect"
"sort"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
)
func TestSaveAndRetrieveAttestation_OK(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
a := &pb.Attestation{
Data: &pb.AttestationData{
Slot: 0,
Shard: 0,
},
}
if err := db.SaveAttestation(context.Background(), a); err != nil {
t.Fatalf("Failed to save attestation: %v", err)
}
aHash, err := hashutil.HashProto(a)
if err != nil {
t.Fatalf("Failed to hash Attestation: %v", err)
}
aPrime, err := db.Attestation(aHash)
if err != nil {
t.Fatalf("Failed to call Attestation: %v", err)
}
aEnc, err := proto.Marshal(a)
if err != nil {
t.Fatalf("Failed to encode: %v", err)
}
aPrimeEnc, err := proto.Marshal(aPrime)
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)
}
}
func TestRetrieveAttestations_OK(t *testing.T) {
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),
},
}
if err := db.SaveAttestation(context.Background(), attestations[i]); err != nil {
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")
}
}
func TestDeleteAttestation_OK(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
a := &pb.Attestation{
Data: &pb.AttestationData{
Slot: 0,
Shard: 0,
},
}
if err := db.SaveAttestation(context.Background(), a); err != nil {
t.Fatalf("Could not save attestation: %v", err)
}
aHash, err := hashutil.HashProto(a)
if err != nil {
t.Fatalf("Failed to hash Attestation: %v", err)
}
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")
}
}
func TestNilAttestation_OK(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
nilHash := [32]byte{}
a, err := db.Attestation(nilHash)
if err != nil {
t.Fatalf("Failed to retrieve nilHash: %v", err)
}
if a != nil {
t.Fatal("Expected nilHash to return no attestation")
}
}
func TestHasAttestation_OK(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
a := &pb.Attestation{
Data: &pb.AttestationData{
Slot: 0,
Shard: 0,
},
}
aHash, err := hashutil.HashProto(a)
if err != nil {
t.Fatalf("Failed to hash Attestation: %v", err)
}
if db.HasAttestation(aHash) {
t.Fatal("Expected HasAttestation to return false")
}
if err := db.SaveAttestation(context.Background(), a); err != nil {
t.Fatalf("Failed to save attestation: %v", err)
}
if !db.HasAttestation(aHash) {
t.Fatal("Expected HasAttestation to return true")
}
}