Skip to content

Conversation

hal3e
Copy link
Contributor

@hal3e hal3e commented Dec 9, 2024

closes: #1481
closes: #1328 as we can read the configurables directly from the binary

BLOCKED until we have a compiler that supports both abi-errors and dynamic configurables.

Release notes

In this release, we:

  • Added support for dynamic str configurables.
  • Added a new configurables reader to contract, script and predicateabigen that can read configurables directly form the binary.
  • Added struct ConfigurablesReader that can be used to read direct and indirect configurables at runtime and compile time.
  • Added tests for configurables with loades for contracts, scripts and predicates.
  • Updated documentation.

Summary

Breaking Changes

  • Contract Regular and Loader methods code(), contract_id(), code_root, state_root return Result
  • Executable Regular and Loader methods code(), data_offset_in_code, loader_code return Result
  • Configurables's update_constants_in returns Result
  • Predicate's with_configurable returns Result
  • AbiFormatter's decode_configurables argument configurable_data now accepts a &[u8]

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

@hal3e hal3e added the blocked label Dec 9, 2024
@hal3e hal3e self-assigned this Dec 9, 2024
Copy link
Contributor

@segfault-magnet segfault-magnet left a comment

Choose a reason for hiding this comment

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

We should probably hold off on merging this until the sway branch catches up with sway master.

I have a feeling it's a bit behind seeing how the configurables offset is 8..16 and not 16..24 seeing as we already support the latter.

Either wait or integrate with the work done in the PR mentioned above so that we detect the compiler version used to generate the sway binary and use 16..24 if it is a modern compiler or 8..16 if it is the older one.

@@ -51,10 +51,11 @@ mod code_types {
}
}

pub(crate) fn code(&self) -> Vec<u8> {
pub(crate) fn code(&self) -> Result<Vec<u8>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wdyt about having with_code and with_configurables return a Result, trying to apply the old configurables to the new code or the new configurables to the old code.

If that passes then we can .expect on all the getters and not have Result everywhere.

Nice consequence is that it will fail closer to the thing that made it fail -- ie the code or the configurables that were invalid, not later on when you try and calculate the contract id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

In the end you chose to not do it @hal3e ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hal3e hal3e requested a review from a team as a code owner April 11, 2025 11:26
@AurelienFT AurelienFT requested review from AurelienFT and removed request for digorithm and Salka1988 April 11, 2025 15:06
Copy link
Contributor

@AurelienFT AurelienFT left a comment

Choose a reason for hiding this comment

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

I don't understand all the details of each line and interactions but I understood the tests and the main idea and didn't see anything that shocked me. If the tests added pass I think the PR answers the need from an un-educated point of view.

In any cases thank you to not let long time PR die !

@@ -51,10 +51,11 @@ mod code_types {
}
}

pub(crate) fn code(&self) -> Vec<u8> {
pub(crate) fn code(&self) -> Result<Vec<u8>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the end you chose to not do it @hal3e ?

new_enum,
6,
"sway-sway".try_into()?,
"forc".try_into()?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to explain why "forc" here. I understood that it's the default configurable in the sway contract but it requires a lot of context for this magic word

Copy link
Contributor Author

@hal3e hal3e May 26, 2025

Choose a reason for hiding this comment

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

It is just a random string we encode and decode. It is hard-coded in the binary and defined in the main.sw file

Ok(base_offset_script(consensus_parameters) + padded_len)
}

pub fn extract_data_offset(binary: &[u8]) -> Result<usize> {
extract_offset_at(binary, 8)
Copy link
Contributor

Choose a reason for hiding this comment

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

Might not compile due to type of size_of returns but the idea :

Suggested change
extract_offset_at(binary, 8)
extract_offset_at(binary, size_of::<usize>())

}

pub fn extract_offset_at(binary: &[u8], offset: usize) -> Result<usize> {
if binary.len() < offset + 8 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might not compile due to type of size_of returns but the idea :

Suggested change
if binary.len() < offset + 8 {
if binary.len() < offset + size_of::<usize>() {

@@ -3,53 +3,297 @@ pub mod traits;
pub mod types;
Copy link
Contributor

Choose a reason for hiding this comment

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

All the functions seems to use a lot of offset in a lot of different orders and modifying some buffers that doesn't make sense for a first-time reader. Can you maybe add some documentation at the top of this file for the module and maybe document the functions you created.

EDIT: The documentation on the tests helps a lot to understands the data we are editing here thanks a lot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hal3e hal3e added the blocked label May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid configurables lead to panic Querying configurable constants and storage from SDK
6 participants