Skip to content
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

[x/programs] Improve State-keys & Argument Passing #623

Merged
merged 78 commits into from
Dec 12, 2023

Conversation

samliok
Copy link
Contributor

@samliok samliok commented Nov 20, 2023

This PR improves argument passing from rust programs to host functions. It also removes the need to call the .to_vec() function on StateKeys, and generalizes external function calls by defaulting to borsh serialization.

Changes

  • All argument types are now serialized using borsh when being passed to/from host.
  • All arguments passed in/from the host are SmartPtr' which are int64 with the format [length][ptr]. Length and Ptr are 4 bytes each.
    • improves function signatures
      • ex. fn _put(caller_id: i64, key: *const u8, value: *const u8) -> i32;
      • instead of fn _put(caller_id: i64, key_ptr: *const u8, key_len: usize, value_ptr: *const u8, value_len: usize) -> i32;
    • this removes the need for the len host function call.
    • get(key) no longer requires calling the len host function call
  • add an Into<Key> implementation for the #[state_keys] proc macro
  • update call_program function to require BorshSerialize instead of Argument trait
    • improves usability for call_program & external function calls
    • the params! declarative macro serializes arguments required for external function calls
  • Update go to use SmartPtr and consolidate associated helper functions into runtime/utils.go

also a small ux improvement from macro change

before

    program
        .state()
        .store(StateKey::TotalSupply.to_vec(), &123456789_i64)
        .expect("failed to store total supply");

now

    program
        .state()
        .store(StateKey::TotalSupply, &123456789_i64)
        .expect("failed to store total supply");

richardpringle
richardpringle previously approved these changes Dec 11, 2023
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
x/programs/runtime/utils.go Outdated Show resolved Hide resolved
Copy link
Contributor

@richardpringle richardpringle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@patrick-ogrady patrick-ogrady merged commit ad3ac94 into main Dec 12, 2023
22 checks passed
@patrick-ogrady patrick-ogrady deleted the refactor-statekey branch December 12, 2023 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
4 participants