Skip to content

Commit

Permalink
feat: add on-chain write lock
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Oct 22, 2024
1 parent 0aaa646 commit 05f9379
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/meterer/onchain_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meterer
import (
"context"
"errors"
"sync"

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/eth"
Expand All @@ -26,6 +27,8 @@ type OnchainPaymentState struct {
ActiveReservations map[string]core.ActiveReservation
OnDemandPayments map[string]core.OnDemandPayment
OnDemandQuorumNumbers []uint8
ReservationsLock sync.RWMutex
PaymentsLock sync.RWMutex
}

func NewOnchainPaymentState(ctx context.Context, tx *eth.Transactor) (OnchainPaymentState, error) {
Expand Down Expand Up @@ -54,6 +57,7 @@ func (pcs *OnchainPaymentState) RefreshOnchainPaymentState(ctx context.Context,
return err
}

pcs.ReservationsLock.Lock()
accountIDs := make([]string, 0, len(pcs.ActiveReservations))
for accountID := range pcs.ActiveReservations {
accountIDs = append(accountIDs, accountID)
Expand All @@ -64,7 +68,9 @@ func (pcs *OnchainPaymentState) RefreshOnchainPaymentState(ctx context.Context,
return err
}
pcs.ActiveReservations = activeReservations
pcs.ReservationsLock.Unlock()

pcs.PaymentsLock.Lock()
accountIDs = make([]string, 0, len(pcs.OnDemandPayments))
for accountID := range pcs.OnDemandPayments {
accountIDs = append(accountIDs, accountID)
Expand All @@ -75,6 +81,7 @@ func (pcs *OnchainPaymentState) RefreshOnchainPaymentState(ctx context.Context,
return err
}
pcs.OnDemandPayments = onDemandPayments
pcs.PaymentsLock.Unlock()

return nil
}
Expand All @@ -94,7 +101,9 @@ func (pcs *OnchainPaymentState) GetActiveReservationByAccount(ctx context.Contex
return core.ActiveReservation{}, errors.New("payment not found")
}

pcs.ReservationsLock.Lock()
pcs.ActiveReservations[accountID] = res
pcs.ReservationsLock.Unlock()
return res, nil
}

Expand All @@ -113,7 +122,9 @@ func (pcs *OnchainPaymentState) GetOnDemandPaymentByAccount(ctx context.Context,
return core.OnDemandPayment{}, errors.New("payment not found")
}

pcs.PaymentsLock.Lock()
pcs.OnDemandPayments[accountID] = res
pcs.PaymentsLock.Unlock()
return res, nil
}

Expand Down

0 comments on commit 05f9379

Please sign in to comment.