Skip to content

Commit

Permalink
assert_signers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz2891 committed Oct 4, 2024
1 parent 7a023f8 commit 1195335
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/core/config_validation.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ use ::core::{config::Config, errors::*, validation::*};
use ::utils::{from_bytes::*, vec::*};

trait Validation {
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) -> u64;

}

impl Validation for Config {

fn check_parameters(self) {
assert_signers(self.signers, self.signer_count_threshold);
}

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);
Expand Down Expand Up @@ -54,8 +61,28 @@ impl Validation for Config {

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::DuplicateSignerFound,
);
}

#[test]
fn test_validate_one_signer() {
let results = make_results();
Expand Down
10 changes: 10 additions & 0 deletions src/core/errors.sw
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ 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: (),
SignerCountThresholdToSmall: (),
DuplicateSignerFound: (),
SenderIsNotTheOwner: (Identity, Option<Identity>),
TimestampMustBeGreaterThanBefore: (u64, u64),
}
3 changes: 2 additions & 1 deletion src/core/processor.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ::core::{
/// * `config` - Configuration of the payload processing.
/// * `payload_bytes` - Network-specific byte-list of the payload to be processed.
pub fn process_input(bytes: Bytes, config: Config) -> (Vec<u256>, u64) {
assert(config.signers.len() > 0);
config.check_parameters();

let payload = Payload::from_bytes(bytes);
let timestamp = config.validate_timestamps(payload);
Expand All @@ -27,6 +27,7 @@ pub fn process_input(bytes: Bytes, config: Config) -> (Vec<u256>, u64) {
let results = get_feed_values(matrix, config);

config.validate_signer_count(results);

(results.aggregated(), timestamp)
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/vec.sw
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ where
None
}

pub fn find_duplicate(self) -> Option<T> {
let mut i = 0;

while (i < self.len()) {
let mut j = i + 1;
while (j < self.len()) {
if (self.get(i) == self.get(j)) {
return Some(self.get(i).unwrap());
}

j += 1;
}

i += 1;
}

None
}

fn sort(ref mut self)
where
T: Ord,
Expand Down

0 comments on commit 1195335

Please sign in to comment.