Skip to content

Commit

Permalink
Merge pull request #56 from stevencartavia/tx_fee
Browse files Browse the repository at this point in the history
transaction fee implementation
  • Loading branch information
maciejka authored Aug 13, 2024
2 parents 09d4330 + 5541d2a commit 54cb3ad
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 217 deletions.
2 changes: 2 additions & 0 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,6 @@ pub struct OutPoint {
pub vout: u32,
/// The index of output in the utreexo set (meta field).
pub txo_index: u64,
// Amount calculated with the txid and vout
pub amount: u64
}
24 changes: 24 additions & 0 deletions src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,27 @@ pub fn double_sha256(a: u256, b: u256) -> u256 {
low: x4.into() * TWO_POW_96 + x5.into() * TWO_POW_64 + x6.into() * TWO_POW_32 + x7.into(),
}
}

fn hex_to_byte(h: u8) -> u8 {
if h >= 48 && h <= 57 {
return h - 48;
} else if h >= 65 && h <= 70 {
return h - 55;
} else if h >= 97 && h <= 102 {
return h - 87;
}
panic!("Wrong hex character: {h}");
0
}

pub fn from_base16(hexs: ByteArray) -> @ByteArray {
let mut result: ByteArray = Default::default();
let mut i = 0;
let len = hexs.len();
while i < len {
result.append_word(hex_to_byte(hexs.at(i).unwrap()).into(), 4);
i += 1;
};

@result
}
71 changes: 64 additions & 7 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::merkle_tree::merkle_root;
use super::utils::{shl, shr};
use super::state::{Block, ChainState, Transaction, UtreexoState};
use super::state::{Block, ChainState, Transaction, UtreexoState, UtreexoSet, TxIn, TxOut};

const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000;

Expand Down Expand Up @@ -43,9 +43,27 @@ impl TransactionValidatorImpl of TransactionValidator {
// TODO: implement
0
}
fn fee(self: @Transaction) -> u256 {
// TODO: implement
0
fn fee(self: @Transaction) -> u64 {
let mut total_input_amount = 0;
let mut total_output_amount = 0;
// Inputs of a transaction
let inputs = *self.inputs;
// Outputs of a transaction
let outputs = *self.outputs;

for input in inputs {
let amount = *input.previous_output.amount;
total_input_amount += amount;
};

for output in outputs {
let value = *output.value;
total_output_amount += value;
};

let tx_fee = total_input_amount - total_output_amount;

tx_fee
}
}

Expand Down Expand Up @@ -181,7 +199,7 @@ fn validate_bits(block: @Block, target: u256) -> Result<(), ByteArray> {
}
}

fn fee_and_merkle_root(block: @Block) -> Result<(u256, u256), ByteArray> {
fn fee_and_merkle_root(block: @Block) -> Result<(u64, u256), ByteArray> {
let mut txids = ArrayTrait::new();
let mut total_fee = 0;

Expand All @@ -193,7 +211,7 @@ fn fee_and_merkle_root(block: @Block) -> Result<(u256, u256), ByteArray> {
Result::Ok((total_fee, merkle_root(ref txids)))
}

fn validate_coinbase(block: @Block, total_fees: u256) -> Result<(), ByteArray> {
fn validate_coinbase(block: @Block, total_fees: u64) -> Result<(), ByteArray> {
//TODO implement
Result::Ok(())
}
Expand All @@ -206,10 +224,12 @@ fn compute_block_reward(block_height: u32) -> u64 {

#[cfg(test)]
mod tests {
use raito::state::{Header, Transaction, TxIn, TxOut};
use raito::state::{Header, Transaction, TxIn, TxOut, OutPoint};
use raito::utils::from_base16;
use super::{
validate_timestamp, validate_proof_of_work, compute_block_reward, compute_total_work,
compute_work_from_target, shr, shl, Block, ChainState, UtreexoState,
TransactionValidatorImpl
};


Expand Down Expand Up @@ -244,6 +264,43 @@ mod tests {
assert!(result.is_err(), "Median time is greater than block's timestamp");
}

#[test]
fn test_tx_fee() {
let tx = Transaction {
version: 1,
is_segwit: false,
inputs: array![
TxIn {
script: from_base16(
"01091d8d76a82122082246acbb6cc51c839d9012ddaca46048de07ca8eec221518200241cdb85fab4815c6c624d6e932774f3fdf5fa2a1d3a1614951afb83269e1454e2002443047"
),
sequence: 0xffffffff,
previous_output: OutPoint {
txid: 0x0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9,
vout: 0x00000000,
txo_index: 0,
amount: 100
},
witness: from_base16("")
}
]
.span(),
outputs: array![
TxOut {
value: 90,
pk_script: from_base16(
"ac4cd86c7e4f702ac7d5debaf126068a3b30b7c1212c145fdfa754f59773b3aae71484a22f30718d37cd74f325229b15f7a2996bf0075f90131bf5c509fe621aae0441"
),
}
]
.span(),
lock_time: 0
};

let fee = TransactionValidatorImpl::fee(@tx);
assert_eq!(fee, 10);
}

#[test]
fn test_compute_work_from_target1() {
let expected_work = 0x0100010001;
Expand Down
5 changes: 4 additions & 1 deletion tests/blocks/block_0.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn block_0() -> Block {
),
sequence: 4294967295,
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
txid: 0_u256,
vout: 0xffffffff_u32,
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
Expand Down
8 changes: 6 additions & 2 deletions tests/blocks/block_170.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub fn block_170() -> Block {
script: from_base16("04ffff001d0102"),
sequence: 4294967295,
previous_output: OutPoint {
txid: 0_u256, vout: 0xffffffff_u32, txo_index: 0, // TODO: implement
txid: 0_u256,
vout: 0xffffffff_u32,
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
Expand Down Expand Up @@ -46,7 +49,8 @@ pub fn block_170() -> Block {
previous_output: OutPoint {
txid: 0x0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9,
vout: 0,
txo_index: 0, // TODO: implement
txo_index: 0,
amount: 0 // TODO: implement
},
witness: @""
}
Expand Down
Loading

0 comments on commit 54cb3ad

Please sign in to comment.