Skip to content

Commit

Permalink
fix u256 at contract storage (FuelLabs#5139)
Browse files Browse the repository at this point in the history
## Description

This PR is part of FuelLabs#4794. `u256`
was not being considered a reference type which was creating some issues
when using it in `storage`. This PR fixes this problem.

## 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).
- [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.
  • Loading branch information
xunilrj authored Sep 26, 2023
1 parent 9c30219 commit 21c48d6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 39 deletions.
22 changes: 10 additions & 12 deletions sway-core/src/ir_generation/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ pub fn serialize_to_storage_slots(
),
)]
}
ConstantValue::U256(n) if ty.is_uint_of(context, 256) => {
ConstantValue::U256(b) if ty.is_uint_of(context, 256) => {
vec![StorageSlot::new(
get_storage_key(ix, indices),
Bytes32::new(n.to_be_bytes()),
Bytes32::new(b.to_be_bytes()),
)]
}
ConstantValue::B256(b) if ty.is_b256(context) => {
Expand Down Expand Up @@ -163,16 +163,14 @@ pub fn serialize_to_words(constant: &Constant, context: &Context, ty: &Type) ->
ConstantValue::Uint(n) if ty.is_uint(context) => {
vec![Bytes8::new(n.to_be_bytes())]
}
ConstantValue::U256(n) if ty.is_uint_of(context, 256) => n
.to_be_bytes()
.chunks_exact(8)
.map(|x| Bytes8::new(x.try_into().expect("unexpected array size")))
.collect(),
ConstantValue::B256(b) if ty.is_b256(context) => b
.to_be_bytes()
.chunks_exact(8)
.map(|x| Bytes8::new(x.try_into().expect("unexpected array size")))
.collect(),
ConstantValue::U256(b) if ty.is_uint_of(context, 256) => {
let b = b.to_be_bytes();
Vec::from_iter((0..4).map(|i| Bytes8::new(b[8 * i..8 * i + 8].try_into().unwrap())))
}
ConstantValue::B256(b) if ty.is_b256(context) => {
let b = b.to_be_bytes();
Vec::from_iter((0..4).map(|i| Bytes8::new(b[8 * i..8 * i + 8].try_into().unwrap())))
}
ConstantValue::String(s) if ty.is_string_array(context) => {
// Turn the bytes into serialized words (Bytes8).
let mut s = s.clone();
Expand Down
5 changes: 4 additions & 1 deletion sway-core/src/type_system/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,10 @@ impl TypeInfo {
matches!(
self,
TypeInfo::Boolean
| TypeInfo::UnsignedInteger(_)
| TypeInfo::UnsignedInteger(IntegerBits::Eight)
| TypeInfo::UnsignedInteger(IntegerBits::Sixteen)
| TypeInfo::UnsignedInteger(IntegerBits::ThirtyTwo)
| TypeInfo::UnsignedInteger(IntegerBits::SixtyFour)
| TypeInfo::RawUntypedPtr
| TypeInfo::Numeric
) || self.is_unit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0x2b30d419a93547d7dfd49efb63805fbaeefe72300c1d5028a313c998457c301d);

let addr = abi(BasicStorage, 0xf01862cb7b641c642815d71795e9ed7d7daf10158ef3a20e8e32045de97bee0a);
let key = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ script;
use increment_abi::Incrementor;

fn main() -> bool {
let the_abi = abi(Incrementor, 0x6c5be5390a29e256039d8a068bbaedddc04a9f95ad7da0c32c1394c1d9669657);
let the_abi = abi(Incrementor, 0xdeb965022530a5e114faa0a5214c7f7a19feba73c5218938615186c60feb9813);
the_abi.increment(5);
the_abi.increment(5);
let result = the_abi.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use storage_access_abi::*;
use std::hash::*;

fn main() -> bool {
let contract_id = 0xaaa573b599fe1519503e9d82cfc08c1fe7ef4ff2c136c178bc751165c8cb90b9;
let contract_id = 0xcbaac3d1670ec094eef042052fb49479f639d0120709ac58dad46a6b80cb6550;
let caller = abi(StorageAccess, contract_id);

// Test initializers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,44 +217,76 @@
"logId": 2,
"loggedType": {
"name": "",
"type": 8,
"type": 9,
"typeArguments": null
}
},
{
"logId": 3,
"loggedType": {
"name": "",
"type": 8,
"type": 9,
"typeArguments": null
}
},
{
"logId": 4,
"loggedType": {
"name": "",
"type": 1,
"type": 8,
"typeArguments": null
}
},
{
"logId": 5,
"loggedType": {
"name": "",
"type": 1,
"type": 8,
"typeArguments": null
}
},
{
"logId": 6,
"loggedType": {
"name": "",
"type": 1,
"type": 8,
"typeArguments": null
}
},
{
"logId": 7,
"loggedType": {
"name": "",
"type": 8,
"typeArguments": null
}
},
{
"logId": 8,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
},
{
"logId": 9,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
},
{
"logId": 10,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
},
{
"logId": 11,
"loggedType": {
"name": "",
"type": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"key": "71217a24656901c411894bb65eb78a828dafa5a6844488ef5024eb5ac0cff79c",
"value": "0000000000000000000000000000000000000000000000000000000000000001"
"value": "0000000000000000000000000000000000000000000000000000000001234567"
},
{
"key": "7f91d1a929dce734e7f930bbb279ccfccdb5474227502ea8845815c74bd930a7",
Expand Down Expand Up @@ -41,7 +41,7 @@
},
{
"key": "c7e08cdde76020f08f4ce5c3257422ae67f9676992689b64b85f35aa58752d9e",
"value": "0000000000000000000000000000000000000000000000000000000000000001"
"value": "0000000000000000000000000000000000000000000000000000000001234567"
},
{
"key": "d02e07f5a716bd3b6670aaf9a73352164e6b946c24db14f72005b7029e67d96a",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use basic_storage_abi::*;
const C1 = 1;
const S5 = __to_str_array("aaaaa");

const CONST_U256 = 0x0000000000000000000000000000000000000000000000000000000000000001u256;
const CONST_B256 = 0x0000000000000000000000000000000000000000000000000000000000000001;

storage {
c1: u64 = C1,
str0: str[0] = __to_str_array(""),
Expand All @@ -21,9 +18,9 @@ storage {
str8: str[8] = __to_str_array("aaaaaaaa"),
str9: str[9] = __to_str_array("aaaaaaaaa"),
str10: str[10] = __to_str_array("aaaaaaaaaa"),
storage_u256: u256 = CONST_U256,
storage_b256: b256 = CONST_B256,

const_u256: u256 = 0x0000000000000000000000000000000000000000000000000000000001234567u256,
const_b256: b256 = 0x0000000000000000000000000000000000000000000000000000000001234567,
}

impl BasicStorage for Contract {
Expand Down Expand Up @@ -210,17 +207,17 @@ fn test_storage() {
assert_streq(storage.str9.read(), "aaaaaaaaa");
assert_streq(storage.str10.read(), "aaaaaaaaaa");

assert_streq(S5, "aaaaa");

assert_eq(storage.c1.read(), C1);
storage.c1.write(2);
assert_eq(storage.c1.read(), 2);

assert_eq(storage.const_u256.read(), 0x0000000000000000000000000000000000000000000000000000000001234567u256);
storage.const_u256.write(0x0000000000000000000000000000000000000000000000000000000012345678u256);
assert_eq(storage.const_u256.read(), 0x0000000000000000000000000000000000000000000000000000000012345678u256);

// assert_eq(storage.storage_u256.read(), CONST_U256);
storage.storage_u256.write(CONST_U256);
assert_eq(storage.storage_u256.read(), CONST_U256);

assert_eq(storage.storage_b256.read(), CONST_B256);
storage.storage_b256.write(CONST_B256);
assert_eq(storage.storage_b256.read(), CONST_B256);
assert_eq(storage.const_b256.read(), 0x0000000000000000000000000000000000000000000000000000000001234567);
storage.const_b256.write(0x0000000000000000000000000000000000000000000000000000000012345678);
assert_eq(storage.const_b256.read(), 0x0000000000000000000000000000000000000000000000000000000012345678);
}

// If these comparisons are done inline just above then it blows out the register allocator due to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
category = "compile"
validate_abi = true
validate_storage_slots = true
expected_warnings = 1

0 comments on commit 21c48d6

Please sign in to comment.