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

updating documentation for u256 #5111

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/book/src/basics/built_in_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Sway has the following primitive types:
1. `u16` (16-bit unsigned integer)
1. `u32` (32-bit unsigned integer)
1. `u64` (64-bit unsigned integer)
1. `u256` (256-bit unsigned integer)
1. `str[]` (fixed-length string)
1. `str` (string slices)
1. `bool` (Boolean `true` or `false`)
Expand Down Expand Up @@ -44,12 +45,14 @@ Numbers can be declared with binary syntax, hexadecimal syntax, base-10 syntax,
<!-- default_num:example:start -->
The default numeric type is `u64`. The FuelVM's word size is 64 bits, and the cases where using a smaller numeric type saves space are minimal.

If a 64-bit arithmetic operation produces an overflow or an underflow,
If a 64-bit or 256-bit arithmetic operation produces an overflow or an underflow,
computation gets reverted automatically by FuelVM.

8/16/32-bit arithmetic operations are emulated using their 64-bit analogues with
additional overflow/underflow checks inserted, which generally results in
somewhat higher gas consumption.

The same does not happen with 256-bit operations, including `b256`, which uses specialized operations and are as performant as possible.
<!-- default_num:example:end -->

## Boolean Type
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/basics/comments_and_logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Note that `ra` will include the value being logged. The additional registers `rc

<!-- This section should explain when `LogData` receipts are produced -->
<!-- log_data_rec:example:start -->
`LogData` is generated for _reference_ types which include all types except for _non_reference_ types.
`LogData` is generated for _reference_ types which include all types except for _non_reference_ types; and for _non-reference_ types bigger than 64-bit integers, for example, `u256`;
<!-- log_data_rec:example:end -->

For example, logging a `b256` variable `b` that holds the value `0x1111111111111111111111111111111111111111111111111111111111111111` using `log(b)` may generate the following receipt:
Expand Down
28 changes: 14 additions & 14 deletions docs/book/src/reference/compiler_intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ __eq<T>(lhs: T, rhs: T) -> bool

**Description:** Returns whether `lhs` and `rhs` are equal.

**Constraints:** `T` is `bool`, `u8`, `u16`, `u32`, `u64`, or `raw_ptr`.
**Constraints:** `T` is `bool`, `u8`, `u16`, `u32`, `u64`, `u256`, `b256` or `raw_ptr`.

___

Expand All @@ -90,7 +90,7 @@ __gt<T>(lhs: T, rhs: T) -> bool

**Description:** Returns whether `lhs` is greater than `rhs`.

**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

```sway
Expand All @@ -99,7 +99,7 @@ __lt<T>(lhs: T, rhs: T) -> bool

**Description:** Returns whether `lhs` is less than `rhs`.

**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

```sway
Expand Down Expand Up @@ -178,7 +178,7 @@ __add<T>(lhs: T, rhs: T) -> T

**Description:** Adds `lhs` and `rhs` and returns the result.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.

___

Expand All @@ -188,7 +188,7 @@ __sub<T>(lhs: T, rhs: T) -> T

**Description:** Subtracts `rhs` from `lhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.

___

Expand All @@ -198,7 +198,7 @@ __mul<T>(lhs: T, rhs: T) -> T

**Description:** Multiplies `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.

___

Expand All @@ -208,7 +208,7 @@ __div<T>(lhs: T, rhs: T) -> T

**Description:** Divides `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.

___

Expand All @@ -218,7 +218,7 @@ __and<T>(lhs: T, rhs: T) -> T

**Description:** Bitwise AND `lhs` and `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.

___

Expand All @@ -228,7 +228,7 @@ __or<T>(lhs: T, rhs: T) -> T

**Description:** Bitwise OR `lhs` and `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.

___

Expand All @@ -238,7 +238,7 @@ __xor<T>(lhs: T, rhs: T) -> T

**Description:** Bitwise XOR `lhs` and `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

```sway
Expand All @@ -247,7 +247,7 @@ __mod<T>(lhs: T, rhs: T) -> T

**Description:** Modulo of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`.
___

```sway
Expand All @@ -256,7 +256,7 @@ __rsh<T>(lhs: T, rhs: u64) -> T

**Description:** Logical right shift of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

```sway
Expand All @@ -265,7 +265,7 @@ __lsh<T>(lhs: T, rhs: u64) -> T

**Description:** Logical left shift of `lhs` by `rhs`.

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___

```sway
Expand Down Expand Up @@ -314,5 +314,5 @@ __not(op: T) -> T

**Description:** Bitwise NOT of `op`

**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`.
**Constraints:** `T` is an integer type, i.e. `u8`, `u16`, `u32`, `u64`, `u256`, `b256`.
___
4 changes: 2 additions & 2 deletions docs/book/src/reference/solidity_differences.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ The underlying virtual machine targeted by Sway is the FuelVM, specified [here](

## Word Size

Words in the FuelVM are 64 bits (8 bytes), rather than the 256 bits (32 bytes) of the EVM. Therefore, primitive integers only go up to `u64`, and hashes (the `b256` type) are not in registers but rather in memory. A `b256` is therefore a pointer to a 32-byte memory region containing the hash value.
Words in the FuelVM are 64 bits (8 bytes), rather than the 256 bits (32 bytes) of the EVM. Therefore, all primitive integers smaller and including `u64` are stored in registers; `u256`, being bigger than the registers, and hashes (the `b256` type) are not stored in registers but rather in memory. They are therefore pointers to a 32-byte memory region containing the their data.

## Unsigned Integers Only

Only unsigned integers are provided as primitives: `u8`, `u16`, `u32`, and `u64`. Signed integer arithmetic is not available in the FuelVM. Signed integers and signed integer arithmetic can be implemented in high-level libraries if needed.
Only unsigned integers are provided as primitives: `u8`, `u16`, `u32`, `u64`, and `u256`. Signed integer arithmetic is not available in the FuelVM. Signed integers and signed integer arithmetic can be implemented in high-level libraries if needed.

## Global Revert

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Sway has the following primitive types:
2. `u16` (16-bit unsigned integer)
3. `u32` (32-bit unsigned integer)
4. `u64` (64-bit unsigned integer)
5. `hexadecimal`, `binary` & `base-10` syntax
5. `u256` (256-bit unsigned integer)
6. `hexadecimal`, `binary` & `base-10` syntax
2. [Boolean](boolean.md)
1. `bool` (true or false)
3. [Strings](string.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/src/documentation/operations/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Each call to `log` appends 1 of 2 types of a [`receipt`](https://fuellabs.github
- [`Log`](https://fuellabs.github.io/fuel-specs/master/protocol/abi/receipts.html#log-receipt)
- Generated for _non-reference_ types: `bool`, `u8`, `u16`, `u32`, and `u64`
- [`LogData`](https://fuellabs.github.io/fuel-specs/master/protocol/abi/receipts.html#logdata-receipt)
- Generated for _reference_ types
- Generated for _reference_ types and `u256`

The [Rust](https://fuellabs.github.io/fuels-rs/latest/) & [Typescript](https://fuellabs.github.io/fuels-ts/) SDKs may be used to decode the data.

Expand Down
60 changes: 39 additions & 21 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ struct TestContext {
deployed_contracts: Arc<Mutex<HashMap<String, ContractId>>>,
}

fn print_receipt(receipt: &Receipt) {
if let Receipt::ReturnData {
data: Some(data), ..
} = receipt
{
println!("Data: {:?}", data);
}
}

impl TestContext {
async fn deploy_contract(&self, contract_path: String) -> Result<ContractId> {
let mut deployed_contracts = self.deployed_contracts.lock().await;
Expand All @@ -90,7 +99,7 @@ impl TestContext {
},
)
}
async fn run(&self, test: TestDescription, output: &mut String) -> Result<()> {
async fn run(&self, test: TestDescription, output: &mut String, verbose: bool) -> Result<()> {
let context = self;
let TestDescription {
name,
Expand Down Expand Up @@ -143,6 +152,12 @@ impl TestContext {
let result = harness::runs_in_vm(compiled.clone(), script_data)?;
let result = match result {
harness::VMExecutionResult::Fuel(state, receipts) => {
if verbose {
for receipt in receipts.iter() {
print_receipt(receipt);
}
}

match state {
ProgramState::Return(v) => TestResult::Return(v),
ProgramState::ReturnData(digest) => {
Expand Down Expand Up @@ -320,24 +335,27 @@ impl TestContext {
*output = out;

result.map(|tested_pkgs| {
let failed: Vec<String> = tested_pkgs
.into_iter()
.flat_map(|tested_pkg| {
tested_pkg
.tests
.into_iter()
.filter(|test| !test.passed())
.map(move |test| {
format!(
"{}: Test '{}' failed with state {:?}, expected: {:?}",
tested_pkg.built.descriptor.name,
test.name,
test.state,
test.condition,
)
})
})
.collect();
let mut failed = vec![];
for pkg in tested_pkgs {
for test in pkg.tests.into_iter() {
if verbose {
println!("Test: {} {}", test.name, test.passed());
for log in test.logs.iter() {
println!("{:?}", log);
}
}

if !test.passed() {
failed.push(format!(
"{}: Test '{}' failed with state {:?}, expected: {:?}",
pkg.built.descriptor.name,
test.name,
test.state,
test.condition,
));
}
}
}

if !failed.is_empty() {
println!("FAILED!! output:\n{}", output);
Expand Down Expand Up @@ -423,11 +441,11 @@ pub async fn run(filter_config: &FilterConfig, run_config: &RunConfig) -> Result

let result = if !filter_config.first_only {
context
.run(test, &mut output)
.run(test, &mut output, run_config.verbose)
.instrument(tracing::trace_span!("E2E", i))
.await
} else {
context.run(test, &mut output).await
context.run(test, &mut output, run_config.verbose).await
};

if let Err(err) = result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ license = "Apache-2.0"
name = "ops"

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
std = { path = "../../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [
{
"logId": 0,
"loggedType": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"messagesTypes": [],
"types": [
{
"components": null,
"type": "u256",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
script;

fn main() -> u256 {
log(0x00000000000000000000000000000000000000000000000000000002u256);
0x00000000000000000000000000000000000000000000000000000001u256
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "run"
expected_result = { action = "return_data", value = "0000000000000000000000000000000000000000000000000000000000000017" }
expected_result = { action = "return_data", value = "0000000000000000000000000000000000000000000000000000000000000001" }
validate_abi = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-969A3D730A6A09C1'

[[package]]
name = 'ops'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-969A3D730A6A09C1'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "ops"

[dependencies]
std = { path = "../../../../../../../../sway-lib-std" }
Loading
Loading