Skip to content

Commit

Permalink
fix lint left overs issues
Browse files Browse the repository at this point in the history
  • Loading branch information
guy muroch committed Jul 17, 2024
1 parent 9f90847 commit af32235
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 98 deletions.
14 changes: 1 addition & 13 deletions eth/eventhandler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,11 @@ func (eh *EventHandler) validatorAddedEventToShare(
selfOperatorID := eh.operatorDataStore.GetOperatorID()
var shareSecret *bls.SecretKey

operators := make([]*spectypes.Operator, 0)
committee := make([]*spectypes.CommitteeMember, 0)
shareMembers := make([]*spectypes.ShareMember, 0)

for i := range event.OperatorIds {
operatorID := event.OperatorIds[i]
od, found, err := eh.nodeStorage.GetOperatorData(txn, operatorID)
_, found, err := eh.nodeStorage.GetOperatorData(txn, operatorID)
if err != nil {
return nil, nil, fmt.Errorf("could not get operator data: %w", err)
}
Expand All @@ -294,11 +292,6 @@ func (eh *EventHandler) validatorAddedEventToShare(
}
}

committee = append(committee, &spectypes.CommitteeMember{
OperatorID: operatorID,
SSVOperatorPubKey: od.PublicKey,
})

shareMembers = append(shareMembers, &spectypes.ShareMember{
Signer: operatorID,
SharePubKey: sharePublicKeys[i],
Expand Down Expand Up @@ -328,11 +321,6 @@ func (eh *EventHandler) validatorAddedEventToShare(
Err: errors.New("share private key does not match public key"),
}
}

operators = append(operators, &spectypes.Operator{
OperatorID: operatorID,
SSVOperatorPubKey: od.PublicKey,
})
}

validatorShare.DomainType = eh.networkConfig.Domain
Expand Down
2 changes: 1 addition & 1 deletion integration/qbft/tests/temp_testing_beacon_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TestingBeaconNodeWrapped struct {
}

func (bn *TestingBeaconNodeWrapped) SetSyncCommitteeAggregatorRootHexes(roots map[string]bool) {
bn.SetSyncCommitteeAggregatorRootHexes(roots)
bn.Bn.SetSyncCommitteeAggregatorRootHexes(roots)
}

func (bn *TestingBeaconNodeWrapped) GetBroadcastedRoots() []phase0.Root {
Expand Down
4 changes: 0 additions & 4 deletions message/validation/consensus_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ func (mv *messageValidator) processSignerState(signedSSVMessage *spectypes.Signe
return nil
}

func (mv *messageValidator) maxSlotsInState() phase0.Slot {
return phase0.Slot(mv.netCfg.SlotsPerEpoch()) + lateSlotAllowance
}

func (mv *messageValidator) validateJustifications(message *specqbft.Message) error {
pj, err := message.GetPrepareJustifications()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion network/topics/params/message_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func expectedSingleSCCommitteeDutiesPerEpochCached(numValidators int) float64 {

// Calculates the message rate for a topic given its committees' configurations (number of operators and number of validators)
func calculateMessageRateForTopic(committees []*storage.Committee) float64 {
if committees == nil || len(committees) == 0 {
if len(committees) == 0 {
return 0
}

Expand Down
9 changes: 2 additions & 7 deletions operator/validator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ type controller struct {
committeesObserversMutex sync.Mutex

recentlyStartedValidators uint64
recentlyStartedCommittees uint64
metadataLastUpdated map[spectypes.ValidatorPK]time.Time
indicesChange chan struct{}
validatorExitCh chan duties.ExitDescriptor
Expand Down Expand Up @@ -889,11 +888,6 @@ func (c *controller) onShareStop(pubKey spectypes.ValidatorPK) {
}
}

// todo wrapper to start both validator and committee
type starter interface {
Start() error
}

func (c *controller) onShareInit(share *ssvtypes.SSVShare) (*validator.Validator, *validator.Committee, error) {
if !share.HasBeaconMetadata() { // fetching index and status in case not exist
c.logger.Warn("skipping validator until it becomes active", fields.PubKey(share.ValidatorPubKey[:]))
Expand Down Expand Up @@ -933,7 +927,8 @@ func (c *controller) onShareInit(share *ssvtypes.SSVShare) (*validator.Validator
if !found {
// Share context with both the validator and the runners,
// so that when the validator is stopped, the runners are stopped as well.
ctx, _ := context.WithCancel(c.context)
ctx, cancel := context.WithCancel(c.context)
_ = cancel

opts := c.validatorOptions
opts.SSVShare = share
Expand Down
11 changes: 0 additions & 11 deletions protocol/v2/qbft/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"sync"
"time"

"github.com/pkg/errors"
spectypes "github.com/ssvlabs/ssv-spec/types"
Expand All @@ -27,7 +26,6 @@ type Instance struct {
forceStop bool
StartValue []byte

started time.Time
metrics *metrics
}

Expand Down Expand Up @@ -62,15 +60,6 @@ func NewInstance(
}
}

// TODO remove
func messageIDFromBytes(mid []byte) spectypes.MessageID {
if len(mid) < 56 {
return spectypes.MessageID{}
}

return spectypes.MessageID(mid)
}

func (i *Instance) ForceStop() {
i.forceStop = true
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/v2/ssv/spectest/debug_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func dumpState(t *testing.T,
func logJSON(t *testing.T, name string, value interface{}) {
bytes, err := json.Marshal(value)
require.NoError(t, err)
err = os.WriteFile(fmt.Sprintf("%s/%s_test_serialized.json", dumpDir, name), bytes, 0644)
err = os.WriteFile(fmt.Sprintf("%s/%s_test_serialized.json", dumpDir, name), bytes, 0600)
require.NoError(t, err)
}
21 changes: 3 additions & 18 deletions protocol/v2/ssv/spectest/msg_processing_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (test *MsgProcessingSpecTest) RunAsPartOfMultiTest(t *testing.T, logger *za
}

network := &spectestingutils.TestingNetwork{}
beaconNetwork := tests.NewTestingBeaconNodeWrapped()
var beaconNetwork *tests.TestingBeaconNodeWrapped
var committee []*spectypes.Operator

switch test.Runner.(type) {
Expand All @@ -150,14 +150,14 @@ func (test *MsgProcessingSpecTest) RunAsPartOfMultiTest(t *testing.T, logger *za
default:
network = v.Network.(*spectestingutils.TestingNetwork)
committee = v.Operator.Committee
beaconNetwork = test.Runner.GetBeaconNode()
beaconNetwork = test.Runner.GetBeaconNode().(*tests.TestingBeaconNodeWrapped)
}

// test output message
spectestingutils.ComparePartialSignatureOutputMessages(t, test.OutputMessages, network.BroadcastedMsgs, committee)

// test beacon broadcasted msgs
spectestingutils.CompareBroadcastedBeaconMsgs(t, test.BeaconBroadcastedRoots, beaconNetwork.(*tests.TestingBeaconNodeWrapped).GetBroadcastedRoots())
spectestingutils.CompareBroadcastedBeaconMsgs(t, test.BeaconBroadcastedRoots, beaconNetwork.GetBroadcastedRoots())

// post root
postRoot, err := test.Runner.GetRoot()
Expand All @@ -169,21 +169,6 @@ func (test *MsgProcessingSpecTest) RunAsPartOfMultiTest(t *testing.T, logger *za
}
}

func (test *MsgProcessingSpecTest) compareBroadcastedBeaconMsgs(t *testing.T) {
broadcastedRoots := test.Runner.GetBeaconNode().(*tests.TestingBeaconNodeWrapped).GetBroadcastedRoots()
require.Len(t, broadcastedRoots, len(test.BeaconBroadcastedRoots))
for _, r1 := range test.BeaconBroadcastedRoots {
found := false
for _, r2 := range broadcastedRoots {
if r1 == hex.EncodeToString(r2[:]) {
found = true
break
}
}
require.Truef(t, found, "broadcasted beacon root not found")
}
}

func (test *MsgProcessingSpecTest) overrideStateComparison(t *testing.T) {
testType := reflect.TypeOf(test).String()
testType = strings.Replace(testType, "spectest.", "tests.", 1)
Expand Down
14 changes: 7 additions & 7 deletions protocol/v2/ssv/testing/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var baseRunner = func(
km,
opSigner,
valCheck,
).(runner.Runner)
)
case spectypes.RoleAggregator:
return runner.NewAggregatorRunner(
spectypes.BeaconTestNetwork,
Expand All @@ -128,7 +128,7 @@ var baseRunner = func(
opSigner,
valCheck,
TestingHighestDecidedSlot,
).(runner.Runner)
)
case spectypes.RoleProposer:
return runner.NewProposerRunner(
spectypes.BeaconTestNetwork,
Expand All @@ -140,7 +140,7 @@ var baseRunner = func(
opSigner,
valCheck,
TestingHighestDecidedSlot,
).(runner.Runner)
)
case spectypes.RoleSyncCommitteeContribution:
return runner.NewSyncCommitteeAggregatorRunner(
spectypes.BeaconTestNetwork,
Expand All @@ -152,7 +152,7 @@ var baseRunner = func(
opSigner,
valCheck,
TestingHighestDecidedSlot,
).(runner.Runner)
)
case spectypes.RoleValidatorRegistration:
return runner.NewValidatorRegistrationRunner(
spectypes.BeaconTestNetwork,
Expand All @@ -162,7 +162,7 @@ var baseRunner = func(
net,
km,
opSigner,
).(runner.Runner)
)
case spectypes.RoleVoluntaryExit:
return runner.NewVoluntaryExitRunner(
spectypes.BeaconTestNetwork,
Expand All @@ -171,7 +171,7 @@ var baseRunner = func(
net,
km,
opSigner,
).(runner.Runner)
)
case spectestingutils.UnknownDutyType:
ret := runner.NewCommitteeRunner(
networkconfig.TestNetwork,
Expand All @@ -184,7 +184,7 @@ var baseRunner = func(
valCheck,
)
ret.(*runner.CommitteeRunner).BaseRunner.RunnerRoleType = spectestingutils.UnknownDutyType
return ret.(runner.Runner)
return ret
default:
panic("unknown role type")
}
Expand Down
22 changes: 6 additions & 16 deletions protocol/v2/ssv/validator/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ func (c *Committee) StartDuty(logger *zap.Logger, duty *spectypes.CommitteeDuty)

logger = c.logger.With(fields.DutyID(fields.FormatCommitteeDutyID(c.Operator.Committee, c.BeaconNetwork.EstimatedEpochAtSlot(duty.Slot), duty.Slot)), fields.Slot(duty.Slot))
// TODO alan: stop queue
go c.ConsumeQueue(logger, duty.Slot, c.ProcessMessage, c.Runners[duty.Slot])
go func() {
err := c.ConsumeQueue(logger, duty.Slot, c.ProcessMessage, c.Runners[duty.Slot])
if err != nil {
logger.Warn("handles error message", zap.Error(err))
}
}()

logger.Info("ℹ️ starting duty processing")
return c.Runners[duty.Slot].StartNewDuty(logger, duty, c.Operator.GetQuorum())
Expand Down Expand Up @@ -316,21 +321,6 @@ func (c *Committee) UnmarshalJSON(data []byte) error {
return nil
}

// updateAttestingSlotMap updates the highest attesting slot map from beacon duties
func (c *Committee) updateAttestingSlotMap(duty *spectypes.CommitteeDuty) {
for _, beaconDuty := range duty.BeaconDuties {
if beaconDuty.Type == spectypes.BNRoleAttester {
validatorPK := spectypes.ValidatorPK(beaconDuty.PubKey)
if _, ok := c.HighestAttestingSlotMap[validatorPK]; !ok {
c.HighestAttestingSlotMap[validatorPK] = beaconDuty.Slot
}
if c.HighestAttestingSlotMap[validatorPK] < beaconDuty.Slot {
c.HighestAttestingSlotMap[validatorPK] = beaconDuty.Slot
}
}
}
}

func (c *Committee) validateMessage(msg *spectypes.SSVMessage) error {
if !(c.Operator.CommitteeID.MessageIDBelongs(msg.GetID())) {
return errors.New("msg ID doesn't match committee ID")
Expand Down
2 changes: 1 addition & 1 deletion protocol/v2/ssv/validator/non_committee_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (ncv *CommitteeObserver) ProcessMessage(msg *queue.DecodedSSVMessage) error
return fmt.Errorf("failed to get partial signature message from network message %w", err)
}
if partialSigMessages.Type != spectypes.PostConsensusPartialSig {
return fmt.Errorf("not processing message type %s", partialSigMessages.Type)
return fmt.Errorf("not processing message type %d", partialSigMessages.Type)
}

slot := partialSigMessages.Slot
Expand Down
46 changes: 28 additions & 18 deletions protocol/v2/testing/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,27 @@ func GetSSVMappingSpecTestJSON(path string, module string) ([]byte, error) {
gzPath := filepath.Join(p, "spectest", "generate", "tests.json.gz")
untypedTests := map[string]interface{}{}

file, err := os.Open(gzPath)
file, err := os.Open(gzPath) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "failed to open gzip file")
}
defer file.Close()
defer func() {
if err := file.Close(); err != nil {
// Handle the error, log it, or handle it as appropriate
log.Printf("Failed to close file: %v", err)
}
}()

gzipReader, err := gzip.NewReader(file)
if err != nil {
return nil, errors.Wrap(err, "failed to create gzip reader")
}
defer gzipReader.Close()
defer func() {
if err := gzipReader.Close(); err != nil {
// Handle the error, log it, or handle it as appropriate
log.Printf("Failed to close reader: %v", err)
}
}()

decompressedData, err := io.ReadAll(gzipReader)
if err != nil {
Expand Down Expand Up @@ -253,7 +263,7 @@ func ExtractTarGz(gzipStream io.Reader) {

tarReader := tar.NewReader(uncompressedStream)

for true {
for {
header, err := tarReader.Next()

if err == io.EOF {
Expand All @@ -266,35 +276,35 @@ func ExtractTarGz(gzipStream io.Reader) {

switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(header.Name, 0755); err != nil {
if err := os.Mkdir(header.Name, 0750); err != nil {
log.Fatalf("ExtractTarGz: Mkdir() failed: %s", err.Error())
}
case tar.TypeReg:
outFile, err := os.Create(header.Name)
if err != nil {
log.Fatalf("ExtractTarGz: Create() failed: %s", err.Error())
}
if _, err := io.Copy(outFile, tarReader); err != nil {
// Set a maximum size limit for the decompressed data
maxSize := int64(50 * 1024 * 1024) // 50 MB, adjust as needed

// Wrap the tarReader with a LimitedReader
limitedReader := &io.LimitedReader{R: tarReader, N: maxSize}

// Perform the copy operation with the limited reader
if _, err := io.Copy(outFile, limitedReader); err != nil {
log.Fatalf("ExtractTarGz: Copy() failed: %s", err.Error())
}
outFile.Close()
err = outFile.Close()
if err != nil {
log.Fatalf("faild to close file: %s", err.Error())
}

default:
log.Fatalf(
"ExtractTarGz: uknown type: %s in %s",
"ExtractTarGz: uknown type: %d in %s",
header.Typeflag,
header.Name)
}

}
}

func unpackTestsJson(path string) error {
r, err := os.Open(fmt.Sprintf("%s.gz", path))
if err != nil {
errors.Wrap(err, "could not open file")
}
ExtractTarGz(r)

return nil
}

0 comments on commit af32235

Please sign in to comment.