Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
marijanp committed Sep 16, 2024
1 parent bc4ef9b commit 8b1e7a9
Show file tree
Hide file tree
Showing 33 changed files with 337 additions and 232 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions casper-deploy-notifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ casper-types.workspace = true
eventsource-stream = "0.2.3"
thiserror = "1.0"
base16 = "0.2.1"
tracing = "0.1.40"

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
mockito = "1"
tracing-subscriber = "0.3.18"
1 change: 1 addition & 0 deletions casper-deploy-notifier/event
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"DeployProcessed":{"account":"01f03bbc42a3d5901c7232987ba84ab2c6d210973a0cfe742284dcb1d8b4cbe1c3","block_hash":"926f7c831d6313b1359a64ae01845e678da337381f228f1be7230476e594899e","dependencies":[],"deploy_hash":"4b4cf10f2ebb9df0e754e1849a4977f57bba0a20a8f58c73b970b69f26153c64","execution_result":{"Success":{"cost":"524513374","effect":{"operations":[],"transforms":[{"key":"account-hash-6174cf2e6f8fed1715c9a3bace9c50bfe572eecb763b0ed3f644532616452008","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-d63c44078a1931b5dc4b80a7a0ec586164fd0470ce9f8b23f6d93b9e86c5944d","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"hash-4475016098705466254edd18d267a9dad43e341d4dafadb507d0fe3cf2d4a74b","transform":"Identity"},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"balance-c81c49b0c44769d1cb841e83452cd08d133b9f8d357a3cc561d0923130047db0","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":"Identity"},{"key":"balance-c81c49b0c44769d1cb841e83452cd08d133b9f8d357a3cc561d0923130047db0","transform":{"WriteCLValue":{"bytes":"06a5d4874e1204","cl_type":"U512","parsed":"4476673447077"}}},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":{"AddUInt512":"5000000000"}},{"key":"hash-bf12dc357758d68706e5e1cd83a7208672b064f3feb63e263ff0c9c6929e5d17","transform":"Identity"},{"key":"hash-a33e1ca85577e4593312338d0957c38d07820f4117ed081e4bc20df375bdb664","transform":"Identity"},{"key":"hash-1662c7c8c0314e30f02f607aba5c2a7d7276cee81d1ad2757fa949dc21df48ae","transform":"Identity"},{"key":"dictionary-efede2879aa85603991041b248a2809592abdb842975177a1207f45b2631e740","transform":"Identity"},{"key":"dictionary-8c03a95f560181cfe0700d03a0f319b01b96989521f32576731c73e08b01afd2","transform":"Identity"},{"key":"dictionary-8c03a95f560181cfe0700d03a0f319b01b96989521f32576731c73e08b01afd2","transform":{"WriteCLValue":{"bytes":"0500000001000000000e0320000000b6bf456cc4b13f1f617a96f6e096efcf5d4df167691fd86410ce473f4a55eab44000000039323962393833336636633366343638326535623339316234376536613531343532386231356661323435666334353130636462636262333534396638393661","cl_type":"Any","parsed":null}}},{"key":"dictionary-cfeea884d140a02d69f60658e79c52d273acae55f87a03ea05d97e948e0c2036","transform":{"WriteCLValue":{"bytes":"060000000200000001ea0e0320000000b6bf456cc4b13f1f617a96f6e096efcf5d4df167691fd86410ce473f4a55eab44000000035653462616434356536623364383261306133343734653362623565373061653663663030633030623437636466303466323936616234383463346365643166","cl_type":"Any","parsed":null}}},{"key":"uref-ac7408079dd7e5eb9e657b5e1188a1c6acb3d5e1654bb85054ec6d96732706da-000","transform":"Identity"},{"key":"dictionary-2e89d89ee7ce94674b959e57519711dd3525d88f83c12cc04c1a292e8d157b6a","transform":{"WriteCLValue":{"bytes":"2b00000027000000120000006576656e745f436f6e6669726d6174696f6e10584014f3efd86369d7152c724113b8f20e0320000000bafb7dad56d5dc280fdaead4ba32eddaa203dad65d7ae01e5890e55923a0e3440400000038323938","cl_type":"Any","parsed":null}}},{"key":"uref-ac7408079dd7e5eb9e657b5e1188a1c6acb3d5e1654bb85054ec6d96732706da-000","transform":{"WriteCLValue":{"bytes":"6b200000","cl_type":"U32","parsed":8299}}},{"key":"deploy-4b4cf10f2ebb9df0e754e1849a4977f57bba0a20a8f58c73b970b69f26153c64","transform":{"WriteDeployInfo":{"deploy_hash":"4b4cf10f2ebb9df0e754e1849a4977f57bba0a20a8f58c73b970b69f26153c64","from":"account-hash-0a12fef621d43e5dfa0845065371adc816a92ad40e35b0c311de9680445eabbd","gas":"524513374","source":"uref-c81c49b0c44769d1cb841e83452cd08d133b9f8d357a3cc561d0923130047db0-007","transfers":[]}}},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"hash-d63c44078a1931b5dc4b80a7a0ec586164fd0470ce9f8b23f6d93b9e86c5944d","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":"Identity"},{"key":"hash-d2469afeb99130f0be7c9ce230a84149e6d756e306ef8cf5b8a49d5182e41676","transform":"Identity"},{"key":"account-hash-0a12fef621d43e5dfa0845065371adc816a92ad40e35b0c311de9680445eabbd","transform":"Identity"},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"hash-4475016098705466254edd18d267a9dad43e341d4dafadb507d0fe3cf2d4a74b","transform":"Identity"},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":"Identity"},{"key":"balance-c81c49b0c44769d1cb841e83452cd08d133b9f8d357a3cc561d0923130047db0","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":{"WriteCLValue":{"bytes":"041158ee21","cl_type":"U512","parsed":"569268241"}}},{"key":"balance-c81c49b0c44769d1cb841e83452cd08d133b9f8d357a3cc561d0923130047db0","transform":{"AddUInt512":"4430731759"}},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"hash-4475016098705466254edd18d267a9dad43e341d4dafadb507d0fe3cf2d4a74b","transform":"Identity"},{"key":"hash-7cc1b1db4e08bbfe7bacf8e1ad828a5d9bcccbb33e55d322808c3a88da53213a","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":"Identity"},{"key":"balance-065a7ba7eebf16ef1f47b7b1f2bb3c8c5019b7762b81cafcbc1bf9c15b553126","transform":"Identity"},{"key":"balance-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef","transform":{"WriteCLValue":{"bytes":"00","cl_type":"U512","parsed":"0"}}},{"key":"balance-065a7ba7eebf16ef1f47b7b1f2bb3c8c5019b7762b81cafcbc1bf9c15b553126","transform":{"AddUInt512":"569268241"}}]},"transfers":[]}},"timestamp":"2024-07-11T10:11:27.512Z","ttl":"30m"}}
12 changes: 5 additions & 7 deletions casper-deploy-notifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ impl DeployNotifier {
// Before running this function again, make sure you established new connection.
pub async fn run(&mut self, tx: mpsc::Sender<Notification>) -> Result<(), SseError> {
// Take stream out of state.
let mut event_stream = match self.event_stream.take() {
Some(s) => Ok(s),
None => Err(SseError::NotConnected),
}?;
let mut event_stream = self.event_stream.take().ok_or(SseError::NotConnected)?;

while let Some(event) = event_stream.try_next().await? {
let data: SseData = serde_json::from_str(&event.data)?;
match data {
SseData::ApiVersion(_) => Err(SseError::UnexpectedHandshake)?,
SseData::Other(_) => {}
SseData::DeployProcessed(event_details) => {
SseData::Other(other) => {tracing::info!("{other}")}
SseData::TransactionProcessed(event_details) => {
let notification = event_details.into();
if let Err(_e) = tx.send(notification).await {
if let Err(err) = tx.send(notification).await {
tracing::info!("Failed to send notification: {err}");
// Receiver probably dropeed.
break;
}
Expand Down
33 changes: 24 additions & 9 deletions casper-deploy-notifier/src/sse_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use casper_types::execution::ExecutionResult;
use casper_types::{
execution::ExecutionResult,
contract_messages::Messages,
TransactionHash,
ProtocolVersion,
Timestamp,
InitiatorAddr,
TimeDiff,
BlockHash,
};
use serde::{Deserialize, Serialize};

/// Casper does not expose SSE types directly, so we have to reimplement them.
Expand All @@ -8,19 +17,25 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum SseData {
/// The version of node's API. First event to receive, used for handshake.
ApiVersion(casper_types::ProtocolVersion),
/// The given deploy has been executed, committed and forms part of the given block.
DeployProcessed(DeployProcessed),
/// The version of this node's API server. This event will always be the first sent to a new
/// client, and will have no associated event ID provided.
ApiVersion(ProtocolVersion),
/// The given transaction has been executed, committed and forms part of the given block.
TransactionProcessed(TransactionProcessed),
/// The node is about to shut down.
Shutdown,
/// Other events, that we are not interested in.
#[serde(untagged)]
Other(serde_json::Value),
}

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct DeployProcessed {
pub deploy_hash: Box<casper_types::DeployHash>,
pub account: Box<casper_types::PublicKey>,
pub execution_result: Box<ExecutionResult>,
pub struct TransactionProcessed {
pub transaction_hash: Box<TransactionHash>,
pub initiator_addr: Box<InitiatorAddr>,
pub timestamp: String,
pub ttl: String,
pub block_hash: Box<BlockHash>,
pub execution_result: Box<ExecutionResult>,
pub messages: Messages,
}
11 changes: 5 additions & 6 deletions casper-deploy-notifier/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use casper_types::{
execution::{ExecutionResult, ExecutionResultV1},
AsymmetricType,
};

use crate::sse_types::DeployProcessed;
use crate::sse_types::TransactionProcessed;

#[derive(Debug)]
pub struct Notification {
Expand All @@ -12,17 +11,17 @@ pub struct Notification {
pub success: bool,
}

impl From<DeployProcessed> for Notification {
fn from(event_details: DeployProcessed) -> Self {
impl From<TransactionProcessed> for Notification {
fn from(event_details: TransactionProcessed) -> Self {
let success = match *event_details.execution_result {
ExecutionResult::V1(execution_result_v1) => match execution_result_v1 {
ExecutionResultV1::Failure { .. } => false,
ExecutionResultV1::Success { .. } => true,
},
ExecutionResult::V2(execution_result_v2) => execution_result_v2.error_message.is_none(),
};
let deploy_hash = base16::encode_lower(event_details.deploy_hash.inner());
let public_key = event_details.account.to_hex();
let deploy_hash = base16::encode_lower(&event_details.transaction_hash.to_string());
let public_key = event_details.initiator_addr.account_hash().to_string();

Notification {
deploy_hash,
Expand Down
1 change: 1 addition & 0 deletions casper-deploy-notifier/tests/casper-deploy-notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ mod tests {

#[tokio::test]
async fn test_parsing_real_deploy_event() {
tracing_subscriber::fmt::init();
let mut server = mockito::Server::new_async().await;
server
.mock("GET", "/")
Expand Down
87 changes: 43 additions & 44 deletions demo-contract-tests/tests/test_fixture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
mod wasm_helper;

use casper_engine_test_support::{
DeployItemBuilder, ExecuteRequestBuilder, WasmTestBuilder, ARG_AMOUNT, DEFAULT_ACCOUNT_ADDR,
DEFAULT_ACCOUNT_INITIAL_BALANCE,
};
use casper_execution_engine::{
core::{engine_state, execution},
storage::global_state::in_memory::InMemoryGlobalState,
DeployItemBuilder, ExecuteRequestBuilder, LmdbWasmTestBuilder, ARG_AMOUNT,
DEFAULT_ACCOUNT_INITIAL_BALANCE, LOCAL_GENESIS_REQUEST,
};
use casper_execution_engine::{engine_state, execution};
use casper_types::{
account::AccountHash,
bytesrepr::Bytes,
contracts::ContractHash,
crypto::{PublicKey, SecretKey},
runtime_args,
system::{handle_payment::ARG_TARGET, mint::ARG_ID},
ApiError, RuntimeArgs, U512,
runtime_args, ApiError, EntityAddr, RuntimeArgs, Transaction, TransactionV1Builder, URef, U512,
};
use rand::Rng;
use std::path::Path;

use casper_engine_test_support::{InMemoryWasmTestBuilder, PRODUCTION_RUN_GENESIS_REQUEST};
use casper_types::{ContractHash, URef};

use self::wasm_helper::get_wasm_directory;

pub const ADMIN_SECRET_KEY: [u8; 32] = [1u8; 32];

#[derive(Default)]
pub struct TestContext {
builder: InMemoryWasmTestBuilder,
builder: LmdbWasmTestBuilder,
pub admin: AccountHash,
contract_hash: ContractHash,
contract_purse: URef,
}

impl TestContext {
pub fn new(initial_trie_root: Option<[u8; 32]>) -> TestContext {
let mut builder = InMemoryWasmTestBuilder::default();
builder.run_genesis(&PRODUCTION_RUN_GENESIS_REQUEST);
let mut builder = LmdbWasmTestBuilder::default();
builder.run_genesis(LOCAL_GENESIS_REQUEST.clone());

let admin_secret_key = SecretKey::ed25519_from_bytes(ADMIN_SECRET_KEY).unwrap();
let admin = create_funded_account_for_secret_key_bytes(&mut builder, admin_secret_key)
Expand All @@ -51,16 +43,15 @@ impl TestContext {
);

let contract_hash = builder
.get_expected_account(admin)
.named_keys()
.get_named_keys(EntityAddr::Account(admin.0))
.get("kairos_contract_hash")
.expect("must have contract hash key as part of contract creation")
.into_hash()
.into_hash_addr()
.map(ContractHash::new)
.expect("must get contract hash");

let contract = builder
.get_contract(contract_hash)
.get_legacy_contract(contract_hash)
.expect("should have contract");
let contract_purse = *contract
.named_keys()
Expand Down Expand Up @@ -94,7 +85,11 @@ impl TestContext {
}

pub fn get_user_balance(&mut self, user: AccountHash) -> U512 {
let user_uref = self.builder.get_expected_account(user).main_purse();
let user_uref = self
.builder
.get_entity_by_account_hash(user)
.unwrap()
.main_purse();
self.builder.get_purse_balance(user_uref)
}

Expand Down Expand Up @@ -196,26 +191,21 @@ impl TestContext {
) -> ApiError {
self.submit_proof_to_contract_commit(sender, proof_serialized);

let exec_results = self
let exec_result = self
.builder
.get_last_exec_results()
.expect("Expected to be called after run()");

// not sure about first here it's what the upstream code does
let exec_result = exec_results
.first()
.expect("Unable to get first deploy result");
.get_last_exec_result()
.expect("Failed to get the deploy result");

match exec_result.as_error() {
Some(engine_state::Error::Exec(execution::Error::Revert(err))) => *err,
match exec_result.error() {
Some(engine_state::Error::Exec(execution::ExecError::Revert(err))) => *err,
Some(err) => panic!("Expected revert ApiError, got {:?}", err),
None => panic!("Expected error"),
}
}
}

pub fn run_session_with_args(
builder: &mut WasmTestBuilder<InMemoryGlobalState>,
builder: &mut LmdbWasmTestBuilder,
session_wasm_path: &Path,
user: AccountHash,
runtime_args: RuntimeArgs,
Expand All @@ -229,20 +219,29 @@ pub fn run_session_with_args(
/// Creates a funded account for the given ed25519 secret key in bytes
/// It panics if the passed secret key bytes cannot be read
pub fn create_funded_account_for_secret_key_bytes(
builder: &mut WasmTestBuilder<InMemoryGlobalState>,
builder: &mut LmdbWasmTestBuilder,
account_secret_key: SecretKey,
) -> PublicKey {
let account_public_key = PublicKey::from(&account_secret_key);
let account_hash = account_public_key.to_account_hash();
let transfer = ExecuteRequestBuilder::transfer(
*DEFAULT_ACCOUNT_ADDR,
runtime_args! {
ARG_AMOUNT => DEFAULT_ACCOUNT_INITIAL_BALANCE / 10_u64,
ARG_TARGET => account_hash,
ARG_ID => Option::<u64>::None,
},
let transfer = TransactionV1Builder::new_transfer(
DEFAULT_ACCOUNT_INITIAL_BALANCE / 10_u64,
None,
account_hash,
None,
)
.build();
.unwrap()
.build()
.unwrap();
let transfer = ExecuteRequestBuilder::from_transaction(&Transaction::V1(transfer))
// *DEFAULT_ACCOUNT_ADDR,
// runtime_args! {
// ARG_AMOUNT => DEFAULT_ACCOUNT_INITIAL_BALANCE / 10_u64,
// ARG_TARGET => account_hash,
// ARG_ID => Option::<u64>::None,
// },
//)
.build();
builder.exec(transfer).expect_success().commit();
account_public_key
}
Expand All @@ -262,11 +261,11 @@ pub fn contract_call_by_hash(

let deploy = DeployItemBuilder::new()
.with_address(sender)
.with_stored_session_hash(contract_hash, entry_point, args)
.with_empty_payment_bytes(runtime_args! { ARG_AMOUNT => payment, })
.with_stored_session_hash(contract_hash.into(), entry_point, args)
.with_payment_bytes(vec![], runtime_args! { ARG_AMOUNT => payment, })
.with_authorization_keys(&[sender])
.with_deploy_hash(deploy_hash)
.build();

ExecuteRequestBuilder::new().push_deploy(deploy)
ExecuteRequestBuilder::from_deploy_item(&deploy)
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
];
};

nativeBuildInputs = [ pkgs.binaryen pkgs.lld pkgs.llvmPackages.bintools pkgs.pkg-config ];
nativeBuildInputs = with pkgs;[ binaryen lld llvmPackages.bintools pkg-config cmake ];
buildInputs = with pkgs; [
openssl.dev
postgresql.lib
Expand Down
2 changes: 1 addition & 1 deletion kairos-cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
let dest_path = Path::new(&out_dir).join("deposit-session-optimized.wasm");

// Copy the file from the source to the destination
fs::copy(source_path, dest_path).expect("Failed to copy WASM file");
//fs::copy(source_path, dest_path).expect("Failed to copy WASM file");
}

fn get_default_wasm_directory(project_root: &str) -> PathBuf {
Expand Down
Loading

0 comments on commit 8b1e7a9

Please sign in to comment.