Skip to content

Commit

Permalink
send transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
YeahNotSewerSide committed May 5, 2024
1 parent 8b0662c commit aa11eae
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 49 deletions.
11 changes: 10 additions & 1 deletion examples/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ fn main() {

let main_chain = tree.get_main_chain();

let wallet = [1u8; 33];
let wallet: [u8; 33] = [
2, 178, 140, 81, 31, 206, 208, 171, 143, 240, 128, 134, 115, 82, 188, 63, 146, 189, 14, 59,
85, 8, 11, 28, 137, 161, 145, 216, 251, 95, 93, 137, 159,
];

loop {
println!("Current height: {}", main_chain.get_height());
Expand Down Expand Up @@ -55,13 +58,19 @@ fn main() {
.block_on(tree.emmit_new_main_block(&pow, &wallet, transactions, timestamp))
.unwrap();

// Node should handle this
tree.send_amount(
&static_values::ROOT_PUBLIC_ADDRESS,
&wallet,
*static_values::MAIN_CHAIN_PAYMENT,
)
.unwrap();

let fee = tools::recalculate_fee(&last_block.get_info().difficulty);
for _ in transactions {
tree.add_amount(&wallet, fee).unwrap();
}

println!("Added new block! {:?}\n", block.hash().unwrap());

rt.block_on(tree.flush()).unwrap();
Expand Down
7 changes: 6 additions & 1 deletion examples/mine_derivative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ fn main() {

let mut tree = BlockChainTree::new().unwrap();

let wallet = [1u8; 33];
let wallet: [u8; 33] = [
2, 178, 140, 81, 31, 206, 208, 171, 143, 240, 128, 134, 115, 82, 188, 63, 146, 189, 14, 59,
85, 8, 11, 28, 137, 161, 145, 216, 251, 95, 93, 137, 159,
];

let chain = tree.get_derivative_chain(&wallet).unwrap();

Expand Down Expand Up @@ -60,12 +63,14 @@ fn main() {
.block_on(tree.emmit_new_derivative_block(&pow, &wallet, timestamp))
.unwrap();

// Node should handle this
tree.add_gas(&wallet, *static_values::MAIN_CHAIN_PAYMENT)
.unwrap();

println!("Added new block! {:?}\n", block.hash().unwrap());

rt.block_on(chain.flush()).unwrap();
rt.block_on(tree.flush()).unwrap();
break;
}
nonce += U256::one();
Expand Down
95 changes: 95 additions & 0 deletions examples/send_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use blockchaintree::static_values::BLOCKS_PER_EPOCH;
use blockchaintree::transaction::Transactionable;
use blockchaintree::{blockchaintree::BlockChainTree, static_values};
use blockchaintree::{tools, transaction};
use primitive_types::U256;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();

let mut tree = BlockChainTree::new().unwrap();

let main_chain = tree.get_main_chain();

let wallet_private: [u8; 32] = [
25, 53, 50, 224, 180, 250, 177, 186, 87, 47, 28, 80, 183, 208, 219, 119, 101, 60, 173, 157,
190, 29, 208, 231, 98, 69, 82, 211, 107, 185, 192, 224,
];
let wallet = [
2, 178, 140, 81, 31, 206, 208, 171, 143, 240, 128, 134, 115, 82, 188, 63, 146, 189, 14, 59,
85, 8, 11, 28, 137, 161, 145, 216, 251, 95, 93, 137, 159,
];
let receiver = static_values::ROOT_PUBLIC_ADDRESS;

println!("Sender amount: {}", tree.get_amount(&wallet).unwrap());
println!("Sender gas amount: {}", tree.get_gas(&wallet).unwrap());
println!("Receiver amount: {}", tree.get_amount(&receiver).unwrap());

let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let transaction = transaction::Transaction::new(
wallet.clone(),
receiver,
timestamp,
U256::from_str_radix("228", 10).unwrap(),
wallet_private,
None,
);
let transaction_hash = transaction.hash();
tree.send_transaction(&transaction).unwrap();

println!("Transaction created: {:?}", &transaction_hash);
println!("Sender amount: {}", tree.get_amount(&wallet).unwrap());
println!("Sender gas amount: {}", tree.get_gas(&wallet).unwrap());
println!("Receiver amount: {}", tree.get_amount(&receiver).unwrap());

// MINING
let mut nonce = U256::zero();
let last_block = main_chain.get_last_block().unwrap().unwrap();
let prev_hash = last_block.hash().unwrap();
let difficulty = last_block.get_info().difficulty;
while nonce < U256::MAX {
let mut pow = [0u8; 32];
nonce.to_big_endian(&mut pow);
if tools::check_pow(&prev_hash, &difficulty, &pow) {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();

let transactions: Vec<[u8; 32]> =
if ((last_block.get_info().height + 1) % BLOCKS_PER_EPOCH).is_zero() {
Vec::with_capacity(0)
} else {
vec![transaction_hash]
};

let block = rt
.block_on(tree.emmit_new_main_block(&pow, &wallet, &transactions, timestamp))
.unwrap();

// Node should handle this
tree.send_amount(
&static_values::ROOT_PUBLIC_ADDRESS,
&wallet,
*static_values::MAIN_CHAIN_PAYMENT,
)
.unwrap();

let fee = tools::recalculate_fee(&last_block.get_info().difficulty);
for _ in transactions {
tree.add_amount(&wallet, fee).unwrap();
}

println!("Added new block! {:?}\n", block.hash().unwrap());

rt.block_on(tree.flush()).unwrap();
break;
}
nonce += U256::one();
}
}
42 changes: 38 additions & 4 deletions src/blockchaintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::{
errors::{BCTreeErrorKind, BlockChainTreeError, ChainErrorKind},
merkletree,
static_values::{
self, AMMOUNT_SUMMARY, BLOCKS_PER_EPOCH, COINS_PER_CYCLE, GAS_SUMMARY,
OLD_AMMOUNT_SUMMARY, OLD_GAS_SUMMARY, ROOT_PUBLIC_ADDRESS,
self, AMMOUNT_SUMMARY, BLOCKS_PER_EPOCH, BYTE_GAS_PRICE, COINS_PER_CYCLE, GAS_SUMMARY,
MAIN_CHAIN_PAYMENT, OLD_AMMOUNT_SUMMARY, OLD_GAS_SUMMARY, ROOT_PUBLIC_ADDRESS,
},
tools,
transaction::Transaction,
transaction::Transactionable,
txpool,
types::Hash,
};
use error_stack::{Report, ResultExt};
Expand Down Expand Up @@ -154,7 +156,7 @@ impl BlockChainTree {
if prev_amount < amount {
return Err(sled::transaction::ConflictableTransactionError::Abort(()));
}
let new_amount = prev_amount + amount;
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)?;
Expand Down Expand Up @@ -248,7 +250,7 @@ impl BlockChainTree {
if prev_amount < amount {
return Err(sled::transaction::ConflictableTransactionError::Abort(()));
}
let new_amount = prev_amount + amount;
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)?;
Expand Down Expand Up @@ -470,6 +472,38 @@ impl BlockChainTree {
Ok(new_block)
}

pub fn send_transaction(
&self,
transaction: &dyn Transactionable,
) -> Result<(), Report<BlockChainTreeError>> {
let sender_gas_amount = self.get_gas(transaction.get_sender())?;
let sender_amount = self.get_amount(transaction.get_sender())?;
let amount_of_bytes = transaction.get_dump_size();
let gas_required = *BYTE_GAS_PRICE * amount_of_bytes;
if sender_gas_amount < gas_required {
return Err(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::NewTransaction,
))
.attach_printable("not enough gas for the transaction");
}
let last_block = self.main_chain.get_last_block()?.unwrap(); // practically cannot fail
let fee = tools::recalculate_fee(&last_block.get_info().difficulty);
if sender_amount < fee + transaction.get_amount().unwrap_or(U256::zero()) {
return Err(BlockChainTreeError::BlockChainTree(
BCTreeErrorKind::NewTransaction,
))
.attach_printable("not enough coins to pay the fee");
}
self.main_chain.add_transaction(transaction)?;
if let Some(amount) = transaction.get_amount() {
// TODO: make into sled transaction
self.send_amount(transaction.get_sender(), transaction.get_receiver(), amount)?;
self.sub_amount(transaction.get_sender(), fee)?;
self.sub_gas(transaction.get_sender(), gas_required)?;
}
Ok(())
}

pub async fn flush(&self) -> Result<(), Report<BlockChainTreeError>> {
self.main_chain.flush().await?;
self.summary_db
Expand Down
18 changes: 18 additions & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ impl MainChain {
Ok(())
}

pub fn add_transaction(
&self,
transaction: &dyn transaction::Transactionable,
) -> Result<(), Report<BlockChainTreeError>> {
let dump = transaction
.dump()
.change_context(BlockChainTreeError::Chain(
ChainErrorKind::AddingTransaction,
))?;
self.transactions
.insert(tools::hash(&dump), dump)
.change_context(BlockChainTreeError::Chain(
ChainErrorKind::AddingTransaction,
))
.attach_printable("Failed to insert transaction")?;
Ok(())
}

pub fn transaction_exists(
&self,
transaction_hash: &[u8; 32],
Expand Down
7 changes: 4 additions & 3 deletions src/static_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ pub static ROOT_PUBLIC_ADDRESS: [u8; 33] = [

pub static INCEPTION_TIMESTAMP: u64 = 1597924800;

pub static BLOCKS_PER_EPOCH: usize = 4;
pub static BLOCKS_PER_EPOCH: usize = 1000000;

pub static TIME_PER_BLOCK: u64 = 4;
pub static TIME_PER_BLOCK: u64 = 600;

lazy_static! {
pub static ref COIN_FRACTIONS: U256 = U256::from_dec_str("1000000000000000000").unwrap();
pub static ref INITIAL_FEE: U256 = U256::from_dec_str("25000000000000000").unwrap(); // 100_000_000//4
pub static ref FEE_STEP: U256 = U256::from_dec_str("625000000000").unwrap(); // 100_000_000//255
pub static ref FEE_STEP: U256 = U256::from_dec_str("62500").unwrap(); // 100_000_000//255
pub static ref MAIN_CHAIN_PAYMENT: U256 = *INITIAL_FEE;
pub static ref COINS_PER_CYCLE: U256 = (*MAIN_CHAIN_PAYMENT*2000usize*BLOCKS_PER_EPOCH) + *COIN_FRACTIONS*10000usize;
pub static ref BYTE_GAS_PRICE: U256 = U256::from_dec_str("625000000000").unwrap();
}
Loading

0 comments on commit aa11eae

Please sign in to comment.