types: implement Header#ValidateBasic (#4638)

- Move core stateless validation of the Header type to a ValidateBasic method.
- Call header.ValidateBasic during a SignedHeader validation.
- Call header.ValidateBasic during a PhantomValidatorEvidence validation.
- Call header.ValidateBasic during a LunaticValidatorEvidence validation.

lite tests are skipped since the package is deprecated, no need to waste time on it

closes: #4572

Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
Alexander Bezobchuk
2020-04-24 11:45:38 +04:00
committed by GitHub
co-authored by Anton Kaliaev
parent 1f64430cb5
commit 9f29672e23
9 changed files with 196 additions and 144 deletions
+19 -19
View File
@@ -29,7 +29,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
vals = keys.ToValidators(20, 10)
bTime, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
header = keys.GenSignedHeader(chainID, lastHeight, bTime, nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys))
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys))
)
testCases := []struct {
@@ -52,7 +52,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// different chainID -> error
1: {
keys.GenSignedHeader("different-chainID", nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -62,7 +62,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// new header's time is before old header's time -> error
2: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(-1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -72,7 +72,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// new header's time is from the future -> error
3: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(3*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -83,7 +83,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
4: {
keys.GenSignedHeader(chainID, nextHeight,
bTime.Add(2*time.Hour).Add(maxClockDrift).Add(-1*time.Millisecond), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -93,7 +93,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// 3/3 signed -> no error
5: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -103,7 +103,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// 2/3 signed -> no error
6: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 1, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 1, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -113,7 +113,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// 1/3 signed -> error
7: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), len(keys)-1, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), len(keys)-1, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -123,7 +123,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// vals does not match with what we have -> error
8: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, keys.ToValidators(10, 1), vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
keys.ToValidators(10, 1),
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -133,7 +133,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// vals are inconsistent with newHeader -> error
9: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
keys.ToValidators(10, 1),
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -143,7 +143,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
// old header has expired -> error
10: {
keys.GenSignedHeader(chainID, nextHeight, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
keys.ToValidators(10, 1),
1 * time.Hour,
bTime.Add(1 * time.Hour),
@@ -181,7 +181,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
vals = keys.ToValidators(20, 10)
bTime, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
header = keys.GenSignedHeader(chainID, lastHeight, bTime, nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys))
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys))
// 30, 40, 50
twoThirds = keys[1:]
@@ -207,7 +207,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 3/3 new vals signed, 3/3 old vals present -> no error
0: {
keys.GenSignedHeader(chainID, 3, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -217,7 +217,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 2/3 new vals signed, 3/3 old vals present -> no error
1: {
keys.GenSignedHeader(chainID, 4, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 1, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 1, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -227,7 +227,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 1/3 new vals signed, 3/3 old vals present -> error
2: {
keys.GenSignedHeader(chainID, 5, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), len(keys)-1, len(keys)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), len(keys)-1, len(keys)),
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -237,7 +237,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 3/3 new vals signed, 2/3 old vals present -> no error
3: {
twoThirds.GenSignedHeader(chainID, 5, bTime.Add(1*time.Hour), nil, twoThirdsVals, twoThirdsVals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(twoThirds)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(twoThirds)),
twoThirdsVals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -247,7 +247,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 3/3 new vals signed, 1/3 old vals present -> no error
4: {
oneThird.GenSignedHeader(chainID, 5, bTime.Add(1*time.Hour), nil, oneThirdVals, oneThirdVals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(oneThird)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(oneThird)),
oneThirdVals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -257,7 +257,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
// 3/3 new vals signed, less than 1/3 old vals present -> error
5: {
lessThanOneThird.GenSignedHeader(chainID, 5, bTime.Add(1*time.Hour), nil, lessThanOneThirdVals, lessThanOneThirdVals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(lessThanOneThird)),
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(lessThanOneThird)),
lessThanOneThirdVals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
@@ -297,7 +297,7 @@ func TestVerifyReturnsErrorIfTrustLevelIsInvalid(t *testing.T) {
vals = keys.ToValidators(20, 10)
bTime, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
header = keys.GenSignedHeader(chainID, lastHeight, bTime, nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys))
hash("app_hash"), hash("cons_hash"), hash("results_hash"), 0, len(keys))
)
err := lite.Verify(chainID, header, vals, header, vals, 2*time.Hour, time.Now(), maxClockDrift,