Skip to content

Commit 3ec132e

Browse files
summarize function
1 parent bda1001 commit 3ec132e

File tree

6 files changed

+103
-50
lines changed

6 files changed

+103
-50
lines changed

src/block.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ macro_rules! bytes_to_u64 {
2727
#[derive(Debug, Clone)]
2828
pub struct BasicInfo {
2929
pub timestamp: u64,
30-
pub pow: U256,
30+
pub pow: [u8; 32],
3131
pub previous_hash: Hash,
3232
pub height: U256,
3333
pub difficulty: Hash,
@@ -37,7 +37,7 @@ pub struct BasicInfo {
3737
impl BasicInfo {
3838
pub fn new(
3939
timestamp: u64,
40-
pow: U256,
40+
pow: [u8; 32],
4141
previous_hash: Hash,
4242
height: U256,
4343
difficulty: Hash,
@@ -54,7 +54,7 @@ impl BasicInfo {
5454
}
5555

5656
pub fn get_dump_size(&self) -> usize {
57-
8 + tools::u256_size(&self.pow) + 32 + tools::u256_size(&self.height) + 32 + 33
57+
8 + 32 + 32 + tools::u256_size(&self.height) + 32 + 33
5858
}
5959
pub fn dump(&self, buffer: &mut Vec<u8>) -> Result<(), BlockError> {
6060
// dumping timestamp
@@ -67,6 +67,9 @@ impl BasicInfo {
6767
buffer.push(*byte);
6868
}
6969

70+
// dumping pow
71+
buffer.extend(self.pow);
72+
7073
// dumping difficulty
7174
buffer.extend(self.difficulty);
7275

@@ -76,9 +79,6 @@ impl BasicInfo {
7679
// dumping height
7780
tools::dump_u256(&self.height, buffer).unwrap();
7881

79-
// dumping PoW
80-
tools::dump_u256(&self.pow, buffer).unwrap();
81-
8282
Ok(())
8383
}
8484

@@ -100,6 +100,10 @@ impl BasicInfo {
100100
let previous_hash: Hash = unsafe { data[index..index + 32].try_into().unwrap_unchecked() };
101101
index += 32;
102102

103+
// parsing difficulty
104+
let pow: Hash = unsafe { data[index..index + 32].try_into().unwrap_unchecked() };
105+
index += 32;
106+
103107
// parsing difficulty
104108
let difficulty: Hash = unsafe { data[index..index + 32].try_into().unwrap_unchecked() };
105109
index += 32;
@@ -113,10 +117,6 @@ impl BasicInfo {
113117
.change_context(BlockError::BasicInfo(BasicInfoErrorKind::Parse))?;
114118
index += height_size + 1;
115119

116-
// parsing POW
117-
let (pow, _) = tools::load_u256(&data[index..])
118-
.change_context(BlockError::BasicInfo(BasicInfoErrorKind::Parse))?;
119-
120120
Ok(BasicInfo {
121121
timestamp,
122122
pow,
@@ -317,13 +317,10 @@ impl Block for DerivativeBlock {
317317
return Ok(false);
318318
}
319319

320-
let mut pow: [u8; 32] = [0; 32];
321-
self.default_info.pow.to_big_endian(&mut pow);
322-
323320
if !check_pow(
324321
&self.get_merkle_root(),
325322
&prev_block.get_info().difficulty,
326-
&pow,
323+
&self.default_info.pow,
327324
) {
328325
return Ok(false);
329326
}
@@ -413,13 +410,10 @@ impl Block for TransactionBlock {
413410
return Ok(false);
414411
}
415412

416-
let mut pow: [u8; 32] = [0; 32];
417-
self.default_info.pow.to_big_endian(&mut pow);
418-
419413
if !check_pow(
420414
&self.merkle_tree_root,
421415
&prev_block.get_info().difficulty,
422-
&pow,
416+
&self.default_info.pow,
423417
) {
424418
return Ok(false);
425419
}
@@ -547,13 +541,10 @@ impl Block for SummarizeBlock {
547541
return Ok(false);
548542
}
549543

550-
let mut pow: [u8; 32] = [0; 32];
551-
self.default_info.pow.to_big_endian(&mut pow);
552-
553544
if !check_pow(
554545
&self.merkle_tree_root,
555546
&prev_block.get_info().difficulty,
556-
&pow,
547+
&self.default_info.pow,
557548
) {
558549
return Ok(false);
559550
}

src/blockchaintree.rs

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{collections::HashMap, path::Path};
22

33
use crate::{
4-
block::{BlockArc, TransactionBlock},
4+
block::{self, BlockArc, TransactionBlock},
55
chain,
66
errors::{BCTreeErrorKind, BlockChainTreeError},
7-
tools,
7+
merkletree, tools,
88
transaction::Transaction,
99
txpool,
1010
};
@@ -42,12 +42,14 @@ static MAX_DIFFICULTY: [u8; 32] = [
4242
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 128,
4343
];
4444

45+
pub static FEE_STEP: u64 = 1000000;
46+
4547
pub static ROOT_PRIVATE_ADDRESS: [u8; 32] = [1u8; 32];
4648
pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [
4749
3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96,
4850
72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
4951
];
50-
52+
pub static BLOCKS_PER_EPOCH: u64 = 100000;
5153
pub static INCEPTION_TIMESTAMP: u64 = 1597924800;
5254

5355
pub struct BlockChainTree {
@@ -313,24 +315,81 @@ impl BlockChainTree {
313315
self.main_chain.add_transactions(transactions).await
314316
}
315317

318+
fn summarize(&self) -> Result<[u8; 32], Report<BlockChainTreeError>> {
319+
let mut hashes: Vec<[u8; 32]> = Vec::with_capacity(self.summary_db.len());
320+
for res in self.summary_db.iter() {
321+
let (address, amount) = res
322+
.change_context(BlockChainTreeError::BlockChainTree(
323+
BCTreeErrorKind::GetFunds,
324+
))
325+
.attach_printable("failed to get funds from summary_db")?;
326+
let gas_amount = self
327+
.gas_db
328+
.get(&address)
329+
.change_context(BlockChainTreeError::BlockChainTree(
330+
BCTreeErrorKind::GetFunds,
331+
))
332+
.attach_printable("failed to get funds from summary_db")?
333+
.map(|val| val.to_vec())
334+
.unwrap_or(Vec::with_capacity(0));
335+
let mut data_to_hash: Vec<u8> =
336+
Vec::with_capacity(address.len() + amount.len() + gas_amount.len() + 2);
337+
data_to_hash.extend(address.iter());
338+
data_to_hash.push(b'|');
339+
data_to_hash.extend(amount.iter());
340+
data_to_hash.push(b'|');
341+
data_to_hash.extend(gas_amount.iter());
342+
343+
hashes.push(tools::hash(&data_to_hash));
344+
}
345+
346+
let merkle_tree = merkletree::MerkleTree::build_tree(&hashes);
347+
348+
Ok(*merkle_tree.get_root())
349+
}
350+
316351
pub async fn emmit_new_main_block(
317352
&self,
318-
pow: &[u8],
353+
pow: [u8; 32],
319354
founder: [u8; 33],
320-
) -> Result<[u8; 33], Report<BlockChainTreeError>> {
355+
transactions: &[Transaction],
356+
timestamp: u64,
357+
) -> Result<[u8; 32], Report<BlockChainTreeError>> {
321358
let last_block = self.main_chain.get_last_block().await?.unwrap(); // practically cannot fail
359+
let prev_hash = last_block
360+
.hash()
361+
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::DumpDb))
362+
.attach_printable("failed to hash block")?;
322363

323-
if !tools::check_pow(
324-
&last_block
325-
.hash()
326-
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::DumpDb))
327-
.attach_printable("failed to hash block")?,
328-
&last_block.get_info().difficulty,
329-
pow,
330-
) {
364+
if !tools::check_pow(&prev_hash, &last_block.get_info().difficulty, &pow) {
331365
return Err(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::WrongPow).into());
332366
};
333367

368+
let default_info = block::BasicInfo {
369+
timestamp,
370+
pow,
371+
previous_hash: prev_hash,
372+
height: last_block.get_info().height,
373+
difficulty: last_block.get_info().difficulty,
374+
founder,
375+
};
376+
if ((last_block.get_info().height + 1) % BLOCKS_PER_EPOCH).is_zero() {
377+
if transactions.len() != 0 {
378+
return Err(BlockChainTreeError::BlockChainTree(
379+
BCTreeErrorKind::SummarizeBlockWrongTransactionsAmount,
380+
)
381+
.into());
382+
}
383+
384+
let summarized_hash = self.summarize()?;
385+
386+
//let merkle_tree = merkletree::MerkleTree::build_tree()
387+
//block::SummarizeBlock {
388+
// default_info,
389+
// merkle_tree_root: todo!(),
390+
//};
391+
}
392+
334393
todo!()
335394
}
336395

src/chain.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,18 @@ impl MainChain {
113113
if height.is_zero() {
114114
let info = BasicInfo::new(
115115
INCEPTION_TIMESTAMP,
116-
U256::zero(),
116+
[0; 32],
117117
[0u8; 32],
118118
U256::zero(),
119119
BEGINNING_DIFFICULTY,
120120
ROOT_PUBLIC_ADDRESS,
121121
);
122122
let mut initial_amount = Vec::<u8>::new();
123123
initial_amount.extend(ROOT_PUBLIC_ADDRESS.iter());
124-
initial_amount.extend([0u8; 32]);
125-
COINS_PER_CYCLE.to_big_endian(&mut initial_amount[33..]);
124+
initial_amount.push(b'|');
125+
initial_amount.extend(COINS_PER_CYCLE.to_string().as_bytes().iter());
126+
initial_amount.push(b'|');
127+
initial_amount.push(b'0');
126128

127129
let merkle_tree = MerkleTree::build_tree(&[tools::hash(&initial_amount)]);
128130
chain

src/errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ sub_errors![
174174
MoveSummaryDB: "failed to move summary database",
175175
NewTransaction: "failed to create new transaction",
176176
CreateMainChainBlock: "failed to create new block for the main chain",
177-
WrongPow: "supplied pow does not satisfy requirements"
177+
WrongPow: "supplied pow does not satisfy requirements",
178+
SummarizeBlockWrongTransactionsAmount: "summarization block should not have transactions"
178179
}
179180
];

tests/block_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use primitive_types::U256;
77
fn dump_parse_basic_info() {
88
let basic_data = block::BasicInfo {
99
timestamp: 160000,
10-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
10+
pow: [0; 32],
1111
previous_hash: [5; 32],
1212
height: U256::from_dec_str("6378216378216387213672813821736").unwrap(),
1313
difficulty: [101; 32],
@@ -34,7 +34,7 @@ fn dump_parse_basic_info() {
3434
fn dump_parse_block() {
3535
let basic_data = block::BasicInfo {
3636
timestamp: 160000,
37-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
37+
pow: [0; 32],
3838
previous_hash: [5; 32],
3939
height: U256::from_dec_str("6378216378216387213672813821736").unwrap(),
4040
difficulty: [101; 32],
@@ -81,7 +81,7 @@ fn dump_parse_block() {
8181
fn dump_parse_summarize_block() {
8282
let basic_data = block::BasicInfo {
8383
timestamp: 160000,
84-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
84+
pow: [0; 32],
8585
previous_hash: [5; 32],
8686
height: U256::from_dec_str("6378216378216387213672813821736").unwrap(),
8787
difficulty: [101; 32],
@@ -124,7 +124,7 @@ fn dump_parse_summarize_block() {
124124
fn validate_block_test() {
125125
let basic_data = block::BasicInfo {
126126
timestamp: 160000,
127-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
127+
pow: [0; 32],
128128
previous_hash: [5; 32],
129129
height: U256::from_dec_str("1").unwrap(),
130130
difficulty: [101; 32],
@@ -139,7 +139,7 @@ fn validate_block_test() {
139139

140140
let basic_data = block::BasicInfo {
141141
timestamp: 160000,
142-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
142+
pow: [0; 32],
143143
previous_hash: prev_block.hash().unwrap(),
144144
height: U256::from_dec_str("2").unwrap(),
145145
difficulty: [101; 32],
@@ -159,7 +159,7 @@ fn validate_block_test() {
159159
fn dump_parse_derivative_block() {
160160
let basic_data = block::BasicInfo {
161161
timestamp: 160000,
162-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
162+
pow: [0; 32],
163163
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
164164
height: U256::from_dec_str("2").unwrap(),
165165
difficulty: [101; 32],
@@ -208,7 +208,7 @@ fn validate_derivative_block() {
208208
let payment_transaction = [0; 32];
209209
let basic_data = block::BasicInfo {
210210
timestamp: 160000,
211-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
211+
pow: [0; 32],
212212
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
213213
height: U256::from_dec_str("2").unwrap(),
214214
difficulty: [101; 32],
@@ -221,7 +221,7 @@ fn validate_derivative_block() {
221221
let payment_transaction = [0; 32];
222222
let basic_data = block::BasicInfo {
223223
timestamp: 160000,
224-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
224+
pow: [0; 32],
225225
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
226226
height: U256::from_dec_str("2").unwrap(),
227227
difficulty: [101; 32],

tests/chain_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async fn init_flush_get_block_by_height_chain_test() {
2121
// generate block
2222
let basic_data = block::BasicInfo {
2323
timestamp: 160000,
24-
pow: U256::from_dec_str("11").unwrap(),
24+
pow: [0; 32],
2525
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
2626
height,
2727
difficulty: [101; 32],
@@ -45,7 +45,7 @@ async fn init_flush_get_block_by_height_chain_test() {
4545

4646
assert_eq!([6; 33], *block.get_founder());
4747
assert_eq!(160000, block.get_info().timestamp);
48-
assert_eq!(U256::from_dec_str("11").unwrap(), block.get_info().pow);
48+
assert_eq!([0; 32], block.get_info().pow);
4949
assert_eq!(height - 1, block.get_info().height);
5050
assert_eq!([101; 32], block.get_info().difficulty);
5151
assert_eq!(U256::from_dec_str("1").unwrap(), block.get_fee());
@@ -113,7 +113,7 @@ async fn init_flush_get_block_by_height_deriv_chain_test() {
113113
// generate block
114114
let basic_data = block::BasicInfo {
115115
timestamp: 160000,
116-
pow: U256::from_dec_str("10000000000000000000001000000001").unwrap(),
116+
pow: [0; 32],
117117
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
118118
height: U256::from_dec_str("0").unwrap(),
119119
difficulty: [101; 32],

0 commit comments

Comments
 (0)