Skip to content

Commit

Permalink
feat: upgrade sway to 0.64.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz2891 committed Oct 4, 2024
1 parent dedce33 commit c9e5b23
Show file tree
Hide file tree
Showing 16 changed files with 433 additions and 149 deletions.
4 changes: 2 additions & 2 deletions Forc.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[package]]
name = "core"
source = "path+from-root-C485BFF5467B6B6C"
source = "path+from-root-4BA3A18A2D620E67"

[[package]]
name = "redstone"
Expand All @@ -9,5 +9,5 @@ dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.63.6#cba9a005ef2a5b61e13eb02ba6e9e93ebd19a31a"
source = "git+https://github.com/fuellabs/sway?tag=v0.64.0#2156bfbbee01ffb85bfca2aae8f185f8e7c715a4"
dependencies = ["core"]
2 changes: 1 addition & 1 deletion Forc.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
authors = ["Łukasz Kalbarczyk"]
entry = "lib.sw"
forc-version = "0.63.6"
forc-version = "0.64.0"
license = "BUSL"
name = "redstone"
organization = "RedStone"
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ especially of the fuel-connector
package (https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/fuel-connector)
and is subject of all their licenses.

### Usage:
### Usage

📟
Prerequisites: [Read how the RedStone Oracles work](https://docs.redstone.finance/docs/smart-contract-devs/how-it-works).
Prerequisites: [Read how the RedStone Oracles work](https://docs.redstone.finance/docs/Introduction/).

Write the following to your `Forc.toml` file:

```toml
[dependencies]
redstone = { git = "https://github.com/redstone-finance/redstone-fuel-sdk", branch = "sway-0.63.1" }
redstone = { git = "https://github.com/redstone-finance/redstone-fuel-sdk", branch = "testnet-0.64.0" }
```

To process a RedStone payload (with the structure
defined [here](https://docs.redstone.finance/docs/smart-contract-devs/how-it-works#data-packing-off-chain-data-encoding))
To process a RedStone payload (with the structure defined [here](hhttps://docs.redstone.finance/img/payload.png))
for a defined list of `feed_ids`, write the `.sw` file as follows:

```rust
Expand Down
4 changes: 2 additions & 2 deletions fuel-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
channel = "testnet"

[components]
fuel-core = "0.35.0"
forc = "0.63.6"
fuel-core = "0.36.0"
forc = "0.64.0"
2 changes: 1 addition & 1 deletion src/core/aggregation.sw
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Vec<Vec<u256>> {
let mut i = 0;
while (i < self.len()) {
let values = self.get(i).unwrap();
aggregated.push(values.median());
aggregated.push(values.median().unwrap());
i += 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/config.sw
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub struct Config {
pub signer_count_threshold: u64,
/// The current block time in timestamp format, used for verifying data timeliness.
///
/// The value's been expressed in milliseconds since the Unix epoch (January 1, 1970) and allows
/// The value's been expressed in seconds since the Unix epoch (January 1, 1970) and allows
/// for determining whether the data is current in the context of blockchain time.
pub block_timestamp: u64, // unix
pub block_timestamp: u64,
}

impl Config {
Expand Down
129 changes: 115 additions & 14 deletions src/core/config_validation.sw
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
library;

use ::protocol::{data_package::DataPackage, payload::Payload};
use ::protocol::{data_package::DataPackage, data_point::DataPoint, payload::Payload};
use ::core::{config::Config, errors::*, validation::*};
use ::utils::{from_bytes::*, vec::*};
use ::utils::{from_bytes::*, test_helpers::*, vec::*};

trait Validation {
fn validate_timestamps(self, payload: Payload);
fn check_parameters(self);
fn validate_timestamps(self, payload: Payload) -> u64;
fn validate_signer_count(self, values: Vec<Vec<u256>>);
fn validate_signer(self, data_package: DataPackage, index: u64) -> Option<u64>;
fn validate_signer(self, data_package: DataPackage, index: u64) -> u64;
}

impl Validation for Config {
fn validate_timestamps(self, payload: Payload) {
let mut i = 0;
while (i < payload.data_packages.len()) {
let timestamp = payload.data_packages.get(i).unwrap().timestamp / 1000;
let block_timestamp = self.block_timestamp;
fn check_parameters(self) {
assert_signers(self.signers, self.signer_count_threshold);

require(self.feed_ids.len() > 0, RedStoneError::EmptyFeedIds);
require(
self.feed_ids
.find_duplicate()
.is_none(),
RedStoneError::DuplicatedFeedId,
)
}

validate_timestamp(i, timestamp, block_timestamp);
fn validate_timestamps(self, payload: Payload) -> u64 {
let first_timestamp = payload.data_packages.get(0).unwrap().timestamp;
validate_timestamp(first_timestamp, self.block_timestamp * 1000);

let mut i = 1;
while (i < payload.data_packages.len()) {
let timestamp = payload.data_packages.get(i).unwrap().timestamp;
if (timestamp != first_timestamp) {
log(timestamp);
log(first_timestamp);
revert(TIMESTAMP_DIFFERENT_THAN_OTHERS + i);
}

i += 1;
}

first_timestamp
}

fn validate_signer_count(self, results: Vec<Vec<u256>>) {
Expand All @@ -36,19 +56,38 @@ impl Validation for Config {
}
}

fn validate_signer(self, data_package: DataPackage, index: u64) -> Option<u64> {
fn validate_signer(self, data_package: DataPackage, index: u64) -> u64 {
let s = self.signer_index(data_package.signer_address);

if s.is_none() {
log(data_package.signer_address);
log(index);
// revert(SIGNER_NOT_RECOGNIZED + index);
revert(SIGNER_NOT_RECOGNIZED + index);
}

s
s.unwrap()
}
}

fn assert_signers(allowed_signers: Vec<b256>, signer_count_threshold: u64) {
require(
allowed_signers
.len() > 0,
RedStoneError::EmptyAllowedSigners,
);
require(
allowed_signers
.len() >= signer_count_threshold,
RedStoneError::SignerCountThresholdToSmall,
);
require(
allowed_signers
.find_duplicate()
.is_none(),
RedStoneError::DuplicatedSigner,
);
}

#[test]
fn test_validate_one_signer() {
let results = make_results();
Expand All @@ -73,6 +112,68 @@ fn test_validate_three_signers() {
config.validate_signer_count(results);
}

#[test]
fn test_check_parameters_threshold_equal_signer_count() {
let config = make_config(3);

config.check_parameters();
}

#[test(should_revert)]
fn test_check_parameters_should_revert_threshold_bigger_than_signer_count() {
let config = make_config(4);

config.check_parameters();
}

#[test(should_revert)]
fn test_check_parameters_should_revert_empty_signer_list() {
let mut config = make_config(0);

config.signers = Vec::new();
config.check_parameters();
}

#[test(should_revert)]
fn test_check_parameters_should_revert_duplicated_feed_id() {
let mut config = make_config(3);

config.feed_ids = Vec::<u256>::new().with(0x444444u256).with(0x445566u256).with(0x444444u256);
config.check_parameters();
}

#[test(should_revert)]
fn test_check_parameters_should_revert_duplicated_signer() {
let mut config = make_config(3);

config.signers = Vec::<b256>::new().with(b256::from(0x02u256)).with(b256::from(0x02u256)).with(b256::from(0x03u256));
config.check_parameters();
}

#[test]
fn test_validate_timestamps() {
let mut config = make_config(2);

let data_package = DataPackage {
signer_address: b256::from(0x0201u256),
data_points: Vec::<DataPoint>::new().with(DataPoint {
feed_id: 0x445566,
value: 0x01u256,
}).with(DataPoint {
feed_id: 0x554466,
value: 0x02u256,
}),
timestamp: 1234,
};

assert(
config
.validate_timestamps(Payload {
data_packages: Vec::<DataPackage>::new().with(data_package).with(data_package),
}) == 1234,
);
}

fn make_results() -> Vec<Vec<u256>> {
let mut results = Vec::<Vec<u256>>::new();

Expand All @@ -88,7 +189,7 @@ fn make_config(signer_count_threshold: u64) -> Config {

let config = Config {
feed_ids,
signers: Vec::new(),
signers: Vec::<b256>::new().with(b256::from(0x01u256)).with(b256::from(0x02u256)).with(b256::from(0x03u256)),
signer_count_threshold,
block_timestamp: 0,
};
Expand Down
20 changes: 17 additions & 3 deletions src/core/errors.sw
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
library;

// 2621440000 + data_package_index
// 2_621_440_000 + 1/2
pub const TIMESTAMP_OUT_OF_RANGE = 0x9C40_0000;

/// 655360000 + feed_index
// 1_966_080_000 + data_package_index
pub const TIMESTAMP_DIFFERENT_THAN_OTHERS = 0x7530_0000;

/// 655_360_000 + feed_index
pub const INSUFFICIENT_SIGNER_COUNT = 0x2710_0000;

/// 1310720000 + data_package_index
/// 1_310_720_000 + data_package_index
pub const SIGNER_NOT_RECOGNIZED = 0x4e20_0000;

/// convert 3_276_800_000 + signer_count * feed_index + signer_index
pub const DUPLICATED_VALUE_FOR_SIGNER = 0xc350_0000;

pub enum RedStoneError {
EmptyAllowedSigners: (),
EmptyFeedIds: (),
SignerCountThresholdToSmall: (),
DuplicatedSigner: (),
DuplicatedFeedId: (),
}
Loading

0 comments on commit c9e5b23

Please sign in to comment.