2021-11-09 16:49:28 +00:00
|
|
|
package history_test
|
2020-11-26 04:09:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-01-22 23:12:22 +00:00
|
|
|
"fmt"
|
2020-11-26 04:09:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2021-01-22 23:12:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/db/kv"
|
2020-11-26 04:09:35 +00:00
|
|
|
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
|
2021-11-09 16:49:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/slashing-protection-history/format"
|
2021-01-22 23:12:22 +00:00
|
|
|
slashtest "github.com/prysmaticlabs/prysm/validator/testing"
|
2020-11-26 04:09:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImportExport_RoundTrip(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-01-22 23:12:22 +00:00
|
|
|
numValidators := 10
|
|
|
|
publicKeys, err := slashtest.CreateRandomPubKeys(numValidators)
|
|
|
|
require.NoError(t, err)
|
2020-11-26 04:09:35 +00:00
|
|
|
validatorDB := dbtest.SetupDB(t, publicKeys)
|
|
|
|
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
|
|
// standard slashing protection format JSON struct.
|
2021-07-01 21:50:56 +00:00
|
|
|
attestingHistory, proposalHistory := slashtest.MockAttestingAndProposalHistories(publicKeys)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
wanted, err := slashtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
|
|
require.NoError(t, err)
|
2020-11-26 04:09:35 +00:00
|
|
|
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
|
|
blob, err := json.Marshal(wanted)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
|
2020-11-26 04:09:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Next up, we export our slashing protection database into the EIP standard file.
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
eipStandard, err := history.ExportStandardProtectionJSON(ctx, validatorDB)
|
2020-11-26 04:09:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We compare the metadata fields from import to export.
|
|
|
|
require.Equal(t, wanted.Metadata, eipStandard.Metadata)
|
|
|
|
|
|
|
|
// The values in the data field of the EIP struct are not guaranteed to be sorted,
|
|
|
|
// so we create a map to verify we have the data we expected.
|
|
|
|
require.Equal(t, len(wanted.Data), len(eipStandard.Data))
|
|
|
|
|
2021-01-22 23:12:22 +00:00
|
|
|
dataByPubKey := make(map[string]*format.ProtectionData)
|
2020-11-26 04:09:35 +00:00
|
|
|
for _, item := range wanted.Data {
|
|
|
|
dataByPubKey[item.Pubkey] = item
|
|
|
|
}
|
|
|
|
for _, item := range eipStandard.Data {
|
|
|
|
want, ok := dataByPubKey[item.Pubkey]
|
|
|
|
require.Equal(t, true, ok)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.Equal(t, len(want.SignedAttestations), len(item.SignedAttestations))
|
|
|
|
require.Equal(t, len(want.SignedBlocks), len(item.SignedBlocks))
|
|
|
|
wantedAttsByRoot := make(map[string]*format.SignedAttestation)
|
|
|
|
for _, att := range want.SignedAttestations {
|
|
|
|
wantedAttsByRoot[att.SigningRoot] = att
|
|
|
|
}
|
|
|
|
for _, att := range item.SignedAttestations {
|
|
|
|
wantedAtt, ok := wantedAttsByRoot[att.SigningRoot]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.DeepEqual(t, wantedAtt, att)
|
|
|
|
}
|
|
|
|
require.DeepEqual(t, want.SignedBlocks, item.SignedBlocks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImportExport_RoundTrip_SkippedAttestationEpochs(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
numValidators := 1
|
|
|
|
pubKeys, err := slashtest.CreateRandomPubKeys(numValidators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
validatorDB := dbtest.SetupDB(t, pubKeys)
|
|
|
|
wanted := &format.EIPSlashingProtectionFormat{
|
|
|
|
Metadata: struct {
|
|
|
|
InterchangeFormatVersion string `json:"interchange_format_version"`
|
|
|
|
GenesisValidatorsRoot string `json:"genesis_validators_root"`
|
|
|
|
}{
|
2021-02-03 16:59:17 +00:00
|
|
|
InterchangeFormatVersion: format.InterchangeFormatVersion,
|
2021-11-03 14:16:57 +00:00
|
|
|
GenesisValidatorsRoot: fmt.Sprintf("%#x", [32]byte{1}),
|
2021-01-22 23:12:22 +00:00
|
|
|
},
|
|
|
|
Data: []*format.ProtectionData{
|
|
|
|
{
|
|
|
|
Pubkey: fmt.Sprintf("%#x", pubKeys[0]),
|
|
|
|
SignedAttestations: []*format.SignedAttestation{
|
|
|
|
{
|
|
|
|
SourceEpoch: "1",
|
|
|
|
TargetEpoch: "2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SourceEpoch: "8",
|
|
|
|
TargetEpoch: "9",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SignedBlocks: make([]*format.SignedBlock, 0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
|
|
blob, err := json.Marshal(wanted)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Next up, we export our slashing protection database into the EIP standard file.
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
eipStandard, err := history.ExportStandardProtectionJSON(ctx, validatorDB)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We compare the metadata fields from import to export.
|
|
|
|
require.Equal(t, wanted.Metadata, eipStandard.Metadata)
|
|
|
|
|
|
|
|
// The values in the data field of the EIP struct are not guaranteed to be sorted,
|
|
|
|
// so we create a map to verify we have the data we expected.
|
|
|
|
require.Equal(t, len(wanted.Data), len(eipStandard.Data))
|
|
|
|
require.DeepEqual(t, wanted.Data, eipStandard.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImportInterchangeData_OK(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
numValidators := 10
|
|
|
|
publicKeys, err := slashtest.CreateRandomPubKeys(numValidators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
validatorDB := dbtest.SetupDB(t, publicKeys)
|
|
|
|
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
|
|
// standard slashing protection format JSON struct.
|
2021-07-01 21:50:56 +00:00
|
|
|
attestingHistory, proposalHistory := slashtest.MockAttestingAndProposalHistories(publicKeys)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
standardProtectionFormat, err := slashtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
|
|
blob, err := json.Marshal(standardProtectionFormat)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Next, we attempt to retrieve the attesting and proposals histories from our database and
|
|
|
|
// verify those indeed match the originally generated mock histories.
|
|
|
|
for i := 0; i < len(publicKeys); i++ {
|
|
|
|
receivedAttestingHistory, err := validatorDB.AttestationHistoryForPubKey(ctx, publicKeys[i])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wantedAttsByRoot := make(map[[32]byte]*kv.AttestationRecord)
|
|
|
|
for _, att := range attestingHistory[i] {
|
|
|
|
wantedAttsByRoot[att.SigningRoot] = att
|
|
|
|
}
|
|
|
|
for _, att := range receivedAttestingHistory {
|
|
|
|
wantedAtt, ok := wantedAttsByRoot[att.SigningRoot]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.DeepEqual(t, wantedAtt, att)
|
|
|
|
}
|
|
|
|
|
|
|
|
proposals := proposalHistory[i].Proposals
|
2021-02-02 15:53:12 +00:00
|
|
|
receivedProposalHistory, err := validatorDB.ProposalHistoryForPubKey(ctx, publicKeys[i])
|
|
|
|
require.NoError(t, err)
|
2021-02-16 07:45:34 +00:00
|
|
|
rootsBySlot := make(map[types.Slot][]byte)
|
2021-02-02 15:53:12 +00:00
|
|
|
for _, proposal := range receivedProposalHistory {
|
|
|
|
rootsBySlot[proposal.Slot] = proposal.SigningRoot
|
|
|
|
}
|
2021-01-22 23:12:22 +00:00
|
|
|
for _, proposal := range proposals {
|
2021-02-02 15:53:12 +00:00
|
|
|
receivedRoot, ok := rootsBySlot[proposal.Slot]
|
|
|
|
require.DeepEqual(t, true, ok)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.DeepEqual(
|
|
|
|
t,
|
2021-02-02 15:53:12 +00:00
|
|
|
receivedRoot,
|
2021-01-22 23:12:22 +00:00
|
|
|
proposal.SigningRoot,
|
|
|
|
"Imported proposals are different then the generated ones",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImportInterchangeData_OK_SavesBlacklistedPublicKeys(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
numValidators := 3
|
|
|
|
publicKeys, err := slashtest.CreateRandomPubKeys(numValidators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
validatorDB := dbtest.SetupDB(t, publicKeys)
|
|
|
|
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
|
|
// standard slashing protection format JSON struct.
|
2021-07-01 21:50:56 +00:00
|
|
|
attestingHistory, proposalHistory := slashtest.MockAttestingAndProposalHistories(publicKeys)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
standardProtectionFormat, err := slashtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We add a slashable block for public key at index 1.
|
|
|
|
pubKey0 := standardProtectionFormat.Data[0].Pubkey
|
|
|
|
standardProtectionFormat.Data[0].SignedBlocks = append(
|
|
|
|
standardProtectionFormat.Data[0].SignedBlocks,
|
|
|
|
&format.SignedBlock{
|
|
|
|
Slot: "700",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{1}),
|
|
|
|
},
|
|
|
|
&format.SignedBlock{
|
|
|
|
Slot: "700",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{2}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// We add a slashable attestation for public key at index 1
|
|
|
|
// representing a double vote event.
|
|
|
|
pubKey1 := standardProtectionFormat.Data[1].Pubkey
|
|
|
|
standardProtectionFormat.Data[1].SignedAttestations = append(
|
|
|
|
standardProtectionFormat.Data[1].SignedAttestations,
|
|
|
|
&format.SignedAttestation{
|
|
|
|
TargetEpoch: "700",
|
|
|
|
SourceEpoch: "699",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{1}),
|
|
|
|
},
|
|
|
|
&format.SignedAttestation{
|
|
|
|
TargetEpoch: "700",
|
|
|
|
SourceEpoch: "699",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{2}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// We add a slashable attestation for public key at index 2
|
|
|
|
// representing a surround vote event.
|
|
|
|
pubKey2 := standardProtectionFormat.Data[2].Pubkey
|
|
|
|
standardProtectionFormat.Data[2].SignedAttestations = append(
|
|
|
|
standardProtectionFormat.Data[2].SignedAttestations,
|
|
|
|
&format.SignedAttestation{
|
|
|
|
TargetEpoch: "800",
|
|
|
|
SourceEpoch: "805",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{4}),
|
|
|
|
},
|
|
|
|
&format.SignedAttestation{
|
|
|
|
TargetEpoch: "801",
|
|
|
|
SourceEpoch: "804",
|
|
|
|
SigningRoot: fmt.Sprintf("%#x", [32]byte{5}),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
|
|
blob, err := json.Marshal(standardProtectionFormat)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
|
|
|
|
// Next, we attempt to import it into our validator database.
|
2021-11-09 16:49:28 +00:00
|
|
|
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert the three slashable keys in the imported JSON were saved to the database.
|
|
|
|
sKeys, err := validatorDB.EIPImportBlacklistedPublicKeys(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
slashableKeys := make(map[string]bool)
|
|
|
|
for _, pubKey := range sKeys {
|
|
|
|
pkString := fmt.Sprintf("%#x", pubKey)
|
|
|
|
slashableKeys[pkString] = true
|
|
|
|
}
|
|
|
|
ok := slashableKeys[pubKey0]
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
ok = slashableKeys[pubKey1]
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
ok = slashableKeys[pubKey2]
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ImportInterchangeData_BadFormat_PreventsDBWrites(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
numValidators := 5
|
|
|
|
publicKeys, err := slashtest.CreateRandomPubKeys(numValidators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
validatorDB := dbtest.SetupDB(t, publicKeys)
|
|
|
|
|
|
|
|
// First we setup some mock attesting and proposal histories and create a mock
|
|
|
|
// standard slashing protection format JSON struct.
|
2021-07-01 21:50:56 +00:00
|
|
|
attestingHistory, proposalHistory := slashtest.MockAttestingAndProposalHistories(publicKeys)
|
2021-01-22 23:12:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
standardProtectionFormat, err := slashtest.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We replace a slot of one of the blocks with junk data.
|
|
|
|
standardProtectionFormat.Data[0].SignedBlocks[0].Slot = "BadSlot"
|
|
|
|
|
|
|
|
// We encode the standard slashing protection struct into a JSON format.
|
|
|
|
blob, err := json.Marshal(standardProtectionFormat)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := bytes.NewBuffer(blob)
|
|
|
|
|
|
|
|
// Next, we attempt to import it into our validator database and check that
|
|
|
|
// we obtain an error during the import process.
|
2021-11-09 16:49:28 +00:00
|
|
|
err = history.ImportStandardProtectionJSON(ctx, validatorDB, buf)
|
2021-01-22 23:12:22 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
// Next, we attempt to retrieve the attesting and proposals histories from our database and
|
|
|
|
// verify nothing was saved to the DB. If there is an error in the import process, we need to make
|
|
|
|
// sure writing is an atomic operation: either the import succeeds and saves the slashing protection
|
|
|
|
// data to our DB, or it does not.
|
|
|
|
for i := 0; i < len(publicKeys); i++ {
|
|
|
|
receivedAttestingHistory, err := validatorDB.AttestationHistoryForPubKey(ctx, publicKeys[i])
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(
|
|
|
|
t,
|
|
|
|
0,
|
|
|
|
len(receivedAttestingHistory),
|
|
|
|
"Imported attestation protection history is different than the empty default",
|
|
|
|
)
|
2021-02-02 15:53:12 +00:00
|
|
|
receivedHistory, err := validatorDB.ProposalHistoryForPubKey(ctx, publicKeys[i])
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(
|
|
|
|
t,
|
|
|
|
make([]*kv.Proposal, 0),
|
|
|
|
receivedHistory,
|
|
|
|
"Imported proposal signing root is different than the empty default",
|
|
|
|
)
|
2020-11-26 04:09:35 +00:00
|
|
|
}
|
|
|
|
}
|