Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add class diagram for the database schema of the hbar limiter redesign #2884

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 107 additions & 15 deletions docs/design/hbar-limiter.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,128 @@ The purpose of the HBar Limiter is to track and control the spending of HBars in
The Hbar limiter will be implemented as a separate service, used by other services/classes that need it. It will have two main purposes - to capture the gas fees for different operation and to check if an operation needs to be paused, due to exceeded Hbar limit

### Class Diagram

#### Service Layer
```mermaid
classDiagram
class sdkClient
class SdkClient {
-hbarLimitService: IHBarLimitService
-metricService: MetricService
+executeTransaction(transaction: Transaction, callerName: string, interactingEntity: string, requestId?: string): Promise<TransactionId>
+executeQuery~T~(query: Query~T~, callerName: string, interactingEntity: string, requestId?: string): Promise<Query~T~>
}

class MetricService {
-hbarLimitService: IHBarLimitService
+captureTransactionFees(transaction: Transaction, callerName: string, interactingEntity: string, requestId?: string) void
+captureQueryFees~T~(query: Query~T~, callerName: string, interactingEntity: string, requestId?: string) void
}

MetricService <|-- sdkClient

class HbarLimitService {
-mirrorNodeClient: MirrorNodeClient
class HBarLimitService {
-hbarSpendingPlanRepository: HbarSpendingPlanRepository
-ethAddressHbarSpendingPlanRepository: EthAddressHbarSpendingPlanRepository
-ipAddressHbarSpendingPlanRepository: IpAddressHbarSpendingPlanRepository
+shouldLimit(txFrom: string, ip?: string) boolean
-getTransactionRecord(transactionld: string) TransactionRecord
+resetLimiter() void
-getOperatorBalance() number
-getAddressLimit(address: string) number
-getAddressSpent(address: string) number
-getIpSpent(ip: string) number
+addExpense(amount: number, txFrom: string, ip?: string) void
-getSpendingPlanOfEthAddress(address: string): HbarSpendingPlan
-getSpendingPlanOfIpAddress(ip: string): HbarSpendingPlan
-checkTotalSpent() boolean
-shouldReset() boolean
}

class IHBarLimitService {
+shouldLimit() boolean
+resetLimiter() void
class IHBarLimitService
<<interface>> IHBarLimitService
IHBarLimitService : shouldLimit() boolean
IHBarLimitService : resetLimiter() void
IHBarLimitService : addExpense() void

SdkClient --> MetricService : uses
SdkClient --> IHBarLimitService : uses
MetricService --> IHBarLimitService : uses
IHBarLimitService <|-- HBarLimitService: implements
```

#### Database Layer:
```mermaid
classDiagram
class HbarSpendingPlan {
-id: string
-subscriptionType: SubscriptionType
-createdAt: Date
-active: boolean
-spendingHistory: HbarSpendingRecord[]
-spentToday: number
}

class HbarSpendingRecord {
-amount: number
-timestamp: Date
}

class EthAddressHbarSpendingPlan {
-ethAddress: string
-planId: string
}

class IpAddressHbarSpendingPlan {
-ipAddress: string
-planId: string
}

class CacheService {
-internalCache: ICacheClient
-sharedCache: ICacheClient
+getAsync<T>(key: string, callingMethod: string, requestIdPrefix?: string): Promise<T>
+set(key: string, value: any, callingMethod: string, ttl?: number, requestIdPrefix?: string): Promise<void>
+multiSet(entries: Record<string, any>, callingMethod: string, ttl?: number, requestIdPrefix?: string): Promise<void>
+delete(key: string, callingMethod: string, requestIdPrefix?: string): Promise<void>
+clear(requestIdPrefix?: string): Promise<void>
+incrBy(key: string, amount: number, callingMethod: string, requestIdPrefix?: string): Promise<number>
+rPush(key: string, value: any, callingMethod: string, requestIdPrefix?: string): Promise<number>
+lRange<T>(key: string, start: number, end: number, callingMethod: string, requestIdPrefix?: string): Promise<T[]>
}

class HbarSpendingPlanRepository {
-cache: CacheService
+findById(id: string): Promise<IHbarSpendingPlan>
+findByIdWithDetails(id: string): Promise<IDetailedHbarSpendingPlan>
+create(subscriptionType: SubscriptionType): Promise<IDetailedHbarSpendingPlan>
+checkExistsAndActive(id: string): Promise<void>
+getSpendingHistory(id: string): Promise<HbarSpendingRecord[]>
+addAmountToSpendingHistory(id: string, amount: number): Promise<number>
+getSpentToday(id: string): Promise<number>
+addAmountToSpentToday(id: string, amount: number): Promise<void>
}

class EthAddressHbarSpendingPlanRepository {
-cache: CacheService
+findByAddress(ethAddress: string): Promise<EthAddressHbarSpendingPlan>
+save(ethAddressPlan: EthAddressHbarSpendingPlan): Promise<void>
+delete(ethAddress: string): Promise<void>
}

class IpAddressHbarSpendingPlanRepository {
-cache: CacheService
+findByIp(ip: string): Promise<IpAddressHbarSpendingPlan>
+save(ipAddressPlan: IpAddressHbarSpendingPlan): Promise<void>
+delete(ip: string): Promise<void>
}
IHBarLimitService <|-- sdkClient
IHBarLimitService <|-- HbarLimitService

class SubscriptionType
<<Enumeration>> SubscriptionType
SubscriptionType : BASIC
SubscriptionType : EXTENDED
SubscriptionType : PRIVILEGED

HbarSpendingPlan --> SubscriptionType : could be one of the types
HbarSpendingPlan --> HbarSpendingRecord : stores history of
EthAddressHbarSpendingPlan --> HbarSpendingPlan : links an ETH address to
IpAddressHbarSpendingPlan --> HbarSpendingPlan : links an IP address to

HbarSpendingPlanRepository --> CacheService : uses
EthAddressHbarSpendingPlanRepository --> CacheService : uses
IpAddressHbarSpendingPlanRepository --> CacheService : uses
```

## Additional Considerations
Expand Down
Loading