summaryrefslogtreecommitdiff
path: root/protocol/tags_test.go
blob: 137bf4e3f73edf1ec893b8bf27613804c2bec8bb (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package protocol

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"strconv"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

// getConstValues uses the AST to get a list of the values of declared const
// variables of the provided typeName in a specified fileName.
// if namesOnly is true, it returns the names of the const variables instead.
func getConstValues(t *testing.T, fileName string, typeName string, namesOnly bool) []string {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, fileName, nil, 0)
	require.NoError(t, err)

	var ret []string

	// Iterate through the declarations in the file
	for _, d := range f.Decls {
		gen, ok := d.(*ast.GenDecl)
		// Check if the declaration is a constant
		if !ok || gen.Tok != token.CONST {
			continue
		}
		// Iterate through the specifications in the declaration
		for _, spec := range gen.Specs {
			// Check if the spec is a value spec
			v, ok := spec.(*ast.ValueSpec)
			if !ok {
				continue
			}
			// Check if the typeName specified is being declared
			if v.Type == nil || v.Type.(*ast.Ident).Name != typeName {
				continue
			}

			if namesOnly {
				ret = append(ret, v.Names[0].Name)
				continue
			}
			// Iterate through the expressions in the value spec
			for _, expr := range v.Values {
				val, ok := expr.(*ast.BasicLit)
				// Check if the expression is a basic literal and if not, continue
				if !ok {
					continue
				}
				// Unquote the value of the basic literal to remove the quotes
				tagVal, err := strconv.Unquote(val.Value)
				require.NoError(t, err)
				ret = append(ret, tagVal)
			}
		}
	}
	return ret
}

// TestTagList checks that the TagList global variable contains
// all the constant Tag variables declared in tags.go.
func TestTagList(t *testing.T) {
	t.Parallel()
	partitiontest.PartitionTest(t)

	constTags := getConstValues(t, "tags.go", "Tag", false)

	// Verify that TagList is not empty and has the same length as constTags
	require.NotEmpty(t, TagList)
	require.Len(t, TagList, len(constTags), "TagList is not complete")
	tagListMap := make(map[Tag]bool)
	for _, tag := range TagList {
		tagListMap[tag] = true
	}
	// Iterate through constTags and check that each element exists in tagListMap
	for _, constTag := range constTags {
		if tagListMap[Tag(constTag)] {
			delete(tagListMap, Tag(constTag)) // check off as seen
		} else {
			require.Fail(t, "const Tag %s is not in TagList", constTag)
		}
	}
	require.Empty(t, tagListMap, "Unseen tags remain in TagList")
}

func TestMaxSizesDefined(t *testing.T) {
	t.Parallel()
	partitiontest.PartitionTest(t)
	// Verify that we have a nonzero max message size for each tag in the TagList
	for _, tag := range TagList {
		require.Greater(t, tag.MaxMessageSize(), uint64(0))
	}
}

// TestMaxSizesTested checks that each Tag in the TagList has a corresponding line in the TestMaxSizesCorrect test in node_test.go
func TestMaxSizesTested(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	constTags := getConstValues(t, "tags.go", "Tag", true)

	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "../node/node_test.go", nil, 0)
	require.NoError(t, err)
	// Iterate through the declarations in the file

	tagsFound := make(map[string]bool)
	for _, d := range f.Decls {
		gen, ok := d.(*ast.FuncDecl)
		// Check if the declaration is a Function Declaration and if it is the TestMaxMessageSize function
		if !ok || gen.Name.Name != "TestMaxSizesCorrect" {
			continue
		}
		// Iterate through stmt in the function
		for _, stmt := range gen.Body.List {
			// Check if the spec is a value spec
			_ = stmt
			switch stmt := stmt.(type) {
			case *ast.ExprStmt:
				expr, ok := stmt.X.(*ast.CallExpr)
				if !ok {
					continue
				}
				sel, ok := expr.Fun.(*ast.SelectorExpr)
				if !ok || fmt.Sprintf("%s.%s", sel.X, sel.Sel.Name) != "require.Equal" {
					continue
				}
				// we are in the require.Equal function call and need to check the third argument
				call, ok := expr.Args[2].(*ast.CallExpr)
				if !ok {
					continue
				}
				tagSel, ok := call.Fun.(*ast.SelectorExpr)
				if !ok || tagSel.Sel.Name != "MaxMessageSize" {
					continue
				}
				tagSel, ok = tagSel.X.(*ast.SelectorExpr)
				if !ok || fmt.Sprintf("%s", tagSel.X) != "protocol" {
					continue
				}
				// We have found the tag name on which MaxMessageSize() is called and used in require.Equal
				// add it to the map
				tagsFound[tagSel.Sel.Name] = true
			default:
				continue
			}
		}
	}

	for _, tag := range constTags {
		require.Truef(t, tagsFound[tag], "Tag %s does not have a corresponding test in TestMaxSizesCorrect", tag)
	}
}

// Switch vs Map justification
// BenchmarkTagsMaxMessageSizeSwitch-8   	11358924	       104.0 ns/op
// BenchmarkTagsMaxMessageSizeMap-8      	10242530	       117.4 ns/op
func BenchmarkTagsMaxMessageSizeSwitch(b *testing.B) {
	// warmup like the Map benchmark below
	tagsmap := make(map[Tag]uint64, len(TagList))
	for _, tag := range TagList {
		tagsmap[tag] = tag.MaxMessageSize()
	}

	b.ResetTimer()

	var total uint64
	for i := 0; i < b.N; i++ {
		for _, tag := range TagList {
			total += tag.MaxMessageSize()
		}
	}
	require.Greater(b, total, uint64(0))
}

func BenchmarkTagsMaxMessageSizeMap(b *testing.B) {
	tagsmap := make(map[Tag]uint64, len(TagList))
	for _, tag := range TagList {
		tagsmap[tag] = tag.MaxMessageSize()
	}

	b.ResetTimer()
	var total uint64
	for i := 0; i < b.N; i++ {
		for _, tag := range TagList {
			total += tagsmap[tag]
		}
	}
	require.Greater(b, total, uint64(0))
}

// TestLockdownTagList locks down the list of tags in the code.
//
// The node will drop the connection when the connecting node requests
// a message of interest which is not in this list. This is a backward
// compatibility problem. When a new tag is introduced, the nodes with
// older version will not connect to the nodes running the new
// version.
//
// It is necessary to check the version of the other node before
// sending a request for a newly added tag. Currently, version
// checking is not implemented.
//
// Similarly, When removing a tag, it is important to support requests
// for the removed tag from nodes running an older version.
func TestLockdownTagList(t *testing.T) {
	partitiontest.PartitionTest(t)
	/************************************************
	 * ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! *
	 *  Read the comment before touching this test!  *
	 * ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! *
	 *************************************************
	 */ ////////////////////////////////////////////////
	var tagList = []Tag{
		AgreementVoteTag,
		MsgOfInterestTag,
		MsgDigestSkipTag,
		NetIDVerificationTag,
		NetPrioResponseTag,
		PingTag,
		PingReplyTag,
		ProposalPayloadTag,
		StateProofSigTag,
		TopicMsgRespTag,
		TxnTag,
		UniEnsBlockReqTag,
		VoteBundleTag,
	}
	require.Equal(t, len(tagList), len(TagList))
	tagMap := make(map[Tag]bool)
	for _, tag := range tagList {
		tagMap[tag] = true
		_, has := TagMap[tag]
		require.True(t, has)
	}
	for _, tag := range TagList {
		require.True(t, tagMap[tag])
	}
}