Skip to content

Commit

Permalink
Change fmt rules (kaspanet#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsutton authored Sep 14, 2022
1 parent 9cea14c commit 6511da3
Show file tree
Hide file tree
Showing 62 changed files with 927 additions and 1,993 deletions.
4 changes: 1 addition & 3 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
max_width = 120
chain_width = 50
fn_args_layout = "Compressed"
max_width = 135
use_field_init_shorthand = true
use_try_shorthand = true
use_small_heuristics = "Max"
Expand Down
22 changes: 9 additions & 13 deletions consensus/core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@ pub struct Block {

impl Block {
pub fn new(
version: u16, parents: Vec<Hash>, timestamp: u64, bits: u32, nonce: u64, daa_score: u64,
blue_work: BlueWorkType, blue_score: u64,
version: u16,
parents: Vec<Hash>,
timestamp: u64,
bits: u32,
nonce: u64,
daa_score: u64,
blue_work: BlueWorkType,
blue_score: u64,
) -> Self {
Self {
header: Header::new(
version,
parents,
Default::default(),
timestamp,
bits,
nonce,
daa_score,
blue_work,
blue_score,
),
header: Header::new(version, parents, Default::default(), timestamp, bits, nonce, daa_score, blue_work, blue_score),
transactions: Arc::new(Vec::new()),
}
}
Expand Down
4 changes: 1 addition & 3 deletions consensus/core/src/hashing/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use hashes::{Hash, Hasher};
/// Returns the header hash.
pub fn hash(header: &Header) -> Hash {
let mut hasher = hashes::BlockHash::new();
hasher
.update(header.version.to_le_bytes())
.write_len(header.parents_by_level.len()); // Write the number of parent levels
hasher.update(header.version.to_le_bytes()).write_len(header.parents_by_level.len()); // Write the number of parent levels

// Write parents at each level
for parents_at_level in header.parents_by_level.iter() {
Expand Down
6 changes: 1 addition & 5 deletions consensus/core/src/hashing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ impl<T: Hasher> HasherExtensions for T {
#[inline(always)]
fn write_blue_work(&mut self, work: BlueWorkType) -> &mut Self {
let be_bytes = work.to_be_bytes();
let start = be_bytes
.iter()
.cloned()
.position(|byte| byte != 0)
.unwrap_or(be_bytes.len());
let start = be_bytes.iter().cloned().position(|byte| byte != 0).unwrap_or(be_bytes.len());

self.write_var_bytes(&be_bytes[start..])
}
Expand Down
67 changes: 11 additions & 56 deletions consensus/core/src/hashing/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ pub(crate) fn id(tx: &Transaction) -> TransactionId {

/// Write the transaction into the provided hasher according to the encoding flags
fn write_transaction<T: Hasher>(hasher: &mut T, tx: &Transaction, encoding_flags: TxEncodingFlags) {
hasher
.update(tx.version.to_le_bytes())
.write_len(tx.inputs.len());
hasher.update(tx.version.to_le_bytes()).write_len(tx.inputs.len());
for input in tx.inputs.iter() {
// Write the tx input
write_input(hasher, input, encoding_flags);
Expand All @@ -43,11 +41,7 @@ fn write_transaction<T: Hasher>(hasher: &mut T, tx: &Transaction, encoding_flags
write_output(hasher, output);
}

hasher
.update(tx.lock_time.to_le_bytes())
.update(&tx.subnetwork_id)
.update(tx.gas.to_le_bytes())
.write_var_bytes(&tx.payload);
hasher.update(tx.lock_time.to_le_bytes()).update(&tx.subnetwork_id).update(tx.gas.to_le_bytes()).write_var_bytes(&tx.payload);
}

#[inline(always)]
Expand All @@ -63,9 +57,7 @@ fn write_input<T: Hasher>(hasher: &mut T, input: &TransactionInput, encoding_fla

#[inline(always)]
fn write_outpoint<T: Hasher>(hasher: &mut T, outpoint: &TransactionOutpoint) {
hasher
.update(outpoint.transaction_id)
.update(outpoint.index.to_le_bytes());
hasher.update(outpoint.transaction_id).update(outpoint.index.to_le_bytes());
}

#[inline(always)]
Expand Down Expand Up @@ -102,13 +94,8 @@ mod tests {
},
];

let inputs = vec![Arc::new(TransactionInput::new(
TransactionOutpoint::new(Hash::from_u64_word(0), 2),
vec![1, 2],
7,
5,
None,
))];
let inputs =
vec![Arc::new(TransactionInput::new(TransactionOutpoint::new(Hash::from_u64_word(0), 2), vec![1, 2], 7, 5, None))];

// Test #2
tests.push(Test {
Expand All @@ -117,8 +104,7 @@ mod tests {
expected_hash: "0d9eda5b1b1eebae2fe2b942cd62fca8ac56b6a05178392a46a9d2fa25c99cf9",
});

let outputs =
vec![Arc::new(TransactionOutput::new(1564, Arc::new(ScriptPublicKey::new(vec![1, 2, 3, 4, 5], 7))))];
let outputs = vec![Arc::new(TransactionOutput::new(1564, Arc::new(ScriptPublicKey::new(vec![1, 2, 3, 4, 5], 7))))];

// Test #3
tests.push(Test {
Expand All @@ -135,10 +121,7 @@ mod tests {
});

let inputs = vec![Arc::new(TransactionInput::new(
TransactionOutpoint::new(
Hash::from_str("59b3d6dc6cdc660c389c3fdb5704c48c598d279cdf1bab54182db586a4c95dd5").unwrap(),
2,
),
TransactionOutpoint::new(Hash::from_str("59b3d6dc6cdc660c389c3fdb5704c48c598d279cdf1bab54182db586a4c95dd5").unwrap(), 2),
vec![1, 2],
7,
5,
Expand All @@ -154,49 +137,21 @@ mod tests {

// Test #6
tests.push(Test {
tx: Transaction::new(
2,
inputs.clone(),
outputs.clone(),
54,
subnets::SUBNETWORK_ID_COINBASE,
3,
Vec::new(),
4,
),
tx: Transaction::new(2, inputs.clone(), outputs.clone(), 54, subnets::SUBNETWORK_ID_COINBASE, 3, Vec::new(), 4),
expected_id: "5a51df2b6c8e6a43cabef451474e5943659babed6005fe7828c3fc3279421bdb",
expected_hash: "5fff5a913cee9d1ffdadd93c0b15280ef74ebe850cd2f9cc396e31037b352668",
});

// Test #7
tests.push(Test {
tx: Transaction::new(
2,
inputs.clone(),
outputs.clone(),
54,
subnets::SUBNETWORK_ID_REGISTRY,
3,
Vec::new(),
4,
),
tx: Transaction::new(2, inputs.clone(), outputs.clone(), 54, subnets::SUBNETWORK_ID_REGISTRY, 3, Vec::new(), 4),
expected_id: "c542a204ab9416df910b01540b0c51b85e6d4e1724e081e224ea199a9e54e1b3",
expected_hash: "262bb342cf082557668364e8f37770cae272e24ba373b18522373fbe1f2ea313",
});

for (i, test) in tests.iter().enumerate() {
assert_eq!(
test.tx.id(),
Hash::from_str(test.expected_id).unwrap(),
"transaction id failed for test {}",
i + 1
);
assert_eq!(
hash(&test.tx),
Hash::from_str(test.expected_hash).unwrap(),
"transaction hash failed for test {}",
i + 1
);
assert_eq!(test.tx.id(), Hash::from_str(test.expected_id).unwrap(), "transaction id failed for test {}", i + 1);
assert_eq!(hash(&test.tx), Hash::from_str(test.expected_hash).unwrap(), "transaction hash failed for test {}", i + 1);
}

// Avoid compiler warnings on the last clone
Expand Down
11 changes: 9 additions & 2 deletions consensus/core/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ pub struct Header {

impl Header {
pub fn new(
version: u16, parents: Vec<Hash>, hash_merkle_root: Hash, timestamp: u64, bits: u32, nonce: u64,
daa_score: u64, blue_work: BlueWorkType, blue_score: u64,
version: u16,
parents: Vec<Hash>,
hash_merkle_root: Hash,
timestamp: u64,
bits: u32,
nonce: u64,
daa_score: u64,
blue_work: BlueWorkType,
blue_score: u64,
) -> Self {
let mut header = Self {
hash: Default::default(), // Temp init before the finalize below
Expand Down
Loading

0 comments on commit 6511da3

Please sign in to comment.