fix: format commit

This commit is contained in:
Oleksandr Pravdyvyi 2024-12-30 09:10:04 +02:00
parent bd092fec81
commit 9807e77c57
11 changed files with 153 additions and 80 deletions

View File

@ -71,7 +71,7 @@ impl Account {
ephemeral_public_key_sender: AffinePoint,
ciphertext: CipherText,
nonce: Nonce,
) -> Vec<u8> {
) -> Result<Vec<u8>, aes_gcm::Error> {
self.key_holder
.decrypt_data(ephemeral_public_key_sender, ciphertext, nonce)
}

View File

@ -63,7 +63,7 @@ impl AddressKeyHolder {
ephemeral_public_key_sender: AffinePoint,
ciphertext: CipherText,
nonce: Nonce,
) -> Vec<u8> {
) -> Result<Vec<u8>, aes_gcm::Error> {
let key_point = self.calculate_shared_secret_receiver(ephemeral_public_key_sender);
let binding = key_point.to_bytes();
let key_raw = &binding.as_slice()[..32];
@ -73,15 +73,27 @@ impl AddressKeyHolder {
let cipher = Aes256Gcm::new(&key);
cipher.decrypt(&nonce, ciphertext.as_slice()).unwrap()
cipher.decrypt(&nonce, ciphertext.as_slice())
}
pub fn log(&self) {
info!("AddressKeyHolder top_secret_key_holder is {:?}", self.top_secret_key_holder);
info!("AddressKeyHolder utxo_secret_key_holder is {:?}", self.utxo_secret_key_holder);
info!(
"AddressKeyHolder top_secret_key_holder is {:?}",
self.top_secret_key_holder
);
info!(
"AddressKeyHolder utxo_secret_key_holder is {:?}",
self.utxo_secret_key_holder
);
info!("AddressKeyHolder address is {:?}", self.address);
info!("AddressKeyHolder nullifer_public_key is {:?}", self.nullifer_public_key);
info!("AddressKeyHolder viewing_public_key is {:?}", self.viewing_public_key);
info!(
"AddressKeyHolder nullifer_public_key is {:?}",
self.nullifer_public_key
);
info!(
"AddressKeyHolder viewing_public_key is {:?}",
self.viewing_public_key
);
}
}
@ -158,11 +170,13 @@ mod tests {
.expect("encryption failure");
// Attempt decryption
let decrypted_data: Vec<u8> = address_key_holder.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext),
nonce.clone(),
);
let decrypted_data: Vec<u8> = address_key_holder
.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext),
nonce.clone(),
)
.unwrap();
// Verify decryption is successful and matches original plaintext
assert_eq!(decrypted_data, plaintext);
@ -225,11 +239,13 @@ mod tests {
// Attempt decryption with an incorrect nonce
let incorrect_nonce = Nonce::from_slice(b"wrong nonce");
let decrypted_data = address_key_holder.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext.clone()),
incorrect_nonce.clone(),
);
let decrypted_data = address_key_holder
.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext.clone()),
incorrect_nonce.clone(),
)
.unwrap();
// The decryption should fail or produce incorrect output due to nonce mismatch
assert_ne!(decrypted_data, plaintext);
@ -266,11 +282,13 @@ mod tests {
corrupted_ciphertext[0] ^= 1; // Flip a bit in the ciphertext
// Attempt decryption
let result = address_key_holder.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(corrupted_ciphertext),
nonce.clone(),
);
let result = address_key_holder
.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(corrupted_ciphertext),
nonce.clone(),
)
.unwrap();
// The decryption should fail or produce incorrect output due to tampered ciphertext
assert_ne!(result, plaintext);
@ -302,11 +320,13 @@ mod tests {
.expect("encryption failure");
// Decrypt the data using the `AddressKeyHolder` instance
let decrypted_data = address_key_holder.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext),
nonce.clone(),
);
let decrypted_data = address_key_holder
.decrypt_data(
ephemeral_public_key_sender,
CipherText::from(ciphertext),
nonce.clone(),
)
.unwrap();
// Verify the decrypted data matches the original plaintext
assert_eq!(decrypted_data, plaintext);

View File

@ -109,7 +109,8 @@ impl NodeCore {
{
let mut storage_guard = wrapped_storage_thread.write().await;
let block_insertion_result = storage_guard.dissect_insert_block(block.block);
let block_insertion_result =
storage_guard.dissect_insert_block(block.block);
if block_insertion_result.is_err() {
info!("Block insertion failed due to {block_insertion_result:?}");
@ -393,20 +394,24 @@ impl NodeCore {
) -> Transaction {
let acc_map_read_guard = self.storage.read().await;
let commitment_in = acc_map_read_guard.utxo_commitments_store.get_tx(comm_gen_hash).unwrap().hash;
let commitment_in = acc_map_read_guard
.utxo_commitments_store
.get_tx(comm_gen_hash)
.unwrap()
.hash;
let accout = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
accout.log();
let nullifier = generate_nullifiers(
&utxo,
&accout
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key
.to_bytes()
.to_vec(),
);
&utxo,
&accout
.key_holder
.utxo_secret_key_holder
.nullifier_secret_key
.to_bytes()
.to_vec(),
);
let (resulting_balances, receipt) = prove_send_utxo_deshielded(utxo, receivers);
@ -444,7 +449,11 @@ impl NodeCore {
let timedelta = (point_after_prove - point_before_prove).as_millis();
info!("Mint utxo proof spent {timedelta:?} milliseconds");
Ok((self.sequencer_client.send_tx(tx).await?, utxo_hash, commitment_generated_hash))
Ok((
self.sequencer_client.send_tx(tx).await?,
utxo_hash,
commitment_generated_hash,
))
}
pub async fn send_public_deposit(
@ -499,7 +508,9 @@ impl NodeCore {
receivers: Vec<(u128, AccountAddress)>,
) -> Result<SendTxResponse> {
let point_before_prove = std::time::Instant::now();
let tx = self.transfer_utxo_deshielded(utxo, comm_gen_hash, receivers).await;
let tx = self
.transfer_utxo_deshielded(utxo, comm_gen_hash, receivers)
.await;
tx.log();
let point_after_prove = std::time::Instant::now();
@ -514,7 +525,8 @@ impl NodeCore {
let acc_addr = self.create_new_account().await;
info!("Account created {acc_addr:?}");
let (resp, new_utxo_hash, comm_gen_hash) = self.send_private_mint_tx(acc_addr, 100).await.unwrap();
let (resp, new_utxo_hash, comm_gen_hash) =
self.send_private_mint_tx(acc_addr, 100).await.unwrap();
info!("Response for mint private is {resp:?}");
info!("Awaiting new blocks");
@ -534,7 +546,7 @@ impl NodeCore {
};
new_utxo.log();
let resp = self
.send_deshielded_send_tx(new_utxo, comm_gen_hash, vec![(100, acc_addr)])
.await
@ -546,7 +558,7 @@ impl NodeCore {
let new_balance = {
let acc_map_read_guard = self.storage.read().await;
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
acc.log();
@ -569,7 +581,7 @@ impl NodeCore {
{
let acc_map_read_guard = self.storage.read().await;
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();;
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
acc.log();
info!("New acconut public balance is {:?}", acc.balance);
@ -607,7 +619,8 @@ impl NodeCore {
pub async fn subscenario_3(&mut self) {
let acc_addr = self.create_new_account().await;
let (resp, new_utxo_hash, comm_gen_hash) = self.send_private_mint_tx(acc_addr, 100).await.unwrap();
let (resp, new_utxo_hash, comm_gen_hash) =
self.send_private_mint_tx(acc_addr, 100).await.unwrap();
info!("Response for mint private is {resp:?}");
info!("Awaiting new blocks");
@ -707,7 +720,8 @@ impl NodeCore {
let acc_addr = self.create_new_account().await;
let acc_addr_rec = self.create_new_account().await;
let (resp, comm_gen_hash, new_utxo_hash) = self.send_private_mint_tx(acc_addr, 100).await.unwrap();
let (resp, new_utxo_hash, comm_gen_hash) =
self.send_private_mint_tx(acc_addr, 100).await.unwrap();
info!("Response for mint private is {resp:?}");
info!("Awaiting new blocks");

View File

@ -68,10 +68,9 @@ impl SequencerRpcRequest {
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct SequencerRpcResponse {
pub jsonrpc: String,
pub result: serde_json::Value,
pub id: u64,
}
}

View File

@ -123,7 +123,10 @@ impl SequencerClient {
let req = serde_json::to_value(genesis_req).unwrap();
let resp = self.call_method_with_payload("get_genesis", req).await.unwrap();
let resp = self
.call_method_with_payload("get_genesis", req)
.await
.unwrap();
let resp_deser = serde_json::from_value(resp).unwrap();

View File

@ -67,7 +67,7 @@ impl NodeChainStore {
if let Some(acc_mut) = acc_mut {
acc_mut.balance += action.amount as u64;
}
},
}
ActionData::SendMoneyDeshieldedTx(action) => {
for (balance, acc_addr) in action.receiver_data {
let acc_mut = self.acc_map.get_mut(&acc_addr);
@ -76,12 +76,13 @@ impl NodeChainStore {
acc_mut.balance += balance as u64;
}
}
},
}
ActionData::SendMoneyShieldedTx(action) => {
let acc_mut = self.acc_map.get_mut(&action.acc_sender);
if let Some(acc_mut) = acc_mut {
acc_mut.balance = acc_mut.balance.saturating_sub(action.amount as u64);
acc_mut.balance =
acc_mut.balance.saturating_sub(action.amount as u64);
}
}
}
@ -128,12 +129,14 @@ impl NodeChainStore {
nonce,
);
let decoded_utxo_try =
serde_json::from_slice::<UTXO>(&decoded_data_curr_acc);
if let Ok(decoded_data_curr_acc) = decoded_data_curr_acc {
let decoded_utxo_try =
serde_json::from_slice::<UTXO>(&decoded_data_curr_acc);
if let Ok(utxo) = decoded_utxo_try {
if &utxo.owner == acc_id {
acc.utxo_tree.insert_item(utxo)?;
if let Ok(utxo) = decoded_utxo_try {
if &utxo.owner == acc_id {
acc.utxo_tree.insert_item(utxo)?;
}
}
}
}

View File

@ -14,7 +14,9 @@ use crate::{
types::{
err_rpc::cast_seq_client_error_into_rpc_error,
rpc_structs::{
ExecuteSubscenarioRequest, ExecuteSubscenarioResponse, GetBlockDataRequest, GetBlockDataResponse, GetLastBlockRequest, GetLastBlockResponse, RegisterAccountRequest, RegisterAccountResponse, SendTxRequest
ExecuteSubscenarioRequest, ExecuteSubscenarioResponse, GetBlockDataRequest,
GetBlockDataResponse, GetLastBlockRequest, GetLastBlockResponse,
RegisterAccountRequest, RegisterAccountResponse, SendTxRequest,
},
},
};
@ -110,9 +112,7 @@ impl JsonHandler {
}
};
let helperstruct = GetBlockDataResponse {
block,
};
let helperstruct = GetBlockDataResponse { block };
respond(helperstruct)
}
@ -126,9 +126,7 @@ impl JsonHandler {
guard.curr_height.load(Ordering::Relaxed)
};
let helperstruct = GetLastBlockResponse {
last_block,
};
let helperstruct = GetLastBlockResponse { last_block };
respond(helperstruct)
}

View File

@ -68,7 +68,6 @@ pub struct GetGenesisIdResponse {
pub genesis_id: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetLastBlockResponse {
pub last_block: u64,

View File

@ -11,7 +11,9 @@ use rpc_primitives::{
use crate::{
rpc_error_responce_inverter,
types::rpc_structs::{
GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse, GetLastBlockRequest, GetLastBlockResponse, HelloRequest, HelloResponse, RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse
GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse,
GetLastBlockRequest, GetLastBlockResponse, HelloRequest, HelloResponse,
RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse,
},
};

View File

@ -95,13 +95,45 @@ impl Transaction {
pub fn log(&self) {
info!("Transaction hash is {:?}", hex::encode(self.hash));
info!("Transaction tx_kind is {:?}", self.tx_kind);
info!("Transaction execution_input is {:?}", hex::encode(self.execution_input.clone()));
info!("Transaction execution_output is {:?}", hex::encode(self.execution_output.clone()));
info!("Transaction utxo_commitments_spent_hashes is {:?}", self.utxo_commitments_spent_hashes.iter().map(|val| hex::encode(val.clone())));
info!("Transaction utxo_commitments_created_hashes is {:?}", self.utxo_commitments_created_hashes.iter().map(|val| hex::encode(val.clone())));
info!("Transaction nullifier_created_hashes is {:?}", self.nullifier_created_hashes.iter().map(|val| hex::encode(val.clone())));
info!("Transaction execution_proof_private is {:?}", self.execution_proof_private);
info!("Transaction encoded_data is {:?}", self.encoded_data.iter().map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone()))));
info!("Transaction ephemeral_pub_key is {:?}", hex::encode(self.ephemeral_pub_key.clone()));
info!(
"Transaction execution_input is {:?}",
hex::encode(self.execution_input.clone())
);
info!(
"Transaction execution_output is {:?}",
hex::encode(self.execution_output.clone())
);
info!(
"Transaction utxo_commitments_spent_hashes is {:?}",
self.utxo_commitments_spent_hashes
.iter()
.map(|val| hex::encode(val.clone()))
);
info!(
"Transaction utxo_commitments_created_hashes is {:?}",
self.utxo_commitments_created_hashes
.iter()
.map(|val| hex::encode(val.clone()))
);
info!(
"Transaction nullifier_created_hashes is {:?}",
self.nullifier_created_hashes
.iter()
.map(|val| hex::encode(val.clone()))
);
info!(
"Transaction execution_proof_private is {:?}",
self.execution_proof_private
);
info!(
"Transaction encoded_data is {:?}",
self.encoded_data
.iter()
.map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone())))
);
info!(
"Transaction ephemeral_pub_key is {:?}",
hex::encode(self.ephemeral_pub_key.clone())
);
}
}
}

View File

@ -63,18 +63,21 @@ impl UTXO {
}
pub fn into_payload(&self) -> UTXOPayload {
UTXOPayload {
owner: self.owner,
asset: self.asset.clone(),
amount: self.amount,
privacy_flag: self.privacy_flag,
UTXOPayload {
owner: self.owner,
asset: self.asset.clone(),
amount: self.amount,
privacy_flag: self.privacy_flag,
}
}
pub fn log(&self) {
info!("UTXO hash is {:?}", hex::encode(self.hash));
info!("UTXO owner is {:?}", self.owner);
info!("UTXO nullifier is {:?}", self.nullifier.clone().map(|val| hex::encode(val.utxo_hash)));
info!(
"UTXO nullifier is {:?}",
self.nullifier.clone().map(|val| hex::encode(val.utxo_hash))
);
info!("UTXO asset is {:?}", hex::encode(self.asset.clone()));
info!("UTXO amount is {:?}", self.amount);
info!("UTXO privacy_flag is {:?}", self.privacy_flag);