Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/deposit-contract' into e…
Browse files Browse the repository at this point in the history
…2e-deploy-contracts
  • Loading branch information
marijanp committed Apr 4, 2024
2 parents 9cdd94b + 61471cc commit e8d1c5d
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 148 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"kairos-server",
"kairos-tx",
"kairos-test-utils",
"kairos-contracts/contract-types",
"kairos-contracts/deposit-contract-tests",
]

Expand Down
36 changes: 25 additions & 11 deletions kairos-contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion kairos-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ members = [
"deposit-contracts/malicious-session",
"deposit-contracts/deposit-session",
"deposit-contracts/contract",
"contract-types",
]

[workspace.package]
Expand Down
9 changes: 0 additions & 9 deletions kairos-contracts/contract-types/Cargo.toml

This file was deleted.

15 changes: 0 additions & 15 deletions kairos-contracts/contract-types/src/lib.rs

This file was deleted.

27 changes: 25 additions & 2 deletions kairos-contracts/deposit-contract-tests/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,37 @@ mod tests {
fixture.install_deposit_contract(init_admin);
// try to update the admin list
let new_admin_list: Vec<Key> = vec![Key::from(new_admin)];
fixture.update_security_badges(new_admin_list.clone(), init_admin, init_admin);
fixture.update_security_badges_as_admin(new_admin_list.clone(), init_admin, init_admin);
// update the admin list as the new_admin
fixture.update_security_badges(new_admin_list, new_admin, init_admin);
fixture.update_security_badges_as_admin(new_admin_list, new_admin, init_admin);
// now remove the admin role from the installer and expect failure.
let new_admin_list: Vec<Key> = vec![];
fixture.unauthorized_update_security_badges(new_admin_list.clone(), init_admin, init_admin);
}

#[test]
fn admin_should_increase_last_processed_counter() {
let (mut fixture, installer, user) = setup();
fixture.install_deposit_contract(installer);

let value_before: u64 =
fixture.read_counter_value(installer, "last_processed_deposit_counter");
assert_eq!(value_before, 0u64);

fixture.admin_increase_last_processed_counter(installer, installer);
let value_after: u64 =
fixture.read_counter_value(installer, "last_processed_deposit_counter");
assert_eq!(value_after, 1u64);
}

#[test]
fn unauthorized_should_not_increase_last_processed_counter() {
let (mut fixture, installer, unauthorized_user) = setup();
fixture.install_deposit_contract(installer);

fixture.unauthorized_increase_last_processed_counter(unauthorized_user, installer);
}

// see malicious-session
#[test]
fn run_malicious_session() {
Expand Down
54 changes: 50 additions & 4 deletions kairos-contracts/deposit-contract-tests/tests/test_fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TestContext {
.clone()
}

pub fn contract_named_keys(
pub fn get_contract_named_key(
&self,
contract_name: &str,
key_name: &str,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl TestContext {

pub fn get_contract_purse_uref(&self, account: AccountHash) -> URef {
let seed_uref: URef = *self
.contract_named_keys("kairos_deposit_contract", "kairos_deposit_purse", account)
.get_contract_named_key("kairos_deposit_contract", "kairos_deposit_purse", account)
.as_uref()
.unwrap();
seed_uref
Expand All @@ -128,14 +128,60 @@ impl TestContext {
#[allow(dead_code)]
pub fn get_contract_purse_balance(&self, account: AccountHash) -> U512 {
let contract_purse_uref: URef = *self
.contract_named_keys("kairos_deposit_contract", "kairos_deposit_purse", account)
.get_contract_named_key("kairos_deposit_contract", "kairos_deposit_purse", account)
.as_uref()
.unwrap();
self.builder.get_purse_balance(contract_purse_uref)
}

// read a u64 counter from the contract named keys e.g. "last_processed_deposit_counter"
pub fn read_counter_value(&mut self, account: AccountHash, name: &str) -> u64 {
let contract_hash = self.contract_hash("kairos_deposit_contract", account);
self.builder.get_value(contract_hash, name)
}

// call the contract as admin to increase the last processed deposit counter value by 1
pub fn admin_increase_last_processed_counter(
&mut self,
caller: AccountHash,
installer: AccountHash,
) {
let session_args = runtime_args! {};
let update_counter_request = ExecuteRequestBuilder::contract_call_by_hash(
caller,
self.contract_hash("kairos_deposit_contract", installer),
"incr_last_processed_deposit_counter",
session_args,
)
.build();
self.builder
.exec(update_counter_request)
.expect_success()
.commit();
}

// an unauthorized attempt of changing the last processed counter
pub fn unauthorized_increase_last_processed_counter(
&mut self,
caller: AccountHash,
installer: AccountHash,
) {
let session_args = runtime_args! {};
let update_counter_request = ExecuteRequestBuilder::contract_call_by_hash(
caller,
self.contract_hash("kairos_deposit_contract", installer),
"incr_last_processed_deposit_counter",
session_args,
)
.build();
self.builder
.exec(update_counter_request)
.expect_failure()
.commit();
}

// try to update the access control / admin list of the deposit contract
pub fn update_security_badges(
pub fn update_security_badges_as_admin(
&mut self,
admin_list: Vec<Key>,
caller: AccountHash,
Expand Down
4 changes: 2 additions & 2 deletions kairos-contracts/deposit-contracts/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ license.workspace = true
[dependencies]
casper-contract = {version="4.0.0", features=["std", "test-support"]}
casper-types = {version="4.0.0", default-features=false}
casper-event-standard="0.5.0"
base64 = { version = "0.20.0", default-features = false, features = ["alloc"] }
contract-types = {path="../../contract-types"}
bincode = "1.3.3"


Expand All @@ -24,4 +24,4 @@ test = false

[profile.release]
codegen-units = 1
lto = true
lto = true
2 changes: 0 additions & 2 deletions kairos-contracts/deposit-contracts/contract/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pub const KAIROS_DEPOSIT_CONTRACT_NAME: &str = "kairos_deposit_contract";
pub const KAIROS_DEPOSIT_CONTRACT_PACKAGE: &str = "deposit_contract_package";
pub const KAIROS_DEPOSIT_CONTRACT: &str = "deposit_contract";

pub const KAIROS_DEPOSIT_EVENT_DICT: &str = "kairos_deposit_event_dict";
pub const KAIROS_MOST_RECENT_DEPOSIT_COUNTER: &str = "most_recent_deposit_counter";
pub const KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER: &str = "last_processed_deposit_counter";
pub const KAIROS_DEPOSIT_PURSE: &str = "kairos_deposit_purse";

Expand Down
11 changes: 11 additions & 0 deletions kairos-contracts/deposit-contracts/contract/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use casper_event_standard::Event;
extern crate alloc;
use alloc::{string::String, vec::Vec};
use casper_types::{Key, U512};

#[derive(Event)]
pub struct Deposit {
pub account: Key,
pub amount: U512,
pub timestamp: Option<String>,
}
61 changes: 15 additions & 46 deletions kairos-contracts/deposit-contracts/contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use casper_types::{
};
mod constants;
use constants::{
ADMIN_LIST, KAIROS_DEPOSIT_CONTRACT, KAIROS_DEPOSIT_CONTRACT_NAME, KAIROS_DEPOSIT_EVENT_DICT,
KAIROS_DEPOSIT_PURSE, KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER,
KAIROS_MOST_RECENT_DEPOSIT_COUNTER, RUNTIME_ARG_AMOUNT, RUNTIME_ARG_TEMP_PURSE,
SECURITY_BADGES,
ADMIN_LIST, KAIROS_DEPOSIT_CONTRACT, KAIROS_DEPOSIT_CONTRACT_NAME,
KAIROS_DEPOSIT_CONTRACT_PACKAGE, KAIROS_DEPOSIT_PURSE, KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER,
RUNTIME_ARG_AMOUNT, RUNTIME_ARG_TEMP_PURSE, SECURITY_BADGES,
};
mod utils;
use utils::{get_immediate_caller, get_optional_named_arg_with_user_errors};
Expand All @@ -24,8 +23,10 @@ use error::DepositError;
mod security;
use security::{access_control_check, SecurityBadge};
mod entry_points;

use contract_types::Deposit;
mod events;
use casper_event_standard;
use casper_event_standard::Schemas;
use events::Deposit;

// This entry point is called once when the contract is installed
// and sets up the security badges with the installer as an admin or the
Expand All @@ -42,6 +43,10 @@ pub extern "C" fn init() {
.unwrap_or_revert_with(DepositError::FailedToCreateSecurityBadgesDict);
let installing_entity = runtime::get_caller();

// initialize event schema
let schemas = Schemas::new().with::<Deposit>();
casper_event_standard::init(schemas);

// Assign the admin role to the installer, regardless of the list of admins that was
// passed to the installation session.
// The installer is by default an admin and the installer's admin role
Expand Down Expand Up @@ -96,38 +101,12 @@ pub extern "C" fn deposit() {
system::transfer_from_purse_to_purse(temp_purse, deposit_purse_uref, amount, None)
.unwrap_or_revert();

let most_recent_deposit_counter_uref = runtime::get_key(KAIROS_MOST_RECENT_DEPOSIT_COUNTER)
.unwrap_or_revert_with(DepositError::MissingKeyMostRecentDepositCounter)
.into_uref()
.unwrap_or_revert();
let mut most_recent_deposit_counter_value: u64 =
storage::read(most_recent_deposit_counter_uref)
.unwrap_or_revert()
.unwrap_or_revert();
let new_deposit_record: Deposit = Deposit {
account: get_immediate_caller().unwrap_or_revert(),
amount,
amount: amount,
timestamp: None,
processed: false,
};

let deposit_event_dict_key: &str = &most_recent_deposit_counter_value.to_string();

let kairos_deposit_event_dict_uref = runtime::get_key(KAIROS_DEPOSIT_EVENT_DICT)
.unwrap_or_revert_with(DepositError::MissingKeyDepositEventDict)
.into_uref()
.unwrap_or_revert();
storage::dictionary_put::<Vec<u8>>(
kairos_deposit_event_dict_uref,
deposit_event_dict_key,
bincode::serialize(&new_deposit_record).unwrap(),
);

most_recent_deposit_counter_value += 1u64;
storage::write(
most_recent_deposit_counter_uref,
most_recent_deposit_counter_value,
);
casper_event_standard::emit(new_deposit_record);
}

// The centralized Kairos service, or a sequencer,
Expand Down Expand Up @@ -192,28 +171,18 @@ pub extern "C" fn call() {
entry_points
};
let mut named_keys = NamedKeys::new();
let event_dict = storage::new_dictionary(KAIROS_DEPOSIT_EVENT_DICT)
.unwrap_or_revert_with(DepositError::FailedToCreateDepositDict);
named_keys.insert(KAIROS_DEPOSIT_EVENT_DICT.to_string(), event_dict.into());
let last_processed_deposit_counter = storage::new_uref(u64::from(0u8));

named_keys.insert(
KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER.to_string(),
last_processed_deposit_counter.into(),
);

let most_recent_deposit_counter = storage::new_uref(u64::from(0u8));
named_keys.insert(
KAIROS_MOST_RECENT_DEPOSIT_COUNTER.to_string(),
most_recent_deposit_counter.into(),
);

let (contract_hash, _) = storage::new_contract(
let (contract_hash, _) = storage::new_locked_contract(
entry_points,
Some(named_keys),
Some(KAIROS_DEPOSIT_CONTRACT.to_string()),
// Some(key) if upgradable
None,
Some(KAIROS_DEPOSIT_CONTRACT_PACKAGE.to_string()),
);
let contract_hash_key = Key::from(contract_hash);
runtime::put_key(KAIROS_DEPOSIT_CONTRACT_NAME, contract_hash_key);
Expand Down
1 change: 1 addition & 0 deletions kairos-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PayloadBody {
#[serde(deserialize_with = "hex_to_vec", serialize_with = "vec_to_hex")]
pub public_key: PublicKey,
#[serde(deserialize_with = "hex_to_vec", serialize_with = "vec_to_hex")]
pub payload: Vec<u8>,
Expand Down
Loading

0 comments on commit e8d1c5d

Please sign in to comment.