Skip to content

Commit

Permalink
(Alan/no-fork) Make spec tests work again (#1480)
Browse files Browse the repository at this point in the history
* fixed compilation errors and runtime panics

* removed incorrect filtering

* removed panic

* added helpers to correclty get signers

* removed convert usage outside of db work

* fixed qbft

* added missing domain provider in fixRunner

* added TODO for filtering

* returned slashable validators. fixed ibft store tests

* fixed comments
  • Loading branch information
AKorpusenko authored Jul 18, 2024
1 parent b5699ea commit de5b645
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 48 deletions.
8 changes: 4 additions & 4 deletions ibft/storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func TestCleanInstances(t *testing.T) {
ks := testingutils.Testing4SharesSet()
logger := logging.TestLogger(t)
msgID := spectypes.NewMsgID(networkconfig.TestNetwork.Domain, []byte("pk"), spectypes.RoleCommittee)
msgID := spectypes.NewMsgID(networkconfig.TestNetwork.DomainType(), []byte("pk"), spectypes.RoleCommittee)
storage, err := newTestIbftStorage(logger, "test")
require.NoError(t, err)

Expand Down Expand Up @@ -57,7 +57,7 @@ func TestCleanInstances(t *testing.T) {
require.NoError(t, storage.SaveHighestInstance(generateInstance(msgID, specqbft.Height(msgsCount))))

// add different msgID
differMsgID := spectypes.NewMsgID(networkconfig.TestNetwork.Domain, []byte("differ_pk"), spectypes.RoleCommittee)
differMsgID := spectypes.NewMsgID(networkconfig.TestNetwork.DomainType(), []byte("differ_pk"), spectypes.RoleCommittee)
require.NoError(t, storage.SaveInstance(generateInstance(differMsgID, specqbft.Height(1))))
require.NoError(t, storage.SaveHighestInstance(generateInstance(differMsgID, specqbft.Height(msgsCount))))
require.NoError(t, storage.SaveHighestAndHistoricalInstance(generateInstance(differMsgID, specqbft.Height(1))))
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCleanInstances(t *testing.T) {
}

func TestSaveAndFetchLastState(t *testing.T) {
identifier := spectypes.NewMsgID(networkconfig.TestNetwork.Domain, []byte("pk"), spectypes.RoleCommittee)
identifier := spectypes.NewMsgID(networkconfig.TestNetwork.DomainType(), []byte("pk"), spectypes.RoleCommittee)

instance := &qbftstorage.StoredInstance{
State: &specqbft.State{
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestSaveAndFetchLastState(t *testing.T) {
}

func TestSaveAndFetchState(t *testing.T) {
identifier := spectypes.NewMsgID(networkconfig.TestNetwork.Domain, []byte("pk"), spectypes.RoleCommittee)
identifier := spectypes.NewMsgID(networkconfig.TestNetwork.DomainType(), []byte("pk"), spectypes.RoleCommittee)

instance := &qbftstorage.StoredInstance{
State: &specqbft.State{
Expand Down
20 changes: 10 additions & 10 deletions ibft/storage/stores_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

specqbft "github.com/ssvlabs/ssv-spec/qbft"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/ssvlabs/ssv/exporter/convert"
"github.com/ssvlabs/ssv/logging"
qbftstorage "github.com/ssvlabs/ssv/protocol/v2/qbft/storage"
"github.com/ssvlabs/ssv/storage/basedb"
Expand All @@ -19,28 +19,28 @@ func TestQBFTStores(t *testing.T) {

store, err := newTestIbftStorage(logger, "")
require.NoError(t, err)
qbftMap.Add(spectypes.RoleCommittee, store)
qbftMap.Add(spectypes.RoleCommittee, store)
qbftMap.Add(convert.RoleCommittee, store)
qbftMap.Add(convert.RoleCommittee, store)

require.NotNil(t, qbftMap.Get(spectypes.RoleCommittee))
require.NotNil(t, qbftMap.Get(spectypes.RoleCommittee))
require.NotNil(t, qbftMap.Get(convert.RoleCommittee))
require.NotNil(t, qbftMap.Get(convert.RoleCommittee))

db, err := kv.NewInMemory(logger.Named(logging.NameBadgerDBLog), basedb.Options{
Reporting: true,
})
require.NoError(t, err)
qbftMap = NewStoresFromRoles(db, spectypes.RoleCommittee, spectypes.RoleProposer)
qbftMap = NewStoresFromRoles(db, convert.RoleCommittee, convert.RoleProposer)

require.NotNil(t, qbftMap.Get(spectypes.RoleCommittee))
require.NotNil(t, qbftMap.Get(spectypes.RoleCommittee))
require.NotNil(t, qbftMap.Get(convert.RoleCommittee))
require.NotNil(t, qbftMap.Get(convert.RoleCommittee))

id := []byte{1, 2, 3}

qbftMap.Each(func(role spectypes.RunnerRole, store qbftstorage.QBFTStore) error {
qbftMap.Each(func(role convert.RunnerRole, store qbftstorage.QBFTStore) error {
return store.SaveInstance(&qbftstorage.StoredInstance{State: &specqbft.State{Height: 1, ID: id}})
})

instance, err := qbftMap.Get(spectypes.RoleCommittee).GetInstance(id, 1)
instance, err := qbftMap.Get(convert.RoleCommittee).GetInstance(id, 1)
require.NoError(t, err)
require.NotNil(t, instance)
require.Equal(t, specqbft.Height(1), instance.State.Height)
Expand Down
1 change: 1 addition & 0 deletions protocol/v2/qbft/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"

qbftstorage "github.com/ssvlabs/ssv/protocol/v2/qbft/storage"

"github.com/pkg/errors"
Expand Down
3 changes: 1 addition & 2 deletions protocol/v2/qbft/spectest/controller_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ssvlabs/ssv/exporter/convert"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -59,7 +58,7 @@ func RunControllerSpecTest(t *testing.T, test *spectests.ControllerSpecTest) {

func generateController(logger *zap.Logger) *controller.Controller {
identifier := []byte{1, 2, 3, 4}
config := qbfttesting.TestingConfig(logger, spectestingutils.Testing4SharesSet(), convert.RoleCommittee)
config := qbfttesting.TestingConfig(logger, spectestingutils.Testing4SharesSet(), spectypes.RoleCommittee)
return qbfttesting.NewTestingQBFTController(
identifier[:],
spectestingutils.TestingCommitteeMember(spectestingutils.Testing4SharesSet()),
Expand Down
8 changes: 5 additions & 3 deletions protocol/v2/qbft/testing/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package testing

import (
"context"
"github.com/ssvlabs/ssv/exporter/convert"
"sync"

"github.com/ssvlabs/ssv/exporter/convert"
qbftstorage "github.com/ssvlabs/ssv/ibft/storage"
"github.com/ssvlabs/ssv/storage/basedb"
"github.com/ssvlabs/ssv/storage/kv"
Expand All @@ -28,12 +28,14 @@ func getDB(logger *zap.Logger) basedb.Database {
}

var allRoles = []convert.RunnerRole{
convert.RoleCommittee,
convert.RoleProposer,
convert.RoleAttester,
convert.RoleAggregator,
convert.RoleProposer,
convert.RoleSyncCommitteeContribution,
convert.RoleSyncCommittee,
convert.RoleValidatorRegistration,
convert.RoleVoluntaryExit,
convert.RoleCommittee,
}

func TestingStores(logger *zap.Logger) *qbftstorage.QBFTStores {
Expand Down
5 changes: 3 additions & 2 deletions protocol/v2/qbft/testing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testing

import (
"bytes"

"github.com/pkg/errors"
"github.com/ssvlabs/ssv/exporter/convert"
"github.com/ssvlabs/ssv/protocol/v2/qbft/roundtimer"
Expand All @@ -14,7 +15,7 @@ import (
"github.com/ssvlabs/ssv/protocol/v2/qbft/controller"
)

var TestingConfig = func(logger *zap.Logger, keySet *testingutils.TestKeySet, role convert.RunnerRole) *qbft.Config {
var TestingConfig = func(logger *zap.Logger, keySet *testingutils.TestKeySet, role types.RunnerRole) *qbft.Config {
return &qbft.Config{
BeaconSigner: testingutils.NewTestingKeyManager(),
OperatorSigner: testingutils.NewTestingOperatorSigner(keySet, 1),
Expand All @@ -34,7 +35,7 @@ var TestingConfig = func(logger *zap.Logger, keySet *testingutils.TestKeySet, ro
ProposerF: func(state *specqbft.State, round specqbft.Round) types.OperatorID {
return 1
},
Storage: TestingStores(logger).Get(role),
Storage: TestingStores(logger).Get(convert.RunnerRole(role)),
Network: testingutils.NewTestingNetwork(1, keySet.OperatorKeys[1]),
Timer: roundtimer.NewTestingTimer(),
SignatureVerification: true,
Expand Down
7 changes: 4 additions & 3 deletions protocol/v2/ssv/runner/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"time"

"github.com/ssvlabs/ssv/protocol/v2/ssv/runner/metrics"

"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
Expand All @@ -20,7 +22,6 @@ import (
"github.com/ssvlabs/ssv/networkconfig"
"github.com/ssvlabs/ssv/protocol/v2/blockchain/beacon"
"github.com/ssvlabs/ssv/protocol/v2/qbft/controller"
"github.com/ssvlabs/ssv/protocol/v2/ssv/runner/metrics"
)

//type Broadcaster interface {
Expand Down Expand Up @@ -387,8 +388,8 @@ func (cr *CommitteeRunner) ProcessPostConsensus(logger *zap.Logger, signedMsg *t
}
specSig := phase0.BLSSignature{}
copy(specSig[:], sig)
vlogger.Debug("🧩 reconstructed partial signatures",
zap.Uint64s("signers", getPostConsensusSigners(cr.BaseRunner.State, root)))
vlogger.Debug("🧩 reconstructed partial signatures committee",
zap.Uint64s("signers", getPostConsensusCommitteeSigners(cr.BaseRunner.State, root)))
// Get the beacon object related to root
if _, exists := beaconObjects[validator]; !exists {
anyErr = errors.Wrap(err, "could not find beacon object for validator")
Expand Down
4 changes: 2 additions & 2 deletions protocol/v2/ssv/runner/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func (r *ProposerRunner) ProcessPostConsensus(logger *zap.Logger, signedMsg *spe
specSig := phase0.BLSSignature{}
copy(specSig[:], sig)
r.metrics.EndPostConsensus()
logger.Debug("🧩 reconstructed partial post consensus signatures",
zap.Uint64s("signers", getPostConsensusSigners(r.GetState(), root)),
logger.Debug("🧩 reconstructed partial post consensus signatures proposer",
zap.Uint64s("signers", getPostConsensusProposerSigners(r.GetState(), root)),
fields.PostConsensusTime(r.metrics.GetPostConsensusTime()))
endSubmission := r.metrics.StartBeaconSubmission()

Expand Down
28 changes: 26 additions & 2 deletions protocol/v2/ssv/runner/runner_state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,35 @@ func getPreConsensusSigners(state *State, root [32]byte) []spectypes.OperatorID
return signers
}

func getPostConsensusSigners(state *State, root [32]byte) []spectypes.OperatorID {
sigs := state.PostConsensusContainer.Signatures[state.StartingDuty.(*spectypes.BeaconDuty).ValidatorIndex][hex.EncodeToString(root[:])]
func getPostConsensusCommitteeSigners(state *State, root [32]byte) []spectypes.OperatorID {
var signers []spectypes.OperatorID

for _, bd := range state.StartingDuty.(*spectypes.CommitteeDuty).BeaconDuties {
sigs := state.PostConsensusContainer.Signatures[bd.ValidatorIndex][hex.EncodeToString(root[:])]
for op := range sigs {
signers = append(signers, op)
}
}

have := make(map[spectypes.OperatorID]struct{})
var signersUnique []spectypes.OperatorID
for _, opId := range signers {
if _, ok := have[opId]; !ok {
have[opId] = struct{}{}
signersUnique = append(signersUnique, opId)
}
}

return signersUnique
}

func getPostConsensusProposerSigners(state *State, root [32]byte) []spectypes.OperatorID {
var signers []spectypes.OperatorID
valIdx := state.StartingDuty.(*spectypes.BeaconDuty).ValidatorIndex
sigs := state.PostConsensusContainer.Signatures[valIdx][hex.EncodeToString(root[:])]
for op := range sigs {
signers = append(signers, op)
}

return signers
}
2 changes: 1 addition & 1 deletion protocol/v2/ssv/spectest/msg_processing_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ var baseCommitteeWithRunnerSample = func(
shareMap[valIdx] = spectestingutils.TestingShare(ks, valIdx)
}

createRunnerF := func(_ phase0.Slot, shareMap map[phase0.ValidatorIndex]*spectypes.Share) *runner.CommitteeRunner {
createRunnerF := func(_ phase0.Slot, shareMap map[phase0.ValidatorIndex]*spectypes.Share, slashableValidators []spectypes.ShareValidatorPK) *runner.CommitteeRunner {
return runner.NewCommitteeRunner(
networkconfig.TestNetwork,
shareMap,
Expand Down
10 changes: 7 additions & 3 deletions protocol/v2/ssv/spectest/ssv_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"github.com/ssvlabs/ssv-spec/types/spectest/tests/partialsigmessage"
"github.com/ssvlabs/ssv-spec/types/testingutils"
spectestingutils "github.com/ssvlabs/ssv-spec/types/testingutils"
"github.com/ssvlabs/ssv/networkconfig"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/exporter/convert"
tests2 "github.com/ssvlabs/ssv/integration/qbft/tests"
"github.com/ssvlabs/ssv/logging"
"github.com/ssvlabs/ssv/protocol/v2/qbft/controller"
Expand Down Expand Up @@ -386,11 +386,15 @@ func fixRunnerForRun(t *testing.T, runnerMap map[string]interface{}, ks *spectes
}
}

if ret.GetBaseRunner().DomainTypeProvider == nil {
ret.GetBaseRunner().DomainTypeProvider = networkconfig.TestNetwork
}

return ret
}

func fixControllerForRun(t *testing.T, logger *zap.Logger, runner runner.Runner, contr *controller.Controller, ks *spectestingutils.TestKeySet) *controller.Controller {
config := qbfttesting.TestingConfig(logger, ks, convert.RoleCommittee)
config := qbfttesting.TestingConfig(logger, ks, spectypes.RoleCommittee)
config.ValueCheckF = runner.GetValCheckF()
newContr := controller.NewController(
contr.Identifier,
Expand Down Expand Up @@ -551,7 +555,7 @@ func fixCommitteeForRun(t *testing.T, ctx context.Context, logger *zap.Logger, c
tests2.NewTestingBeaconNodeWrapped().GetBeaconNetwork(),
&specCommittee.CommitteeMember,
testingutils.NewTestingVerifier(),
func(slot phase0.Slot, shareMap map[phase0.ValidatorIndex]*spectypes.Share) *runner.CommitteeRunner {
func(slot phase0.Slot, shareMap map[phase0.ValidatorIndex]*spectypes.Share, slashableValidators []spectypes.ShareValidatorPK) *runner.CommitteeRunner {
return ssvtesting.CommitteeRunnerWithShareMap(logger, shareMap).(*runner.CommitteeRunner)
},
)
Expand Down
15 changes: 12 additions & 3 deletions protocol/v2/ssv/testing/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ssvlabs/ssv/integration/qbft/tests"
"github.com/ssvlabs/ssv/networkconfig"
"github.com/ssvlabs/ssv/operator/validator"
"github.com/ssvlabs/ssv/protocol/v2/qbft/testing"
"github.com/ssvlabs/ssv/protocol/v2/ssv/runner"
"go.uber.org/zap"
Expand All @@ -22,13 +21,13 @@ var TestingHighestDecidedSlot = phase0.Slot(0)
var CommitteeRunner = func(logger *zap.Logger, keySet *spectestingutils.TestKeySet) runner.Runner {
// TODO fixme ?

return baseRunner(logger, spectypes.RoleCommittee, validator.TempBeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), keySet)
return baseRunner(logger, spectypes.RoleCommittee, specssv.BeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), keySet)
//return baseRunner(logger, spectypes.RoleCommittee, specssv.BeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), keySet)
}

var CommitteeRunnerWithShareMap = func(logger *zap.Logger, shareMap map[phase0.ValidatorIndex]*spectypes.Share) runner.Runner {
// TODO fixme ?
return baseRunnerWithShareMap(logger, spectypes.RoleCommittee, validator.TempBeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), shareMap)
return baseRunnerWithShareMap(logger, spectypes.RoleCommittee, specssv.BeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), shareMap)
//return baseRunnerWithShareMap(logger, spectypes.RoleCommittee, specssv.BeaconVoteValueCheckF(spectestingutils.NewTestingKeyManager(), spectestingutils.TestingDutySlot, nil, spectestingutils.TestingDutyEpoch), shareMap)
}

Expand Down Expand Up @@ -118,6 +117,7 @@ var baseRunner = func(
).(runner.Runner)
case spectypes.RoleAggregator:
return runner.NewAggregatorRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -130,6 +130,7 @@ var baseRunner = func(
).(runner.Runner)
case spectypes.RoleProposer:
return runner.NewProposerRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -142,6 +143,7 @@ var baseRunner = func(
).(runner.Runner)
case spectypes.RoleSyncCommitteeContribution:
return runner.NewSyncCommitteeAggregatorRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -154,6 +156,7 @@ var baseRunner = func(
).(runner.Runner)
case spectypes.RoleValidatorRegistration:
return runner.NewValidatorRegistrationRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -164,6 +167,7 @@ var baseRunner = func(
).(runner.Runner)
case spectypes.RoleVoluntaryExit:
return runner.NewVoluntaryExitRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
tests.NewTestingBeaconNodeWrapped(),
Expand Down Expand Up @@ -318,6 +322,7 @@ var baseRunnerWithShareMap = func(
)
case spectypes.RoleAggregator:
return runner.NewAggregatorRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -330,6 +335,7 @@ var baseRunnerWithShareMap = func(
)
case spectypes.RoleProposer:
return runner.NewProposerRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -342,6 +348,7 @@ var baseRunnerWithShareMap = func(
)
case spectypes.RoleSyncCommitteeContribution:
return runner.NewSyncCommitteeAggregatorRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -354,6 +361,7 @@ var baseRunnerWithShareMap = func(
)
case spectypes.RoleValidatorRegistration:
return runner.NewValidatorRegistrationRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
contr,
Expand All @@ -364,6 +372,7 @@ var baseRunnerWithShareMap = func(
)
case spectypes.RoleVoluntaryExit:
return runner.NewVoluntaryExitRunner(
networkconfig.TestNetwork,
spectypes.BeaconTestNetwork,
shareMap,
tests.NewTestingBeaconNodeWrapped(),
Expand Down
Loading

0 comments on commit de5b645

Please sign in to comment.