Skip to content

fix(evolution): encode a JS boolean Bool constant as its own value (#398) - #630

Open
Kshot3000 wants to merge 1 commit into
IntersectMBO:mainfrom
Kshot3000:fix/uplc-bool-constant-encoder
Open

Kshot3000 wants to merge 1 commit into
IntersectMBO:mainfrom
Kshot3000:fix/uplc-bool-constant-encoder

Conversation

@Kshot3000

Copy link
Copy Markdown

Fixes #398.

The bug

ConstantValue permits a plain JS boolean for a Bool constant, but the flat encoder's Bool branch read .index off the value:

const boolVal = value as { index: bigint }
pushBit(state, boolVal.index === 0n ? 0 : 1)

A JS boolean has no .index, so undefined === 0n is false and false was encoded with the True bit. con bool false and con bool true produced identical flat bytes (0100004a21 for a bare constant program), so a script built from a false boolean computed that constant as true.

The fix

The Bool branch of encodeData now handles the primitive form the type advertises, as the issue suggests:

const bit = typeof value === "boolean" ? (value ? 1 : 0) : (value as { index: bigint }).index === 0n ? 0 : 1
pushBit(state, bit)

The decoded { index } form encodes exactly as before, and because the list/pair branches route through the same encodeData, JS booleans nested in a list Bool are fixed by the same change.

Tests

Four new tests in test/UPLC.test.ts, all written against main first:

  • JS false encodes differently from JS true and decodes back to { index: 0n } — on main both encoded to 0100004a21 (RED)
  • JS true decodes back to { index: 1n } (control, passed before and after)
  • JS booleans match the { index } representation byte for byte — on main JS false gave 0100004a21 against the { index: 0n } form's 0100004a01 (RED)
  • JS booleans nested in a list of Bool roundtrip with the right indices (RED on main)

With the fix: UPLC test file 43/43, full package suite 1682 passed / 75 skipped / 0 failed (test files 101 passed / 1 skipped of 102; main baseline 1678 + the 4 new tests), tsc --noEmit and eslint clean. A patch changeset is included.


Independent builder — not affiliated with the Cardano Foundation, IOG or Intersect.

@Kshot9000 (https://x.com/kshot9000) · ADA: addr1q8hnl6vl5a6k3rw3n5g3jtte696zcl76kfatzv7gpswa9r0dj7fma6klq55y4ffm7tf0em09udnyhuk4ah92pl5x9jpqjae44v

cc @cardano-foundation @IntersectMBO

…ntersectMBO#398)

ConstantValue permits a plain JS boolean, but the flat encoder's Bool
branch read .index off the value, so con bool false encoded with the
True bit and produced the same flat bytes as con bool true. Handle the
boolean form in encodeData; the decoded { index } form is unchanged,
and booleans nested in a list of Bool go through the same branch.

Tests: 4 new in UPLC.test.ts (3 RED on main, 1 control). UPLC file
43/43; full suite 1682 passed / 75 skipped / 0 failed.

This branch has not been deployed

No deployments
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.

UPLC: handle JS boolean in the Bool constant encoder

1 participant