-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better error message when initAztecJs is not called #3618
Comments
We can't lazy load it as it's async, and that has massive async propagation that seems an unacceptable tradeoff. My bad on not updating the docs around this... |
I understand that before we did lazy load, which made every method that depended on the singleton async. I take it it's unacceptable because of developer convenience? I don't think the performance hit is significant. But assuming the asyncs are unacceptable, then +1 to auto init. I'm curious though: what happens in cjs, since it doesn't like top-level awaits? I saw a ts-ignore related to it, but I don't know what actually happens if you try running it when in cjs. |
IIRC performance hit can become significant if you're e.g. having to do lots and lots of hashing in a short amount of time (tree computations etc). I guess this could be alleviated though by lazy fetching the wasm earlier in the stack. Maybe I just can't get past how much more verbose the code becomes, and how otherwise logically synchronous functions all have to become async and incur event queue overhead because of an implementation detail. Good point on CJS. We can't auto-init in cjs land. So you're forced to either async everything, have an init function, or not support CJS. Hmmph. I drew great satisfaction in de-asyncifying several dozen functions. Maybe was mistake... |
I'm not even sure how to improve the error as it's thrown from bb.js but could get emitted by any method that leads to bb.js, so it can't be captured and transformed (easily). edit: Well ok, there's about 10 places we call getSingleton so it could be done. edit2: Can I just improve the error message and add initAztecJs to docs? I can't face asyncifying everything right now. |
I hate it too, but I believe it's needed for the VSCode extension. |
+1. That was the original spirit of this issue. |
Just realized that to transform the error (properly), it has to be done in the aztec.js project, so somewhere within whichever of the many functions we export there, they could throw, they need to be wrapped etc. Is there some pattern whereby we can make any use of aztec.js fail with a good error, if init hasn't been called? 🤔 |
By leveraging more granular imports (and fixing some import cycles), I'm able to run "cli help" without it triggering a "top level await" init of bb.js. (And got it down to 0.095s to just display the help!) I'm coming across the dreaded If we want such functions, I would group array level "utils" into a module in foundation called "array", and ensure that module didn't import anything else irrelevant to it. In this particular case that was causing bb.js to init, I just inlined the buffer check in TxHash so it didn't have to import the circuits.js mega import that imports everything else. (There were other things I've had to solve, like circular imports to top level mega imports). constructor(
/**
* The buffer containing the hash.
*/
public buffer: Buffer,
) {
if (buffer.length !== TxHash.SIZE) {
throw new Error(`Expected buffer to have length ${TxHash.SIZE} but was ${buffer.length}`);
}
} Anyway, r.e. the approach of granular imports with top level init. This does mean however, that for the CJS path, the developer will have to remember to init the library at an appropriate point in time, as there is no top level await (and that case will need documentation). I think this is an acceptable deviation however, as we should generally hold the view that CJS will die, and that users of it can jump through the additional hoop of calling an init function until it does. This is PR so far. Happy to discuss further. #3629 |
…t too early (#3629) Related to this issue: AztecProtocol/aztec-packages#3618 Gets `aztec-cli help` down to 0.01s!!!!
Not that I can think of. We could wrap each function of aztec.js in another function that does the try/catch or play with decorators (since they're meant for this kind of stuff!), but that requires tweaking each function in aztec.js that potentially depends on bb.init. Another option could be iterating over all Honestly, with the improvement for esm, and the better error message in bb.js itself (that points the user in a right direction), I'd close this issue as fixed for now. |
…t too early (AztecProtocol#3629) Related to this issue: AztecProtocol#3618 Gets `aztec-cli help` down to 0.01s!!!!
If a user forgets to call
initAztecJs
, the error message they get isInitialise first via initSingleton
, which is not referenced in our docs or aztecjs API. We should throw an error that referencesinitAztecJs
instead.Consider also removing the need for calling
initAztecJs
explicitly, and lazy loading instead when it's actually used.The text was updated successfully, but these errors were encountered: