Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit c920651

Browse files
authored
Merge pull request paritytech#173 from subspace/heaviest-chain
Fix block weight calculation
2 parents 96238c6 + c6a5b11 commit c920651

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

crates/sp-consensus-subspace/src/digests.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ impl PreDigest {
7272
/// Returns the weight _added_ by this digest, not the cumulative weight
7373
/// of the chain.
7474
pub fn added_weight(&self) -> SubspaceBlockWeight {
75-
1
75+
let target = u64::from_be_bytes(self.solution.local_challenge.derive_target());
76+
let tag = u64::from_be_bytes(self.solution.tag);
77+
let diff = target.wrapping_sub(tag);
78+
let diff2 = tag.wrapping_sub(target);
79+
// Find smaller diff between 2 directions.
80+
let bidirectional_diff = (diff).min(diff2);
81+
u128::from(u64::MAX - bidirectional_diff)
7682
}
7783
}
7884

crates/sp-consensus-subspace/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ pub type EquivocationProof<H> = sp_consensus_slots::EquivocationProof<H, FarmerP
6969
/// The cumulative weight of a Subspace block, i.e. sum of block weights starting
7070
/// at this block until the genesis block.
7171
///
72-
/// Primary blocks have a weight of 1 whereas secondary blocks have a weight
73-
/// of 0 (regardless of whether they are plain or vrf secondary blocks).
74-
pub type SubspaceBlockWeight = u32;
72+
/// The closer solution's tag is to the target, the heavier it is.
73+
pub type SubspaceBlockWeight = u128;
7574

7675
/// An consensus log item for Subspace.
7776
#[derive(Decode, Encode, Clone, PartialEq, Eq, RuntimeDebug)]

0 commit comments

Comments
 (0)