-
Couldn't load subscription status.
- Fork 140
wasm-js!: Remove system program dependency #240
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
Conversation
#### Problem While working on the refactor of all wasm-js logic into sdk-wasm-js, I noticed that no matter what I did, `SystemInstruction` still made it into the final build. That made it impossible to build the package with breaking changes. #### Summary of changes Remove the usage of `SystemInstruction` in the tests. This required exposing `Instruction` and `AccountMeta` through wasm-bindgen to be able to create instructions. The API doesn't need to be perfect, just usable, so I went with a builder pattern for adding `AccountMeta`s to an `Instruction`, which hugely simplifies the logic. Otherwise we'd need to perform conversions between JS arrays and `Vec<AccountMeta>`, which I was too lazy to do properly because wasm-js crate is more of a proof-of-concept. While doing that, I moved the wasm-js deps to the component crates that were required for the tests.
| [target.'cfg(target_arch = "wasm32")'.dependencies] | ||
| console_error_panic_hook = { workspace = true } | ||
| console_log = { workspace = true } | ||
| getrandom = { version = "0.1.1", features = ["wasm-bindgen"] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like getRandom does not inherit the newer version in the workspace. Any reason for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we need it for randomness to work in wasm with the current versions that we use. At least, we needed it a year or so ago, so maybe all of the newer versions work now. I can take a look at it in follow-up work!
| solana_program::*, | ||
| solana_sdk::{ | ||
| // These imports exist in both solana_sdk and solana_program, so we use | ||
| // direct imports to suppress ambiguous re-export warnings. | ||
| declare_deprecated_id, | ||
| declare_id, | ||
| entrypoint, | ||
| entrypoint_deprecated, | ||
| example_mocks, | ||
| hash, | ||
| program_stubs, | ||
| pubkey, | ||
| *, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guessing this is a breaking change that requires a major version bump?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep!
| //! from when wasm-bindgen lacked Vec<T> support | ||
| //! (ref: https://github.com/rustwasm/wasm-bindgen/issues/111) | ||
| use {crate::Instruction, wasm_bindgen::prelude::*}; | ||
| #![allow(non_snake_case)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also use the attribute if you wanted:
#[wasm_bindgen(js_name = pushAccount)]
pub fn push_account(mut self, account_meta: AccountMeta) -> SelfNot sure why one is better than the other though tbh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically I've found that js_name is more useful for renaming functions totally. Last time I checked this, maybe a year ago, there was no default way to just have functions renamed into camel-case, so I'd prefer to go with this.
instruction/src/wasm.rs
Outdated
| pub fn pushAccount(mut self, account_meta: AccountMeta) -> Self { | ||
| self.accounts.push(account_meta); | ||
| self | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think someone familiar with javascript APIs would expect to be able to do this?:
const instruction = new Instruction(programId, instructionData);
instruction.pushAccount(metadataA);
instruction.pushAccount(metadataB);When I tried that in your test I got a Error: Attempt to use a moved value.
One idea is to be more explicit with the builder and require a build(). Something that would look like:
const ix = new InstructionBuilder(programId)
.addAccount(srcAccountMeta)
.addAccount(dstAccountMeta)
.setData(instructionData)
.build();What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that a builder API would be neater. However, a builder pattern would be inconsistent with all of the other APIs offered in the SDK. If this were an important API, then we could consider adopting builder patterns all over the SDK, but since sdk-wasm-js is more of a proof-of-concept, I would prefer to keep things simple and avoid adding wasm-js-only types.
Unfortunately, wasm-bindgen doesn't allow for returning references, so I went with just normal mutating functions instead of any kind of builder pattern. Let me know how it looks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
* wasm-js: Remove system program dependency #### Problem While working on the refactor of all wasm-js logic into sdk-wasm-js, I noticed that no matter what I did, `SystemInstruction` still made it into the final build. That made it impossible to build the package with breaking changes. #### Summary of changes Remove the usage of `SystemInstruction` in the tests. This required exposing `Instruction` and `AccountMeta` through wasm-bindgen to be able to create instructions. The API doesn't need to be perfect, just usable, so I went with a builder pattern for adding `AccountMeta`s to an `Instruction`, which hugely simplifies the logic. Otherwise we'd need to perform conversions between JS arrays and `Vec<AccountMeta>`, which I was too lazy to do properly because wasm-js crate is more of a proof-of-concept. While doing that, I moved the wasm-js deps to the component crates that were required for the tests. * Regenerate cargo.lock, fixup wasmbindgen attribute * No builder pattern
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
Signed-off-by: vetclippy <[email protected]>
* Add derive address helpers * Update lock file * Fix doc links * Add missing dependency * Refactor directory structure (#18) * Use macro rules * Update directory structure * Add account info and pubkey helpers (#21) * Rename unchecked methods (#22) * Add map and filter_map to Ref and RefMut (#27) * Add map and filter_map to Ref and RefMut * Add unit tests * Apply suggestions * Add bit flag for original data length (#28) * Add bit flag * Remove declarative macro * Add `checked_create_program_address` helper (#30) * Add unchecked helper * Fix lint * Add inline * Rename to checked * Cosmetics * Fix sol log params * A close macro implementation for AccountInfo (#42) * Added close and based_close * added docs comments + wrapped up and tested both function * cargo clippy and fmt * added the new close and changed the name for * fixed and tested after comments * Fixed Realloc Macro (#45) * Fixed compiler bitching about realloc * Added a better alterantive to the black_box * Fixed latest comments * deleted some line after the refactor * Update comments * Fix last u64 owner index on close (#55) * Fix clippy warnings (#59) * Improve close unchecked * sdk: Improve comments (#64) * [wip]: Add new scripts * [wip]: Use matric strategy * [wip]: Fix members parsing * [wip]: Add CI env variables * [wip]: Remove nothrow * [wip]: Filter changes * [wip]: Add audit step * [wip]: Add semver checks * [wip]: Refactor publish workflow * [wip]: Refactor * [wip]: Fix commands * Fix formatting * Remove detect changes step * Review comments * Fix lint comments * Expand crate comment * Ignore crate comment tests * Add missing docs * More missing docs * Add missing release component * Pin cargo-release version * Fix merge * Review comments * sdk: Lightweight borrow check (#65) * [wip]: Add new scripts * [wip]: Add CI env variables * [wip]: Remove nothrow * [wip]: Filter changes * [wip]: Add audit step * [wip]: Add semver checks * [wip]: Refactor publish workflow * [wip]: Refactor * [wip]: Fix commands * Fix formatting * Remove detect changes step * Add check methods * Use check variant on close * Fix merge * Address review comments (#78) * [wip]: Address review comments * [wip]: Fix pointer reference * [wip]: Add logger buffer size tests * Remove unused * More logger tests * Rename program to cpi * Remove dynamic allocation * Fixed signed tests * Fix review comments * Fix unsigned test case * Add is_owner_by helper * Account borrow state check (#147) * Improve fallback and docs * Add borrow state check * Add inline * Review comments * Revert doc link merge change * Update doc comments on close account (#173) * Update doc comments * Update sdk/pinocchio/src/account_info.rs Co-authored-by: Jon C <[email protected]> * Update sdk/pinocchio/src/account_info.rs Co-authored-by: Jon C <[email protected]> --------- Co-authored-by: Jon C <[email protected]> * Remove *const cast (#170) * Add cargo miri test to CI (#178) * Add miri step * Fix miri issues * Install miri component * Deprecate AccountInfo::realloc (#174) * Add resize * Deprecate realloc * Simplify program entrypoint (#166) * Fix review comments * Revert offset increment change * Add invoke instruction helper * Typos * Remove new helpers * Remove unused * Address review comments * Tweak inline attributes * Use invoke signed unchecked * Refactor inline * Renamed to with_bounds * Update docs * Revert change * Add constant length check * Simplify accounts deserialization * Invert borrow state logic * Use expr instead * Add missing import * Address review comments * Revert unnecessary repr * Fix rebase * Tweak docs * Fix doc reference * Fix miri errors * More review comments * Simplify realloc logic (#175) * Simplify realloc logic * Address review comments * Fix `assign` unsoundness (#180) * Fix assign unsoundness * Remove unsafe * ci: Add spellcheck step (#164) * Add invoke instruction helper * Typos * Remove new helpers * Remove unused * Address review comments * Tweak inline attributes * Use invoke signed unchecked * Refactor inline * Renamed to with_bounds * Update docs * Revert change * Add constant length check * Add spellcheck step * Tweak action * Fix typos * More fixes * Yet more fixes * Fixes * Add j1 option * More and more fixes * Add missing acronym * Fix merge * Fix spelling * Fix spelling * Clarify the use of constant values (#200) * Add comments on constants * Improve offset comments * Add bitmask to dictionary * Renamed to field_at_offset * Ignore `zero_init` parameter (#203) Ignore zero_init parameter * Add resize_unchecked method to account info (#230) * add resize_unchecked method to account info * Apply suggestion from @febo Co-authored-by: Fernando Otero <[email protected]> * Apply suggestion from @febo Co-authored-by: Fernando Otero <[email protected]> * Apply suggestion from @febo Co-authored-by: Fernando Otero <[email protected]> --------- Co-authored-by: Fernando Otero <[email protected]> * Feat: Add debug/copy derives and enable missing debug/copy lint (#228) * Add debug/copy derives and enable missing debug/copy lint * Update sdk/pinocchio/src/sysvars/rent.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/entrypoint/mod.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/instruction.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/sysvars/clock.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/sysvars/fees.rs * Update sdk/pinocchio/src/sysvars/fees.rs * Update sdk/pinocchio/src/sysvars/instructions.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/sysvars/instructions.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/sysvars/instructions.rs Co-authored-by: Fernando Otero <[email protected]> * Update sdk/pinocchio/src/sysvars/clock.rs * Fix syntax error in Instructions struct derive macro --------- Co-authored-by: Fernando Otero <[email protected]> * Feat: make AccountInfo::data_ptr public (#232) * make data_ptr public * Update sdk/pinocchio/src/account_info.rs Co-authored-by: Fernando Otero <[email protected]> * add some tests for data ptr * Fix spelling --------- Co-authored-by: Fernando Otero <[email protected]> * Feat: Add try_maps on AccountInfo Ref/RefMut (#229) * Add try_maps on AccountInfo Ref/RefMut * update tests and api for try map ref * pinocchio: Move `NON_DUP_MARKER` const (#245) Move NON_DUP_MARKER const * chore: fix typo in comment (#240) Signed-off-by: vetclippy <[email protected]> * pinocchio: Add `pubkey_eq` helper (#248) * Add pubkey_eq helper * Fix typo * Update pubkey comparison * Add proptest * Add unlikely * Replace proptest * pinocchio: Add `AccountInfo` invariant details (#254) Add invariant details * Add account view * Improve account borrows * Add constructor * Add account_ptr method * Add copy feature * Add docs configuration * Rename to address * Use clone * Remove unused * Add missing doc_auto_cfg * Fix lints * Add no_std check * Rename account type --------- Signed-off-by: vetclippy <[email protected]> Co-authored-by: Jean Marchand (Exotic Markets) <[email protected]> Co-authored-by: Leonardo Donatacci <[email protected]> Co-authored-by: Dimitris Apostolou <[email protected]> Co-authored-by: Jon C <[email protected]> Co-authored-by: Sammy Harris <[email protected]> Co-authored-by: vetclippy <[email protected]>
Problem
While working on the refactor of all wasm-js logic into sdk-wasm-js, I noticed that no matter what I did,
SystemInstructionstill made it into the final build. That made it impossible to build the package with breaking changes.Summary of changes
Remove the usage of
SystemInstructionin the tests. This required exposingInstructionandAccountMetathrough wasm-bindgen to be able to create instructions.The API doesn't need to be perfect, just usable, so I went with a builder pattern for adding
AccountMetas to anInstruction, which hugely simplifies the logic. Otherwise we'd need to perform conversions between JS arrays andVec<AccountMeta>, which I was too lazy to do properly because wasm-js crate is more of a proof-of-concept.While doing that, I moved the wasm-js deps to the component crates that were required for the tests.