Skip to content

Commit

Permalink
updating documentation for u256
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Sep 12, 2023
1 parent 544002e commit 8831f95
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 240 deletions.
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; and hashes (the `b256` type) are not stored in registers but rather in memory. A `b256` is therefore a pointer to a 32-byte memory region containing the hash value.

## 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,6 +13,7 @@ Sway has the following primitive types:
2. `u16` (16-bit unsigned integer)
3. `u32` (32-bit unsigned integer)
4. `u64` (64-bit unsigned integer)
4. `u256` (256-bit unsigned integer)
5. `hexadecimal`, `binary` & `base-10` syntax
2. [Boolean](boolean.md)
1. `bool` (true or false)
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
62 changes: 41 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,17 @@ struct TestContext {
deployed_contracts: Arc<Mutex<HashMap<String, ContractId>>>,
}

fn print_receipt(receipt: &Receipt) {
match receipt {
Receipt::ReturnData { data, .. } => {
if let Some(data) = data {
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 +101,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 +154,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 +337,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 +443,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
13 changes: 0 additions & 13 deletions test/src/e2e_vm_tests/test_programs/should_fail/u256/Forc.lock

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8831f95

Please sign in to comment.