Skip to content

Commit

Permalink
refactor: reservation config with bytes per second instead of bin limit
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Oct 8, 2024
1 parent 0323624 commit 6351c57
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions core/meterer/meterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,15 @@ func (m *Meterer) IncrementBinUsage(ctx context.Context, PaymentMetadata Payment
}

// metered usage stays within the bin limit
if newUsage <= reservation.DataRate {
usageLimit := m.GetReservationBinLimit(reservation)
if newUsage <= usageLimit {
return nil
} else if newUsage-recordedSize >= reservation.DataRate {
} else if newUsage-recordedSize >= usageLimit {
// metered usage before updating the size already exceeded the limit
return fmt.Errorf("bin has already been filled")
}
if newUsage <= 2*reservation.DataRate && PaymentMetadata.BinIndex+2 <= GetBinIndex(reservation.EndTimestamp, m.ReservationWindow) {
m.OffchainStore.UpdateReservationBin(ctx, PaymentMetadata.AccountID, uint64(PaymentMetadata.BinIndex+2), newUsage-reservation.DataRate)
if newUsage <= 2*usageLimit && PaymentMetadata.BinIndex+2 <= GetBinIndex(reservation.EndTimestamp, m.ReservationWindow) {
m.OffchainStore.UpdateReservationBin(ctx, PaymentMetadata.AccountID, uint64(PaymentMetadata.BinIndex+2), newUsage-usageLimit)
return nil
}
return fmt.Errorf("overflow usage exceeds bin limit")
Expand Down Expand Up @@ -290,3 +291,8 @@ func (m *Meterer) IncrementGlobalBinUsage(ctx context.Context, symbolsCharged ui
}
return nil
}

// GetReservationBinLimit returns the bin limit for a given reservation
func (m *Meterer) GetReservationBinLimit(reservation *ActiveReservation) uint64 {
return reservation.BytesPerSec * uint64(m.ReservationWindow)
}
6 changes: 3 additions & 3 deletions core/meterer/meterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func InitializeMockPayments(pcs *meterer.OnchainPaymentState, privateKey1 *ecdsa
// Initialize mock active reservations
now := uint64(time.Now().Unix())
pcs.ActiveReservations.Reservations = map[string]*meterer.ActiveReservation{
crypto.PubkeyToAddress(privateKey1.PublicKey).Hex(): {DataRate: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplit: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}},
crypto.PubkeyToAddress(privateKey2.PublicKey).Hex(): {DataRate: 200, StartTimestamp: now - 120, EndTimestamp: now + 180, QuorumSplit: []byte{30, 70}, QuorumNumbers: []uint8{0, 1}},
crypto.PubkeyToAddress(privateKey1.PublicKey).Hex(): {BytesPerSec: 100, StartTimestamp: now + 1200, EndTimestamp: now + 1800, QuorumSplit: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}},
crypto.PubkeyToAddress(privateKey2.PublicKey).Hex(): {BytesPerSec: 200, StartTimestamp: now - 120, EndTimestamp: now + 180, QuorumSplit: []byte{30, 70}, QuorumNumbers: []uint8{0, 1}},
}
pcs.OnDemandPayments.Payments = map[string]*meterer.OnDemandPayment{
crypto.PubkeyToAddress(privateKey1.PublicKey).Hex(): {CumulativePayment: 1500},
Expand Down Expand Up @@ -107,7 +107,7 @@ func setup(_ *testing.M) {
PricePerChargeable: 1,
MinChargeableSize: 1,
GlobalBytesPerSecond: 1000,
ReservationWindow: 60,
ReservationWindow: 1,
}

paymentChainState := meterer.NewOnchainPaymentState()
Expand Down
4 changes: 2 additions & 2 deletions core/meterer/onchain_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
DummyMinimumChargeableSize = uint32(128)
DummyMinimumChargeablePayment = uint32(128)

DummyReservation = ActiveReservation{DataRate: DummyReservationBytesLimit, StartTimestamp: 0, EndTimestamp: math.MaxUint32, QuorumSplit: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}
DummyReservation = ActiveReservation{BytesPerSec: DummyReservationBytesLimit, StartTimestamp: 0, EndTimestamp: math.MaxUint32, QuorumSplit: []byte{50, 50}, QuorumNumbers: []uint8{0, 1}}
DummyOnDemandPayment = OnDemandPayment{CumulativePayment: DummyPaymentLimit}
)

Expand All @@ -28,7 +28,7 @@ type TokenAmount uint64 // TODO: change to uint128
// OperatorInfo contains information about an operator which is stored on the blockchain state,
// corresponding to a particular quorum
type ActiveReservation struct {
DataRate uint64 // Bandwidth per reservation bin
BytesPerSec uint64 // Bandwidth per reservation bin
StartTimestamp uint64 // Unix timestamp that's valid for basically eternity
EndTimestamp uint64

Expand Down
2 changes: 1 addition & 1 deletion core/meterer/onchain_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestGetActiveReservations(t *testing.T) {
expectedReservations := &meterer.ActiveReservations{
Reservations: map[string]*meterer.ActiveReservation{
"account1": {
DataRate: 100,
BytesPerSec: 100,
StartTimestamp: 1000,
EndTimestamp: 2000,
QuorumSplit: []byte{50, 50},
Expand Down

0 comments on commit 6351c57

Please sign in to comment.