Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Alan/no-fork) Make spec tests work again #1480

Merged
merged 11 commits into from
Jul 18, 2024
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
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 @@ -4,9 +4,10 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/ssvlabs/ssv/protocol/v2/ssv/runner/metrics"
"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 Down Expand Up @@ -386,8 +387,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 @@ -231,8 +231,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
}
3 changes: 1 addition & 2 deletions protocol/v2/ssv/spectest/ssv_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"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 @@ -390,7 +389,7 @@ func fixRunnerForRun(t *testing.T, runnerMap map[string]interface{}, ks *spectes
}

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
53 changes: 28 additions & 25 deletions protocol/v2/ssv/validator/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,35 @@ func (c *Committee) StartDuty(logger *zap.Logger, duty *spectypes.CommitteeDuty)
return errors.New(fmt.Sprintf("CommitteeRunner for slot %d already exists", duty.Slot))
}

validatorShares := make(map[phase0.ValidatorIndex]*spectypes.Share, len(duty.BeaconDuties))
toRemove := make([]int, 0)
// Remove beacon duties that don't have a share
for i, bd := range duty.BeaconDuties {
share, ok := c.Shares[bd.ValidatorIndex]
if !ok {
toRemove = append(toRemove, i)
continue
}
validatorShares[bd.ValidatorIndex] = share
}
// Remove beacon duties that don't have a share
if len(toRemove) > 0 {
newDuties, err := removeIndices(duty.BeaconDuties, toRemove)
if err != nil {
logger.Warn("could not remove beacon duties", zap.Error(err), zap.Ints("indices", toRemove))
} else {
duty.BeaconDuties = newDuties
}
//validatorShares := make(map[phase0.ValidatorIndex]*spectypes.Share, len(duty.BeaconDuties))
//toRemove := make([]int, 0)
//// Remove beacon duties that don't have a share
//for i, bd := range duty.BeaconDuties {
// share, ok := c.Shares[bd.ValidatorIndex]
// if !ok {
// toRemove = append(toRemove, i)
// continue
// }
// validatorShares[bd.ValidatorIndex] = share
//}
//// Remove beacon duties that don't have a share
//if len(toRemove) > 0 {
// newDuties, err := removeIndices(duty.BeaconDuties, toRemove)
// if err != nil {
// logger.Warn("could not remove beacon duties", zap.Error(err), zap.Ints("indices", toRemove))
// } else {
// duty.BeaconDuties = newDuties
// }
//}
//
//if len(duty.BeaconDuties) == 0 {
// return errors.New("CommitteeDuty has no valid beacon duties")
//}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to filter the shares and execute the committeeRunner only with the relevant shares and duties (these that have a duty in this slot)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GalRogozinski, shouldn't we do changes in spec tests then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AKorpusenko what test is failing because of that?

Copy link
Contributor

@y0sher y0sher Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will work once ssvlabs/ssv-spec#467 is merged and code is aligned. please add a todo to revert it once its aligned and done.

var sharesCopy = make(map[phase0.ValidatorIndex]*spectypes.Share, len(c.Shares))
for k, v := range c.Shares {
sharesCopy[k] = v
}

if len(duty.BeaconDuties) == 0 {
return errors.New("CommitteeDuty has no valid beacon duties")
}

r := c.CreateRunnerFn(duty.Slot, validatorShares)
r := c.CreateRunnerFn(duty.Slot, sharesCopy)
// Set timeout function.
r.GetBaseRunner().TimeoutF = c.onTimeout
c.Runners[duty.Slot] = r
Expand Down
Loading