mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
83416f31a5
* adding changes to blocks * trying out expiration * adding implementation, have WIP for tests * adding unit tests for cache * fixing bazel complaints * fix linting * adding safe check for unint type * changing approach to safety check * adding cache to bazel to test fixing build * reverting bazel change and adding flag to usage * implementing interface on mock to fix build error * fixing unit tests * fixing unit test * fixing unit tests * fixing linting * fixing more unit tests * fixing produce blinded block tests * Update beacon-chain/cache/registration.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * resolving review comments * fixing cache * Update beacon-chain/cache/registration.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Update beacon-chain/cache/registration.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * fixing time logic * adding context to trace * fix bazel lint * fixing context dependency * fix linting * Update cmd/beacon-chain/flags/base.go Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * addressing review comments * fixing deepsource issues * improving the default settings * fixing bazel * removing irrelevant unit test * updating name --------- Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
83 lines
2.9 KiB
Go
83 lines
2.9 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/v4/math"
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
log "github.com/sirupsen/logrus"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
// RegistrationCache is used to store the cached results of an Validator Registration request.
|
|
// beacon api /eth/v1/validator/register_validator
|
|
type RegistrationCache struct {
|
|
indexToRegistration map[primitives.ValidatorIndex]*ethpb.ValidatorRegistrationV1
|
|
lock sync.RWMutex
|
|
}
|
|
|
|
// NewRegistrationCache initializes the map and underlying cache.
|
|
func NewRegistrationCache() *RegistrationCache {
|
|
return &RegistrationCache{
|
|
indexToRegistration: make(map[primitives.ValidatorIndex]*ethpb.ValidatorRegistrationV1),
|
|
lock: sync.RWMutex{},
|
|
}
|
|
}
|
|
|
|
// RegistrationByIndex returns the registration by index in the cache and also removes items in the cache if expired.
|
|
func (regCache *RegistrationCache) RegistrationByIndex(id primitives.ValidatorIndex) (*ethpb.ValidatorRegistrationV1, error) {
|
|
regCache.lock.RLock()
|
|
v, ok := regCache.indexToRegistration[id]
|
|
if !ok {
|
|
regCache.lock.RUnlock()
|
|
return nil, errors.Wrapf(ErrNotFoundRegistration, "validator id %d", id)
|
|
}
|
|
isExpired, err := RegistrationTimeStampExpired(v.Timestamp)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to check registration expiration")
|
|
}
|
|
if isExpired {
|
|
regCache.lock.RUnlock()
|
|
regCache.lock.Lock()
|
|
defer regCache.lock.Unlock()
|
|
delete(regCache.indexToRegistration, id)
|
|
log.Warnf("registration for validator index %d expired at unix time %d", id, v.Timestamp)
|
|
return nil, errors.Wrapf(ErrNotFoundRegistration, "validator id %d", id)
|
|
}
|
|
regCache.lock.RUnlock()
|
|
return v, nil
|
|
}
|
|
|
|
func RegistrationTimeStampExpired(ts uint64) (bool, error) {
|
|
// safely convert unint64 to int64
|
|
i, err := math.Int(ts)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
expiryDuration := params.BeaconConfig().RegistrationDuration
|
|
// registered time + expiration duration < current time = expired
|
|
return time.Unix(int64(i), 0).Add(expiryDuration).Before(time.Now()), nil
|
|
}
|
|
|
|
// UpdateIndexToRegisteredMap adds or updates values in the cache based on the argument.
|
|
func (regCache *RegistrationCache) UpdateIndexToRegisteredMap(ctx context.Context, m map[primitives.ValidatorIndex]*ethpb.ValidatorRegistrationV1) {
|
|
_, span := trace.StartSpan(ctx, "RegistrationCache.UpdateIndexToRegisteredMap")
|
|
defer span.End()
|
|
regCache.lock.Lock()
|
|
defer regCache.lock.Unlock()
|
|
for key, value := range m {
|
|
regCache.indexToRegistration[key] = ðpb.ValidatorRegistrationV1{
|
|
Pubkey: bytesutil.SafeCopyBytes(value.Pubkey),
|
|
FeeRecipient: bytesutil.SafeCopyBytes(value.FeeRecipient),
|
|
GasLimit: value.GasLimit,
|
|
Timestamp: value.Timestamp,
|
|
}
|
|
}
|
|
}
|