Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
feat(ink): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jan 9, 2024
1 parent 3b11798 commit cc4aabd
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 49 deletions.
10 changes: 5 additions & 5 deletions ink/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Version {
V1 = 1,
}

#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub struct IncomingAction {
pub id: u64,
pub payload: VersionedIncomingActionPayload,
Expand All @@ -34,25 +34,25 @@ impl PartialOrd for IncomingAction {
}
}

#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub enum VersionedIncomingActionPayload {
V1(IncomingActionPayloadV1),
}

#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub enum IncomingActionPayloadV1 {
AssignJobProcessor(AssignProcessorPayloadV1),
FinalizeJob(FinalizeJobPayloadV1),
Noop,
}

#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub struct AssignProcessorPayloadV1 {
pub job_id: u128,
pub processor: [u8; 32],
}

#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub struct FinalizeJobPayloadV1 {
pub job_id: u128,
pub unused_reward: u128,
Expand Down
3 changes: 3 additions & 0 deletions ink/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ acurast-state-ink = { path = "../state", default-features = false, features = ["
acurast-validator-ink = { path = "../validator", default-features = false, features = ["ink-as-dependency"] }
acurast-consumer-ink = { path = "../consumer", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
hex-literal = "0.4.1"

[lib]
path = "lib.rs"

Expand Down
52 changes: 31 additions & 21 deletions ink/proxy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,9 @@ mod proxy {
}

fn decode_incoming_action(payload: &Vec<u8>) -> Result<IncomingAction, Error> {
match RawIncomingAction::decode(&mut payload.as_slice()) {
match IncomingAction::decode(&mut payload.as_slice()) {
Err(err) => Err(Error::InvalidIncomingAction(format!("{:?}", err))),
Ok(action) => Ok(IncomingAction {
id: action.id,
payload: decode_versioned_incoming_action_payload(action)?,
}),
}
}

fn decode_versioned_incoming_action_payload(
action: RawIncomingAction,
) -> Result<VersionedIncomingActionPayload, Error> {
match action.payload_version {
v if v == Version::V1 as u16 => {
let action = IncomingActionPayloadV1::decode(&mut action.payload.as_slice())
.map_err(|err| {
Error::Verbose(format!("Cannot decode incoming action V1 {:?}", err))
})?;

Ok(VersionedIncomingActionPayload::V1(action))
}
v => Err(Error::UnknownIncomingActionVersion(v)),
Ok(action) => Ok(action),
}
}

Expand Down Expand Up @@ -786,6 +767,11 @@ mod proxy {
// Views
//

#[ink(message)]
pub fn is_action_processed(&self, action_id: u64) -> bool {
self.processed_incoming_actions.contains(action_id)
}

/// The purpose of this method is to generate proofs for outgoing actions
#[ink(message)]
pub fn generate_proof(&self, from: u64, to: u64) -> Result<MerkleProof<[u8; 32]>, Error> {
Expand Down Expand Up @@ -851,4 +837,28 @@ mod proxy {
}
}
}

#[cfg(test)]
mod tests {
use hex_literal::hex;

/// Imports all the definitions from the outer scope so we can use them here.
use super::*;

#[ink::test]
fn test_action_encoding() {
let encoded_incoming_action = hex!("00000000000000000002");

let decoded_incoming_action =
IncomingAction::decode(&mut encoded_incoming_action.as_slice());

assert_eq!(
decoded_incoming_action.unwrap(),
IncomingAction {
id: 0,
payload: VersionedIncomingActionPayload::V1(IncomingActionPayloadV1::Noop)
}
);
}
}
}
34 changes: 27 additions & 7 deletions ink/state/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ pub mod state_aggregator {
}
}

fn fail_if_not_owner(&self) {
assert!(self.config.owner.eq(&self.env().caller()), "NOT_OWNER");
fn fail_if_not_owner(&self) -> Result<(), Error> {
if self.config.owner.eq(&self.env().caller()) {
return Ok(());
}
Err(Error::NotAllowed)
}

fn finalize_snapshot(&mut self, required: bool) {
Expand All @@ -385,7 +388,6 @@ pub mod state_aggregator {

if self.snapshot_start_level + self.config.snapshot_duration < current_block_number {
// Finalize snapshot
self.snapshot_counter += 1;

// Snapshot previous block level
let snapshot_level = current_block_number - 1;
Expand All @@ -400,15 +402,17 @@ pub mod state_aggregator {
snapshot: self.snapshot_counter,
level: snapshot_level,
});

self.snapshot_counter += 1;
} else {
assert!(!required, "CANNOT_SNAPSHOT");
}
}

#[ink(message)]
pub fn configure(&mut self, configure: Vec<ConfigureArgument>) {
pub fn configure(&mut self, configure: Vec<ConfigureArgument>) -> Result<(), Error> {
// Only the administrator can configure the contract
self.fail_if_not_owner();
self.fail_if_not_owner()?;

for c in configure {
match c {
Expand All @@ -421,6 +425,8 @@ pub mod state_aggregator {
}
}
}

Ok(())
}

#[ink(message)]
Expand Down Expand Up @@ -468,8 +474,13 @@ pub mod state_aggregator {
}

#[ink(message)]
pub fn mmr_size(&self) -> u64 {
self.mmr_size
pub fn next_snapshot(&self) -> u128 {
self.snapshot_counter
}

#[ink(message)]
pub fn snapshot_level(&self, snapshot: u128) -> u32 {
self.snapshot_level.get(snapshot).expect("UNKNOWN_SNAPSHOT")
}

#[ink(message)]
Expand All @@ -478,6 +489,15 @@ pub mod state_aggregator {

mmr.get_root(&self.tree).expect("COULD_NOT_GET_ROOT")
}

#[ink(message)]
pub fn can_snapshot(&self) -> bool {
let current_block = Self::env().block_number();

let not_empty = self.mmr_size > 0;

not_empty && (self.snapshot_start_level + self.config.snapshot_duration) < current_block
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions ink/validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ scale-info = { version = "2.6", default-features = false, features = ["derive"],

ckb-merkle-mountain-range = { version = "0.6.0", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"

[lib]
path = "lib.rs"

Expand Down
Loading

0 comments on commit cc4aabd

Please sign in to comment.