Skip to content

Commit 39b4fc0

Browse files
emit new block, function finalized
1 parent 3ec132e commit 39b4fc0

File tree

4 files changed

+74
-71
lines changed

4 files changed

+74
-71
lines changed

src/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ impl BasicInfo {
113113
index += 33;
114114

115115
// parsing height
116-
let (height, height_size) = tools::load_u256(&data[index..])
116+
let (height, _) = tools::load_u256(&data[index..])
117117
.change_context(BlockError::BasicInfo(BasicInfoErrorKind::Parse))?;
118-
index += height_size + 1;
118+
//index += height_size + 1;
119119

120120
Ok(BasicInfo {
121121
timestamp,

src/blockchaintree.rs

+50-64
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,22 @@
1-
use std::{collections::HashMap, path::Path};
1+
use std::{collections::HashMap, path::Path, sync::Arc};
22

33
use crate::{
44
block::{self, BlockArc, TransactionBlock},
55
chain,
66
errors::{BCTreeErrorKind, BlockChainTreeError},
7-
merkletree, tools,
7+
merkletree,
8+
static_values::{
9+
AMMOUNT_SUMMARY, BLOCKS_PER_EPOCH, GAS_SUMMARY, OLD_AMMOUNT_SUMMARY, OLD_GAS_SUMMARY,
10+
},
11+
tools,
812
transaction::Transaction,
913
txpool,
14+
types::Hash,
1015
};
1116
use error_stack::{Report, ResultExt};
1217
use primitive_types::U256;
1318
use sled::Db;
1419

15-
static BLOCKCHAIN_DIRECTORY: &str = "./BlockChainTree/";
16-
17-
static AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARY/";
18-
static OLD_AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARYOLD/";
19-
20-
static GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARY/";
21-
static OLD_GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARYOLD/";
22-
23-
static MAIN_CHAIN_DIRECTORY: &str = "./BlockChainTree/MAIN/";
24-
25-
static DERIVATIVE_CHAINS_DIRECTORY: &str = "./BlockChainTree/DERIVATIVES/";
26-
static CHAINS_FOLDER: &str = "CHAINS/";
27-
28-
static BLOCKS_FOLDER: &str = "BLOCKS/";
29-
static REFERENCES_FOLDER: &str = "REF/";
30-
static TRANSACTIONS_FOLDER: &str = "TRANSACTIONS/";
31-
32-
static CONFIG_FILE: &str = "Chain.config";
33-
static LOOKUP_TABLE_FILE: &str = "LookUpTable.dat";
34-
static TRANSACTIONS_POOL: &str = "TRXS_POOL.pool";
35-
36-
pub static BEGINNING_DIFFICULTY: [u8; 32] = [
37-
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
38-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
39-
];
40-
static MAX_DIFFICULTY: [u8; 32] = [
41-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 128,
43-
];
44-
45-
pub static FEE_STEP: u64 = 1000000;
46-
47-
pub static ROOT_PRIVATE_ADDRESS: [u8; 32] = [1u8; 32];
48-
pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [
49-
3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96,
50-
72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
51-
];
52-
pub static BLOCKS_PER_EPOCH: u64 = 100000;
53-
pub static INCEPTION_TIMESTAMP: u64 = 1597924800;
54-
5520
pub struct BlockChainTree {
5621
pub main_chain: chain::MainChain,
5722
pub derivative_chains: HashMap<[u8; 32], chain::DerivativeChain>,
@@ -352,9 +317,9 @@ impl BlockChainTree {
352317
&self,
353318
pow: [u8; 32],
354319
founder: [u8; 33],
355-
transactions: &[Transaction],
320+
transactions: &[Hash],
356321
timestamp: u64,
357-
) -> Result<[u8; 32], Report<BlockChainTreeError>> {
322+
) -> Result<block::BlockArc, Report<BlockChainTreeError>> {
358323
let last_block = self.main_chain.get_last_block().await?.unwrap(); // practically cannot fail
359324
let prev_hash = last_block
360325
.hash()
@@ -364,33 +329,54 @@ impl BlockChainTree {
364329
if !tools::check_pow(&prev_hash, &last_block.get_info().difficulty, &pow) {
365330
return Err(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::WrongPow).into());
366331
};
367-
332+
let mut difficulty = last_block.get_info().difficulty;
333+
tools::recalculate_difficulty(last_block.get_info().timestamp, timestamp, &mut difficulty);
334+
let fee = tools::recalculate_fee(&difficulty);
368335
let default_info = block::BasicInfo {
369336
timestamp,
370337
pow,
371338
previous_hash: prev_hash,
372339
height: last_block.get_info().height,
373-
difficulty: last_block.get_info().difficulty,
340+
difficulty,
374341
founder,
375342
};
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-
393-
todo!()
343+
let new_block: block::BlockArc =
344+
if ((last_block.get_info().height + 1) % BLOCKS_PER_EPOCH).is_zero() {
345+
if transactions.len() != 0 {
346+
return Err(BlockChainTreeError::BlockChainTree(
347+
BCTreeErrorKind::SummarizeBlockWrongTransactionsAmount,
348+
)
349+
.into());
350+
}
351+
352+
let merkle_tree_root = self.summarize()?;
353+
354+
let summarize_block = Arc::new(block::SummarizeBlock {
355+
default_info,
356+
merkle_tree_root,
357+
});
358+
359+
summarize_block
360+
} else {
361+
if transactions.len() == 0 {
362+
return Err(BlockChainTreeError::BlockChainTree(
363+
BCTreeErrorKind::CreateMainChainBlock,
364+
)
365+
.into());
366+
}
367+
368+
let merkle_tree = merkletree::MerkleTree::build_tree(transactions);
369+
let transaction_block = Arc::new(block::TransactionBlock::new(
370+
fee,
371+
default_info,
372+
*merkle_tree.get_root(),
373+
Vec::from_iter(transactions.iter().cloned()),
374+
));
375+
transaction_block
376+
};
377+
378+
self.main_chain.add_block(new_block.clone()).await?;
379+
Ok(new_block)
394380
}
395381

396382
pub async fn flush(&self) -> Result<(), Report<BlockChainTreeError>> {

src/static_values.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ pub static BLOCKCHAIN_DIRECTORY: &str = "./BlockChainTree/";
66
pub static AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARY/";
77
pub static OLD_AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARYOLD/";
88

9+
pub static GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARY/";
10+
pub static OLD_GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARYOLD/";
11+
912
pub static MAIN_CHAIN_DIRECTORY: &str = "./BlockChainTree/MAIN/";
1013

1114
pub static DERIVATIVE_CHAINS_DIRECTORY: &str = "./BlockChainTree/DERIVATIVES/";
1215
pub static CHAINS_FOLDER: &str = "CHAINS/";
13-
//static DERIVATIVE_DB_DIRECTORY: BlockChainTreeError = "./BlockChainTree/DERIVATIVE/DB/";
1416

1517
pub static BLOCKS_FOLDER: &str = "BLOCKS/";
1618
pub static REFERENCES_FOLDER: &str = "REF/";
@@ -25,11 +27,20 @@ pub static BEGINNING_DIFFICULTY: [u8; 32] = [
2527
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2628
];
2729

28-
pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [0; 33];
30+
pub static MAX_DIFFICULTY: [u8; 32] = [
31+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 128,
33+
];
34+
35+
pub static ROOT_PRIVATE_ADDRESS: [u8; 32] = [1u8; 32];
36+
pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [
37+
3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96,
38+
72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
39+
];
2940

3041
pub static INCEPTION_TIMESTAMP: u64 = 1597924800;
3142

32-
pub static BLOCKS_PER_ITERATION: usize = 12960;
43+
pub static BLOCKS_PER_EPOCH: usize = 1000000;
3344

3445
pub static TIME_PER_BLOCK: u64 = 600;
3546

@@ -38,5 +49,5 @@ lazy_static! {
3849
pub static ref INITIAL_FEE: U256 = U256::from_dec_str("25000000000000000").unwrap(); // 100_000_000//4
3950
pub static ref FEE_STEP: U256 = U256::from_dec_str("625000000000").unwrap(); // 100_000_000//255
4051
pub static ref MAIN_CHAIN_PAYMENT: U256 = *INITIAL_FEE;
41-
pub static ref COINS_PER_CYCLE: U256 = (*MAIN_CHAIN_PAYMENT*2000usize*BLOCKS_PER_ITERATION) + *COIN_FRACTIONS*10000usize;
52+
pub static ref COINS_PER_CYCLE: U256 = (*MAIN_CHAIN_PAYMENT*2000usize*BLOCKS_PER_EPOCH) + *COIN_FRACTIONS*10000usize;
4253
}

src/tools.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::errors::*;
2-
use crate::static_values::TIME_PER_BLOCK;
2+
use crate::static_values::{FEE_STEP, TIME_PER_BLOCK};
33
use crate::types::Hash;
44
use error_stack::{Report, Result, ResultExt};
55
use num_bigint::BigUint;
@@ -236,6 +236,12 @@ pub fn recalculate_difficulty(prev_timestamp: u64, timestamp: u64, prev_difficul
236236
}
237237
}
238238

239+
pub fn recalculate_fee(current_difficulty: &Hash) -> U256 {
240+
let leading_zeros = count_leading_zeros(current_difficulty);
241+
242+
FEE_STEP.clone() * leading_zeros
243+
}
244+
239245
#[cfg(test)]
240246
mod tests {
241247

0 commit comments

Comments
 (0)