prysm-pulse/slasher/db/kv/highest_attestation_test.go
Alon Muroch a04b7c2e4f
Slasher highest source target (#7604)
* WIP - slasher highest attestation start

* fixed previous

* highest source and target

* highest attestation cache

* cleanup

* persist + fixes

* PR fixes and cleanup

* slashing proto

* highest att. api

* cleanup + tests

* increased highest att. cache to 300K

* removed highest att. api (for a separate PR)

* fixed linting

* bazel build fix

* highest att. kv test

* slasher highest att. test + purge + fix on eviction persist performance

* cleanup + linting

* linting + test fixes

* bazel gazelle run

* PR fixes

* run goimports

* go mod tidy

* ineffectual assignment fix

* run gazelle

* bazel gazelle run

* test fixes

* linter fix

* Apply suggestions from code review

Co-authored-by: Shay Zluf <thezluf@gmail.com>

* goimports run

* cache tests

* A bunch of small fixes

* gazelle fix + gofmt

* merge fixes

* kv ordering fix

* small typos and text fixes

* capital letter fix

Co-authored-by: Shay Zluf <thezluf@gmail.com>
2020-10-26 14:15:42 +02:00

81 lines
1.9 KiB
Go

package kv
import (
"context"
"testing"
slashbp "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestSaveHighestAttestation(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
tests := []struct {
name string
toSave []*slashbp.HighestAttestation
cacheEnabled bool
}{
{
name: "save to cache",
toSave: []*slashbp.HighestAttestation{
{
HighestTargetEpoch: 1,
HighestSourceEpoch: 0,
ValidatorId: 1,
},
},
cacheEnabled: true,
},
{
name: "save to db",
toSave: []*slashbp.HighestAttestation{
{
HighestTargetEpoch: 1,
HighestSourceEpoch: 0,
ValidatorId: 2,
},
},
cacheEnabled: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, att := range tt.toSave {
db.highestAttCacheEnabled = tt.cacheEnabled
require.NoError(t, db.SaveHighestAttestation(ctx, att), "Save highest attestation failed")
found, err := db.HighestAttestation(ctx, att.ValidatorId)
require.NoError(t, err)
require.NotNil(t, found)
require.Equal(t, att.ValidatorId, found.ValidatorId)
require.Equal(t, att.HighestSourceEpoch, found.HighestSourceEpoch)
require.Equal(t, att.HighestTargetEpoch, found.HighestTargetEpoch)
}
})
}
}
func TestFetchNonExistingHighestAttestation(t *testing.T) {
db := setupDB(t)
ctx := context.Background()
t.Run("cached", func(t *testing.T) {
db.highestAttCacheEnabled = true
found, err := db.HighestAttestation(ctx, 1)
require.NoError(t, err)
require.Equal(t, (*slashbp.HighestAttestation)(nil), found, "should not find HighestAttestation")
})
t.Run("disk", func(t *testing.T) {
db.highestAttCacheEnabled = false
found, err := db.HighestAttestation(ctx, 1)
require.NoError(t, err)
require.Equal(t, (*slashbp.HighestAttestation)(nil), found, "should not find HighestAttestation")
})
}