Skip to content

Commit

Permalink
Merge pull request #87 from algorandfoundation/fix/arc4-struct-immutable
Browse files Browse the repository at this point in the history
fix: ARC4 structs should be immutable for now, we can introduce a mut…
  • Loading branch information
tristanmenzel authored Jan 21, 2025
2 parents a20212c + 31f5421 commit 32892bd
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ jobs:
#Requires permissions.checks: write
- name: Publish test results
if: ${{ inputs.output-test-results && always() }}
uses: phoenix-actions/test-reporting@v10
uses: phoenix-actions/test-reporting@v15
with:
name: Test results
path: ${{ inputs.test-results-file-pattern }}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/algo-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@algorandfoundation/algorand-typescript",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "This package contains definitions for the types which comprise Algorand TypeScript which can be compiled to run on the Algorand Virtual Machine using the Puya compiler.",
"private": false,
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/algo-ts/src/arc4/encoded-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class StructImpl<T extends StructConstraint> extends StructBase {
}
}

type StructConstructor = new <T extends StructConstraint>(initial: T) => StructBase & T
type StructConstructor = new <T extends StructConstraint>(initial: T) => StructBase & Readonly<T>

export const Struct = StructImpl as StructConstructor

Expand Down
1 change: 1 addition & 0 deletions src/awst_build/arc4-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function ptypeToArc4EncodedType(ptype: PType, sourceLocation: SourceLocat
module: ptype.module,
description: ptype.description,
fields: Object.fromEntries(ptype.orderedProperties().map(([p, pt]) => [p, ptypeToArc4EncodedType(pt, sourceLocation)])),
frozen: true,
})

throw new CodeError(`${ptype} cannot be encoded to an ARC4 type`, { sourceLocation })
Expand Down
1 change: 1 addition & 0 deletions src/awst_build/eb/arc28/arc-28-emit-function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class Arc28EmitFunctionBuilder extends FunctionBuilder {
fields,
description: undefined,
sourceLocation,
frozen: true,
})
const structExpression = nodeFactory.newStruct({
wtype: structType.wtype,
Expand Down
7 changes: 6 additions & 1 deletion src/awst_build/ptypes/arc4-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,26 @@ export class ARC4StructType extends ARC4EncodedType {
readonly singleton = false
readonly fields: Record<string, ARC4EncodedType>
readonly sourceLocation: SourceLocation | undefined
readonly frozen: boolean
constructor({
name,
frozen,
module,
fields,
description,
sourceLocation,
}: {
name: string
module: string
frozen: boolean
description: string | undefined
fields: Record<string, ARC4EncodedType>
sourceLocation?: SourceLocation
}) {
super()
this.name = name
this.module = module
this.frozen = frozen
this.fields = fields
this.description = description
this.sourceLocation = sourceLocation
Expand All @@ -162,7 +166,7 @@ export class ARC4StructType extends ARC4EncodedType {
fields: Object.fromEntries(Object.entries(this.fields).map(([f, t]) => [f, t.wtype])),
sourceLocation: this.sourceLocation,
desc: this.description ?? null,
frozen: false,
frozen: this.frozen,
})
}

Expand All @@ -189,6 +193,7 @@ export const arc4StructBaseType = new ARC4StructType({
module: Constants.arc4EncodedTypesModuleName,
fields: {},
description: undefined,
frozen: true,
})

export const Arc4TupleClass = new LibClassType({
Expand Down
1 change: 1 addition & 0 deletions src/awst_build/type-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export class TypeResolver {
fields: fields,
sourceLocation: sourceLocation,
description: tryGetTypeDescription(tsType),
frozen: baseType.frozen,
})
}

Expand Down
3 changes: 1 addition & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export const Constants = {
zeroAddressEncoded: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ',

supportedAvmVersions: [10n, 11n],
mainNetAvmVersion: [10n],
targetedPuyaVersion: '4.1.1',
targetedPuyaVersion: '4.2.0',
} as const

export type SupportedAvmVersion = (typeof Constants.supportedAvmVersions)[number]
61 changes: 45 additions & 16 deletions src/puya/check-puya-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,81 @@ import { Constants } from '../constants'
import { logger } from '../logger'
import { runPuya } from './run-puya'

export function checkPuyaVersion() {
export enum VersionCompareVerdict {
ExactMatch = 'ExactMatch',
Inconclusive = 'Inconclusive',
MajorMismatch = 'MajorMismatch',
MinorMismatch = 'MinorMismatch',
RevisionMismatch = 'RevisionMismatch',
}

export function comparePuyaVersion(): {
target: string
found?: string
verdict: VersionCompareVerdict
} {
const target = Constants.targetedPuyaVersion

const versionParser = new VersionParser()
runPuya({
command: 'puya',
args: ['--version'],
onOutput: (line) => versionParser.receiveLine(line),
})

if (versionParser.version) {
const ver = versionParser.version
// Compare
const [major, minor, rev] = Constants.targetedPuyaVersion.split('.').map((x) => Number(x))
if (major !== ver.major || minor !== ver.minor) {
if (!versionParser.version) return { target, verdict: VersionCompareVerdict.Inconclusive }
const ver = versionParser.version

// Compare
const [major, minor, rev] = target.split('.').map((x) => Number(x))
if (ver.major !== major) return { verdict: VersionCompareVerdict.MajorMismatch, target, found: ver.formatted }
if (ver.minor !== minor) return { verdict: VersionCompareVerdict.MinorMismatch, target, found: ver.formatted }
if (ver.rev !== rev) return { verdict: VersionCompareVerdict.RevisionMismatch, target, found: ver.formatted }
return { verdict: VersionCompareVerdict.ExactMatch, target, found: ver.formatted }
}

export function checkPuyaVersion() {
const result = comparePuyaVersion()
switch (result.verdict) {
case VersionCompareVerdict.Inconclusive:
logger.warn(undefined, `Unable to verify installed puya version. Please ensure version ${result.target} is available`)
break
case VersionCompareVerdict.MajorMismatch:
case VersionCompareVerdict.MinorMismatch:
logger.warn(
undefined,
`Installed version of puya (${ver.major}.${ver.minor}.${ver.rev}) does not match targeted version for puya-ts (${Constants.targetedPuyaVersion}). There may be compatability issues.`,
`Installed version of puya (${result.found}) does not match targeted version for puya-ts (${result.target}). There may be compatability issues.`,
)
} else if (rev !== ver.rev) {
break
case VersionCompareVerdict.RevisionMismatch:
logger.debug(
undefined,
`Installed revision of puya (${ver.major}.${ver.minor}.${ver.rev}) does not match targeted revision for puya-ts (${Constants.targetedPuyaVersion})`,
`Installed revision of puya (${result.found}) does not match targeted revision for puya-ts (${Constants.targetedPuyaVersion})`,
)
}
} else {
logger.warn(undefined, `Unable to verify installed puya version. Please ensure version ${Constants.targetedPuyaVersion} is available`)
break
}
}

type SemVer = {
major: number
minor: number
rev: number
formatted: string
}

class VersionParser {
#ver: SemVer | undefined

receiveLine(line: string): void {
const matched = /^puya (\d+)\.(\d+)\.(\d+)$/.exec(line)
const matched = /^puya ((\d+)\.(\d+)\.(\d+))$/.exec(line)
if (!matched) {
logger.debug(undefined, `'puya --version' command returned unexpected output: "${line}"`)
} else {
this.#ver = {
major: Number(matched[1]),
minor: Number(matched[2]),
rev: Number(matched[3]),
formatted: matched[1],
major: Number(matched[2]),
minor: Number(matched[3]),
rev: Number(matched[4]),
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions tests/approvals/out/arc-28-events/arc-28-events.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
}
},
"source_location": null,
"frozen": false,
"frozen": true,
"desc": null
},
"value": {
Expand Down Expand Up @@ -1027,7 +1027,7 @@
}
},
"source_location": null,
"frozen": false,
"frozen": true,
"desc": null
},
"value": {
Expand Down Expand Up @@ -1156,7 +1156,7 @@
"column": 10,
"end_column": 11
},
"frozen": false,
"frozen": true,
"desc": null
},
"name": "y"
Expand Down Expand Up @@ -1219,7 +1219,7 @@
"column": 18,
"end_column": 29
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -1698,7 +1698,7 @@
"column": 9,
"end_column": 10
},
"frozen": false,
"frozen": true,
"desc": null
},
"name": "y"
Expand Down Expand Up @@ -1789,7 +1789,7 @@
"column": 4,
"end_column": 25
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -1935,7 +1935,7 @@
"column": 4,
"end_column": 38
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -2109,7 +2109,7 @@
"column": 4,
"end_column": 60
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -2500,7 +2500,7 @@
"column": 4,
"end_column": 30
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -2710,7 +2710,7 @@
"column": 4,
"end_column": 43
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -3058,7 +3058,7 @@
"column": 4,
"end_column": 35
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@
}
},
"source_location": null,
"frozen": false,
"frozen": true,
"desc": null
},
"value": {
Expand Down Expand Up @@ -1477,7 +1477,7 @@
"column": 43,
"end_column": 53
},
"frozen": false,
"frozen": true,
"desc": null
},
"values": {
Expand Down Expand Up @@ -3101,7 +3101,7 @@
}
},
"source_location": null,
"frozen": false,
"frozen": true,
"desc": null
},
"expr": {
Expand Down Expand Up @@ -3457,7 +3457,7 @@
}
},
"source_location": null,
"frozen": false,
"frozen": true,
"desc": null
},
"expr": {
Expand Down
Loading

0 comments on commit 32892bd

Please sign in to comment.