@seljs/runtime 1.0.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/CHANGELOG.md +8 -0
- package/LICENSE.md +190 -0
- package/README.md +5 -0
- package/dist/analysis/call-collector.d.ts +20 -0
- package/dist/analysis/call-collector.d.ts.map +1 -0
- package/dist/analysis/call-collector.js +272 -0
- package/dist/analysis/dependency-analyzer.d.ts +14 -0
- package/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/dist/analysis/dependency-analyzer.js +76 -0
- package/dist/analysis/index.d.ts +2 -0
- package/dist/analysis/index.d.ts.map +1 -0
- package/dist/analysis/index.js +1 -0
- package/dist/analysis/round-planner.d.ts +32 -0
- package/dist/analysis/round-planner.d.ts.map +1 -0
- package/dist/analysis/round-planner.js +69 -0
- package/dist/analysis/types.d.ts +113 -0
- package/dist/analysis/types.d.ts.map +1 -0
- package/dist/analysis/types.js +1 -0
- package/dist/debug.d.ts +13 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +12 -0
- package/dist/environment/context.d.ts +3 -0
- package/dist/environment/context.d.ts.map +1 -0
- package/dist/environment/context.js +8 -0
- package/dist/environment/contract-caller.d.ts +25 -0
- package/dist/environment/contract-caller.d.ts.map +1 -0
- package/dist/environment/contract-caller.js +85 -0
- package/dist/environment/environment.d.ts +81 -0
- package/dist/environment/environment.d.ts.map +1 -0
- package/dist/environment/environment.js +279 -0
- package/dist/environment/error-wrapper.d.ts +11 -0
- package/dist/environment/error-wrapper.d.ts.map +1 -0
- package/dist/environment/error-wrapper.js +33 -0
- package/dist/environment/index.d.ts +3 -0
- package/dist/environment/index.d.ts.map +1 -0
- package/dist/environment/index.js +2 -0
- package/dist/environment/replay-cache.d.ts +23 -0
- package/dist/environment/replay-cache.d.ts.map +1 -0
- package/dist/environment/replay-cache.js +51 -0
- package/dist/environment/types.d.ts +57 -0
- package/dist/environment/types.d.ts.map +1 -0
- package/dist/environment/types.js +1 -0
- package/dist/errors/errors.d.ts +63 -0
- package/dist/errors/errors.d.ts.map +1 -0
- package/dist/errors/errors.js +63 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/execution/index.d.ts +2 -0
- package/dist/execution/index.d.ts.map +1 -0
- package/dist/execution/index.js +1 -0
- package/dist/execution/multi-round-executor.d.ts +17 -0
- package/dist/execution/multi-round-executor.d.ts.map +1 -0
- package/dist/execution/multi-round-executor.js +47 -0
- package/dist/execution/multicall-batcher.d.ts +14 -0
- package/dist/execution/multicall-batcher.d.ts.map +1 -0
- package/dist/execution/multicall-batcher.js +53 -0
- package/dist/execution/multicall.d.ts +42 -0
- package/dist/execution/multicall.d.ts.map +1 -0
- package/dist/execution/multicall.js +29 -0
- package/dist/execution/result-cache.d.ts +47 -0
- package/dist/execution/result-cache.d.ts.map +1 -0
- package/dist/execution/result-cache.js +65 -0
- package/dist/execution/round-executor.d.ts +18 -0
- package/dist/execution/round-executor.d.ts.map +1 -0
- package/dist/execution/round-executor.js +95 -0
- package/dist/execution/types.d.ts +55 -0
- package/dist/execution/types.d.ts.map +1 -0
- package/dist/execution/types.js +1 -0
- package/dist/factory.d.ts +3 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +2 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/package.json +76 -0
package/CHANGELOG.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 abi group GmbH
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CollectedCall } from "./types.js";
|
|
2
|
+
import type { ASTNode } from "@marcbachmann/cel-js";
|
|
3
|
+
/** Minimal interface required by collectCalls for contract lookup. */
|
|
4
|
+
export interface ContractLookup {
|
|
5
|
+
get: (name: string) => unknown;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Collects contract calls from a CEL AST by traversing the tree.
|
|
9
|
+
*
|
|
10
|
+
* Walks the AST looking for `rcall` nodes whose receiver matches a registered
|
|
11
|
+
* contract. For each matched call, classifies arguments as literals, variable
|
|
12
|
+
* references, or results of other contract calls (creating dependency edges).
|
|
13
|
+
* Skips `cel.bind()` calls while still collecting any nested contract calls.
|
|
14
|
+
*
|
|
15
|
+
* @param ast The root AST node from a parsed CEL expression
|
|
16
|
+
* @param registry Contract lookup to validate receiver names against
|
|
17
|
+
* @returns Array of collected calls with dependency information
|
|
18
|
+
*/
|
|
19
|
+
export declare const collectCalls: (ast: ASTNode, registry: ContractLookup) => CollectedCall[];
|
|
20
|
+
//# sourceMappingURL=call-collector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call-collector.d.ts","sourceRoot":"","sources":["../../src/analysis/call-collector.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAgB,aAAa,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD,sEAAsE;AACtE,MAAM,WAAW,cAAc;IAC9B,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;CAC/B;AAwHD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,GACxB,KAAK,OAAO,EACZ,UAAU,cAAc,KACtB,aAAa,EA2Nf,CAAC"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { COMPREHENSION_MACROS, SCALAR_WRAPPER_FUNCTIONS } from "@seljs/checker";
|
|
2
|
+
import { isAstNode } from "@seljs/common";
|
|
3
|
+
import { createLogger } from "../debug.js";
|
|
4
|
+
const debug = createLogger("analysis:collect");
|
|
5
|
+
/**
|
|
6
|
+
* Generates a deterministic call identifier from contract name, method, and arguments.
|
|
7
|
+
*
|
|
8
|
+
* Format: "contract:method:arg1,arg2,..."
|
|
9
|
+
* Used for deduplication and dependency tracking between calls.
|
|
10
|
+
*/
|
|
11
|
+
const generateCallId = (contract, method, args) => {
|
|
12
|
+
const argKey = args
|
|
13
|
+
.map((arg) => {
|
|
14
|
+
if (arg.variableName !== undefined) {
|
|
15
|
+
return arg.variableName;
|
|
16
|
+
}
|
|
17
|
+
if (arg.value !== undefined) {
|
|
18
|
+
const v = arg.value;
|
|
19
|
+
return typeof v === "string" ||
|
|
20
|
+
typeof v === "number" ||
|
|
21
|
+
typeof v === "boolean" ||
|
|
22
|
+
typeof v === "bigint"
|
|
23
|
+
? String(v)
|
|
24
|
+
: "?";
|
|
25
|
+
}
|
|
26
|
+
if (arg.dependsOnCallId !== undefined) {
|
|
27
|
+
return arg.dependsOnCallId;
|
|
28
|
+
}
|
|
29
|
+
return "?";
|
|
30
|
+
})
|
|
31
|
+
.join(",");
|
|
32
|
+
return `${contract}:${method}:${argKey}`;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Attempts to extract a scalar argument from a Solidity cast/wrapper call
|
|
36
|
+
* (e.g. `uint256(42)`, `solAddress("0x...")`, `int256(tokenId)`).
|
|
37
|
+
*
|
|
38
|
+
* Handles both literal values and variable references.
|
|
39
|
+
* Returns undefined if the node is not a recognized scalar wrapper.
|
|
40
|
+
*/
|
|
41
|
+
const collectScalarArgument = (argNode) => {
|
|
42
|
+
if (!isAstNode(argNode) || argNode.op !== "call") {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
46
|
+
if (!Array.isArray(argNode.args) || argNode.args.length !== 2) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const fnName = argNode.args[0];
|
|
50
|
+
const fnArgs = argNode.args[1];
|
|
51
|
+
if (typeof fnName !== "string" ||
|
|
52
|
+
!Array.isArray(fnArgs) ||
|
|
53
|
+
fnArgs.length !== 1 ||
|
|
54
|
+
!SCALAR_WRAPPER_FUNCTIONS.has(fnName)) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const inner = fnArgs[0];
|
|
58
|
+
if (!isAstNode(inner)) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (inner.op === "value") {
|
|
62
|
+
return { type: "literal", value: inner.args };
|
|
63
|
+
}
|
|
64
|
+
if (inner.op === "id" && typeof inner.args === "string") {
|
|
65
|
+
return { type: "variable", variableName: inner.args };
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Extracts the method name, receiver identifier, and argument nodes from an rcall AST node.
|
|
71
|
+
*
|
|
72
|
+
* An rcall node has the structure: `["methodName", receiverNode, [argNodes...]]`
|
|
73
|
+
*/
|
|
74
|
+
const getRCallArgs = (node) => {
|
|
75
|
+
if (!Array.isArray(node.args) || node.args.length < 3) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const methodNode = node.args[0];
|
|
79
|
+
const receiverNode = node.args[1];
|
|
80
|
+
const argNodes = node.args[2];
|
|
81
|
+
if (typeof methodNode !== "string" || !Array.isArray(argNodes)) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
let receiverName;
|
|
85
|
+
if (isAstNode(receiverNode) &&
|
|
86
|
+
receiverNode.op === "id" &&
|
|
87
|
+
typeof receiverNode.args === "string") {
|
|
88
|
+
receiverName = receiverNode.args;
|
|
89
|
+
}
|
|
90
|
+
return { method: methodNode, receiverName, args: argNodes };
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Collects contract calls from a CEL AST by traversing the tree.
|
|
94
|
+
*
|
|
95
|
+
* Walks the AST looking for `rcall` nodes whose receiver matches a registered
|
|
96
|
+
* contract. For each matched call, classifies arguments as literals, variable
|
|
97
|
+
* references, or results of other contract calls (creating dependency edges).
|
|
98
|
+
* Skips `cel.bind()` calls while still collecting any nested contract calls.
|
|
99
|
+
*
|
|
100
|
+
* @param ast The root AST node from a parsed CEL expression
|
|
101
|
+
* @param registry Contract lookup to validate receiver names against
|
|
102
|
+
* @returns Array of collected calls with dependency information
|
|
103
|
+
*/
|
|
104
|
+
export const collectCalls = (ast, registry) => {
|
|
105
|
+
const calls = [];
|
|
106
|
+
/** Variable names that are scoped (comprehension iteration vars, cel.bind vars) — NOT user context */
|
|
107
|
+
const scopedVars = new Set();
|
|
108
|
+
/** rcall AST nodes skipped because they have unresolvable args */
|
|
109
|
+
const deferredNodes = new WeakSet();
|
|
110
|
+
const traverse = (node) => {
|
|
111
|
+
if (Array.isArray(node)) {
|
|
112
|
+
for (const item of node) {
|
|
113
|
+
traverse(item);
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (!isAstNode(node)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (node.op === "rcall") {
|
|
121
|
+
collectRCall(node);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
traverse(node.args);
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Handles cel.bind() — registers the bind variable name for the body scope,
|
|
128
|
+
* traverses the initializer and body, then removes the scoped var.
|
|
129
|
+
*/
|
|
130
|
+
const handleCelBind = (args) => {
|
|
131
|
+
if (args.length >= 3) {
|
|
132
|
+
const nameNode = args[0];
|
|
133
|
+
if (isAstNode(nameNode) &&
|
|
134
|
+
nameNode.op === "id" &&
|
|
135
|
+
typeof nameNode.args === "string") {
|
|
136
|
+
traverse(args[1]);
|
|
137
|
+
scopedVars.add(nameNode.args);
|
|
138
|
+
traverse(args[2]);
|
|
139
|
+
scopedVars.delete(nameNode.args);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const argNode of args) {
|
|
144
|
+
traverse(argNode);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Handles rcall nodes whose receiver is NOT a registered contract.
|
|
149
|
+
* Traverses the receiver node and args, registering comprehension
|
|
150
|
+
* iteration variables for their body scope.
|
|
151
|
+
*/
|
|
152
|
+
const handleNonContractRCall = (node, method, args) => {
|
|
153
|
+
// Always traverse the receiver to collect contract calls within it
|
|
154
|
+
if (Array.isArray(node.args) && node.args.length >= 2) {
|
|
155
|
+
traverse(node.args[1]);
|
|
156
|
+
}
|
|
157
|
+
// Comprehension macros: register iteration variable for body scope
|
|
158
|
+
if (COMPREHENSION_MACROS.has(method) && args.length >= 2) {
|
|
159
|
+
const iterVarNode = args[0];
|
|
160
|
+
if (isAstNode(iterVarNode) &&
|
|
161
|
+
iterVarNode.op === "id" &&
|
|
162
|
+
typeof iterVarNode.args === "string") {
|
|
163
|
+
scopedVars.add(iterVarNode.args);
|
|
164
|
+
for (let i = 1; i < args.length; i++) {
|
|
165
|
+
traverse(args[i]);
|
|
166
|
+
}
|
|
167
|
+
scopedVars.delete(iterVarNode.args);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const argNode of args) {
|
|
172
|
+
traverse(argNode);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Classifies a single argument node, returning the resolved CallArgument
|
|
177
|
+
* or `undefined` if unresolvable. When `undefined` is returned, the caller
|
|
178
|
+
* should mark the parent call as deferred.
|
|
179
|
+
*/
|
|
180
|
+
const classifyArg = (argNode) => {
|
|
181
|
+
const scalarArg = collectScalarArgument(argNode);
|
|
182
|
+
if (scalarArg) {
|
|
183
|
+
return { arg: scalarArg, traversed: false };
|
|
184
|
+
}
|
|
185
|
+
if (isAstNode(argNode) && argNode.op === "value") {
|
|
186
|
+
return {
|
|
187
|
+
arg: { type: "literal", value: argNode.args },
|
|
188
|
+
traversed: false,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (isAstNode(argNode) &&
|
|
192
|
+
argNode.op === "id" &&
|
|
193
|
+
typeof argNode.args === "string") {
|
|
194
|
+
if (scopedVars.has(argNode.args)) {
|
|
195
|
+
return { arg: undefined, traversed: false };
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
arg: { type: "variable", variableName: argNode.args },
|
|
199
|
+
traversed: false,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (isAstNode(argNode) && argNode.op === "rcall") {
|
|
203
|
+
const innerCall = collectRCall(argNode);
|
|
204
|
+
if (innerCall) {
|
|
205
|
+
return {
|
|
206
|
+
arg: { type: "call_result", dependsOnCallId: innerCall.id },
|
|
207
|
+
traversed: true,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
// Inner rcall was not collected — check if it was deferred
|
|
211
|
+
if (deferredNodes.has(argNode)) {
|
|
212
|
+
// inner calls already collected by recursive collectRCall
|
|
213
|
+
return { arg: undefined, traversed: true };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return { arg: undefined, traversed: false };
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Processes an rcall AST node, collecting it as a contract call if the
|
|
220
|
+
* receiver is a registered contract. Recursively processes nested rcalls
|
|
221
|
+
* in arguments to build dependency chains.
|
|
222
|
+
*/
|
|
223
|
+
const collectRCall = (node) => {
|
|
224
|
+
const callArgs = getRCallArgs(node);
|
|
225
|
+
if (!callArgs) {
|
|
226
|
+
traverse(node.args);
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
const { method, receiverName, args } = callArgs;
|
|
230
|
+
if (receiverName === "cel" && method === "bind") {
|
|
231
|
+
handleCelBind(args);
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
if (!receiverName || !registry.get(receiverName)) {
|
|
235
|
+
handleNonContractRCall(node, method, args);
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
const collectedArgs = [];
|
|
239
|
+
let hasUnresolvableArg = false;
|
|
240
|
+
for (const argNode of args) {
|
|
241
|
+
const { arg, traversed } = classifyArg(argNode);
|
|
242
|
+
if (arg) {
|
|
243
|
+
collectedArgs.push(arg);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
hasUnresolvableArg = true;
|
|
247
|
+
if (!traversed) {
|
|
248
|
+
traverse(argNode);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (hasUnresolvableArg) {
|
|
253
|
+
deferredNodes.add(node);
|
|
254
|
+
debug("deferred %s.%s (unresolvable args)", receiverName, method);
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const call = {
|
|
258
|
+
id: generateCallId(receiverName, method, collectedArgs),
|
|
259
|
+
contract: receiverName,
|
|
260
|
+
method,
|
|
261
|
+
args: collectedArgs,
|
|
262
|
+
astNode: node,
|
|
263
|
+
};
|
|
264
|
+
calls.push(call);
|
|
265
|
+
debug("found %s.%s (id=%s)", receiverName, method, call.id);
|
|
266
|
+
return call;
|
|
267
|
+
};
|
|
268
|
+
// Start traversal from the root of the AST
|
|
269
|
+
traverse(ast);
|
|
270
|
+
debug("collected %d total calls", calls.length);
|
|
271
|
+
return calls;
|
|
272
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CollectedCall, DependencyGraph } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Builds a dependency graph from collected contract calls.
|
|
4
|
+
*
|
|
5
|
+
* Creates a directed graph where nodes represent contract calls and edges
|
|
6
|
+
* represent data dependencies (when one call's argument is the result of
|
|
7
|
+
* another call). Validates that no circular dependencies exist.
|
|
8
|
+
*
|
|
9
|
+
* @param calls - Array of collected contract calls from AST traversal
|
|
10
|
+
* @returns A dependency graph with nodes and directed edges
|
|
11
|
+
* @throws {@link CircularDependencyError} If a circular dependency is detected between calls
|
|
12
|
+
*/
|
|
13
|
+
export declare const analyzeDependencies: (calls: CollectedCall[]) => DependencyGraph;
|
|
14
|
+
//# sourceMappingURL=dependency-analyzer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dependency-analyzer.d.ts","sourceRoot":"","sources":["../../src/analysis/dependency-analyzer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAa,MAAM,YAAY,CAAC;AAiD5E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC/B,OAAO,aAAa,EAAE,KACpB,eAwCF,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createLogger } from "../debug.js";
|
|
2
|
+
import { CircularDependencyError } from "../errors/index.js";
|
|
3
|
+
const debug = createLogger("analysis:dependency");
|
|
4
|
+
/**
|
|
5
|
+
* Detects circular dependencies in the graph using DFS with a recursion stack.
|
|
6
|
+
*
|
|
7
|
+
* @throws {@link CircularDependencyError} If a cycle is found, including the call IDs forming the cycle
|
|
8
|
+
*/
|
|
9
|
+
const detectCycles = (nodes, edges) => {
|
|
10
|
+
const visited = new Set();
|
|
11
|
+
const inStack = new Set();
|
|
12
|
+
const dfs = (nodeId, path) => {
|
|
13
|
+
if (inStack.has(nodeId)) {
|
|
14
|
+
const cycleStart = path.indexOf(nodeId);
|
|
15
|
+
const cycle = path.slice(cycleStart);
|
|
16
|
+
throw new CircularDependencyError(`Circular dependency detected: ${cycle.join(" -> ")} -> ${nodeId}`, { callIds: cycle });
|
|
17
|
+
}
|
|
18
|
+
if (visited.has(nodeId)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
visited.add(nodeId);
|
|
22
|
+
inStack.add(nodeId);
|
|
23
|
+
path.push(nodeId);
|
|
24
|
+
for (const depId of edges.get(nodeId) ?? []) {
|
|
25
|
+
dfs(depId, path);
|
|
26
|
+
}
|
|
27
|
+
path.pop();
|
|
28
|
+
inStack.delete(nodeId);
|
|
29
|
+
};
|
|
30
|
+
for (const nodeId of nodes.keys()) {
|
|
31
|
+
if (!visited.has(nodeId)) {
|
|
32
|
+
dfs(nodeId, []);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Builds a dependency graph from collected contract calls.
|
|
38
|
+
*
|
|
39
|
+
* Creates a directed graph where nodes represent contract calls and edges
|
|
40
|
+
* represent data dependencies (when one call's argument is the result of
|
|
41
|
+
* another call). Validates that no circular dependencies exist.
|
|
42
|
+
*
|
|
43
|
+
* @param calls - Array of collected contract calls from AST traversal
|
|
44
|
+
* @returns A dependency graph with nodes and directed edges
|
|
45
|
+
* @throws {@link CircularDependencyError} If a circular dependency is detected between calls
|
|
46
|
+
*/
|
|
47
|
+
export const analyzeDependencies = (calls) => {
|
|
48
|
+
const nodes = new Map();
|
|
49
|
+
const edges = new Map();
|
|
50
|
+
// First pass: create a node and empty edge set for each call
|
|
51
|
+
for (const call of calls) {
|
|
52
|
+
nodes.set(call.id, { call, dependsOn: [], dependedOnBy: [] });
|
|
53
|
+
edges.set(call.id, new Set());
|
|
54
|
+
}
|
|
55
|
+
// Second pass: wire up dependency edges from call_result arguments
|
|
56
|
+
for (const call of calls) {
|
|
57
|
+
for (const arg of call.args) {
|
|
58
|
+
if (arg.type === "call_result" && arg.dependsOnCallId) {
|
|
59
|
+
const depId = arg.dependsOnCallId;
|
|
60
|
+
edges.get(call.id)?.add(depId);
|
|
61
|
+
const callNode = nodes.get(call.id);
|
|
62
|
+
if (callNode && !callNode.dependsOn.includes(depId)) {
|
|
63
|
+
callNode.dependsOn.push(depId);
|
|
64
|
+
}
|
|
65
|
+
const depNode = nodes.get(depId);
|
|
66
|
+
if (depNode && !depNode.dependedOnBy.includes(call.id)) {
|
|
67
|
+
depNode.dependedOnBy.push(call.id);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Run cycle detection to ensure there are no circular dependencies in the graph.
|
|
73
|
+
detectCycles(nodes, edges);
|
|
74
|
+
debug("graph: %d nodes, %d edges", nodes.size, [...edges.values()].reduce((sum, deps) => sum + deps.size, 0));
|
|
75
|
+
return { nodes, edges };
|
|
76
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/analysis/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DependencyGraph, ExecutionPlan } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Defines limits for the round planner to prevent excessive execution.
|
|
4
|
+
*/
|
|
5
|
+
export interface RoundPlannerLimits {
|
|
6
|
+
/**
|
|
7
|
+
* Upper limit on the number of execution rounds.
|
|
8
|
+
*
|
|
9
|
+
* @default 10
|
|
10
|
+
*/
|
|
11
|
+
maxRounds?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Upper limit on the total number of calls in the graph.
|
|
14
|
+
*
|
|
15
|
+
* @default 100
|
|
16
|
+
*/
|
|
17
|
+
maxCalls?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Plans execution rounds from a dependency graph using topological sorting.
|
|
21
|
+
*
|
|
22
|
+
* Groups independent calls into parallel rounds, ensuring all dependencies
|
|
23
|
+
* of a call are executed in previous rounds. Enforces limits on the number
|
|
24
|
+
* of rounds and total calls to prevent excessive execution.
|
|
25
|
+
*
|
|
26
|
+
* @param graph Dependency graph representing calls and their relationships.
|
|
27
|
+
* @param limits Optional limits for maximum rounds and total calls.
|
|
28
|
+
* @returns Execution plan containing planned rounds, total calls, and maximum depth.
|
|
29
|
+
* @throws ExecutionLimitError If total calls exceed maxCalls or rounds exceed maxRounds.
|
|
30
|
+
*/
|
|
31
|
+
export declare const planRounds: (graph: DependencyGraph, limits?: RoundPlannerLimits) => ExecutionPlan;
|
|
32
|
+
//# sourceMappingURL=round-planner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"round-planner.d.ts","sourceRoot":"","sources":["../../src/analysis/round-planner.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,eAAe,EACf,aAAa,EAEb,MAAM,YAAY,CAAC;AAIpB;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,UAAU,GACtB,OAAO,eAAe,EACtB,SAAQ,kBAAuB,KAC7B,aA6EF,CAAC"}
|