mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-07 15:53:14 +00:00
fix: block structs update
This commit is contained in:
parent
512d96d384
commit
ca131b2c26
@ -1,20 +1,34 @@
|
|||||||
use std::io::{Cursor, Read};
|
use std::io::{Cursor, Read};
|
||||||
|
|
||||||
|
use k256::ecdsa::Signature;
|
||||||
use rs_merkle::Hasher;
|
use rs_merkle::Hasher;
|
||||||
|
|
||||||
use crate::merkle_tree_public::hasher::OwnHasher;
|
use crate::{merkle_tree_public::hasher::OwnHasher, transaction::AuthenticatedTransaction};
|
||||||
use nssa;
|
use nssa;
|
||||||
|
|
||||||
pub type BlockHash = [u8; 32];
|
pub type BlockHash = [u8; 32];
|
||||||
pub type BlockId = u64;
|
pub type BlockId = u64;
|
||||||
|
pub type TimeStamp = u64;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Block {
|
pub struct BlockHeader {
|
||||||
pub block_id: BlockId,
|
pub block_id: BlockId,
|
||||||
pub prev_block_id: BlockId,
|
pub prev_block_id: BlockId,
|
||||||
pub prev_block_hash: BlockHash,
|
pub prev_block_hash: BlockHash,
|
||||||
pub 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)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
@ -22,7 +36,9 @@ pub struct HashableBlockData {
|
|||||||
pub block_id: BlockId,
|
pub block_id: BlockId,
|
||||||
pub prev_block_id: BlockId,
|
pub prev_block_id: BlockId,
|
||||||
pub prev_block_hash: BlockHash,
|
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 {
|
impl From<HashableBlockData> for Block {
|
||||||
@ -31,11 +47,17 @@ impl From<HashableBlockData> for Block {
|
|||||||
let hash = OwnHasher::hash(&data);
|
let hash = OwnHasher::hash(&data);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
header: BlockHeader {
|
||||||
block_id: value.block_id,
|
block_id: value.block_id,
|
||||||
prev_block_id: value.prev_block_id,
|
prev_block_id: value.prev_block_id,
|
||||||
hash,
|
|
||||||
transactions: value.transactions,
|
|
||||||
prev_block_hash: value.prev_block_hash,
|
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,
|
block_id: value.block_id,
|
||||||
prev_block_id: value.prev_block_id,
|
prev_block_id: value.prev_block_id,
|
||||||
prev_block_hash: value.prev_block_hash,
|
prev_block_hash: value.prev_block_hash,
|
||||||
|
timestamp: value.timestamp,
|
||||||
|
signature: value.signature,
|
||||||
transactions: value.transactions,
|
transactions: value.transactions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,39 +25,49 @@ pub type Tag = u8;
|
|||||||
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum TxKind {
|
pub enum TxKind {
|
||||||
Public,
|
Public,
|
||||||
Private,
|
PrivacyPreserving,
|
||||||
Shielded,
|
|
||||||
Deshielded,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
///General transaction object
|
///General transaction object
|
||||||
pub struct TransactionBody {
|
pub struct TransactionBody {
|
||||||
pub tx_kind: TxKind,
|
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
|
///Encoded blobs of data
|
||||||
pub encoded_data: Vec<(CipherText, Vec<u8>, Tag)>,
|
pub encoded_transaction_data: Vec<u8>,
|
||||||
///Transaction senders ephemeral pub key
|
}
|
||||||
pub ephemeral_pub_key: Vec<u8>,
|
|
||||||
///Public (Pedersen) commitment
|
impl From<nssa::NSSATransaction> for TransactionBody {
|
||||||
pub commitment: Vec<PedersenCommitment>,
|
fn from(value: nssa::NSSATransaction) -> Self {
|
||||||
///tweak
|
match value {
|
||||||
pub tweak: Tweak,
|
nssa::NSSATransaction::Public(tx) => {
|
||||||
///secret_r
|
Self {
|
||||||
pub secret_r: [u8; 32],
|
tx_kind: TxKind::Public,
|
||||||
///Hex-encoded address of a smart contract account called
|
encoded_transaction_data: tx.to_bytes(),
|
||||||
pub sc_addr: String,
|
}
|
||||||
|
},
|
||||||
|
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)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@ -172,41 +182,6 @@ impl TransactionBody {
|
|||||||
pub fn log(&self) {
|
pub fn log(&self) {
|
||||||
info!("Transaction hash is {:?}", hex::encode(self.hash()));
|
info!("Transaction hash is {:?}", hex::encode(self.hash()));
|
||||||
info!("Transaction tx_kind is {:?}", self.tx_kind);
|
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!(
|
info!(
|
||||||
"Transaction encoded_data is {:?}",
|
"Transaction encoded_data is {:?}",
|
||||||
self.encoded_data
|
self.encoded_data
|
||||||
@ -214,10 +189,6 @@ impl TransactionBody {
|
|||||||
.map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone())))
|
.map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone())))
|
||||||
.collect::<Vec<_>>()
|
.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 {
|
fn test_transaction_body() -> TransactionBody {
|
||||||
TransactionBody {
|
TransactionBody {
|
||||||
tx_kind: TxKind::Public,
|
tx_kind: TxKind::Public,
|
||||||
execution_input: vec![1, 2, 3, 4],
|
encoded_transaction_data: 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(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,3 +16,21 @@ pub use signature::PrivateKey;
|
|||||||
pub use signature::PublicKey;
|
pub use signature::PublicKey;
|
||||||
pub use signature::Signature;
|
pub use signature::Signature;
|
||||||
pub use state::V01State;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -77,18 +77,33 @@ fn block_to_transactions_map(block: &Block) -> HashMap<TreeHashType, u64> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use common::block::{BlockBody, BlockHeader};
|
||||||
|
use k256::{
|
||||||
|
ecdsa::{signature::SignerMut, Signature, SigningKey},
|
||||||
|
FieldBytes,
|
||||||
|
};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_transaction_by_hash() {
|
fn test_get_transaction_by_hash() {
|
||||||
let temp_dir = tempdir().unwrap();
|
let temp_dir = tempdir().unwrap();
|
||||||
let path = temp_dir.path();
|
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 {
|
let genesis_block = Block {
|
||||||
|
header: BlockHeader {
|
||||||
block_id: 0,
|
block_id: 0,
|
||||||
prev_block_id: 0,
|
prev_block_id: 0,
|
||||||
prev_block_hash: [0; 32],
|
prev_block_hash: [0; 32],
|
||||||
hash: [1; 32],
|
hash: [1; 32],
|
||||||
|
timestamp: 0,
|
||||||
|
signature: private_key.sign(&[42; 32]),
|
||||||
|
},
|
||||||
|
body: BlockBody {
|
||||||
transactions: vec![],
|
transactions: vec![],
|
||||||
|
},
|
||||||
};
|
};
|
||||||
// Start an empty node store
|
// Start an empty node store
|
||||||
let mut node_store =
|
let mut node_store =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user