Skip to content

Commit

Permalink
Correct code_size function implementation (#6304)
Browse files Browse the repository at this point in the history
## Description
The current code_size function returns a pointer, rather than the value,
this pr corrects this mistake.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: IGI-111 <igi-111@protonmail.com>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent da0caf9 commit 68b7b34
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 4 additions & 3 deletions sway-lib-std/src/call_frames.sw
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ pub fn msg_asset_id() -> AssetId {
/// }
/// ```
pub fn code_size() -> u64 {
asm(size, ptr, offset: 576) {
let ptr = asm(size, ptr, offset: 576) {
add size fp offset;
size: u64
}
size: raw_ptr
};
ptr.read::<u64>()
}

/// Get the first parameter from the current call frame.
Expand Down
9 changes: 3 additions & 6 deletions test/src/sdk-harness/test_projects/call_frames/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use fuel_vm::consts::VM_MAX_RAM;
use fuels::{accounts::wallet::WalletUnlocked, prelude::*, types::ContractId};

abigen!(Contract(
Expand Down Expand Up @@ -36,7 +35,9 @@ async fn can_get_id_contract_id_this() {
async fn can_get_code_size() {
let (instance, _id) = get_call_frames_instance().await;
let result = instance.methods().get_code_size().call().await.unwrap();
assert!(is_within_range(result.value));
// Check if codesize is between 1000 and 7000. Arbitrary endpoints, current codesize is 6816
// but the lower bound future proofs against compiler optimizations
assert!(result.value > 1000 && result.value < 7000);
}

#[tokio::test]
Expand Down Expand Up @@ -110,7 +111,3 @@ async fn can_get_second_param_multiple_params2() {
.unwrap();
assert_eq!(result.value, (300, expected_struct, expected_struct2));
}

fn is_within_range(n: u64) -> bool {
n > 0 && n <= VM_MAX_RAM
}

0 comments on commit 68b7b34

Please sign in to comment.