-
Notifications
You must be signed in to change notification settings - Fork 11
/
validation.go
79 lines (70 loc) · 2.34 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package types
import (
"fmt"
"math/bits"
"time"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/bls12381"
"github.com/dashpay/tenderdash/crypto/ed25519"
)
// ValidateHash returns an error if the hash is not empty, but its
// size != crypto.HashSize.
func ValidateHash(h []byte) error {
if len(h) > 0 && len(h) != crypto.HashSize {
return fmt.Errorf("expected size to be %d bytes, got %d bytes",
crypto.HashSize,
len(h),
)
}
return nil
}
// ValidateAppHash returns an error if the app hash size is invalid.
func ValidateAppHash(h []byte) error {
if len(h) != crypto.DefaultAppHashSize {
return fmt.Errorf("expected size to be %d bytes, got %d bytes", crypto.DefaultAppHashSize, len(h))
}
return nil
}
// ValidateSignatureSize returns an error if the signature is not empty, but its
// size != hash.Size.
func ValidateSignatureSize(keyType crypto.KeyType, h []byte) error {
var signatureSize int // default
switch keyType {
case crypto.Ed25519:
signatureSize = ed25519.SignatureSize
case crypto.BLS12381:
signatureSize = bls12381.SignatureSize
default:
panic("key type unknown")
}
if len(h) > 0 && len(h) != signatureSize {
return fmt.Errorf("expected size to be %d bytes, got %d bytes",
bls12381.SignatureSize,
len(h),
)
}
return nil
}
func isTimely(timestamp time.Time, recvTime time.Time, sp SynchronyParams, round int32) bool {
// The message delay values are scaled as rounds progress.
// Every 10 rounds, the message delay is doubled to allow consensus to
// proceed in the case that the chosen value was too small for the given network conditions.
// For more information and discussion on this mechanism, see the relevant github issue:
// https://github.com/tendermint/spec/issues/371
maxShift := bits.LeadingZeros64(uint64(sp.MessageDelay)) - 1
nShift := int((round / 10))
if nShift > maxShift {
// if the number of 'doublings' would would overflow the size of the int, use the
// maximum instead.
nShift = maxShift
}
msgDelay := sp.MessageDelay * time.Duration(1<<nShift)
// lhs is `proposedBlockTime - Precision` in the first inequality
lhs := timestamp.Add(-sp.Precision)
// rhs is `proposedBlockTime + MsgDelay + Precision` in the second inequality
rhs := timestamp.Add(msgDelay).Add(sp.Precision)
if recvTime.Before(lhs) || recvTime.After(rhs) {
return false
}
return true
}