Skip to content

Commit

Permalink
Sway 0.63.6 (#8)
Browse files Browse the repository at this point in the history
* chore: fuel optimizations

* feat: fuel mainnet
  • Loading branch information
Lukasz2891 authored Sep 19, 2024
1 parent 9868789 commit dedce33
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 105 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-D19F39D92CA9923C"
source = "path+from-root-C485BFF5467B6B6C"

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

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.63.1#169f91ae0a6a698bd1cb459c4c203bab646a38ec"
source = "git+https://github.com/fuellabs/sway?tag=v0.63.6#cba9a005ef2a5b61e13eb02ba6e9e93ebd19a31a"
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.1"
forc-version = "0.63.6"
license = "BUSL"
name = "redstone"
organization = "RedStone"
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.34.0"
forc = "0.63.1"
fuel-core = "0.35.0"
forc = "0.63.6"
2 changes: 1 addition & 1 deletion src/core/aggregation.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library;

use std::vec::*;
use ::utils::{from_bytes::*, vec::*};
use ::utils::{from_bytes::*, test_helpers::*, vec::*};

impl Vec<Vec<u256>> {
pub fn aggregated(self) -> Vec<u256> {
Expand Down
46 changes: 23 additions & 23 deletions src/core/config_validation.sw
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,6 @@ impl Validation for Config {
}
}

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

let set1 = Vec::<u256>::new().with(0x111u256).with(0x777u256);
let set2 = Vec::<u256>::new().with(0x444u256).with(0x555u256).with(0x666u256);
let set3 = Vec::<u256>::new().with(0x222u256).with(0x333u256);

results.with(set1).with(set2).with(set3)
}

fn make_config(signer_count_threshold: u64) -> Config {
let feed_ids = Vec::<u256>::new().with(0x444444u256).with(0x445566u256).with(0x556644u256);

let config = Config {
feed_ids,
signers: Vec::new(),
signer_count_threshold,
block_timestamp: 0,
};

config
}

#[test]
fn test_validate_one_signer() {
let results = make_results();
Expand All @@ -95,3 +72,26 @@ fn test_validate_three_signers() {

config.validate_signer_count(results);
}

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

let set1 = Vec::<u256>::new().with(0x111u256).with(0x777u256);
let set2 = Vec::<u256>::new().with(0x444u256).with(0x555u256).with(0x666u256);
let set3 = Vec::<u256>::new().with(0x222u256).with(0x333u256);

results.with(set1).with(set2).with(set3)
}

fn make_config(signer_count_threshold: u64) -> Config {
let feed_ids = Vec::<u256>::new().with(0x444444u256).with(0x445566u256).with(0x556644u256);

let config = Config {
feed_ids,
signers: Vec::new(),
signer_count_threshold,
block_timestamp: 0,
};

config
}
14 changes: 7 additions & 7 deletions src/crypto/recover.sw
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
use ::utils::sample::{SAMPLE_ID_V27, SAMPLE_ID_V28, SampleDataPackage};

pub fn recover_signer_address(signature_bytes: Bytes, signable_bytes: Bytes) -> b256 {
let (r_bytes, mut s_bytes) = signature_bytes.slice_tail_offset(32, 1);
let (r_bytes, s_bytes) = signature_bytes.slice_tail_offset(32, 1);
let v = signature_bytes.get(signature_bytes.len() - 1).unwrap();
let r_number = b256::from_be_bytes(r_bytes);
let s_number = b256::from_be_bytes(s_bytes);
Expand All @@ -38,13 +38,13 @@ fn recover_public_address(
v: u8,
msg_hash: b256,
) -> Result<EvmAddress, EcRecoverError> {
let mut v_256: b256 = ZERO_B256;
if (v == 28) {
v_256 = 0x0000000000000000000000000000000000000000000000000000000000000001;
}

let mut s_with_parity = s | (v_256 << 255);
let v_256 = (if (v == 28) {
0x0000000000000000000000000000000000000000000000000000000000000001
} else {
ZERO_B256
});

let s_with_parity = s | (v_256 << 255);
let signature = B512::from((r, s_with_parity));

ec_recover_evm_address(signature, msg_hash)
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/data_package.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library;

use std::{bytes::*, logging::log, vec::Vec};
use ::crypto::recover::recover_signer_address;
use ::utils::{bytes::*, from_bytes::FromBytes, sample::SampleDataPackage, vec::*};
use ::utils::{bytes::*, from_bytes::FromBytes, sample::SampleDataPackage, test_helpers::*, vec::*};
use ::protocol::{constants::*, data_point::DataPoint};

pub struct DataPackage {
Expand Down
5 changes: 3 additions & 2 deletions src/protocol/data_point.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ::utils::{
from_bytes::FromBytes,
from_bytes_convertible::*,
sample::SampleDataPackage,
test_helpers::*,
};
use ::protocol::constants::*;

Expand All @@ -20,8 +21,8 @@ impl Eq for DataPoint {
}
}

impl FromBytes for DataPoint {
fn from_bytes(bytes: Bytes) -> Self {
impl DataPoint {
pub fn from_bytes(bytes: Bytes) -> Self {
let (feed_id_bytes, value_bytes) = bytes.slice_tail(bytes.len() - DATA_FEED_ID_BS);

Self {
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/payload.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library;

use std::bytes::*;
use ::utils::{bytes::*, from_bytes::FromBytes, sample::SamplePayload, vec::*};
use ::utils::{bytes::*, from_bytes::FromBytes, sample::SamplePayload, test_helpers::*, vec::*};
use ::protocol::{
constants::*,
data_package::{
Expand All @@ -21,8 +21,8 @@ impl Eq for Payload {
}
}

impl FromBytes for Payload {
fn from_bytes(bytes: Bytes) -> Self {
impl Payload {
pub fn from_bytes(bytes: Bytes) -> Self {
let (marker_rest, marker_bytes) = bytes.slice_tail(REDSTONE_MARKER_BS);
let mut i = 0;
while (i < REDSTONE_MARKER_BS) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
library;

pub mod numbers;
pub mod from_bytes_convertible;
pub mod from_bytes;
pub mod test_helpers;
pub mod vec;
pub mod bytes;
pub mod sample;
7 changes: 0 additions & 7 deletions src/utils/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ use std::{bytes::*, bytes_conversions::u256::*, math::*};
use ::utils::{from_bytes::FromBytes, from_bytes_convertible::*};

impl Bytes {
pub fn with(self, byte: u8) -> Bytes {
let mut bytes = self;
bytes.push(byte);

bytes
}

pub fn cut(self, offset: u64) -> Bytes {
let (head, _) = self.split_at(self.len() - offset);

Expand Down
18 changes: 18 additions & 0 deletions src/utils/numbers.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library;

impl u256 {
pub fn avg_with(self, other: Self) -> Self {
self.rsh(1) + other.rsh(1) + (self % 2 + other % 2) / 2
}
}

#[test]
fn test_avg_with() {
assert(0x444u256.avg_with(0x222u256) == 0x333u256);
assert(0x444u256.avg_with(0x444u256) == 0x444u256);
assert(0x444u256.avg_with(0x0u256) == 0x222u256);
assert(0x333u256.avg_with(0x222u256) == 0x2aau256);
assert(0x333u256.avg_with(0x333u256) == 0x333u256);
assert(0x0u256.avg_with(0x0u256) == 0x0u256);
assert(u256::max().avg_with(u256::max()) == u256::max());
}
57 changes: 57 additions & 0 deletions src/utils/test_helpers.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
library;

use std::{bytes::*, vec::*};

pub trait With<T> {
fn with(self, value: T) -> Self;
}

impl<T> With<T> for Vec<T> {
fn with(self, value: T) -> Self {
let mut vec = self;
vec.push(value);

vec
}
}

impl With<u8> for Bytes {
fn with(self, byte: u8) -> Bytes {
let mut bytes = self;
bytes.push(byte);

bytes
}
}

impl Vec<u256> {
pub fn log(self) {
let mut i = 0;
while (i < self.len()) {
log(self.get(i).unwrap());
i += 1;
}
}
}

impl<T> Eq for Vec<T>
where
T: Eq,
{
fn eq(self, other: Self) -> bool {
if (self.len() != other.len()) {
return false;
}

let mut i = 0;
while (i < self.len()) {
if self.get(i).unwrap() != other.get(i).unwrap() {
return false;
}

i += 1;
}

true
}
}
57 changes: 1 addition & 56 deletions src/utils/vec.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library;

use std::vec::*;
use ::utils::{numbers::*, test_helpers::With};

impl<T> Vec<T>
where
Expand Down Expand Up @@ -36,43 +37,6 @@ where
}
}

impl<T> Vec<T> {
pub fn with(self, value: T) -> Self {
let mut vec = self;
vec.push(value);

vec
}
}

impl<T> Eq for Vec<T>
where
T: Eq,
{
fn eq(self, other: Self) -> bool {
if (self.len() != other.len()) {
return false;
}

let mut i = 0;
while (i < self.len()) {
if self.get(i).unwrap() != other.get(i).unwrap() {
return false;
}

i += 1;
}

true
}
}

impl u256 {
fn avg_with(self, other: Self) -> Self {
self.rsh(1) + other.rsh(1) + (self % 2 + other % 2) / 2
}
}

impl Vec<u256> {
pub fn median(self) -> u256 {
match self.len() {
Expand All @@ -93,25 +57,6 @@ impl Vec<u256> {
}
}
}

pub fn log(self) {
let mut i = 0;
while (i < self.len()) {
log(self.get(i).unwrap());
i += 1;
}
}
}

#[test]
fn test_avg_with() {
assert(0x444u256.avg_with(0x222u256) == 0x333u256);
assert(0x444u256.avg_with(0x444u256) == 0x444u256);
assert(0x444u256.avg_with(0x0u256) == 0x222u256);
assert(0x333u256.avg_with(0x222u256) == 0x2aau256);
assert(0x333u256.avg_with(0x333u256) == 0x333u256);
assert(0x0u256.avg_with(0x0u256) == 0x0u256);
assert(u256::max().avg_with(u256::max()) == u256::max());
}

#[test]
Expand Down

0 comments on commit dedce33

Please sign in to comment.