Skip to content

Commit

Permalink
refactor: move isActive check
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 12, 2024
1 parent 7ae3c60 commit 42b42fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
3 changes: 3 additions & 0 deletions core/meterer/meterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 4 additions & 20 deletions core/meterer/onchain_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit 42b42fa

Please sign in to comment.