-
Notifications
You must be signed in to change notification settings - Fork 208
feat(db): add resource_usage_logs table with repository functions #246
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
Merged
AbdulmalikAlayande
merged 5 commits into
TegoLabs:main
from
funds0033-cmyk:feat/164-resource-usage-logs
Jun 30, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21e96ac
feat(db): add resource_usage_logs table with repository functions
funds0033-cmyk a166016
fix(db): add type assertions for snapshotRow and currentDayRow in rep…
funds0033-cmyk d0fbf3d
fix(core): replace empty RentWindowProjection interface with type alias
funds0033-cmyk 0a77b16
Merge main into pr-246
AbdulmalikAlayande 1d9226b
fix: resolve schema corruption and linting bypasses
AbdulmalikAlayande File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Add resource_usage_logs table to SQLite | ||
|
|
||
| Closes #164 | ||
|
|
||
| ## What | ||
|
|
||
| Introduces a dedicated `resource_usage_logs` table that records per-transaction | ||
| CPU, memory, and fee-parameter snapshots for any tracked Soroban contract. | ||
|
|
||
| Previously, resource data was tied exclusively to TTL-extension transactions | ||
| in `extension_history`. This new table is transaction-agnostic, making it | ||
| usable by any future code path that wants to persist resource consumption | ||
| without performing an extension. | ||
|
|
||
| ## Schema | ||
|
|
||
| New table added to `src/db/schema.sql`: | ||
|
|
||
| ```sql | ||
| CREATE TABLE IF NOT EXISTS resource_usage_logs ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| contract_id TEXT NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, | ||
| cpu_insns INTEGER NOT NULL, | ||
| mem_bytes INTEGER NOT NULL, | ||
| fee_instructions INTEGER, | ||
| fee_read_ledger_entries INTEGER, | ||
| fee_write_ledger_entries INTEGER, | ||
| fee_read_bytes INTEGER, | ||
| fee_write_bytes INTEGER, | ||
| fee_transaction_size INTEGER, | ||
| fee_historical_ledger INTEGER, | ||
| fee_rent_ledger INTEGER, | ||
| fee_refundable INTEGER, | ||
| recorded_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
| CREATE INDEX IF NOT EXISTS idx_resource_usage_logs_contract_id ON resource_usage_logs(contract_id); | ||
| CREATE INDEX IF NOT EXISTS idx_resource_usage_logs_recorded_at ON resource_usage_logs(recorded_at DESC); | ||
| ``` | ||
|
|
||
| All fee columns are nullable — not every RPC response includes a full fee breakdown. | ||
| `cpu_insns` and `mem_bytes` are required as they are always present in a simulation result. | ||
|
|
||
| A migration file (`src/db/migrations/001_resource_usage_logs.sql`) is also | ||
| included so that existing `sorokeep.db` files are upgraded automatically on | ||
| next startup via the `Migrator`. | ||
|
|
||
| ## Repository API (`src/db/repositories.ts`) | ||
|
|
||
| Three new exported functions following the exact patterns used by the rest | ||
| of the file: | ||
|
|
||
| | Function | Description | | ||
| |---|---| | ||
| | `insertResourceUsageLog(db, log)` | Insert a new log entry; returns the new row id | | ||
| | `getResourceUsageLogs(db, contractId, options?)` | Query logs newest-first; supports `limit` and `since` filters | | ||
| | `getLatestResourceUsageLog(db, contractId)` | Convenience helper — returns the single most-recent row | | ||
|
|
||
| ## Tests | ||
|
|
||
| Written **before** the implementation (TDD): | ||
|
|
||
| `tests/db/resource_usage_logs.test.ts` — 20 tests covering: | ||
|
|
||
| - Schema: table existence, index existence, foreign-key enforcement | ||
| - `insertResourceUsageLog`: happy path, null fee columns, duplicate entries | ||
| allowed, invalid `contract_id` rejected, zero values, caller-supplied | ||
| timestamp | ||
| - `getResourceUsageLogs`: empty result, DESC ordering, per-contract isolation, | ||
| `limit` option, negative-limit guard, `since` filter | ||
| - `getLatestResourceUsageLog`: empty result, most-recent selection, cross-contract isolation | ||
|
|
||
| ## Checklist | ||
|
|
||
| - [x] Tests written first (red → green) | ||
| - [x] Schema migration runs on a fresh in-memory DB | ||
| - [x] All 20 new tests pass | ||
| - [x] Full test suite passes (495 tests, 0 failures) | ||
| - [x] No existing tests broken |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| -- Migration 001: add resource_usage_logs table (issue #164) | ||
| -- | ||
| -- Stores per-transaction CPU, memory, and fee-parameter snapshots so that | ||
| -- operators can track resource consumption over time without relying on | ||
| -- extension_history (which is specific to TTL-extension transactions). | ||
|
|
||
| CREATE TABLE IF NOT EXISTS resource_usage_logs ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| contract_id TEXT NOT NULL REFERENCES contracts(id) ON DELETE CASCADE, | ||
| cpu_insns INTEGER NOT NULL, | ||
| mem_bytes INTEGER NOT NULL, | ||
| fee_instructions INTEGER, | ||
| fee_read_ledger_entries INTEGER, | ||
| fee_write_ledger_entries INTEGER, | ||
| fee_read_bytes INTEGER, | ||
| fee_write_bytes INTEGER, | ||
| fee_transaction_size INTEGER, | ||
| fee_historical_ledger INTEGER, | ||
| fee_rent_ledger INTEGER, | ||
| fee_refundable INTEGER, | ||
| recorded_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_resource_usage_logs_contract_id | ||
| ON resource_usage_logs(contract_id); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_resource_usage_logs_recorded_at | ||
| ON resource_usage_logs(recorded_at DESC); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.