Skip to content

Commit

Permalink
add (empty) submit batch entry point and key in contract state for tr…
Browse files Browse the repository at this point in the history
…ie root
  • Loading branch information
jonas089 committed May 12, 2024
1 parent e5a767b commit d62f288
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion kairos-contracts/demo-contract/contract/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pub const KAIROS_DEPOSIT_CONTRACT_PACKAGE: &str = "kairos_contract_package";
pub const KAIROS_DEPOSIT_CONTRACT_UREF: &str = "demo_contract";

pub const KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER: &str = "last_processed_deposit_counter";
pub const KAIROS_CURRENT_TRIE_ROOT_HASH: &str = "current_trie_root_hash";
pub const KAIROS_DEPOSIT_PURSE: &str = "kairos_deposit_purse";

pub const RUNTIME_ARG_TEMP_PURSE: &str = "temp_purse";

pub const RUNTIME_ARG_AMOUNT: &str = "amount";
pub const RUNTIME_ARG_BATCH: &str = "batch";

pub const EP_INIT_NAME: &str = "init";
pub const EP_GET_PURSE_NAME: &str = "get_purse";
pub const EP_DEPOSIT_NAME: &str = "deposit";
pub const EP_SUBMIT_BATCH_NAME: &str = "submit_batch";
13 changes: 12 additions & 1 deletion kairos-contracts/demo-contract/contract/src/entry_points.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{
EP_DEPOSIT_NAME, EP_GET_PURSE_NAME, EP_INIT_NAME, RUNTIME_ARG_AMOUNT, RUNTIME_ARG_TEMP_PURSE,
EP_DEPOSIT_NAME, EP_GET_PURSE_NAME, EP_INIT_NAME, EP_SUBMIT_BATCH_NAME, RUNTIME_ARG_AMOUNT,
RUNTIME_ARG_BATCH, RUNTIME_ARG_TEMP_PURSE,
};
use alloc::vec;
use casper_types::{CLType, EntryPoint, EntryPointAccess, EntryPointType, Parameter};
Expand Down Expand Up @@ -36,3 +37,13 @@ pub fn deposit() -> EntryPoint {
EntryPointType::Contract,
)
}

pub fn submit_batch() -> EntryPoint {
EntryPoint::new(
EP_SUBMIT_BATCH_NAME,
vec![Parameter::new(RUNTIME_ARG_BATCH, CLType::Any)],
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract,
)
}
21 changes: 17 additions & 4 deletions kairos-contracts/demo-contract/contract/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
extern crate alloc;
use alloc::string::ToString;
use alloc::string::{String, ToString};
use casper_contract::{
contract_api::{runtime, storage, system},
unwrap_or_revert::UnwrapOrRevert,
Expand All @@ -13,9 +13,9 @@ use casper_types::{
};
mod constants;
use constants::{
KAIROS_DEPOSIT_CONTRACT_NAME, KAIROS_DEPOSIT_CONTRACT_PACKAGE, KAIROS_DEPOSIT_CONTRACT_UREF,
KAIROS_DEPOSIT_PURSE, KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER, RUNTIME_ARG_AMOUNT,
RUNTIME_ARG_TEMP_PURSE,
KAIROS_CURRENT_TRIE_ROOT_HASH, KAIROS_DEPOSIT_CONTRACT_NAME, KAIROS_DEPOSIT_CONTRACT_PACKAGE,
KAIROS_DEPOSIT_CONTRACT_UREF, KAIROS_DEPOSIT_PURSE, KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER,
RUNTIME_ARG_AMOUNT, RUNTIME_ARG_TEMP_PURSE,
};
mod entry_points;
mod utils;
Expand Down Expand Up @@ -57,6 +57,11 @@ pub extern "C" fn get_purse() {
);
}

#[no_mangle]
pub extern "C" fn submit_batch() {
todo!("Implement submit_batch entry point");
}

// Entry point called by a user through session code to deposit funds.
// Due to Casper < 2.0 purse management and access control, it is necessary that
// a temporary purse is funded and passed to the deposit contract, since this is
Expand Down Expand Up @@ -92,16 +97,24 @@ pub extern "C" fn call() {
entry_points.add_entry_point(entry_points::init());
entry_points.add_entry_point(entry_points::get_purse());
entry_points.add_entry_point(entry_points::deposit());
entry_points.add_entry_point(entry_points::submit_batch());
entry_points
};
// this counter will be udpated by the entry point that processes / verifies batches
let mut named_keys = NamedKeys::new();
let last_processed_deposit_counter = storage::new_uref(u64::from(0u8));

let initial_trie_root_hash: Option<String> = None;
let current_trie_root_hash = storage::new_uref(initial_trie_root_hash);

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

let (contract_hash, _) = storage::new_locked_contract(
entry_points,
Expand Down
5 changes: 1 addition & 4 deletions kairos-l1-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use casper_client::{
get_state_root_hash, query_global_state,
rpcs::results::{QueryBalanceResult, ResponseResult},
types::StoredValue,
JsonRpcId, Verbosity,
};
Expand All @@ -13,7 +12,7 @@ use casper_client::{
SuccessResponse,
};
use casper_types::{crypto::SecretKey, Key, RuntimeArgs};
use std::{fs, result};
use std::fs;

pub const DEFAULT_PAYMENT_AMOUNT: u64 = 1_000_000_000_000;

Expand Down Expand Up @@ -288,8 +287,6 @@ mod tests {
.await
.unwrap();
println!("Deploy was processed successfully.");
thread::sleep(Duration::from_secs(1));

let public_key_path = network.assets_dir.join("users/user-1/public_key.pem");
let public_key: PublicKey =
PublicKey::from_file(public_key_path.to_str().unwrap()).unwrap();
Expand Down

0 comments on commit d62f288

Please sign in to comment.