Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit a081658

Browse files
committed
Discrete propagation phase
1 parent bbc79f1 commit a081658

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

src/Chainweb/ForkState.hs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,61 @@ forkVotes = forkState . forkVotes'
8888
forkEpochLength :: Natural
8989
forkEpochLength = 120 * 120 -- 5 days
9090

91+
votingPeriodLength :: Natural
92+
votingPeriodLength = forkEpochLength - propagationTimeLength
93+
94+
propagationTimeLength :: Natural
95+
propagationTimeLength = 16
96+
97+
isVoteOver :: BlockHeader -> Bool
98+
isVoteOver hdr =
99+
mod (hdr ^. blockHeight) forkEpochLength > votingPeriodLength
100+
91101
isForkEpoch :: BlockHeader -> Bool
92-
isForkEpoch hdr = mod (view blockHeight hdr) forkEpochLength == 0
102+
isForkEpoch hdr =
103+
mod (hdr ^. blockHeight) forkEpochLength == 0
93104

94105
getForkNumber :: BlockHeader -> Word32
95106
getForkNumber = view signaledForkNumber . view forkState
96107

97108
getForkNumberVotes :: BlockHeader -> Word32
98109
getForkNumberVotes = view signaledNumber . view forkState
99110

100-
validateForkNumber :: Parent BlockHeader -> BlockHeader -> Bool
101-
validateForkNumber (Parent parent) hdr
102-
| isForkEpoch hdr =
103-
if view getForkNumberVotes parent >= (forkEpochLength * 2 `div 3)
104-
then
105-
view getForkNumber hdr == view getForkNumber parent + 1
106-
&& view getForkNumberVotes hdr <= 1
107-
else
108-
view getForkNumber hdr == view getForkNumber parent
109-
&& view getForkNumberVotes hdr <= 1
111+
-- | During the fork Epoch, we vote.
112+
-- After the end, we propagate votes from adjacents.
113+
114+
-- At the fork epoch end, the fork votes are actually storing votes.
115+
-- At end + 1, the "fork votes" are 1 if all adjacent chains pass the voting threshold.
116+
-- At the block after that, the "fork votes" are the conjunction of the fork votes of all adjacent chains.
117+
118+
-- This continues until the `degree`th block after the fork epoch ends. are `degree` blocks after the fork epoch ends. At
119+
-- this point the fork number increments if the fork votes are equal to 1.
120+
validateForkNumber :: WebStep -> Bool
121+
validateForkNumber webStep
122+
| isVoteOver (_webStepHeader webStep) =
123+
if votePassed parent
124+
&& all votePassed adjs
125+
then hdr ^. getForkNumber == parent ^. getForkNumber
126+
&& hdr ^. getForkNumberVotes == maxBound
127+
else
128+
hdr ^. getForkNumber == parent ^. getForkNumber
129+
&& hdr ^. getForkNumberVotes == 0
130+
| isForkEpoch (_webStepHeader webStep) =
131+
if votePassed parent
132+
&& all votePassed adjs
133+
then
134+
hdr ^. getForkNumber == parent ^. getForkNumber + 1
135+
&& hdr ^. getForkNumberVotes <= 1
136+
else
137+
hdr ^. getForkNumber == parent ^. getForkNumber
138+
&& hdr ^. getForkNumberVotes <= 1
110139
| otherwise =
111-
view getForkNumber hdr == view getForkNumber (parent hdr)
112-
&& view getForkNumberVotes hdr <= view getForkNumberVotes (parent hdr) + 1
140+
hdr ^. getForkNumber == parent ^. getForkNumber
141+
&&
142+
(hdr ^. getForkNumberVotes == parent ^. getForkNumberVotes
143+
|| hdr ^. getForkNumberVotes == parent ^. getForkNumberVotes + 1)
144+
where
145+
votePassed bh = parent ^. getForkNumberVotes >= forkEpochLength * 2 `div` 3
146+
hdr = _webStepHeader webStep
147+
parent = _webStepParent webStep
148+
adjs = _webStepAdjs webStep

0 commit comments

Comments
 (0)