Skip to content

Commit 19b17a5

Browse files
committed
feat: fix all error
2 parents e67bad9 + 58d6823 commit 19b17a5

51 files changed

Lines changed: 7695 additions & 11834 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
1+
# Rust / Cargo
2+
target/
3+
Cargo.lock
4+
*.d
5+
6+
# WASM build artifacts
7+
*.wasm
8+
!contracts/*/wasm/*.wasm
9+
*.wasm.sha256
10+
11+
# Node / JS
112
node_modules/
213
dist/
14+
build/
315
.next/
4-
target/
5-
*.wasm.sha256
16+
.cache/
17+
*.tsbuildinfo
18+
pnpm-lock.yaml
19+
package-lock.json
20+
21+
# Env / secrets
622
.env
7-
.env.local
23+
.env.*
24+
!.env.example
25+
26+
# Editor
27+
.vscode/
28+
.idea/
29+
*.swp
30+
*.swo
31+
*.orig
32+
33+
# OS
34+
.DS_Store
35+
.DS_Store?
36+
._*
37+
Thumbs.db
38+
39+
# Kiro
40+
.kiro/
41+
42+
# Test artifacts
43+
**/test_snapshots/
44+
proptest-regressions/
45+
46+
# Logs
47+
*.log
48+
npm-debug.log*
49+
yarn-debug.log*
50+
51+
# Misc
52+
*.bak
53+
*.tmp
54+
55+
package-lock.json

PERFORMANCE.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# PERFORMANCE.md
2+
3+
## Storage & CPU Micro-optimizations — Issue #25
4+
5+
All optimizations are measured or reasoned from first principles.
6+
No semantic changes were made.
7+
8+
---
9+
10+
## 1. Removed dead compile-time branch in `generate_premium` (`policy.rs`)
11+
12+
**Before:**
13+
```rust
14+
if QUOTE_TTL_LEDGERS == 0 {
15+
return Err(QuoteError::InvalidQuoteTtl);
16+
}
17+
```
18+
19+
**After:** branch removed with comment.
20+
21+
**Justification:** `QUOTE_TTL_LEDGERS` is a `const u32 = 100`. The compiler cannot
22+
eliminate this branch in WASM without optimization hints; removing it saves 1
23+
conditional instruction per `generate_premium` call and removes a dead error
24+
variant from the hot path.
25+
26+
**Write count delta:** 0 (no storage involved).
27+
28+
---
29+
30+
## 2. Removed unchecked `compute_premium` (`premium.rs`)
31+
32+
**Before:** `compute_premium` used bare `*` and `/` on `i128`, risking silent
33+
wrapping on adversarial inputs (e.g. `risk_score` cast to `i128` then multiplied
34+
by `BASE = 10_000_000`).
35+
36+
**After:** removed. All callers use `compute_premium_checked` which uses
37+
`checked_mul` / `checked_div` throughout.
38+
39+
**Justification:** correctness + security. No performance regression — the
40+
checked path is identical in the non-overflow case and the compiler optimizes
41+
`checked_*` to native instructions on known-bounded inputs.
42+
43+
---
44+
45+
## 3. Eliminated redundant factor recomputation in `build_line_items` (`premium.rs`)
46+
47+
**Before:** `type_factor`, `region_factor`, `age_factor` were each called twice —
48+
once to compute `amount` and implicitly again via the struct field `factor`.
49+
50+
**After:** each factor computed once, stored in a local, reused for both `factor`
51+
and `amount` fields.
52+
53+
**CPU delta:** −3 match arms per `build_line_items` call (3 helpers × 1 redundant
54+
call each). Negligible in isolation but correct practice for hot paths.
55+
56+
**Write count delta:** 0 (pure computation).
57+
58+
---
59+
60+
## 4. Storage tier audit — `ClaimCounter` vs `PolicyCounter`
61+
62+
| Key | Tier | Rationale |
63+
|-----|------|-----------|
64+
| `ClaimCounter` | `instance` | Global singleton; cheapest read/write tier |
65+
| `PolicyCounter(holder)` | `persistent` | Per-holder; must survive instance eviction |
66+
| `Policy(holder, id)` | `persistent` | Long-lived record |
67+
| `Admin`, `Token`, `Initialized` | `instance` | Set-once config; cheapest tier |
68+
69+
No changes needed — tiers are already optimal.
70+
71+
---
72+
73+
## 5. Integer width and struct field audit (`types.rs`)
74+
75+
| Field | Type | Justification |
76+
|-------|------|---------------|
77+
| `premium`, `coverage`, `amount` | `i128` | Required by Soroban SEP-41 token standard |
78+
| `claim_id` | `u64` | Global monotonic counter; `u32` would overflow at ~4B claims |
79+
| `policy_id`, `start_ledger`, `end_ledger`, `approve_votes`, `reject_votes` | `u32` | Ledger sequence and per-holder counters are provably ≤ u32::MAX |
80+
| `DETAILS_MAX_LEN`, `IMAGE_URLS_MAX` | `u32` | Match Soroban `String::len()` / `Vec::len()` return type — no cast needed |
81+
82+
No width changes required — all fields are already at the smallest provably-safe type.
83+
All struct fields are actively used; no dead fields to remove.
84+
85+
---
86+
87+
## 6. Hot paths not yet implemented
88+
89+
`initiate_policy`, `vote_on_claim`, `finalize_claim` are stubs pending
90+
`feat/policy-lifecycle` and `feat/claim-voting`. Storage write budgets for
91+
those paths are documented in their respective issue specs and will be
92+
profiled when implemented.
93+
94+
---
95+
96+
## Baseline test results
97+
98+
```
99+
test result: ok. 29 passed; 0 failed
100+
```
101+
102+
All existing tests pass with no semantic regressions.

backend/jest.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
roots: ["<rootDir>/tests"],
6+
testMatch: ["**/*.test.ts"],
7+
globals: {
8+
"ts-jest": {
9+
tsconfig: "tsconfig.test.json",
10+
},
11+
},
12+
};

0 commit comments

Comments
 (0)