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

fix(CommitteeObserver): post consensus mem leak #1859

Open
wants to merge 4 commits into
base: stage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions message/validation/common_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (mv *messageValidator) messageLateness(slot phase0.Slot, role spectypes.Run
var ttl phase0.Slot
switch role {
case spectypes.RoleProposer, spectypes.RoleSyncCommitteeContribution:
ttl = 1 + lateSlotAllowance
ttl = 1 + LateSlotAllowance
case spectypes.RoleCommittee, spectypes.RoleAggregator:
ttl = phase0.Slot(mv.netCfg.Beacon.SlotsPerEpoch()) + lateSlotAllowance
ttl = phase0.Slot(mv.netCfg.Beacon.SlotsPerEpoch()) + LateSlotAllowance
case spectypes.RoleValidatorRegistration, spectypes.RoleVoluntaryExit:
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion message/validation/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
clockErrorTolerance = time.Millisecond * 50
allowedRoundsInFuture = 1
allowedRoundsInPast = 2
lateSlotAllowance = 2
LateSlotAllowance = 2
syncCommitteeSize = 512
rsaSignatureSize = 256
operatorIDSize = 8 // uint64
Expand Down
80 changes: 55 additions & 25 deletions protocol/v2/ssv/validator/non_committee_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/herumi/bls-eth-go-binary/bls"
"go.uber.org/zap"

"github.com/jellydator/ttlcache/v3"
specqbft "github.com/ssvlabs/ssv-spec/qbft"
spectypes "github.com/ssvlabs/ssv-spec/types"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/exporter/convert"
"github.com/ssvlabs/ssv/ibft/storage"
"github.com/ssvlabs/ssv/logging/fields"
msgvalidation "github.com/ssvlabs/ssv/message/validation"
"github.com/ssvlabs/ssv/networkconfig"
"github.com/ssvlabs/ssv/protocol/v2/blockchain/beacon"
"github.com/ssvlabs/ssv/protocol/v2/qbft"
Expand All @@ -32,16 +33,18 @@ import (
)

type CommitteeObserver struct {
logger *zap.Logger
Storage *storage.QBFTStores
beaconNetwork beacon.BeaconNetwork
qbftController *qbftcontroller.Controller
ValidatorStore registrystorage.ValidatorStore
newDecidedHandler qbftcontroller.NewDecidedHandler
attesterRoots *ttlcache.Cache[phase0.Root, struct{}]
syncCommRoots *ttlcache.Cache[phase0.Root, struct{}]
domainCache *DomainCache
postConsensusContainer map[phase0.ValidatorIndex]*ssv.PartialSigContainer
logger *zap.Logger
netCfg networkconfig.NetworkConfig
Storage *storage.QBFTStores
beaconNetwork beacon.BeaconNetwork
qbftController *qbftcontroller.Controller
ValidatorStore registrystorage.ValidatorStore
newDecidedHandler qbftcontroller.NewDecidedHandler
attesterRoots *ttlcache.Cache[phase0.Root, struct{}]
syncCommRoots *ttlcache.Cache[phase0.Root, struct{}]
domainCache *DomainCache
// TODO: consider using round-robin container as []map[phase0.ValidatorIndex]*ssv.PartialSigContainer similar to what is used in OperatorState
postConsensusContainer map[phase0.Slot]map[phase0.ValidatorIndex]*ssv.PartialSigContainer
}

type CommitteeObserverOptions struct {
Expand Down Expand Up @@ -72,18 +75,21 @@ func NewCommitteeObserver(identifier convert.MessageID, opts CommitteeObserverOp
ctrl := qbftcontroller.NewController(identifier[:], opts.Operator, config, opts.OperatorSigner, opts.FullNode)
ctrl.StoredInstances = make(qbftcontroller.InstanceContainer, 0, nonCommitteeInstanceContainerCapacity(opts.FullNode))

return &CommitteeObserver{
qbftController: ctrl,
logger: opts.Logger,
Storage: opts.Storage,
beaconNetwork: opts.NetworkConfig.Beacon,
ValidatorStore: opts.ValidatorStore,
newDecidedHandler: opts.NewDecidedHandler,
attesterRoots: opts.AttesterRoots,
syncCommRoots: opts.SyncCommRoots,
domainCache: opts.DomainCache,
postConsensusContainer: make(map[phase0.ValidatorIndex]*ssv.PartialSigContainer),
co := &CommitteeObserver{
qbftController: ctrl,
logger: opts.Logger,
netCfg: opts.NetworkConfig,
Storage: opts.Storage,
beaconNetwork: opts.NetworkConfig.Beacon,
ValidatorStore: opts.ValidatorStore,
newDecidedHandler: opts.NewDecidedHandler,
attesterRoots: opts.AttesterRoots,
syncCommRoots: opts.SyncCommRoots,
domainCache: opts.DomainCache,
}
co.postConsensusContainer = make(map[phase0.Slot]map[phase0.ValidatorIndex]*ssv.PartialSigContainer, co.postConsensusContainerCapacity())

return co
}

func (ncv *CommitteeObserver) ProcessMessage(msg *queue.SSVMessage) error {
Expand Down Expand Up @@ -220,15 +226,22 @@ func (ncv *CommitteeObserver) processMessage(
) (map[validatorIndexAndRoot][]spectypes.OperatorID, error) {
quorums := make(map[validatorIndexAndRoot][]spectypes.OperatorID)

currentSlot := signedMsg.Slot
slotValidators, exist := ncv.postConsensusContainer[currentSlot]
if !exist {
slotValidators = make(map[phase0.ValidatorIndex]*ssv.PartialSigContainer)
ncv.postConsensusContainer[signedMsg.Slot] = slotValidators
}

for _, msg := range signedMsg.Messages {
validator, exists := ncv.ValidatorStore.ValidatorByIndex(msg.ValidatorIndex)
if !exists {
return nil, fmt.Errorf("could not find share for validator with index %d", msg.ValidatorIndex)
}
container, ok := ncv.postConsensusContainer[msg.ValidatorIndex]
container, ok := slotValidators[msg.ValidatorIndex]
if !ok {
container = ssv.NewPartialSigContainer(validator.Quorum())
ncv.postConsensusContainer[msg.ValidatorIndex] = container
slotValidators[msg.ValidatorIndex] = container
}
if container.HasSignature(msg.ValidatorIndex, msg.Signer, msg.SigningRoot) {
ncv.resolveDuplicateSignature(container, msg, validator)
Expand All @@ -250,6 +263,18 @@ func (ncv *CommitteeObserver) processMessage(
}
}
}

// Remove older slots container
if len(ncv.postConsensusContainer) >= ncv.postConsensusContainerCapacity() {
// #nosec G115 -- capacity must be low epoch not to cause overflow
thresholdSlot := currentSlot - phase0.Slot(ncv.postConsensusContainerCapacity())
for slot := range ncv.postConsensusContainer {
if slot < thresholdSlot {
delete(ncv.postConsensusContainer, slot)
}
}
}

return quorums, nil
}

Expand Down Expand Up @@ -362,6 +387,11 @@ func (ncv *CommitteeObserver) saveSyncCommRoots(epoch phase0.Epoch, beaconVote *
return nil
}

func (ncv *CommitteeObserver) postConsensusContainerCapacity() int {
// #nosec G115 -- slots per epoch must be low epoch not to cause overflow
return int(ncv.netCfg.SlotsPerEpoch()) + msgvalidation.LateSlotAllowance
}

func constructAttestationData(vote *spectypes.BeaconVote, slot phase0.Slot, committeeIndex phase0.CommitteeIndex) *phase0.AttestationData {
return &phase0.AttestationData{
Slot: slot,
Expand Down