Skip to content

Move values to chain spec.#576

Merged
tomusdrw merged 3 commits intomainfrom
td-update-spec-values
Sep 1, 2025
Merged

Move values to chain spec.#576
tomusdrw merged 3 commits intomainfrom
td-update-spec-values

Conversation

@tomusdrw
Copy link
Copy Markdown
Member

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 31, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Gas limits and erasure-coded segment sizing are now configurable via network settings.
    • Added new chain parameters (e.g., max block gas, max refine gas, preimage expunge period); erasure-coded piece size is computed from segment size.
  • Refactor
    • Replaced hardcoded constants with values derived from the active network configuration for greater consistency across environments.
    • Standardized numeric handling in configuration for improved validation and reliability.
  • Tests
    • Updated tests to use the new segment size source and revised formulas accordingly.
  • Chores
    • Added a new numbers utility dependency.

Walkthrough

This change replaces several hard-coded gas/erasure-code constants with values sourced from ChainSpec, introduces typed numeric wrappers, adds EC_SEGMENT_SIZE and derived erasureCodedPieceSize, updates encoding and accumulation logic to use ChainSpec fields, removes unused constants, adjusts tests and statistics to reference EC_SEGMENT_SIZE, and adds a numbers dependency.

Changes

Cohort / File(s) Summary
Remove static GP constants
packages/jam/block/gp-constants.ts
Deleted exported constants: G_R, W_E, W_P, W_G. Other constants remain unchanged.
Config: typed numbers and EC sizing
packages/jam/config/chain-spec.ts
Added EC_SEGMENT_SIZE = 4104. Migrated many ChainSpec fields to U8/U16/U32/U64. Added preimageExpungePeriod, erasureCodedPieceSize (derived), maxBlockGas, maxRefineGas. Updated constructor signature and computations.
Config dependency
packages/jam/config/package.json
Added dependency @typeberry/numbers@0.0.1.
Accumulate gas clamp uses ChainSpec
packages/jam/transition/accumulate/accumulate.ts
Removed ACCUMULATE_TOTAL_GAS. getGasLimit now clamps to chainSpec.maxBlockGas. Updated doc references.
Externalities encoding uses ChainSpec
packages/jam/transition/externalities/fetch-externalities.ts
Replaced imports of G_R, ACCUMULATE_TOTAL_GAS, W_E, W_P. Now encode from chainSpec.maxRefineGas, chainSpec.maxBlockGas, chainSpec.erasureCodedPieceSize, chainSpec.numberECPiecesPerSegment, and validatorsCount.
Statistics use EC_SEGMENT_SIZE
packages/jam/transition/statistics.ts, packages/jam/transition/statistics.test.ts
Replaced W_G with EC_SEGMENT_SIZE in DAS calculations and tests. Updated imports to bring in EC_SEGMENT_SIZE from config.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Accumulate as Accumulate
  participant ChainSpec as ChainSpec

  Caller->>Accumulate: getGasLimit(input)
  Accumulate->>Accumulate: compute calculatedGasLimit
  Accumulate->>ChainSpec: read maxBlockGas
  Accumulate->>Accumulate: clamp = min(calculatedGasLimit, maxBlockGas)
  Accumulate-->>Caller: gasLimit=clamp
  note over Accumulate,ChainSpec: Gas ceiling now sourced from ChainSpec.maxBlockGas
Loading
sequenceDiagram
  autonumber
  actor Runtime
  participant FetchExt as fetch-externalities
  participant ChainSpec as ChainSpec

  Runtime->>FetchExt: getEncodedConstants()
  FetchExt->>ChainSpec: read validatorsCount (V)
  FetchExt->>ChainSpec: read maxRefineGas (G_R)
  FetchExt->>ChainSpec: read maxBlockGas (G_T)
  FetchExt->>ChainSpec: read erasureCodedPieceSize (W_E)
  FetchExt->>ChainSpec: read numberECPiecesPerSegment (W_P)
  FetchExt-->>Runtime: encoded constants blob
  note over FetchExt: Static constants replaced by ChainSpec-derived values
Loading
sequenceDiagram
  autonumber
  actor StatsCaller
  participant Stats as statistics.calculateDAScoreCore
  participant Config as Config

  StatsCaller->>Stats: calculateDAScoreCore(workPackages)
  Stats->>Config: read EC_SEGMENT_SIZE
  loop for each workPackage
    Stats->>Stats: sum += length + EC_SEGMENT_SIZE * segmentCount
  end
  Stats-->>StatsCaller: score
  note over Stats,Config: W_G replaced with EC_SEGMENT_SIZE
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

I nibble on bytes in clovered light,
New numbers hop with typed delight.
Gas caps shift, the segments sing,
EC beats in a tidy ring.
Constants burrowed, specs now guide—
I thump approval, whiskers wide. 🐇✨


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/jam/transition/accumulate/accumulate.ts (1)

441-445: Gas limit “clamp” uses max instead of min (breaks cap at maxBlockGas).

This returns the larger of calculatedGasLimit and maxBlockGas; it should cap at maxBlockGas.

Apply:

-    const gasLimit = tryAsServiceGas(
-      this.chainSpec.maxBlockGas > calculatedGasLimit ? this.chainSpec.maxBlockGas : calculatedGasLimit,
-    );
-
-    return tryAsServiceGas(gasLimit);
+    const gasLimit = calculatedGasLimit > this.chainSpec.maxBlockGas
+      ? this.chainSpec.maxBlockGas
+      : calculatedGasLimit;
+    return tryAsServiceGas(gasLimit);
packages/jam/config/chain-spec.ts (1)

81-99: Avoid Omit for constructor input; define a dedicated init type.

Using Omit<ChainSpec, ...> on a class is brittle and may accidentally include inherited instance members from WithDebug. Define an explicit init type and use it in the constructor.

Apply this diff within the constructor signature:

-  constructor(data: Omit<ChainSpec, "validatorsSuperMajority" | "thirdOfValidators" | "erasureCodedPieceSize">) {
+  constructor(data: ChainSpecInit) {

And add this type near the class (outside this hunk):

export type ChainSpecInit = {
  validatorsCount: U16;
  coresCount: U16;
  slotDuration: U16;
  epochLength: U32;
  rotationPeriod: U16;
  contestLength: U32;
  ticketsPerValidator: U8;
  maxTicketsPerExtrinsic: U8;
  numberECPiecesPerSegment: U32;
  preimageExpungePeriod: U32;
  maxBlockGas: U64;
  maxRefineGas: U64;
};
🧹 Nitpick comments (7)
packages/jam/transition/accumulate/accumulate.ts (1)

432-434: JSDoc nit: remove W_G reference and clarify clamp semantics.

W_G is gone; prefer “min(calculatedGasLimit, maxBlockGas)”.

-   * Please note it cannot overflow because we use `BigInt`, and the final result is clamped to `maxBlockGas` (W_G).
+   * Uses BigInt math; final result is clamped to maxBlockGas (i.e., min(calculatedGasLimit, maxBlockGas)).
packages/jam/transition/statistics.ts (1)

111-115: Update inline comment to match EC_SEGMENT_SIZE.

The code uses EC_SEGMENT_SIZE but the note still mentions W_G.

-    /** Available work report score can be up to `W_R + W_G * ((W_M * 65) / 64) = 0x00C4_2180` */
+    /** Available work report score can be up to `W_R + EC_SEGMENT_SIZE * ceil((W_M * 65) / 64) = 0x00C4_2180` */
packages/jam/transition/statistics.test.ts (1)

50-52: Mirror production formula with Math.ceil for clarity.

Align the bound with code’s ceil-based segment count.

-      assert.strictEqual(isU32(W_R + EC_SEGMENT_SIZE * ((W_M * 65) / 64)), true);
+      assert.strictEqual(isU32(W_R + EC_SEGMENT_SIZE * Math.ceil((W_M * 65) / 64)), true);
packages/jam/config/chain-spec.ts (4)

35-37: Add spec link for EC_SEGMENT_SIZE (W_G).

Consider adding a Gray Paper link (same versioning scheme used elsewhere) to document the 4104-octet segment size source.


96-96: Guard divisibility of W_G by W_P for clearer errors.

tryAsU32(EC_SEGMENT_SIZE / W_P) will throw on non-integers but with a generic message. Add an explicit check to surface a precise cause.

-    this.erasureCodedPieceSize = tryAsU32(EC_SEGMENT_SIZE / data.numberECPiecesPerSegment);
+    if (EC_SEGMENT_SIZE % data.numberECPiecesPerSegment !== 0) {
+      throw new Error(
+        `EC_SEGMENT_SIZE (${EC_SEGMENT_SIZE}) must be divisible by numberECPiecesPerSegment (${data.numberECPiecesPerSegment}).`
+      );
+    }
+    this.erasureCodedPieceSize = tryAsU32(EC_SEGMENT_SIZE / data.numberECPiecesPerSegment);

114-116: Use isSuite(suite, version) helper instead of two checks.

Readability nit: the API supports passing version to isSuite.

-    Compatibility.isSuite(TestSuite.JAMDUNA) && Compatibility.is(GpVersion.V0_6_4) ? 6 : 32,
+    Compatibility.isSuite(TestSuite.JAMDUNA, GpVersion.V0_6_4) ? 6 : 32,

121-124: Update comment: “full” vs “tiny” values no longer mostly copied.

Several fields differ (contestLength, tickets, W_P, gas, preimage delay). Adjust the doc to avoid confusion.

- * Please note that only validatorsCount and epochLength are "full", the rest is copied from "tiny".
+ * Note: multiple fields differ from "tiny" (e.g., contest/tickets/W_P/gas/preimage delay); see values below.
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between b9e5844 and 4656f36.

📒 Files selected for processing (7)
  • packages/jam/block/gp-constants.ts (0 hunks)
  • packages/jam/config/chain-spec.ts (3 hunks)
  • packages/jam/config/package.json (1 hunks)
  • packages/jam/transition/accumulate/accumulate.ts (2 hunks)
  • packages/jam/transition/externalities/fetch-externalities.ts (3 hunks)
  • packages/jam/transition/statistics.test.ts (3 hunks)
  • packages/jam/transition/statistics.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • packages/jam/block/gp-constants.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*.ts

⚙️ CodeRabbit configuration file

**/*.ts: as conversions must not be used. Suggest using tryAs conversion methods.

**/*.ts: Classes with static Codec field must have private constructor and static create method.

**/*.ts: Casting a bigint (or U64) using Number(x) must have an explanation comment why
it is safe.

**/*.ts: When making changes to code with comments containing links (in classes, constants, methods, etc.)
to graypaper.fluffylabs.dev, ensure those links point to the current version for this update.

Files:

  • packages/jam/transition/statistics.ts
  • packages/jam/transition/accumulate/accumulate.ts
  • packages/jam/transition/externalities/fetch-externalities.ts
  • packages/jam/transition/statistics.test.ts
  • packages/jam/config/chain-spec.ts
🧠 Learnings (1)
📚 Learning: 2025-06-18T20:35:13.536Z
Learnt from: tomusdrw
PR: FluffyLabs/typeberry#442
File: packages/core/pvm-debugger-adapter/index.ts:22-40
Timestamp: 2025-06-18T20:35:13.536Z
Learning: The `typeberry/utils` package has browser compatibility issues due to Node.js-specific code like `measure` function using `process.hrtime()` and `testUtils` importing `node:assert`, causing white screens in browser environments.

Applied to files:

  • packages/jam/config/chain-spec.ts
🧬 Code graph analysis (4)
packages/jam/transition/statistics.ts (1)
packages/jam/config/chain-spec.ts (1)
  • EC_SEGMENT_SIZE (36-36)
packages/jam/transition/externalities/fetch-externalities.ts (2)
packages/core/numbers/index.ts (2)
  • tryAsU64 (50-53)
  • tryAsU32 (43-44)
packages/jam/block/gp-constants.ts (4)
  • W_A (42-42)
  • W_B (45-45)
  • W_C (48-48)
  • W_M (51-51)
packages/jam/transition/statistics.test.ts (3)
packages/core/numbers/index.ts (1)
  • isU32 (47-47)
packages/jam/block/gp-constants.ts (2)
  • W_R (54-54)
  • W_M (51-51)
packages/jam/config/chain-spec.ts (1)
  • EC_SEGMENT_SIZE (36-36)
packages/jam/config/chain-spec.ts (2)
packages/core/numbers/index.ts (8)
  • U16 (21-21)
  • U32 (24-24)
  • U8 (18-18)
  • U64 (27-27)
  • tryAsU16 (37-38)
  • tryAsU32 (43-44)
  • tryAsU8 (31-32)
  • tryAsU64 (50-53)
packages/core/utils/compatibility.ts (1)
  • Compatibility (46-102)
🔇 Additional comments (4)
packages/jam/transition/accumulate/accumulate.ts (1)

133-135: Confirm Graypaper link versions
The following URLs still target v=0.6.7 (lines 133, 156, 214, 227, 237, 245, 274, 337, 410, 434) and v=0.7.0 (line 524). If the PR’s semantic changes require a newer Graypaper revision, update these anchors to the correct version.

packages/jam/transition/statistics.test.ts (1)

23-24: Import looks good.

Using EC_SEGMENT_SIZE from config matches the move away from W_G.

packages/jam/config/chain-spec.ts (2)

1-1: Good move: use typed numeric wrappers from @typeberry/numbers.

This aligns with the guideline to avoid as casts and adds range checks at construction time.


9-10: Align Gray Paper links to commit-anchored URLs
Update all Gray Paper reader links in this file to the canonical URLs for the specific commit anchors (no ?v= query), as verified against the jam-test-vectors references:

  • Validators estimate: https://graypaper.fluffylabs.dev/#/5f542d7/418800418800
  • Cores: https://graypaper.fluffylabs.dev/#/5f542d7/414200414200
  • Epoch length: https://graypaper.fluffylabs.dev/#/5f542d7/414800414800
  • Preimage expunge: https://graypaper.fluffylabs.dev/#/9a08063/445800445800
  • Rotation period: https://graypaper.fluffylabs.dev/#/5f542d7/417f00417f00

Apply these updates at lines 9–10, 23–24, 31–32, 53–55, and 69–70.

Comment thread packages/jam/config/chain-spec.ts
Comment thread packages/jam/config/package.json
Comment thread packages/jam/transition/externalities/fetch-externalities.ts
@tomusdrw tomusdrw enabled auto-merge (squash) September 1, 2025 09:37
@tomusdrw tomusdrw merged commit 83eaca5 into main Sep 1, 2025
11 checks passed
@tomusdrw tomusdrw deleted the td-update-spec-values branch September 1, 2025 09:49
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 1, 2025

View all
File Benchmark Ops
codec/decoding.ts[0] manual decode 22093459.1 ±0.65% 85.94% slower
codec/decoding.ts[1] int32array decode 157120342.73 ±3.52% fastest ✅
codec/decoding.ts[2] dataview decode 156277902.64 ±3.37% 0.54% slower
codec/encoding.ts[0] manual encode 3254748.61 ±0.34% 18.39% slower
codec/encoding.ts[1] int32array encode 3987976.76 ±0.48% fastest ✅
codec/encoding.ts[2] dataview encode 3933223.37 ±0.75% 1.37% slower
bytes/hex-from.ts[0] parse hex using Number with NaN checking 116794.45 ±1.35% 85.53% slower
bytes/hex-from.ts[1] parse hex from char codes 807017.68 ±0.94% fastest ✅
bytes/hex-from.ts[2] parse hex from string nibbles 539222.89 ±0.34% 33.18% slower
bytes/hex-to.ts[0] number toString + padding 292009.66 ±1.93% fastest ✅
bytes/hex-to.ts[1] manual 15566.77 ±1.05% 94.67% slower
codec/bigint.compare.ts[0] compare custom 240330670.37 ±5.96% fastest ✅
codec/bigint.compare.ts[1] compare bigint 237647095.75 ±7.55% 1.12% slower
codec/bigint.decode.ts[0] decode custom 145072112.82 ±7.18% fastest ✅
codec/bigint.decode.ts[1] decode bigint 100025114.66 ±2.72% 31.05% slower
collections/map-set.ts[0] 2 gets + conditional set 116500.08 ±0.94% fastest ✅
collections/map-set.ts[1] 1 get 1 set 60272.11 ±0.14% 48.26% slower
hash/index.ts[0] hash with numeric representation 190.34 ±0.39% 24.28% slower
hash/index.ts[1] hash with string representation 111.28 ±0.44% 55.73% slower
hash/index.ts[2] hash with symbol representation 176.54 ±0.5% 29.77% slower
hash/index.ts[3] hash with uint8 representation 161.5 ±0.29% 35.76% slower
hash/index.ts[4] hash with packed representation 251.39 ±0.76% fastest ✅
hash/index.ts[5] hash with bigint representation 180.63 ±0.67% 28.15% slower
hash/index.ts[6] hash with uint32 representation 192.66 ±0.63% 23.36% slower
logger/index.ts[0] console.log with string concat 6719595.34 ±35.02% fastest ✅
logger/index.ts[1] console.log with args 1504674.9 ±86.1% 77.61% slower
math/add_one_overflow.ts[0] add and take modulus 218509069.79 ±6.39% 5.59% slower
math/add_one_overflow.ts[1] condition before calculation 231438356.6 ±6% fastest ✅
math/count-bits-u32.ts[0] standard method 83510864.25 ±1.59% 61.88% slower
math/count-bits-u32.ts[1] magic 219088677.7 ±5.59% fastest ✅
math/count-bits-u64.ts[0] standard method 2682247.43 ±4.34% 95.8% slower
math/count-bits-u64.ts[1] magic 63916804.67 ±2.88% fastest ✅
math/mul_overflow.ts[0] multiply and bring back to u32 242804356.06 ±5.68% fastest ✅
math/mul_overflow.ts[1] multiply and take modulus 239800142.44 ±5.85% 1.24% slower
math/switch.ts[0] switch 224072382.64 ±7.15% 0.37% slower
math/switch.ts[1] if 224898971.71 ±6.08% fastest ✅
codec/view_vs_collection.ts[0] Get first element from Decoded 19311.82 ±2.47% 64.4% slower
codec/view_vs_collection.ts[1] Get first element from View 54250.39 ±0.49% fastest ✅
codec/view_vs_collection.ts[2] Get 50th element from Decoded 19874 ±2.52% 63.37% slower
codec/view_vs_collection.ts[3] Get 50th element from View 26803.94 ±1.36% 50.59% slower
codec/view_vs_collection.ts[4] Get last element from Decoded 23769.6 ±4.03% 56.19% slower
codec/view_vs_collection.ts[5] Get last element from View 24740.51 ±3.35% 54.4% slower
codec/view_vs_object.ts[0] Get the first field from Decoded 343975.65 ±1.32% 5.98% slower
codec/view_vs_object.ts[1] Get the first field from View 87480.18 ±1.44% 76.09% slower
codec/view_vs_object.ts[2] Get the first field as view from View 79821.78 ±1.51% 78.18% slower
codec/view_vs_object.ts[3] Get two fields from Decoded 365859.38 ±3.66% fastest ✅
codec/view_vs_object.ts[4] Get two fields from View 81942.82 ±0.25% 77.6% slower
codec/view_vs_object.ts[5] Get two fields from materialized from View 167860.78 ±0.97% 54.12% slower
codec/view_vs_object.ts[6] Get two fields as views from View 66333.76 ±1.36% 81.87% slower
codec/view_vs_object.ts[7] Get only third field from Decoded 319215.65 ±2.09% 12.75% slower
codec/view_vs_object.ts[8] Get only third field from View 85714.96 ±1.35% 76.57% slower
codec/view_vs_object.ts[9] Get only third field as view from View 81648.14 ±1.77% 77.68% slower
bytes/compare.ts[0] Comparing Uint32 bytes 21057.19 ±0.11% 4.83% slower
bytes/compare.ts[1] Comparing raw bytes 22124.93 ±0.09% fastest ✅
hash/blake2b.ts[0] hasher with simple allocator 0.0453 ±0.29% 0.22% slower
hash/blake2b.ts[1] hasher with page allocator 0.0454 ±0.07% fastest ✅
collections/map_vs_sorted.ts[0] Map 311907.19 ±0.05% fastest ✅
collections/map_vs_sorted.ts[1] Map-array 103685.58 ±0.02% 66.76% slower
collections/map_vs_sorted.ts[2] Array 73377.58 ±0.16% 76.47% slower
collections/map_vs_sorted.ts[3] SortedArray 198739.51 ±0.24% 36.28% slower
crypto/ed25519.ts[0] native crypto 6.03 ±16.05% 79.76% slower
crypto/ed25519.ts[1] wasm lib 10.64 ±0.02% 64.28% slower
crypto/ed25519.ts[2] wasm lib batch 29.79 ±0.57% fastest ✅

Benchmarks summary: 63/63 OK ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants