Skip to content

Commit

Permalink
refactor: clarify data request transaction weight formula
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Jun 10, 2021
1 parent dcf198d commit b672c72
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions data_structures/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ use std::sync::{Arc, RwLock};
use witnet_crypto::{hash::calculate_sha256, merkle::FullMerkleTree};

// These constants were calculated in:
// TODO: add link to WIP about transaction weights
// https://github.com/witnet/WIPs/blob/master/wip-0007.md
pub const INPUT_SIZE: u32 = 133;
pub const OUTPUT_SIZE: u32 = 36;
pub const COMMIT_WEIGHT: u32 = 400;
pub const REVEAL_WEIGHT: u32 = 200;
pub const TALLY_WEIGHT: u32 = 100;
/// Data request complexity factor
pub const ALPHA: u32 = 1;
/// Data request return type factor
pub const BETA: u32 = 1;
/// Output multiplicative factor.
/// Value transfers that join several UTXOs are favored in detriment of value transfer that split
/// to many UTXOs
pub const GAMMA: u32 = 10;

pub trait MemoizedHashable {
Expand Down Expand Up @@ -379,20 +384,18 @@ impl DRTransactionBody {

/// Data Request Transaction weight
pub fn weight(&self) -> u32 {
// DR_weight = DR_size*alpha + W*COMMIT + W*REVEAL*beta + TALLY*beta + W*OUTPUT_SIZE
// DR_weight = DR_output_size*alpha + W*COMMIT + W*REVEAL*beta + TALLY*beta + N*INPUT_SIZE + (W + M)*OUTPUT_SIZE

let inputs_len = u32::try_from(self.inputs.len()).unwrap_or(u32::MAX);
let outputs_len = u32::try_from(self.outputs.len()).unwrap_or(u32::MAX);
let dr_weight = self.dr_output.weight().saturating_mul(ALPHA);
let dr_extra_weight = self.dr_output.extra_weight();
let inputs_weight = inputs_len.saturating_mul(INPUT_SIZE);
let outputs_weight = outputs_len.saturating_mul(OUTPUT_SIZE);

let dr_weight = inputs_weight
dr_weight
.saturating_add(dr_extra_weight)
.saturating_add(inputs_weight)
.saturating_add(outputs_weight)
.saturating_add(self.dr_output.weight())
.saturating_mul(ALPHA);

let dr_extra_weight = self.dr_output.extra_weight();
dr_weight.saturating_add(dr_extra_weight)
}

/// Specified data to be divided in a new level in the proof of inclusion
Expand Down

0 comments on commit b672c72

Please sign in to comment.