handle duration overflow

This commit is contained in:
William Banfield
2022-02-01 14:34:51 -05:00
parent 5ccd8ed804
commit 44fbb9c1b2
2 changed files with 17 additions and 0 deletions
+6
View File
@@ -99,6 +99,12 @@ func (p *Proposal) IsTimely(recvTime time.Time, sp SynchronyParams, round int32)
roundModifier := time.Duration(math.Exp2(float64(round / 10)))
msgDelay := sp.MessageDelay * roundModifier
if msgDelay <= 0 {
// In the case that messaeg delay overflows after applying the round modifier, use the maximum
// duration instead.
msgDelay = time.Nanosecond * math.MaxInt64
}
// lhs is `proposedBlockTime - Precision` in the first inequality
lhs := p.Timestamp.Add(-sp.Precision)
// rhs is `proposedBlockTime + MsgDelay + Precision` in the second inequality
+11
View File
@@ -266,6 +266,17 @@ func TestIsTimely(t *testing.T) {
expectTimely: true,
round: 10,
},
{
// check that values that overflow time.Duration still correctly register
// as timely when round relaxation applied.
name: "message delay fixed to not overflow time.Duration",
proposalTime: genesisTime,
recvTime: genesisTime.Add(4 * time.Nanosecond),
precision: time.Nanosecond * 2,
msgDelay: time.Nanosecond,
expectTimely: true,
round: 5000,
},
}
for _, testCase := range testCases {