fix: block structs update

This commit is contained in:
Oleksandr Pravdyvyi 2025-08-28 12:00:04 +03:00
parent 512d96d384
commit ca131b2c26
No known key found for this signature in database
GPG Key ID: 9F8955C63C443871
4 changed files with 108 additions and 91 deletions

View File

@ -1,20 +1,34 @@
use std::io::{Cursor, Read};
use k256::ecdsa::Signature;
use rs_merkle::Hasher;
use crate::merkle_tree_public::hasher::OwnHasher;
use crate::{merkle_tree_public::hasher::OwnHasher, transaction::AuthenticatedTransaction};
use nssa;
pub type BlockHash = [u8; 32];
pub type BlockId = u64;
pub type TimeStamp = u64;
#[derive(Debug, Clone)]
pub struct Block {
pub struct BlockHeader {
pub block_id: BlockId,
pub prev_block_id: BlockId,
pub prev_block_hash: BlockHash,
pub hash: BlockHash,
pub transactions: Vec<nssa::PublicTransaction>,
pub timestamp: TimeStamp,
pub signature: Signature,
}
#[derive(Debug, Clone)]
pub struct BlockBody {
pub transactions: Vec<AuthenticatedTransaction>,
}
#[derive(Debug, Clone)]
pub struct Block {
pub header: BlockHeader,
pub body: BlockBody,
}
#[derive(Debug, PartialEq, Eq)]
@ -22,7 +36,9 @@ pub struct HashableBlockData {
pub block_id: BlockId,
pub prev_block_id: BlockId,
pub prev_block_hash: BlockHash,
pub transactions: Vec<nssa::PublicTransaction>,
pub timestamp: TimeStamp,
pub signature: Signature,
pub transactions: Vec<AuthenticatedTransaction>,
}
impl From<HashableBlockData> for Block {
@ -31,11 +47,17 @@ impl From<HashableBlockData> for Block {
let hash = OwnHasher::hash(&data);
Self {
block_id: value.block_id,
prev_block_id: value.prev_block_id,
hash,
transactions: value.transactions,
prev_block_hash: value.prev_block_hash,
header: BlockHeader {
block_id: value.block_id,
prev_block_id: value.prev_block_id,
prev_block_hash: value.prev_block_hash,
hash,
timestamp: value.timestamp,
signature: value.signature,
},
body: BlockBody {
transactions: value.transactions,
},
}
}
}
@ -46,6 +68,8 @@ impl From<Block> for HashableBlockData {
block_id: value.block_id,
prev_block_id: value.prev_block_id,
prev_block_hash: value.prev_block_hash,
timestamp: value.timestamp,
signature: value.signature,
transactions: value.transactions,
}
}

View File

@ -25,39 +25,49 @@ pub type Tag = u8;
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum TxKind {
Public,
Private,
Shielded,
Deshielded,
PrivacyPreserving,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
///General transaction object
pub struct TransactionBody {
pub tx_kind: TxKind,
///Tx input data (public part)
pub execution_input: Vec<u8>,
///Tx output data (public_part)
pub execution_output: Vec<u8>,
///Tx input utxo commitments
pub utxo_commitments_spent_hashes: Vec<TreeHashType>,
///Tx output utxo commitments
pub utxo_commitments_created_hashes: Vec<TreeHashType>,
///Tx output nullifiers
pub nullifier_created_hashes: Vec<TreeHashType>,
///Execution proof (private part)
pub execution_proof_private: String,
///Encoded blobs of data
pub encoded_data: Vec<(CipherText, Vec<u8>, Tag)>,
///Transaction senders ephemeral pub key
pub ephemeral_pub_key: Vec<u8>,
///Public (Pedersen) commitment
pub commitment: Vec<PedersenCommitment>,
///tweak
pub tweak: Tweak,
///secret_r
pub secret_r: [u8; 32],
///Hex-encoded address of a smart contract account called
pub sc_addr: String,
pub encoded_transaction_data: Vec<u8>,
}
impl From<nssa::NSSATransaction> for TransactionBody {
fn from(value: nssa::NSSATransaction) -> Self {
match value {
nssa::NSSATransaction::Public(tx) => {
Self {
tx_kind: TxKind::Public,
encoded_transaction_data: tx.to_bytes(),
}
},
nssa::NSSATransaction::PrivacyPreserving(tx) => {
Self {
tx_kind: TxKind::PrivacyPreserving,
encoded_transaction_data: tx.to_bytes(),
}
}
}
}
}
impl TryFrom<TransactionBody> for nssa::NSSATransaction {
type Error = nssa::error::NssaError;
fn try_from(value: TransactionBody) -> Result<Self, Self::Error> {
match value.tx_kind {
TxKind::Public => {
nssa::PublicTransaction::from_bytes(&value.encoded_transaction_data).map(|tx| tx.into())
},
TxKind::PrivacyPreserving => {
nssa::PrivacyPreservingTransaction::from_bytes(&value.encoded_transaction_data).map(|tx| tx.into())
},
}
}
}
#[derive(Debug, Serialize, Deserialize)]
@ -172,41 +182,6 @@ impl TransactionBody {
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 {:?}", {
if let Ok(action) = serde_json::from_slice::<ActionData>(&self.execution_input) {
action.into_hexed_print()
} else {
"".to_string()
}
});
info!("Transaction execution_output is {:?}", {
if let Ok(action) = serde_json::from_slice::<ActionData>(&self.execution_output) {
action.into_hexed_print()
} else {
"".to_string()
}
});
info!(
"Transaction utxo_commitments_spent_hashes is {:?}",
self.utxo_commitments_spent_hashes
.iter()
.map(|val| hex::encode(*val))
.collect::<Vec<_>>()
);
info!(
"Transaction utxo_commitments_created_hashes is {:?}",
self.utxo_commitments_created_hashes
.iter()
.map(|val| hex::encode(*val))
.collect::<Vec<_>>()
);
info!(
"Transaction nullifier_created_hashes is {:?}",
self.nullifier_created_hashes
.iter()
.map(|val| hex::encode(*val))
.collect::<Vec<_>>()
);
info!(
"Transaction encoded_data is {:?}",
self.encoded_data
@ -214,10 +189,6 @@ impl TransactionBody {
.map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone())))
.collect::<Vec<_>>()
);
info!(
"Transaction ephemeral_pub_key is {:?}",
hex::encode(self.ephemeral_pub_key.clone())
);
}
}
@ -309,18 +280,7 @@ mod tests {
fn test_transaction_body() -> TransactionBody {
TransactionBody {
tx_kind: TxKind::Public,
execution_input: vec![1, 2, 3, 4],
execution_output: vec![5, 6, 7, 8],
utxo_commitments_spent_hashes: vec![[9; 32], [10; 32], [11; 32], [12; 32]],
utxo_commitments_created_hashes: vec![[13; 32]],
nullifier_created_hashes: vec![[0; 32], [1; 32], [2; 32], [3; 32]],
execution_proof_private: "loremipsum".to_string(),
encoded_data: vec![(vec![255, 255, 255], vec![254, 254, 254], 1)],
ephemeral_pub_key: vec![5; 32],
commitment: vec![],
tweak: Tweak::from_slice(&[7; SECRET_KEY_SIZE]).unwrap(),
secret_r: [8; 32],
sc_addr: "someAddress".to_string(),
encoded_transaction_data: vec![1,2,3,4],
}
}

View File

@ -16,3 +16,21 @@ pub use signature::PrivateKey;
pub use signature::PublicKey;
pub use signature::Signature;
pub use state::V01State;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NSSATransaction {
Public(PublicTransaction),
PrivacyPreserving(PrivacyPreservingTransaction),
}
impl From<PublicTransaction> for NSSATransaction {
fn from(value: PublicTransaction) -> Self {
Self::Public(value)
}
}
impl From<PrivacyPreservingTransaction> for NSSATransaction {
fn from(value: PrivacyPreservingTransaction) -> Self {
Self::PrivacyPreserving(value)
}
}

View File

@ -77,18 +77,33 @@ fn block_to_transactions_map(block: &Block) -> HashMap<TreeHashType, u64> {
mod tests {
use super::*;
use common::block::{BlockBody, BlockHeader};
use k256::{
ecdsa::{signature::SignerMut, Signature, SigningKey},
FieldBytes,
};
use tempfile::tempdir;
#[test]
fn test_get_transaction_by_hash() {
let temp_dir = tempdir().unwrap();
let path = temp_dir.path();
let key_bytes = FieldBytes::from_slice(&[37; 32]);
let mut private_key: SigningKey = SigningKey::from_bytes(key_bytes).unwrap();
let genesis_block = Block {
block_id: 0,
prev_block_id: 0,
prev_block_hash: [0; 32],
hash: [1; 32],
transactions: vec![],
header: BlockHeader {
block_id: 0,
prev_block_id: 0,
prev_block_hash: [0; 32],
hash: [1; 32],
timestamp: 0,
signature: private_key.sign(&[42; 32]),
},
body: BlockBody {
transactions: vec![],
},
};
// Start an empty node store
let mut node_store =