@givelotus/compiler 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -2
- package/artifact-v2.schema.json +58 -0
- package/artifact.schema.json +19 -0
- package/dist/ast.d.ts +292 -0
- package/dist/ast.js +150 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +134 -0
- package/dist/call-runtime.d.ts +84 -0
- package/dist/call-runtime.js +249 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +22 -0
- package/dist/compiler.d.ts +79 -0
- package/dist/compiler.js +125 -0
- package/dist/diagnostics.d.ts +163 -0
- package/dist/diagnostics.js +498 -0
- package/dist/ecash.d.ts +41 -0
- package/dist/ecash.js +190 -0
- package/dist/fees.d.ts +164 -0
- package/dist/fees.js +208 -0
- package/dist/fixture-cli.d.ts +2 -0
- package/dist/fixture-cli.js +41 -0
- package/dist/fixtures.d.ts +68 -0
- package/dist/fixtures.js +46 -0
- package/dist/forth-ir.d.ts +73 -0
- package/dist/forth-ir.js +147 -0
- package/dist/generic-cli.d.ts +2 -0
- package/dist/generic-cli.js +140 -0
- package/dist/generic.d.ts +48 -0
- package/dist/generic.js +2566 -0
- package/dist/lints.d.ts +8 -0
- package/dist/lints.js +204 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +7 -0
- package/dist/opcodes.d.ts +154 -0
- package/dist/opcodes.js +162 -0
- package/dist/optimizer.d.ts +58 -0
- package/dist/optimizer.js +786 -0
- package/dist/package-fetch-cli.d.ts +2 -0
- package/dist/package-fetch-cli.js +17 -0
- package/dist/package-lock-cli.d.ts +2 -0
- package/dist/package-lock-cli.js +28 -0
- package/dist/package-sources.d.ts +82 -0
- package/dist/package-sources.js +785 -0
- package/dist/package-update-cli.d.ts +2 -0
- package/dist/package-update-cli.js +20 -0
- package/dist/packages.d.ts +231 -0
- package/dist/packages.js +1307 -0
- package/dist/parser.d.ts +32 -0
- package/dist/parser.js +575 -0
- package/dist/policy.d.ts +83 -0
- package/dist/policy.js +207 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +126 -0
- package/dist/scheduler.d.ts +38 -0
- package/dist/scheduler.js +76 -0
- package/dist/semantic-ir.d.ts +76 -0
- package/dist/semantic-ir.js +231 -0
- package/dist/std.d.ts +27 -0
- package/dist/std.js +225 -0
- package/dist/target-compiler.d.ts +9 -0
- package/dist/target-compiler.js +139 -0
- package/dist/targets.d.ts +53 -0
- package/dist/targets.js +67 -0
- package/dist/toml-subset.d.ts +19 -0
- package/dist/toml-subset.js +81 -0
- package/dist/transaction-runtime.d.ts +182 -0
- package/dist/transaction-runtime.js +646 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +3 -0
- package/dist/vm-cost.d.ts +11 -0
- package/dist/vm-cost.js +541 -0
- package/package.json +71 -4
- package/policy-artifact.schema.json +20 -0
- package/std/custody/arbiter-escrow.lotus +20 -0
- package/std/custody/htlc.lotus +15 -0
- package/std/custody/multisig-2of3.lotus +13 -0
- package/std/custody/single-owner-wallet.lotus +9 -0
- package/std/custody/vault-with-recovery.lotus +14 -0
- package/std/defi/amm-pool-v2.lotus +97 -0
- package/std/defi/indexed-limit-order.lotus +37 -0
- package/std/nft/nft-collection.lotus +51 -0
- package/std/nft/royalty-transfer-rules.lotus +38 -0
- package/std/nft/unique-nft.lotus +30 -0
- package/std/tokens/capped-supply-token.lotus +76 -0
- package/std/tokens/fixed-supply-token.lotus +29 -0
- package/std/tokens/standard-transfer-rules.lotus +15 -0
- package/std/tokens/voucher-mint-token.lotus +31 -0
package/dist/lints.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisory lints over a parsed program. Lints never change compilation: a contract
|
|
3
|
+
* with warnings compiles to the same artifact. Each warning has a stable `W` code.
|
|
4
|
+
*/
|
|
5
|
+
import type { ContractArtifact, Program } from './generic.js';
|
|
6
|
+
import { type Diagnostic } from './diagnostics.js';
|
|
7
|
+
/** Lint the methods of contract `name` in an already compiled program. */
|
|
8
|
+
export declare function lintProgram(program: Program, name: string, file?: string, compiled?: ContractArtifact): Diagnostic[];
|
package/dist/lints.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { at, diagnostic } from './diagnostics.js';
|
|
2
|
+
function walkExpr(expr, visit) {
|
|
3
|
+
if (!expr)
|
|
4
|
+
return;
|
|
5
|
+
visit(expr);
|
|
6
|
+
walkExpr(expr.left, visit);
|
|
7
|
+
walkExpr(expr.right, visit);
|
|
8
|
+
for (const arg of expr.args ?? [])
|
|
9
|
+
walkExpr(arg, visit);
|
|
10
|
+
}
|
|
11
|
+
function walkStmts(body, visit, loops = []) {
|
|
12
|
+
for (const s of body ?? []) {
|
|
13
|
+
visit(s, loops);
|
|
14
|
+
const inner = s.kind === 'for' ? [...loops, s] : loops;
|
|
15
|
+
walkStmts(s.then, visit, inner);
|
|
16
|
+
walkStmts(s.otherwise, visit, inner);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function mentions(expr, name) {
|
|
20
|
+
let found = false;
|
|
21
|
+
walkExpr(expr, e => { if (e.value === name || (e.kind === 'name' && e.value?.startsWith(`${name}.`)))
|
|
22
|
+
found = true; });
|
|
23
|
+
return found;
|
|
24
|
+
}
|
|
25
|
+
const COMPARISONS = new Set(['==', '!=', '<', '>', '<=', '>=']);
|
|
26
|
+
/**
|
|
27
|
+
* W0701: a loop over the outputs compares an output's value with `this.coin.value`, and
|
|
28
|
+
* no loop over the inputs refers to `this.root`. Each input under this policy runs the
|
|
29
|
+
* method on its own coin, so two such coins can share one payout and the other value
|
|
30
|
+
* becomes fee. Count the inputs that carry `this.root` (and require one), or compare the
|
|
31
|
+
* payout with their sum.
|
|
32
|
+
*/
|
|
33
|
+
function isIndexedOutputValue(expr) {
|
|
34
|
+
let found = false;
|
|
35
|
+
walkExpr(expr, e => {
|
|
36
|
+
if (e.kind === 'member' && e.value === 'value' && e.left?.kind === 'index' && e.left.left?.kind === 'name' && e.left.left.value === 'tx.outputs') {
|
|
37
|
+
found = true;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return found;
|
|
41
|
+
}
|
|
42
|
+
function payoutMerge(method, file) {
|
|
43
|
+
let payout, bound = false;
|
|
44
|
+
walkStmts(method.body, (s, loops) => {
|
|
45
|
+
const outputLoops = loops.filter(l => l.collection === 'tx.outputs').map(l => l.variable);
|
|
46
|
+
const inputLoop = loops.some(l => l.collection === 'tx.inputs');
|
|
47
|
+
const exprs = [s.expr];
|
|
48
|
+
for (const e of exprs) {
|
|
49
|
+
if (inputLoop && mentions(e, 'this.root'))
|
|
50
|
+
bound = true;
|
|
51
|
+
walkExpr(e, x => {
|
|
52
|
+
if (payout || x.kind !== 'binary' || !COMPARISONS.has(x.value))
|
|
53
|
+
return;
|
|
54
|
+
const value = (side) => outputLoops.some(v => mentions(side, `${v}.value`)) || isIndexedOutputValue(side);
|
|
55
|
+
const coin = (side) => mentions(side, 'this.coin.value');
|
|
56
|
+
if ((value(x.left) && coin(x.right)) || (value(x.right) && coin(x.left)))
|
|
57
|
+
payout = x;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (!payout || bound)
|
|
62
|
+
return undefined;
|
|
63
|
+
return diagnostic('W0701', `method ${method.name} compares an output value with this.coin.value, but no input loop refers to this.root`, at(payout.span, file), { severity: 'warning',
|
|
64
|
+
notes: ['each input under this policy runs the method against its own coin, so two such coins can share one payout and the rest becomes fee'],
|
|
65
|
+
help: 'count the spend inputs whose coin requires this.root and require exactly one, or compare the payout with their summed value' });
|
|
66
|
+
}
|
|
67
|
+
/** W0704: division or modulo by a dynamic identifier without an obvious non-zero guard. */
|
|
68
|
+
function unguardedDivisors(method, file) {
|
|
69
|
+
const out = [];
|
|
70
|
+
const extractGuards = (expr, target) => {
|
|
71
|
+
if (!expr || expr.kind !== 'binary')
|
|
72
|
+
return;
|
|
73
|
+
if (expr.value === '&&') {
|
|
74
|
+
extractGuards(expr.left, target);
|
|
75
|
+
extractGuards(expr.right, target);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if ((expr.value === '>' || expr.value === '>=' || expr.value === '!=') && expr.left?.kind === 'name' && expr.right?.kind === 'number') {
|
|
79
|
+
const n = BigInt(expr.right.value);
|
|
80
|
+
if ((expr.value === '>' && n >= 0n) || (expr.value === '>=' && n >= 1n) || (expr.value === '!=' && n === 0n)) {
|
|
81
|
+
target.add(expr.left.value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if ((expr.value === '<' || expr.value === '<=' || expr.value === '!=') && expr.right?.kind === 'name' && expr.left?.kind === 'number') {
|
|
85
|
+
const n = BigInt(expr.left.value);
|
|
86
|
+
if ((expr.value === '<' && n >= 0n) || (expr.value === '<=' && n >= 1n) || (expr.value === '!=' && n === 0n)) {
|
|
87
|
+
target.add(expr.right.value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const checkExpr = (expr, guarded) => {
|
|
92
|
+
if (!expr)
|
|
93
|
+
return;
|
|
94
|
+
if (expr.kind === 'binary' && expr.value === '&&') {
|
|
95
|
+
checkExpr(expr.left, guarded);
|
|
96
|
+
const next = new Set(guarded);
|
|
97
|
+
extractGuards(expr.left, next);
|
|
98
|
+
checkExpr(expr.right, next);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (expr.kind === 'binary' && (expr.value === '/' || expr.value === '%') && expr.right?.kind === 'name' && !guarded.has(expr.right.value)) {
|
|
102
|
+
out.push(diagnostic('W0704', `method ${method.name} uses '${expr.value}' with dynamic divisor ${expr.right.value} without a prior non-zero require`, at(expr.right.span, file), { severity: 'warning',
|
|
103
|
+
notes: ['division or modulo by zero aborts script execution at runtime'],
|
|
104
|
+
help: `guard it with require(${expr.right.value} > 0); or require(${expr.right.value} != 0); before dividing` }));
|
|
105
|
+
}
|
|
106
|
+
checkExpr(expr.left, guarded);
|
|
107
|
+
checkExpr(expr.right, guarded);
|
|
108
|
+
for (const arg of expr.args ?? [])
|
|
109
|
+
checkExpr(arg, guarded);
|
|
110
|
+
};
|
|
111
|
+
const walkBlock = (body, incoming) => {
|
|
112
|
+
const guarded = new Set(incoming);
|
|
113
|
+
for (const s of body ?? []) {
|
|
114
|
+
if (s.kind === 'require') {
|
|
115
|
+
checkExpr(s.expr, guarded);
|
|
116
|
+
extractGuards(s.expr, guarded);
|
|
117
|
+
}
|
|
118
|
+
else if (s.kind === 'if') {
|
|
119
|
+
checkExpr(s.expr, guarded);
|
|
120
|
+
const thenGuarded = new Set(guarded);
|
|
121
|
+
extractGuards(s.expr, thenGuarded);
|
|
122
|
+
walkBlock(s.then, thenGuarded);
|
|
123
|
+
walkBlock(s.otherwise, guarded);
|
|
124
|
+
}
|
|
125
|
+
else if (s.kind === 'for') {
|
|
126
|
+
walkBlock(s.then, guarded);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
checkExpr(s.expr, guarded);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
walkBlock(method.body, new Set());
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
// Prove simple conditions that are true for every well-formed argument. A
|
|
137
|
+
// condition may vary without failing, but calls and arithmetic can abort.
|
|
138
|
+
function booleanResult(expr, numericArgs) {
|
|
139
|
+
if (expr.kind === 'bool')
|
|
140
|
+
return expr.value === 'true';
|
|
141
|
+
if (expr.kind !== 'binary')
|
|
142
|
+
return undefined;
|
|
143
|
+
const left = expr.left, right = expr.right;
|
|
144
|
+
const numeric = (term) => term.kind === 'number' || (term.kind === 'name' && numericArgs.has(term.value));
|
|
145
|
+
if (COMPARISONS.has(expr.value) && numeric(left) && numeric(right))
|
|
146
|
+
return 'varying';
|
|
147
|
+
const isOr = expr.value === '||';
|
|
148
|
+
if (!isOr && expr.value !== '&&')
|
|
149
|
+
return undefined;
|
|
150
|
+
const a = booleanResult(left, numericArgs);
|
|
151
|
+
if (a === undefined || a === isOr)
|
|
152
|
+
return a;
|
|
153
|
+
const b = booleanResult(right, numericArgs);
|
|
154
|
+
if (a !== 'varying' || b === isOr || b === undefined)
|
|
155
|
+
return b;
|
|
156
|
+
return 'varying';
|
|
157
|
+
}
|
|
158
|
+
/** W0702: a method has no condition that restricts spending or minting. */
|
|
159
|
+
function alwaysSucceeds(method, mint, compiled, file) {
|
|
160
|
+
const output = compiled?.methods.find(m => m.name === method.name);
|
|
161
|
+
const ops = output?.sourceMap.map(entry => entry.op);
|
|
162
|
+
// LC-1 checks the invocation kind before the user's code. The guard has
|
|
163
|
+
// already succeeded in the only kind this method can be called in.
|
|
164
|
+
const invocationGuard = ['INVOCATIONKIND', mint ? 'N(3)' : 'N(1)', 'NUMEQUAL', 'VERIFY'];
|
|
165
|
+
const guarded = compiled?.languageVersion === 1 && invocationGuard.every((op, i) => ops?.[i] === op);
|
|
166
|
+
const body = guarded ? ops?.slice(invocationGuard.length) : ops;
|
|
167
|
+
const cleanupOnly = body?.at(-1) === 'N(1)' && body.slice(0, -1).every(op => op === 'DROP');
|
|
168
|
+
const numericArgs = new Set(method.params.filter(p => p.type === 'Int' || p.type === 'Amount').map(p => p.name));
|
|
169
|
+
const unconditional = method.body.every(stmt => stmt.kind === 'require' && booleanResult(stmt.expr, numericArgs) === true);
|
|
170
|
+
if (!cleanupOnly && !unconditional)
|
|
171
|
+
return undefined;
|
|
172
|
+
return diagnostic('W0702', `method ${method.name} always succeeds: anyone can ${mint ? 'mint this token' : 'spend a coin under it'}`, at(method.span, file), { severity: 'warning',
|
|
173
|
+
notes: ['no method requirement can reject an invocation with well-formed arguments'],
|
|
174
|
+
help: 'require a signature, a preimage or a transaction condition that a spender must meet' });
|
|
175
|
+
}
|
|
176
|
+
/** W0703: a Signature argument that nothing in the method reads. */
|
|
177
|
+
function unusedSignatures(method, file) {
|
|
178
|
+
const names = new Set();
|
|
179
|
+
walkStmts(method.body, s => walkExpr(s.expr, e => {
|
|
180
|
+
if (e.kind === 'name')
|
|
181
|
+
names.add(e.value);
|
|
182
|
+
}));
|
|
183
|
+
return method.params.filter(p => p.type === 'Signature' && !names.has(p.name)).map(p => diagnostic('W0703', `method ${method.name} never uses its Signature argument ${p.name}`, at(p.span, file), { severity: 'warning',
|
|
184
|
+
notes: ['the spender must still supply it, and nothing checks what they supply'],
|
|
185
|
+
help: `check it with checksig(${p.name}, key), or remove the argument` }));
|
|
186
|
+
}
|
|
187
|
+
/** Lint the methods of contract `name` in an already compiled program. */
|
|
188
|
+
export function lintProgram(program, name, file = '<source>', compiled) {
|
|
189
|
+
const contract = program.contracts.get(name);
|
|
190
|
+
if (!contract)
|
|
191
|
+
return [];
|
|
192
|
+
const out = [];
|
|
193
|
+
for (const method of contract.methods) {
|
|
194
|
+
const merge = payoutMerge(method, file);
|
|
195
|
+
if (merge)
|
|
196
|
+
out.push(merge);
|
|
197
|
+
const open = alwaysSucceeds(method, contract.kind === 'token', compiled, file);
|
|
198
|
+
if (open)
|
|
199
|
+
out.push(open);
|
|
200
|
+
out.push(...unusedSignatures(method, file));
|
|
201
|
+
out.push(...unguardedDivisors(method, file));
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
package/dist/names.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function assertPortableName(name: string): string;
|
package/dist/names.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// One portable surface namespace for generated TypeScript and Python clients.
|
|
2
|
+
const RESERVED = new Set(`and as assert async await break case catch class const continue debugger default def del delete do elif else enum except export extends False false finally for from function global if import in instanceof interface is lambda let new nonlocal not null None of or package pass private protected public raise return self static super switch this throw True true try type typeof var void while with yield constructor`.split(/\s+/));
|
|
3
|
+
export function assertPortableName(name) {
|
|
4
|
+
if (!/^[A-Za-z_][A-Za-z_0-9]*$/.test(name) || RESERVED.has(name))
|
|
5
|
+
throw new Error(`reserved or invalid binding name ${name}`);
|
|
6
|
+
return name;
|
|
7
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical P03 VM opcode registry for Lotus V2, aligned with
|
|
3
|
+
* `docs/lotus-v2/tools/opcodes.json` and `daemon/lotusd/src/lotus_v2/vm.cpp`.
|
|
4
|
+
*
|
|
5
|
+
* Single source of truth for bytecode values, static stack effects, and
|
|
6
|
+
* symbolic-optimizer eligibility.
|
|
7
|
+
*/
|
|
8
|
+
export type OpcodeGroup = 'control' | 'stack' | 'bytes' | 'arithmetic' | 'cryptography' | 'transaction' | 'input' | 'output' | 'invocation_and_locks';
|
|
9
|
+
export type OpcodeSpec = {
|
|
10
|
+
readonly byte: number;
|
|
11
|
+
readonly group: OpcodeGroup;
|
|
12
|
+
/** Number of stack items popped, or null if dynamic/control-dependent. */
|
|
13
|
+
readonly pops: number | null;
|
|
14
|
+
/** Number of stack items pushed, or null if dynamic/control-dependent. */
|
|
15
|
+
readonly pushes: number | null;
|
|
16
|
+
/** Net stack delta (`pushes - pops`), or null if dynamic/control-dependent. */
|
|
17
|
+
readonly delta: number | null;
|
|
18
|
+
};
|
|
19
|
+
export declare const P03_OPCODES: {
|
|
20
|
+
readonly NOP: OpcodeSpec;
|
|
21
|
+
readonly IF: OpcodeSpec;
|
|
22
|
+
readonly NOTIF: OpcodeSpec;
|
|
23
|
+
readonly ELSE: OpcodeSpec;
|
|
24
|
+
readonly ENDIF: OpcodeSpec;
|
|
25
|
+
readonly VERIFY: OpcodeSpec;
|
|
26
|
+
readonly RETURN: OpcodeSpec;
|
|
27
|
+
readonly ENUMERATE: OpcodeSpec;
|
|
28
|
+
readonly ENDENUMERATE: OpcodeSpec;
|
|
29
|
+
readonly BREAK: OpcodeSpec;
|
|
30
|
+
readonly EMITLOG: OpcodeSpec;
|
|
31
|
+
readonly COND_BREAK: OpcodeSpec;
|
|
32
|
+
readonly TOALTSTACK: OpcodeSpec;
|
|
33
|
+
readonly FROMALTSTACK: OpcodeSpec;
|
|
34
|
+
readonly '2DROP': OpcodeSpec;
|
|
35
|
+
readonly '2DUP': OpcodeSpec;
|
|
36
|
+
readonly '3DUP': OpcodeSpec;
|
|
37
|
+
readonly '2OVER': OpcodeSpec;
|
|
38
|
+
readonly '2ROT': OpcodeSpec;
|
|
39
|
+
readonly '2SWAP': OpcodeSpec;
|
|
40
|
+
readonly DEPTH: OpcodeSpec;
|
|
41
|
+
readonly DROP: OpcodeSpec;
|
|
42
|
+
readonly DUP: OpcodeSpec;
|
|
43
|
+
readonly NIP: OpcodeSpec;
|
|
44
|
+
readonly OVER: OpcodeSpec;
|
|
45
|
+
readonly PICK: OpcodeSpec;
|
|
46
|
+
readonly ROLL: OpcodeSpec;
|
|
47
|
+
readonly ROT: OpcodeSpec;
|
|
48
|
+
readonly SWAP: OpcodeSpec;
|
|
49
|
+
readonly TUCK: OpcodeSpec;
|
|
50
|
+
readonly CAT: OpcodeSpec;
|
|
51
|
+
readonly SPLIT: OpcodeSpec;
|
|
52
|
+
readonly NUM2BIN: OpcodeSpec;
|
|
53
|
+
readonly BIN2NUM: OpcodeSpec;
|
|
54
|
+
readonly SIZE: OpcodeSpec;
|
|
55
|
+
readonly INVERT: OpcodeSpec;
|
|
56
|
+
readonly AND: OpcodeSpec;
|
|
57
|
+
readonly OR: OpcodeSpec;
|
|
58
|
+
readonly XOR: OpcodeSpec;
|
|
59
|
+
readonly EQUAL: OpcodeSpec;
|
|
60
|
+
readonly EQUALVERIFY: OpcodeSpec;
|
|
61
|
+
readonly LESSSWAP: OpcodeSpec;
|
|
62
|
+
readonly RAWLEFTBITSHIFT: OpcodeSpec;
|
|
63
|
+
readonly '1ADD': OpcodeSpec;
|
|
64
|
+
readonly '1SUB': OpcodeSpec;
|
|
65
|
+
readonly NEGATE: OpcodeSpec;
|
|
66
|
+
readonly ABS: OpcodeSpec;
|
|
67
|
+
readonly NOT: OpcodeSpec;
|
|
68
|
+
readonly '0NOTEQUAL': OpcodeSpec;
|
|
69
|
+
readonly ADD: OpcodeSpec;
|
|
70
|
+
readonly SUB: OpcodeSpec;
|
|
71
|
+
readonly MUL: OpcodeSpec;
|
|
72
|
+
readonly DIV: OpcodeSpec;
|
|
73
|
+
readonly MOD: OpcodeSpec;
|
|
74
|
+
readonly MULPOW2: OpcodeSpec;
|
|
75
|
+
readonly BOOLAND: OpcodeSpec;
|
|
76
|
+
readonly BOOLOR: OpcodeSpec;
|
|
77
|
+
readonly NUMEQUAL: OpcodeSpec;
|
|
78
|
+
readonly NUMEQUALVERIFY: OpcodeSpec;
|
|
79
|
+
readonly NUMNOTEQUAL: OpcodeSpec;
|
|
80
|
+
readonly LESSTHAN: OpcodeSpec;
|
|
81
|
+
readonly GREATERTHAN: OpcodeSpec;
|
|
82
|
+
readonly LESSTHANOREQUAL: OpcodeSpec;
|
|
83
|
+
readonly GREATERTHANOREQUAL: OpcodeSpec;
|
|
84
|
+
readonly MIN: OpcodeSpec;
|
|
85
|
+
readonly MAX: OpcodeSpec;
|
|
86
|
+
readonly WITHIN: OpcodeSpec;
|
|
87
|
+
readonly ADDCARRY: OpcodeSpec;
|
|
88
|
+
readonly SUBCARRY: OpcodeSpec;
|
|
89
|
+
readonly MUL128_WIDE: OpcodeSpec;
|
|
90
|
+
readonly LOG: OpcodeSpec;
|
|
91
|
+
readonly SQRT: OpcodeSpec;
|
|
92
|
+
readonly SHA256: OpcodeSpec;
|
|
93
|
+
readonly HASH256: OpcodeSpec;
|
|
94
|
+
readonly BLAKE3: OpcodeSpec;
|
|
95
|
+
readonly CHECKSIG: OpcodeSpec;
|
|
96
|
+
readonly CHECKSIGVERIFY: OpcodeSpec;
|
|
97
|
+
readonly CHECKDATASIG: OpcodeSpec;
|
|
98
|
+
readonly LEAFHASH: OpcodeSpec;
|
|
99
|
+
readonly NODEHASH: OpcodeSpec;
|
|
100
|
+
readonly MERKLEROOT_SORTED: OpcodeSpec;
|
|
101
|
+
readonly MERKLEROOT_INDEXED: OpcodeSpec;
|
|
102
|
+
readonly SIGHASH: OpcodeSpec;
|
|
103
|
+
readonly STATEHASH: OpcodeSpec;
|
|
104
|
+
readonly TXVERSION: OpcodeSpec;
|
|
105
|
+
readonly TXINPUTCOUNT: OpcodeSpec;
|
|
106
|
+
readonly TXOUTPUTCOUNT: OpcodeSpec;
|
|
107
|
+
readonly TXMINTCOUNT: OpcodeSpec;
|
|
108
|
+
readonly TXLOCKTIME: OpcodeSpec;
|
|
109
|
+
readonly TXLOCKKIND: OpcodeSpec;
|
|
110
|
+
readonly TXID: OpcodeSpec;
|
|
111
|
+
readonly CHAINID: OpcodeSpec;
|
|
112
|
+
readonly INPUTINDEX: OpcodeSpec;
|
|
113
|
+
readonly INPUTFLAGS: OpcodeSpec;
|
|
114
|
+
readonly OUTPOINTTXID: OpcodeSpec;
|
|
115
|
+
readonly OUTPOINTINDEX: OpcodeSpec;
|
|
116
|
+
readonly INPUTSEQUENCENUMBER: OpcodeSpec;
|
|
117
|
+
readonly INPUTPATHINDEX: OpcodeSpec;
|
|
118
|
+
readonly UTXOVALUE: OpcodeSpec;
|
|
119
|
+
readonly UTXOTOKENID: OpcodeSpec;
|
|
120
|
+
readonly UTXOBRANCHCOUNT: OpcodeSpec;
|
|
121
|
+
readonly UTXOBRANCH: OpcodeSpec;
|
|
122
|
+
readonly UTXOPATHCOUNT: OpcodeSpec;
|
|
123
|
+
readonly UTXOPATH: OpcodeSpec;
|
|
124
|
+
readonly UTXOOUTPUT: OpcodeSpec;
|
|
125
|
+
readonly UTXOREQUIRESBRANCH: OpcodeSpec;
|
|
126
|
+
readonly UTXOFLAGS: OpcodeSpec;
|
|
127
|
+
readonly INPUTWITNESSARG: OpcodeSpec;
|
|
128
|
+
readonly OUTPUTFLAGS: OpcodeSpec;
|
|
129
|
+
readonly OUTPUTVALUE: OpcodeSpec;
|
|
130
|
+
readonly OUTPUTTOKENID: OpcodeSpec;
|
|
131
|
+
readonly OUTPUTBRANCHCOUNT: OpcodeSpec;
|
|
132
|
+
readonly OUTPUTBRANCH: OpcodeSpec;
|
|
133
|
+
readonly OUTPUTPATHCOUNT: OpcodeSpec;
|
|
134
|
+
readonly OUTPUTPATH: OpcodeSpec;
|
|
135
|
+
readonly OUTPUTRECORD: OpcodeSpec;
|
|
136
|
+
readonly OUTPUTREQUIRESBRANCH: OpcodeSpec;
|
|
137
|
+
readonly OUTPUTEXCLUSIVEBRANCH: OpcodeSpec;
|
|
138
|
+
readonly OUTPUTCONTINUESBRANCH: OpcodeSpec;
|
|
139
|
+
readonly ENFORCEOUTPUT: OpcodeSpec;
|
|
140
|
+
readonly TXTOKENBALANCE: OpcodeSpec;
|
|
141
|
+
readonly STATEVERIFY: OpcodeSpec;
|
|
142
|
+
readonly UTXOSTATEROOT: OpcodeSpec;
|
|
143
|
+
readonly OUTPUTSTATEROOT: OpcodeSpec;
|
|
144
|
+
readonly ACTIVEBYTECODE: OpcodeSpec;
|
|
145
|
+
readonly INVOCATIONKIND: OpcodeSpec;
|
|
146
|
+
readonly BRANCHROOT: OpcodeSpec;
|
|
147
|
+
readonly CHECKLOCKTIMEVERIFY: OpcodeSpec;
|
|
148
|
+
readonly CHECKSEQUENCEVERIFY: OpcodeSpec;
|
|
149
|
+
readonly ACTIVEBRANCHINDEX: OpcodeSpec;
|
|
150
|
+
readonly ACTIVEWITNESSINDEX: OpcodeSpec;
|
|
151
|
+
};
|
|
152
|
+
export type P03OpcodeName = keyof typeof P03_OPCODES;
|
|
153
|
+
export declare function opcodeByte(name: P03OpcodeName): number;
|
|
154
|
+
export declare function opcodeSpec(name: string): OpcodeSpec | undefined;
|
package/dist/opcodes.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical P03 VM opcode registry for Lotus V2, aligned with
|
|
3
|
+
* `docs/lotus-v2/tools/opcodes.json` and `daemon/lotusd/src/lotus_v2/vm.cpp`.
|
|
4
|
+
*
|
|
5
|
+
* Single source of truth for bytecode values, static stack effects, and
|
|
6
|
+
* symbolic-optimizer eligibility.
|
|
7
|
+
*/
|
|
8
|
+
const op = (byte, group, pops, pushes) => ({
|
|
9
|
+
byte,
|
|
10
|
+
group,
|
|
11
|
+
pops,
|
|
12
|
+
pushes,
|
|
13
|
+
delta: pops !== null && pushes !== null ? pushes - pops : null,
|
|
14
|
+
});
|
|
15
|
+
export const P03_OPCODES = {
|
|
16
|
+
// Control
|
|
17
|
+
NOP: op(0x60, 'control', 0, 0),
|
|
18
|
+
IF: op(0x61, 'control', 1, 0),
|
|
19
|
+
NOTIF: op(0x62, 'control', 1, 0),
|
|
20
|
+
ELSE: op(0x63, 'control', 0, 0),
|
|
21
|
+
ENDIF: op(0x64, 'control', 0, 0),
|
|
22
|
+
VERIFY: op(0x65, 'control', 1, 0),
|
|
23
|
+
RETURN: op(0x66, 'control', 0, 0),
|
|
24
|
+
ENUMERATE: op(0x69, 'control', 1, 1),
|
|
25
|
+
ENDENUMERATE: op(0x6a, 'control', 0, 0),
|
|
26
|
+
BREAK: op(0x6b, 'control', 2, 0),
|
|
27
|
+
EMITLOG: op(0x6c, 'control', null, 0),
|
|
28
|
+
COND_BREAK: op(0x6d, 'control', 1, 0),
|
|
29
|
+
// Stack
|
|
30
|
+
TOALTSTACK: op(0x70, 'stack', 1, 0),
|
|
31
|
+
FROMALTSTACK: op(0x71, 'stack', 0, 1),
|
|
32
|
+
'2DROP': op(0x72, 'stack', 2, 0),
|
|
33
|
+
'2DUP': op(0x73, 'stack', 2, 4),
|
|
34
|
+
'3DUP': op(0x74, 'stack', 3, 6),
|
|
35
|
+
'2OVER': op(0x75, 'stack', 4, 6),
|
|
36
|
+
'2ROT': op(0x76, 'stack', 6, 6),
|
|
37
|
+
'2SWAP': op(0x77, 'stack', 4, 4),
|
|
38
|
+
DEPTH: op(0x78, 'stack', 0, 1),
|
|
39
|
+
DROP: op(0x79, 'stack', 1, 0),
|
|
40
|
+
DUP: op(0x7a, 'stack', 1, 2),
|
|
41
|
+
NIP: op(0x7b, 'stack', 2, 1),
|
|
42
|
+
OVER: op(0x7c, 'stack', 2, 3),
|
|
43
|
+
PICK: op(0x7d, 'stack', 1, 1),
|
|
44
|
+
ROLL: op(0x7e, 'stack', 1, 0),
|
|
45
|
+
ROT: op(0x7f, 'stack', 3, 3),
|
|
46
|
+
SWAP: op(0x80, 'stack', 2, 2),
|
|
47
|
+
TUCK: op(0x81, 'stack', 2, 3),
|
|
48
|
+
// Bytes & bitwise
|
|
49
|
+
CAT: op(0x88, 'bytes', 2, 1),
|
|
50
|
+
SPLIT: op(0x89, 'bytes', 2, 2),
|
|
51
|
+
NUM2BIN: op(0x8a, 'bytes', 2, 1),
|
|
52
|
+
BIN2NUM: op(0x8b, 'bytes', 1, 1),
|
|
53
|
+
SIZE: op(0x8c, 'bytes', 1, 2),
|
|
54
|
+
INVERT: op(0x8d, 'bytes', 1, 1),
|
|
55
|
+
AND: op(0x8e, 'bytes', 2, 1),
|
|
56
|
+
OR: op(0x8f, 'bytes', 2, 1),
|
|
57
|
+
XOR: op(0x90, 'bytes', 2, 1),
|
|
58
|
+
EQUAL: op(0x91, 'bytes', 2, 1),
|
|
59
|
+
EQUALVERIFY: op(0x92, 'bytes', 2, 0),
|
|
60
|
+
LESSSWAP: op(0x93, 'bytes', 2, 2),
|
|
61
|
+
RAWLEFTBITSHIFT: op(0x94, 'bytes', 2, 1),
|
|
62
|
+
// Arithmetic
|
|
63
|
+
'1ADD': op(0x98, 'arithmetic', 1, 1),
|
|
64
|
+
'1SUB': op(0x99, 'arithmetic', 1, 1),
|
|
65
|
+
NEGATE: op(0x9a, 'arithmetic', 1, 1),
|
|
66
|
+
ABS: op(0x9b, 'arithmetic', 1, 1),
|
|
67
|
+
NOT: op(0x9c, 'arithmetic', 1, 1),
|
|
68
|
+
'0NOTEQUAL': op(0x9d, 'arithmetic', 1, 1),
|
|
69
|
+
ADD: op(0x9e, 'arithmetic', 2, 1),
|
|
70
|
+
SUB: op(0x9f, 'arithmetic', 2, 1),
|
|
71
|
+
MUL: op(0xa0, 'arithmetic', 2, 1),
|
|
72
|
+
DIV: op(0xa1, 'arithmetic', 2, 1),
|
|
73
|
+
MOD: op(0xa2, 'arithmetic', 2, 1),
|
|
74
|
+
MULPOW2: op(0xa3, 'arithmetic', 2, 1),
|
|
75
|
+
BOOLAND: op(0xa4, 'arithmetic', 2, 1),
|
|
76
|
+
BOOLOR: op(0xa5, 'arithmetic', 2, 1),
|
|
77
|
+
NUMEQUAL: op(0xa6, 'arithmetic', 2, 1),
|
|
78
|
+
NUMEQUALVERIFY: op(0xa7, 'arithmetic', 2, 0),
|
|
79
|
+
NUMNOTEQUAL: op(0xa8, 'arithmetic', 2, 1),
|
|
80
|
+
LESSTHAN: op(0xa9, 'arithmetic', 2, 1),
|
|
81
|
+
GREATERTHAN: op(0xaa, 'arithmetic', 2, 1),
|
|
82
|
+
LESSTHANOREQUAL: op(0xab, 'arithmetic', 2, 1),
|
|
83
|
+
GREATERTHANOREQUAL: op(0xac, 'arithmetic', 2, 1),
|
|
84
|
+
MIN: op(0xad, 'arithmetic', 2, 1),
|
|
85
|
+
MAX: op(0xae, 'arithmetic', 2, 1),
|
|
86
|
+
WITHIN: op(0xaf, 'arithmetic', 3, 1),
|
|
87
|
+
ADDCARRY: op(0xb0, 'arithmetic', 3, 2),
|
|
88
|
+
SUBCARRY: op(0xb1, 'arithmetic', 3, 2),
|
|
89
|
+
MUL128_WIDE: op(0xb2, 'arithmetic', 2, 2),
|
|
90
|
+
LOG: op(0xb3, 'arithmetic', 1, 1),
|
|
91
|
+
SQRT: op(0xb4, 'arithmetic', 1, 1),
|
|
92
|
+
// Cryptography & state hashing
|
|
93
|
+
SHA256: op(0xb8, 'cryptography', 1, 1),
|
|
94
|
+
HASH256: op(0xb9, 'cryptography', 1, 1),
|
|
95
|
+
BLAKE3: op(0xba, 'cryptography', 1, 1),
|
|
96
|
+
CHECKSIG: op(0xbb, 'cryptography', 2, 1),
|
|
97
|
+
CHECKSIGVERIFY: op(0xbc, 'cryptography', 2, 0),
|
|
98
|
+
CHECKDATASIG: op(0xbd, 'cryptography', 4, 1),
|
|
99
|
+
LEAFHASH: op(0xbe, 'cryptography', 1, 1),
|
|
100
|
+
NODEHASH: op(0xbf, 'cryptography', 2, 1),
|
|
101
|
+
MERKLEROOT_SORTED: op(0xc0, 'cryptography', 3, 1),
|
|
102
|
+
MERKLEROOT_INDEXED: op(0xc1, 'cryptography', 4, 1),
|
|
103
|
+
SIGHASH: op(0xc2, 'cryptography', 1, 1),
|
|
104
|
+
STATEHASH: op(0xc3, 'cryptography', null, 1),
|
|
105
|
+
// Transaction introspection
|
|
106
|
+
TXVERSION: op(0xc8, 'transaction', 0, 1),
|
|
107
|
+
TXINPUTCOUNT: op(0xc9, 'transaction', 0, 1),
|
|
108
|
+
TXOUTPUTCOUNT: op(0xca, 'transaction', 0, 1),
|
|
109
|
+
TXMINTCOUNT: op(0xcb, 'transaction', 0, 1),
|
|
110
|
+
TXLOCKTIME: op(0xcc, 'transaction', 0, 1),
|
|
111
|
+
TXLOCKKIND: op(0xcd, 'transaction', 0, 1),
|
|
112
|
+
TXID: op(0xce, 'transaction', 0, 1),
|
|
113
|
+
CHAINID: op(0xcf, 'transaction', 0, 1),
|
|
114
|
+
// Input introspection
|
|
115
|
+
INPUTINDEX: op(0xd0, 'input', 0, 1),
|
|
116
|
+
INPUTFLAGS: op(0xd1, 'input', 1, 1),
|
|
117
|
+
OUTPOINTTXID: op(0xd2, 'input', 1, 1),
|
|
118
|
+
OUTPOINTINDEX: op(0xd3, 'input', 1, 1),
|
|
119
|
+
INPUTSEQUENCENUMBER: op(0xd4, 'input', 1, 1),
|
|
120
|
+
INPUTPATHINDEX: op(0xd5, 'input', 1, 1),
|
|
121
|
+
UTXOVALUE: op(0xd6, 'input', 1, 1),
|
|
122
|
+
UTXOTOKENID: op(0xd7, 'input', 1, 1),
|
|
123
|
+
UTXOBRANCHCOUNT: op(0xd8, 'input', 1, 1),
|
|
124
|
+
UTXOBRANCH: op(0xd9, 'input', 2, 1),
|
|
125
|
+
UTXOPATHCOUNT: op(0xda, 'input', 1, 1),
|
|
126
|
+
UTXOPATH: op(0xdb, 'input', 2, 1),
|
|
127
|
+
UTXOOUTPUT: op(0xdc, 'input', 1, 1),
|
|
128
|
+
UTXOREQUIRESBRANCH: op(0xdd, 'input', 2, 1),
|
|
129
|
+
UTXOFLAGS: op(0xde, 'input', 1, 1),
|
|
130
|
+
INPUTWITNESSARG: op(0xdf, 'input', 3, 1),
|
|
131
|
+
// Output introspection & enforcement
|
|
132
|
+
OUTPUTFLAGS: op(0xe0, 'output', 1, 1),
|
|
133
|
+
OUTPUTVALUE: op(0xe1, 'output', 1, 1),
|
|
134
|
+
OUTPUTTOKENID: op(0xe2, 'output', 1, 1),
|
|
135
|
+
OUTPUTBRANCHCOUNT: op(0xe3, 'output', 1, 1),
|
|
136
|
+
OUTPUTBRANCH: op(0xe4, 'output', 2, 1),
|
|
137
|
+
OUTPUTPATHCOUNT: op(0xe5, 'output', 1, 1),
|
|
138
|
+
OUTPUTPATH: op(0xe6, 'output', 2, 1),
|
|
139
|
+
OUTPUTRECORD: op(0xe7, 'output', 1, 1),
|
|
140
|
+
OUTPUTREQUIRESBRANCH: op(0xe8, 'output', 2, 1),
|
|
141
|
+
OUTPUTEXCLUSIVEBRANCH: op(0xe9, 'output', 2, 1),
|
|
142
|
+
OUTPUTCONTINUESBRANCH: op(0xea, 'output', 2, 1),
|
|
143
|
+
ENFORCEOUTPUT: op(0xeb, 'output', null, 0),
|
|
144
|
+
TXTOKENBALANCE: op(0xec, 'output', 1, 2),
|
|
145
|
+
STATEVERIFY: op(0xed, 'output', 1, 0),
|
|
146
|
+
UTXOSTATEROOT: op(0xee, 'output', 1, 1),
|
|
147
|
+
OUTPUTSTATEROOT: op(0xef, 'output', 1, 1),
|
|
148
|
+
// Invocation & locks
|
|
149
|
+
ACTIVEBYTECODE: op(0xf0, 'invocation_and_locks', 0, 1),
|
|
150
|
+
INVOCATIONKIND: op(0xf1, 'invocation_and_locks', 0, 1),
|
|
151
|
+
BRANCHROOT: op(0xf2, 'invocation_and_locks', 0, 1),
|
|
152
|
+
CHECKLOCKTIMEVERIFY: op(0xf3, 'invocation_and_locks', 2, 0),
|
|
153
|
+
CHECKSEQUENCEVERIFY: op(0xf4, 'invocation_and_locks', 2, 0),
|
|
154
|
+
ACTIVEBRANCHINDEX: op(0xf5, 'invocation_and_locks', 0, 1),
|
|
155
|
+
ACTIVEWITNESSINDEX: op(0xf6, 'invocation_and_locks', 0, 1),
|
|
156
|
+
};
|
|
157
|
+
export function opcodeByte(name) {
|
|
158
|
+
return P03_OPCODES[name].byte;
|
|
159
|
+
}
|
|
160
|
+
export function opcodeSpec(name) {
|
|
161
|
+
return Object.hasOwn(P03_OPCODES, name) ? P03_OPCODES[name] : undefined;
|
|
162
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** Bounded symbolic stack rewrites over lowered P03 instructions. */
|
|
2
|
+
import { type Span } from './compiler.js';
|
|
3
|
+
export type SourceEntry = {
|
|
4
|
+
source: Span;
|
|
5
|
+
expandedFrom?: Span;
|
|
6
|
+
optimizedFrom?: {
|
|
7
|
+
op: string;
|
|
8
|
+
source: Span;
|
|
9
|
+
expandedFrom?: Span;
|
|
10
|
+
}[];
|
|
11
|
+
byteStart: number;
|
|
12
|
+
byteEnd: number;
|
|
13
|
+
op: string;
|
|
14
|
+
};
|
|
15
|
+
export type Instruction = {
|
|
16
|
+
wire: Uint8Array;
|
|
17
|
+
map: SourceEntry;
|
|
18
|
+
delta: number;
|
|
19
|
+
foldable?: boolean;
|
|
20
|
+
file?: string;
|
|
21
|
+
};
|
|
22
|
+
/** Structured optimizer error carrying source-span provenance for Clang-style diagnostics. */
|
|
23
|
+
export declare class OptimizerError extends Error {
|
|
24
|
+
readonly code: 'E0708' | 'E9001';
|
|
25
|
+
readonly span?: Span | undefined;
|
|
26
|
+
readonly relatedSpans: readonly {
|
|
27
|
+
op: string;
|
|
28
|
+
source: Span;
|
|
29
|
+
expandedFrom?: Span;
|
|
30
|
+
}[];
|
|
31
|
+
constructor(code: 'E0708' | 'E9001', message: string, span?: Span | undefined, relatedSpans?: readonly {
|
|
32
|
+
op: string;
|
|
33
|
+
source: Span;
|
|
34
|
+
expandedFrom?: Span;
|
|
35
|
+
}[]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Each rewrite strictly reduces opcode count, so the fuel bound is also an
|
|
39
|
+
* assertion against an accidentally nonterminating future rule. ZERO/PICK is
|
|
40
|
+
* the symbolic stack identity `S x -> S x 0 -> S x x`; DUP has that exact
|
|
41
|
+
* effect and the same error behavior for a valid x. No operation with a
|
|
42
|
+
* potential runtime error is deleted or reordered.
|
|
43
|
+
*/
|
|
44
|
+
export declare const SUPPORTED_COMPILER_EDITIONS: readonly ["2026.1", "2026.2"];
|
|
45
|
+
export type CompilerEdition = (typeof SUPPORTED_COMPILER_EDITIONS)[number];
|
|
46
|
+
export declare const DEFAULT_COMPILER_EDITION: CompilerEdition;
|
|
47
|
+
export declare const SUPPORTED_OPTIMIZER_VERSIONS: readonly ["none", "v1", "v2"];
|
|
48
|
+
export type OptimizerVersion = (typeof SUPPORTED_OPTIMIZER_VERSIONS)[number];
|
|
49
|
+
export declare const DEFAULT_OPTIMIZER_VERSION: OptimizerVersion;
|
|
50
|
+
export declare function isSupportedCompilerEdition(edition: unknown): edition is CompilerEdition;
|
|
51
|
+
export declare function isSupportedOptimizerVersion(version: unknown): version is OptimizerVersion;
|
|
52
|
+
export declare function resolveCompilerEdition(edition?: CompilerEdition): CompilerEdition;
|
|
53
|
+
export declare function resolveOptimizerVersion(options?: {
|
|
54
|
+
optimize?: boolean;
|
|
55
|
+
optimizerVersion?: OptimizerVersion;
|
|
56
|
+
}): OptimizerVersion;
|
|
57
|
+
export declare const OPTIMIZER_PASSES_BY_VERSION: Readonly<Record<OptimizerVersion, (original: readonly Instruction[], argumentCount: number) => Instruction[]>>;
|
|
58
|
+
export declare function optimizeInstructions(original: readonly Instruction[], argumentCount: number, version?: OptimizerVersion): Instruction[];
|