forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the
TIME
instruction (FuelLabs#2752)
* Add support for the time instruction * Add a test * Add a test * Address some comments
- Loading branch information
1 parent
d1700ad
commit 095bfe0
Showing
16 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
test/src/sdk-harness/test_artifacts/block_test_abi/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
14 changes: 14 additions & 0 deletions
14
test/src/sdk-harness/test_artifacts/block_test_abi/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[[package]] | ||
name = 'block_test_abi' | ||
source = 'root' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-C0259B895CF50289' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-C0259B895CF50289' | ||
dependencies = ['core'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "block_test_abi" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../sway-lib-std" } |
11 changes: 11 additions & 0 deletions
11
test/src/sdk-harness/test_artifacts/block_test_abi/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
library block_test_abi; | ||
|
||
abi BlockTest { | ||
fn get_block_height() -> u64; | ||
|
||
fn get_timestamp() -> u64; | ||
|
||
fn get_timestamp_of_block(block_height: u64) -> u64; | ||
|
||
fn get_block_and_timestamp() -> (u64, u64); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[[package]] | ||
name = 'block' | ||
source = 'root' | ||
dependencies = [ | ||
'block_test_abi', | ||
'std', | ||
] | ||
|
||
[[package]] | ||
name = 'block_test_abi' | ||
source = 'path+from-root-9A6BFB98DBD0352C' | ||
dependencies = ['std'] | ||
|
||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-9A6BFB98DBD0352C' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-9A6BFB98DBD0352C' | ||
dependencies = ['core'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "block" | ||
|
||
[dependencies] | ||
block_test_abi = { path = "../../test_artifacts/block_test_abi" } | ||
std = { path = "../../../../../sway-lib-std" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use fuels::prelude::*; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
abigen!( | ||
BlockTestContract, | ||
"test_projects/block/out/debug/block-abi.json" | ||
); | ||
|
||
async fn get_block_instance() -> (BlockTestContract, ContractId) { | ||
let wallet = launch_provider_and_get_wallet().await; | ||
let id = Contract::deploy( | ||
"test_projects/block/out/debug/block.bin", | ||
&wallet, | ||
TxParameters::default(), | ||
StorageConfiguration::with_storage_path(Some( | ||
"test_projects/block/out/debug/block-storage_slots.json".to_string(), | ||
)), | ||
) | ||
.await | ||
.unwrap(); | ||
let instance = BlockTestContractBuilder::new(id.to_string(), wallet).build(); | ||
|
||
(instance, id.into()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn can_get_block_height() { | ||
let (instance, _id) = get_block_instance().await; | ||
let block_0 = instance.get_block_height().call().await.unwrap(); | ||
let block_1 = instance.get_block_height().call().await.unwrap(); | ||
let block_2 = instance.get_block_height().call().await.unwrap(); | ||
|
||
// Probably consecutive blocks but we may have multiple tx per block so be conservative to | ||
// guarantee the stability of the test | ||
assert!(block_1.value <= block_0.value + 1); | ||
assert!(block_2.value <= block_1.value + 1); | ||
} | ||
|
||
#[tokio::test] | ||
async fn can_get_timestamp() { | ||
let (instance, _id) = get_block_instance().await; | ||
let block_0_time = instance.get_timestamp().call().await.unwrap(); | ||
let now = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Time went backwards"); | ||
|
||
// This should really be zero in most cases, but be conservative to guarantee the stability of | ||
// the test | ||
assert!(now.as_secs() - block_0_time.value <= 1); | ||
|
||
// Wait 1 seconds and request another block | ||
sleep(Duration::from_secs(1)).await; | ||
let block_1_time = instance.get_timestamp().call().await.unwrap(); | ||
|
||
// The difference should be 1 second in most cases, but be conservative to guarantee the | ||
// stability of the test | ||
assert!( | ||
1 <= block_1_time.value - block_0_time.value | ||
&& block_1_time.value - block_0_time.value <= 2 | ||
); | ||
// Wait 2 seconds and request another block | ||
sleep(Duration::from_secs(2)).await; | ||
let block_2_time = instance.get_timestamp().call().await.unwrap(); | ||
|
||
// The difference should be 2 seconds in most cases, but be conservative to guarantee the | ||
// stability of the test | ||
assert!( | ||
2 <= block_2_time.value - block_1_time.value | ||
&& block_2_time.value - block_1_time.value <= 3 | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn can_get_timestamp_of_block() { | ||
let (instance, _id) = get_block_instance().await; | ||
|
||
let block_0 = instance.get_block_and_timestamp().call().await.unwrap(); | ||
|
||
sleep(Duration::from_secs(1)).await; | ||
let block_1 = instance.get_block_and_timestamp().call().await.unwrap(); | ||
|
||
sleep(Duration::from_secs(2)).await; | ||
let block_2 = instance.get_block_and_timestamp().call().await.unwrap(); | ||
|
||
// Check that the result of `timestamp_of_block` matches the recorded result of `timestamp()` | ||
// above called via `get_block_and_timestamp`. | ||
assert_eq!( | ||
instance | ||
.get_timestamp_of_block(block_0.value.0) | ||
.call() | ||
.await | ||
.unwrap() | ||
.value, | ||
block_0.value.1 | ||
); | ||
assert_eq!( | ||
instance | ||
.get_timestamp_of_block(block_1.value.0) | ||
.call() | ||
.await | ||
.unwrap() | ||
.value, | ||
block_1.value.1 | ||
); | ||
assert_eq!( | ||
instance | ||
.get_timestamp_of_block(block_2.value.0) | ||
.call() | ||
.await | ||
.unwrap() | ||
.value, | ||
block_2.value.1 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
contract; | ||
|
||
use std::block::{height, timestamp, timestamp_of_block}; | ||
use block_test_abi::BlockTest; | ||
|
||
impl BlockTest for Contract { | ||
fn get_block_height() -> u64 { | ||
height() | ||
} | ||
|
||
fn get_timestamp() -> u64 { | ||
timestamp() | ||
} | ||
|
||
fn get_timestamp_of_block(block_height: u64) -> u64 { | ||
timestamp_of_block(block_height) | ||
} | ||
|
||
fn get_block_and_timestamp() -> (u64, u64) { | ||
( | ||
height(), | ||
timestamp(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Add test modules here: | ||
|
||
mod auth; | ||
mod block; | ||
mod call_frames; | ||
mod context; | ||
mod evm; | ||
|