fix: utxo decoding added

This commit is contained in:
Oleksandr Pravdyvyi 2024-12-25 09:50:54 +02:00
parent 52a3ff9cbf
commit 24853fa1da
7 changed files with 158 additions and 58 deletions

1
Cargo.lock generated
View File

@ -2705,6 +2705,7 @@ dependencies = [
"consensus",
"env_logger",
"futures",
"hex",
"log",
"networking",
"node_core",

View File

@ -6,6 +6,8 @@ use std::{
},
};
use k256::elliptic_curve::group::GroupEncoding;
use ::storage::transaction::{Transaction, TransactionPayload, TxKind};
use accounts::account_core::{Account, AccountAddress};
use anyhow::Result;
@ -14,17 +16,20 @@ use executions::{
private_exec::{generate_commitments, generate_nullifiers},
se::{commit, tag_random},
};
use log::info;
use rand::thread_rng;
use secp256k1_zkp::{CommitmentSecrets, Tweak};
use sequencer_client::{json::SendTxResponse, SequencerClient};
use serde::{Deserialize, Serialize};
use storage::NodeChainStore;
use tokio::{sync::Mutex, task::JoinHandle};
use tokio::{sync::RwLock, task::JoinHandle};
use utxo::utxo_core::UTXO;
use zkvm::{
prove_mint_utxo, prove_send_utxo, prove_send_utxo_deshielded, prove_send_utxo_shielded,
};
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
pub mod config;
pub mod executions;
pub mod sequencer_client;
@ -55,9 +60,8 @@ pub enum ActionData {
}
pub struct NodeCore {
pub storage: Arc<Mutex<NodeChainStore>>,
pub storage: Arc<RwLock<NodeChainStore>>,
pub curr_height: Arc<AtomicU64>,
pub acc_map: HashMap<AccountAddress, Account>,
pub node_config: NodeConfig,
pub db_updater_handle: JoinHandle<Result<()>>,
pub sequencer_client: Arc<SequencerClient>,
@ -72,8 +76,6 @@ impl NodeCore {
let mut storage = NodeChainStore::new_with_genesis(&config.home, genesis_block);
let account_map = HashMap::new();
let mut chain_height = genesis_id.genesis_id;
//Chain update loop
@ -89,7 +91,7 @@ impl NodeCore {
chain_height += 1;
}
let wrapped_storage = Arc::new(Mutex::new(storage));
let wrapped_storage = Arc::new(RwLock::new(storage));
let chain_height_wrapped = Arc::new(AtomicU64::new(chain_height));
let wrapped_storage_thread = wrapped_storage.clone();
@ -102,7 +104,7 @@ impl NodeCore {
if let Ok(block) = client_thread.get_block(next_block).await {
{
let mut storage_guard = wrapped_storage_thread.lock().await;
let mut storage_guard = wrapped_storage_thread.write().await;
storage_guard.dissect_insert_block(block.block)?;
}
@ -120,20 +122,39 @@ impl NodeCore {
Ok(Self {
storage: wrapped_storage,
curr_height: chain_height_wrapped,
acc_map: account_map,
node_config: config.clone(),
db_updater_handle: updater_handle,
sequencer_client: client.clone(),
})
}
pub fn mint_utxo_private(&self, acc: AccountAddress, amount: u128) -> Transaction {
pub async fn create_new_account(&mut self) -> AccountAddress {
let account = Account::new();
let addr = account.address;
{
let mut write_guard = self.storage.write().await;
write_guard.acc_map.insert(account.address, account);
}
addr
}
pub async fn mint_utxo_private(&self, acc: AccountAddress, amount: u128) -> Transaction {
let (utxo, receipt) = prove_mint_utxo(amount, acc);
let accout = self.acc_map.get(&acc).unwrap();
let acc_map_read_guard = self.storage.read().await;
let accout = acc_map_read_guard.acc_map.get(&acc).unwrap();
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let encoded_data = Account::encrypt_data(
&accout.produce_ephemeral_key_holder(),
&ephm_key_holder,
accout.key_holder.viewing_public_key,
&serde_json::to_vec(&utxo).unwrap(),
);
@ -152,6 +173,7 @@ impl NodeCore {
nullifier_created_hashes: vec![],
execution_proof_private: serde_json::to_string(&receipt).unwrap(),
encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec())],
ephemeral_pub_key: eph_pub_key.to_vec(),
}
.into()
}
@ -169,6 +191,7 @@ impl NodeCore {
nullifier_created_hashes: vec![],
execution_proof_private: "".to_string(),
encoded_data: vec![],
ephemeral_pub_key: vec![],
}
.into()
}
@ -178,10 +201,12 @@ impl NodeCore {
utxo: UTXO,
receivers: Vec<(u128, AccountAddress)>,
) -> Transaction {
let accout = self.acc_map.get(&utxo.owner).unwrap();
let acc_map_read_guard = self.storage.read().await;
let accout = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
let commitment_in = {
let guard = self.storage.lock().await;
let guard = self.storage.write().await;
guard.utxo_commitments_store.get_tx(utxo.hash).unwrap().hash
};
@ -203,13 +228,17 @@ impl NodeCore {
.map(|(utxo, _)| utxo.clone())
.collect();
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let encoded_data: Vec<(Vec<u8>, Vec<u8>)> = utxos
.iter()
.map(|utxo_enc| {
let accout_enc = self.acc_map.get(&utxo_enc.owner).unwrap();
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
let (ciphertext, nonce) = Account::encrypt_data(
&accout.produce_ephemeral_key_holder(),
&ephm_key_holder,
accout_enc.key_holder.viewing_public_key,
&serde_json::to_vec(&utxo_enc).unwrap(),
);
@ -232,6 +261,7 @@ impl NodeCore {
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: serde_json::to_string(&receipt).unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
.into()
}
@ -242,7 +272,9 @@ impl NodeCore {
balance: u64,
receivers: Vec<(u128, AccountAddress)>,
) -> Transaction {
let accout = self.acc_map.get(&acc).unwrap();
let acc_map_read_guard = self.storage.read().await;
let accout = acc_map_read_guard.acc_map.get(&acc).unwrap();
let commitment_secrets = CommitmentSecrets {
value: balance,
@ -278,13 +310,17 @@ impl NodeCore {
.map(|(utxo, _)| utxo.clone())
.collect();
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes();
let encoded_data: Vec<(Vec<u8>, Vec<u8>)> = utxos
.iter()
.map(|utxo_enc| {
let accout_enc = self.acc_map.get(&utxo_enc.owner).unwrap();
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
let (ciphertext, nonce) = Account::encrypt_data(
&accout.produce_ephemeral_key_holder(),
&ephm_key_holder,
accout_enc.key_holder.viewing_public_key,
&serde_json::to_vec(&utxo_enc).unwrap(),
);
@ -313,6 +349,7 @@ impl NodeCore {
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: serde_json::to_string(&receipt).unwrap(),
encoded_data,
ephemeral_pub_key: eph_pub_key.to_vec(),
}
.into()
}
@ -322,10 +359,12 @@ impl NodeCore {
utxo: UTXO,
receivers: Vec<(u128, AccountAddress)>,
) -> Transaction {
let accout = self.acc_map.get(&utxo.owner).unwrap();
let acc_map_read_guard = self.storage.read().await;
let accout = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
let commitment_in = {
let guard = self.storage.lock().await;
let guard = self.storage.write().await;
guard.utxo_commitments_store.get_tx(utxo.hash).unwrap().hash
};
@ -356,6 +395,7 @@ impl NodeCore {
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
execution_proof_private: serde_json::to_string(&receipt).unwrap(),
encoded_data: vec![],
ephemeral_pub_key: vec![],
}
.into()
}
@ -367,7 +407,7 @@ impl NodeCore {
) -> Result<SendTxResponse> {
Ok(self
.sequencer_client
.send_tx(self.mint_utxo_private(acc, amount))
.send_tx(self.mint_utxo_private(acc, amount).await)
.await?)
}
@ -415,4 +455,44 @@ impl NodeCore {
.send_tx(self.transfer_utxo_deshielded(utxo, receivers).await)
.await?)
}
pub async fn scenario_1(&mut self) {
let acc_addr = self.create_new_account().await;
let resp = self.send_private_mint_tx(acc_addr, 100).await.unwrap();
info!("Response for mint private is {resp:?}");
let new_utxo_hash: [u8; 32] = hex::decode(resp.additional_data.unwrap())
.unwrap()
.try_into()
.unwrap();
info!("Awaiting new blocks");
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
let new_utxo = {
let mut write_guard = self.storage.write().await;
let acc = write_guard.acc_map.get_mut(&acc_addr).unwrap();
acc.utxo_tree
.get_item(new_utxo_hash)
.unwrap()
.unwrap()
.clone()
};
let acc_map_read_guard = self.storage.read().await;
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
let resp = self
.send_deshielded_send_tx(new_utxo, vec![(100, acc_addr)])
.await
.unwrap();
info!("Response for send deshielded is {resp:?}");
info!("Awaiting new blocks");
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
info!("New account public balance is {:?}", acc.balance);
}
}

View File

@ -33,6 +33,7 @@ pub struct RegisterAccountResponse {
#[derive(Serialize, Deserialize, Debug)]
pub struct SendTxResponse {
pub status: String,
pub additional_data: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]

View File

@ -1,9 +1,11 @@
use std::path::Path;
use std::{collections::HashMap, path::Path};
use accounts::account_core::{Account, AccountAddress};
use accounts_store::NodeAccountsStore;
use anyhow::Result;
use block_store::NodeBlockStore;
use elliptic_curve::group::GroupEncoding;
use k256::AffinePoint;
use storage::{
block::Block,
merkle_tree_public::{
@ -15,24 +17,22 @@ use storage::{
transaction::Transaction,
utxo_commitment::UTXOCommitment,
};
use utxo::utxo_core::UTXO;
pub mod accounts_store;
pub mod block_store;
pub struct NodeChainStore {
pub acc_store: NodeAccountsStore,
pub acc_map: HashMap<AccountAddress, Account>,
pub block_store: NodeBlockStore,
pub nullifier_store: NullifierSparseMerkleTree,
pub utxo_commitments_store: UTXOCommitmentsMerkleTree,
pub pub_tx_store: PublicTransactionMerkleTree,
///For simplicity, we will allow only one account per node.
/// ToDo: Change it in future
node_main_account_info: Account,
}
impl NodeChainStore {
pub fn new_with_genesis(home_dir: &Path, genesis_block: Block) -> Self {
let acc_store = NodeAccountsStore::default();
let acc_map = HashMap::new();
let nullifier_store = NullifierSparseMerkleTree::default();
let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]);
let pub_tx_store = PublicTransactionMerkleTree::new(vec![]);
@ -44,19 +44,14 @@ impl NodeChainStore {
.unwrap();
Self {
acc_store,
acc_map,
block_store,
nullifier_store,
utxo_commitments_store,
pub_tx_store,
node_main_account_info: Account::new(),
}
}
pub fn get_main_account_addr(&self) -> AccountAddress {
self.node_main_account_info.address
}
pub fn dissect_insert_block(&mut self, block: Block) -> Result<()> {
for tx in &block.transactions {
self.utxo_commitments_store.add_tx_multiple(
@ -75,6 +70,36 @@ impl NodeChainStore {
.collect(),
)?;
let slice_try: Result<[u8; 33],_> = tx.ephemeral_pub_key.clone().try_into();
let eph_key_compressed = slice_try.and_then(|inner| Ok(<AffinePoint as GroupEncoding>::Repr::from(inner)));
if let Ok(eph_key_compressed) = eph_key_compressed {
let ephemeral_public_key_sender = AffinePoint::from_bytes(&eph_key_compressed);
if ephemeral_public_key_sender.is_some().into() {
let ephemeral_public_key_sender = ephemeral_public_key_sender.unwrap();
for (ciphertext, nonce) in tx.encoded_data.clone() {
let slice = nonce.as_slice();
let nonce = accounts::key_management::constants_types::Nonce::clone_from_slice(slice);
for (acc_id, acc) in self.acc_map.iter_mut() {
let decoded_data_curr_acc =
acc.decrypt_data(ephemeral_public_key_sender, ciphertext.clone(), nonce);
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)?;
}
}
}
}
}
}
self.pub_tx_store.add_tx(tx.clone());
}
@ -82,13 +107,4 @@ impl NodeChainStore {
Ok(())
}
pub fn calculate_transaction_execution(
&mut self,
_acc_addr: AccountAddress,
_contract_addr: TreeHashType,
_call_data: Vec<u8>,
) -> Transaction {
todo!()
}
}

View File

@ -13,6 +13,7 @@ actix.workspace = true
actix-cors.workspace = true
futures.workspace = true
tokio.workspace = true
hex.workspace = true
actix-web.workspace = true

View File

@ -34,25 +34,21 @@ impl JsonHandler {
}
}
// async fn process_register_account(&self, request: Request) -> Result<Value, RpcErr> {
// let req = RegisterAccountRequest::parse(Some(request.params))?;
async fn process_register_account(&self, request: Request) -> Result<Value, RpcErr> {
let _req = RegisterAccountRequest::parse(Some(request.params))?;
// {
// let guard = self.node_chain_store.lock().await;
let acc_addr = {
let mut guard = self.node_chain_store.lock().await;
// guard
// .sequencer_client
// .register_account(&guard.main_acc)
// .await
// .map_err(cast_seq_client_error_into_rpc_error)?;
// }
guard.create_new_account().await
};
// let helperstruct = RegisterAccountResponse {
// status: "success".to_string(),
// };
let helperstruct = RegisterAccountResponse {
status: hex::encode(acc_addr),
};
// respond(helperstruct)
// }
respond(helperstruct)
}
async fn process_send_tx(&self, request: Request) -> Result<Value, RpcErr> {
let req = SendTxRequest::parse(Some(request.params))?;
@ -77,7 +73,7 @@ impl JsonHandler {
pub async fn process_request_internal(&self, request: Request) -> Result<Value, RpcErr> {
match request.method.as_ref() {
//Todo : Add handling of more JSON RPC methods
//"register_account" => self.process_register_account(request).await,
"register_account" => self.process_register_account(request).await,
"send_tx" => self.process_send_tx(request).await,
_ => Err(RpcErr(RpcError::method_not_found(request.method))),
}

View File

@ -39,6 +39,8 @@ pub struct Transaction {
pub execution_proof_private: String,
///Encoded blobs of data
pub encoded_data: Vec<(CipherText, Vec<u8>)>,
///Transaction senders ephemeral pub key
pub ephemeral_pub_key: Vec<u8>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -59,6 +61,8 @@ pub struct TransactionPayload {
pub execution_proof_private: String,
///Encoded blobs of data
pub encoded_data: Vec<(CipherText, Vec<u8>)>,
///Transaction senders ephemeral pub key
pub ephemeral_pub_key: Vec<u8>,
}
impl From<TransactionPayload> for Transaction {
@ -81,6 +85,7 @@ impl From<TransactionPayload> for Transaction {
nullifier_created_hashes: value.nullifier_created_hashes,
execution_proof_private: value.execution_proof_private,
encoded_data: value.encoded_data,
ephemeral_pub_key: value.ephemeral_pub_key,
}
}
}