2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-17 06:11:24 +00:00
|
|
|
"bytes"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
|
2018-10-05 17:14:50 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func TestSaveAndRetrieveAttestation(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
a := types.NewAttestation(&pb.AggregatedAttestation{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 0,
|
2018-10-05 17:14:50 +00:00
|
|
|
})
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
if err := db.SaveAttestation(a); err != nil {
|
|
|
|
t.Fatalf("Failed to save attestation: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
aHash := a.Key()
|
|
|
|
aPrime, err := db.GetAttestation(aHash)
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to call GetAttestation: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
aEnc, err := a.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to encode: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
aPrimeEnc, err := aPrime.Marshal()
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func TestGetNilAttestation(t *testing.T) {
|
|
|
|
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{}
|
|
|
|
a, err := db.GetAttestation(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
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func TestGetHasAttestation(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
a := types.NewAttestation(&pb.AggregatedAttestation{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 0,
|
|
|
|
})
|
|
|
|
hash := a.Key()
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
if db.HasAttestation(hash) {
|
|
|
|
t.Fatal("Expected HasAttestation to return false")
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
if err := db.SaveAttestation(a); err != nil {
|
|
|
|
t.Fatalf("Failed to save attestation: %v", err)
|
|
|
|
}
|
|
|
|
if !db.HasAttestation(hash) {
|
|
|
|
t.Fatal("Expected HasAttestation to return true")
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
}
|