switch to using built in before function

This commit is contained in:
William Banfield
2021-11-01 12:04:46 +01:00
parent 7180750d1f
commit ee3a25b9bb
2 changed files with 13 additions and 13 deletions

View File

@@ -100,15 +100,15 @@ func (b *Block) ValidateBasic() error {
// Precision and MsgDelay parameters.
// Specifically, a proposed block timestamp is considered timely if it is satisfies the following inequalities:
//
// proposedBlockTime < validatorLocalTime + Precision + MsgDelay && proposedBlockTime > validatorLocaltime - Precision.
// proposedBlockTime > validatorLocaltime - Precision && proposedBlockTime < validatorLocalTime + Precision + MsgDelay.
//
// For more information on the meaning of 'timely', see the proposer-based timestamp specification:
// https://github.com/tendermint/spec/tree/master/spec/consensus/proposer-based-timestamp
func (b *Block) IsTimely(clock tmtime.Source, p TimestampParams) bool {
lt := clock.Now()
lhs := lt.Add(-p.Precision).UnixMilli()
rhs := lt.Add(p.Precision).Add(p.MsgDelay).UnixMilli()
if lhs < b.Header.Time.UnixMilli() && b.Header.Time.UnixMilli() < rhs {
lhs := lt.Add(-p.Precision)
rhs := lt.Add(p.Precision).Add(p.MsgDelay)
if lhs.Before(b.Header.Time) && b.Header.Time.Before(rhs) {
return true
}
return false

View File

@@ -1365,9 +1365,9 @@ func TestIsTimely(t *testing.T) {
// 1 - 2 < 0 < 1 + 2 + 1
name: "basic timely",
blockTime: genesisTime,
localTime: genesisTime.Add(1 * time.Millisecond),
precision: time.Millisecond * 2,
msgDelay: time.Millisecond,
localTime: genesisTime.Add(1 * time.Nanosecond),
precision: time.Nanosecond * 2,
msgDelay: time.Nanosecond,
expectTimely: true,
},
{
@@ -1375,19 +1375,19 @@ func TestIsTimely(t *testing.T) {
// 3 - 2 < 0 < 3 + 2 + 1
name: "local time too large",
blockTime: genesisTime,
localTime: genesisTime.Add(3 * time.Millisecond),
precision: time.Millisecond * 2,
msgDelay: time.Millisecond,
localTime: genesisTime.Add(3 * time.Nanosecond),
precision: time.Nanosecond * 2,
msgDelay: time.Nanosecond,
expectTimely: false,
},
{
// Checking that the following inequality evaluates to false:
// 0 - 2 < 2 < 2 + 1
name: "block time too large",
blockTime: genesisTime.Add(4 * time.Millisecond),
blockTime: genesisTime.Add(4 * time.Nanosecond),
localTime: genesisTime,
precision: time.Millisecond * 2,
msgDelay: time.Millisecond,
precision: time.Nanosecond * 2,
msgDelay: time.Nanosecond,
expectTimely: false,
},
}