From 42b42faeebbf55833d5413cb130b58d24c7c61f7 Mon Sep 17 00:00:00 2001 From: hopeyen Date: Thu, 12 Dec 2024 12:50:31 -0800 Subject: [PATCH] refactor: move isActive check --- core/meterer/meterer.go | 3 +++ core/meterer/onchain_state.go | 24 ++++-------------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/core/meterer/meterer.go b/core/meterer/meterer.go index 2fd9f403b8..681c6d8c6c 100644 --- a/core/meterer/meterer.go +++ b/core/meterer/meterer.go @@ -98,6 +98,9 @@ func (m *Meterer) MeterRequest(ctx context.Context, header core.PaymentMetadata, // ServeReservationRequest handles the rate limiting logic for incoming requests func (m *Meterer) ServeReservationRequest(ctx context.Context, header core.PaymentMetadata, reservation *core.ReservedPayment, numSymbols uint, quorumNumbers []uint8) error { + if !reservation.IsActive(uint64(time.Now().Unix())) { + return fmt.Errorf("reservation not active") + } if err := m.ValidateQuorum(quorumNumbers, reservation.QuorumNumbers); err != nil { return fmt.Errorf("invalid quorum for reservation: %w", err) } diff --git a/core/meterer/onchain_state.go b/core/meterer/onchain_state.go index 221a7b21d2..85d6a9c932 100644 --- a/core/meterer/onchain_state.go +++ b/core/meterer/onchain_state.go @@ -2,10 +2,8 @@ package meterer import ( "context" - "fmt" "sync" "sync/atomic" - "time" "github.com/Layr-Labs/eigenda/core" "github.com/Layr-Labs/eigenda/core/eth" @@ -147,18 +145,9 @@ func (pcs *OnchainPaymentState) RefreshOnchainPaymentState(ctx context.Context, // GetReservedPaymentByAccount returns a pointer to the active reservation for the given account ID; no writes will be made to the reservation func (pcs *OnchainPaymentState) GetReservedPaymentByAccount(ctx context.Context, accountID gethcommon.Address) (*core.ReservedPayment, error) { - timestamp := uint64(time.Now().Unix()) - pcs.ReservationsLock.Lock() - defer pcs.ReservationsLock.Unlock() - + pcs.ReservationsLock.RLock() + defer pcs.ReservationsLock.RUnlock() if reservation, ok := (pcs.ReservedPayments)[accountID]; ok { - if !reservation.IsActive(timestamp) { - // if reservation is expired, remove it from the local state; if it is not activated, we leave the reservation in the local state - if reservation.EndTimestamp < timestamp { - delete(pcs.ReservedPayments, accountID) - } - return nil, fmt.Errorf("reservation not active") - } return reservation, nil } @@ -167,14 +156,9 @@ func (pcs *OnchainPaymentState) GetReservedPaymentByAccount(ctx context.Context, if err != nil { return nil, err } - if !res.IsActive(timestamp) { - if res.StartTimestamp > timestamp { - // if reservation is not activated yet, we add it to the local state to reduce future on-chain calls - (pcs.ReservedPayments)[accountID] = res - } - return nil, fmt.Errorf("reservation not active") - } + pcs.ReservationsLock.Lock() (pcs.ReservedPayments)[accountID] = res + pcs.ReservationsLock.Unlock() return res, nil }