Skip to content

Commit

Permalink
started blockchain tree
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed Apr 27, 2024
1 parent 62b4610 commit 7a154ab
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
149 changes: 148 additions & 1 deletion src/blockchaintree.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use crate::txpool;
use std::{collections::HashMap, path::Path};

use crate::{
chain,
errors::{BCTreeErrorKind, BlockChainTreeError},
tools, txpool,
};
use error_stack::{Report, ResultExt};
use lazy_static::lazy_static;
use primitive_types::U256;
use sled::Db;

static BLOCKCHAIN_DIRECTORY: &str = "./BlockChainTree/";

static AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARY/";
static OLD_AMMOUNT_SUMMARY: &str = "./BlockChainTree/SUMMARYOLD/";

static GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARY/";
static OLD_GAS_SUMMARY: &str = "./BlockChainTree/GASSUMMARYOLD/";

static MAIN_CHAIN_DIRECTORY: &str = "./BlockChainTree/MAIN/";

static DERIVATIVE_CHAINS_DIRECTORY: &str = "./BlockChainTree/DERIVATIVES/";
Expand Down Expand Up @@ -36,3 +47,139 @@ pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [
];

pub static INCEPTION_TIMESTAMP: u64 = 1597924800;

pub struct BlockChainTree {
main_chain: chain::MainChain,
derivative_chains: HashMap<[u8; 32], chain::DerivativeChain>,
summary_db: Db,
old_summary_db: Db,
gas_db: Db,
old_gas_db: Db,
}

impl BlockChainTree {
pub async fn new() -> Result<Self, Report<BlockChainTreeError>> {
let path_summary = Path::new(AMMOUNT_SUMMARY);
let path_summary_old = Path::new(OLD_AMMOUNT_SUMMARY);
let path_gas = Path::new(GAS_SUMMARY);
let path_gas_old = Path::new(OLD_GAS_SUMMARY);

// open summary DB
let summary_db = sled::open(path_summary)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open summary db")?;

// open old summary DB
let old_summary_db = sled::open(path_summary_old)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open old summary db")?;

// open gas DB
let gas_db = sled::open(path_gas)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open gas db")?;

let old_gas_db = sled::open(path_gas_old)
.change_context(BlockChainTreeError::BlockChainTree(BCTreeErrorKind::Init))
.attach_printable("failed to open old gas db")?;

Ok(Self {
main_chain: chain::MainChain::new().await?,
derivative_chains: HashMap::new(),
summary_db,
old_summary_db,
gas_db,
old_gas_db,
})
}

pub async fn add_amount(
&self,
owner: &[u8; 32],
amount: U256,
) -> Result<(), Report<BlockChainTreeError>> {
self.summary_db
.transaction(
|db| -> Result<(), sled::transaction::ConflictableTransactionError<()>> {
let prev_amount = match db.get(owner)? {
Some(v) => tools::load_u256(&v).unwrap().0,
None => U256::zero(),
};
let new_amount = prev_amount + amount;
let mut buf: Vec<u8> = Vec::with_capacity(tools::u256_size(&new_amount));
tools::dump_u256(&new_amount, &mut buf).unwrap();
db.insert(owner, buf)?;
Ok(())
},
)
.unwrap();

Ok(())
}

pub async fn sub_amount(
&self,
owner: &[u8; 32],
amount: U256,
) -> Result<(), Report<BlockChainTreeError>> {
self.summary_db
.transaction(
|db| -> Result<(), sled::transaction::ConflictableTransactionError<()>> {
let prev_amount = match db.get(owner)? {
Some(v) => tools::load_u256(&v).unwrap().0,
None => U256::zero(),
};
if prev_amount < amount {
return Err(sled::transaction::ConflictableTransactionError::Abort(()));
}
let new_amount = prev_amount + amount;
let mut buf: Vec<u8> = Vec::with_capacity(tools::u256_size(&new_amount));
tools::dump_u256(&new_amount, &mut buf).unwrap();
db.insert(owner, buf)?;
Ok(())
},
)
.unwrap();

Ok(())
}

pub async fn send_amount(
&self,
from: &[u8; 32],
to: &[u8; 32],
amount: U256,
) -> Result<(), Report<BlockChainTreeError>> {
self.summary_db
.transaction(
|db| -> Result<(), sled::transaction::ConflictableTransactionError<()>> {
let mut from_amount = match db.get(from)? {
Some(v) => tools::load_u256(&v).unwrap().0,
None => U256::zero(),
};
let mut to_amount = match db.get(to)? {
Some(v) => tools::load_u256(&v).unwrap().0,
None => U256::zero(),
};
if from_amount < amount {
return Err(sled::transaction::ConflictableTransactionError::Abort(()));
}

from_amount -= amount;
to_amount += amount;

let mut buf: Vec<u8> = Vec::with_capacity(tools::u256_size(&from_amount));
tools::dump_u256(&from_amount, &mut buf).unwrap();
db.insert(from, buf)?;

let mut buf: Vec<u8> = Vec::with_capacity(tools::u256_size(&to_amount));
tools::dump_u256(&from_amount, &mut buf).unwrap();
db.insert(to, buf)?;
Ok(())
},
)
.unwrap();

Ok(())
}
}
10 changes: 7 additions & 3 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,13 @@ impl DerivativeChain {
let raw_transaction = self.get_transaction_raw(transaction_hash)?;

if let Some(tr) = raw_transaction {
return Ok(Some(transaction::Transaction::parse(&tr).change_context(
BlockChainTreeError::Chain(ChainErrorKind::FindByHashE),
)?));
if !tr.get(0).unwrap_or(&10).eq(&(Headers::Transaction as u8)) {
return Err(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE).into());
}
return Ok(Some(
transaction::Transaction::parse(&tr[1..])
.change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE))?,
));
}

Ok(None)
Expand Down

0 comments on commit 7a154ab

Please sign in to comment.