summaryrefslogtreecommitdiff
path: root/data/transactions/logic/fields_test.go
blob: 7cec0b936f6ef95625c6e2819f98baffb22d71c9 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 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 logic

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/test/partitiontest"
)

// ensure v2+ fields fail in TEAL assembler and evaluator on a version before they introduced
// ensure v2+ fields error in v1 program
func TestGlobalFieldsVersions(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	var fields []globalFieldSpec
	for _, fs := range globalFieldSpecs {
		if fs.version > 1 {
			fields = append(fields, fs)
		}
	}
	require.Greater(t, len(fields), 1)

	for _, field := range fields {
		text := fmt.Sprintf("global %s", field.field.String())
		// check assembler fails if version before introduction
		testLine(t, text, assemblerNoVersion, "...was introduced in...")
		for v := uint64(0); v < field.version; v++ {
			testLine(t, text, v, "...was introduced in...")
		}
		testLine(t, text, field.version, "")

		ops := testProg(t, text, AssemblerMaxVersion)

		// check on a version before the field version
		preVersion := field.version - 1
		ep := defaultSigParamsWithVersion(preVersion)

		// check failure from whole program version check
		testLogicBytes(t, ops.Program, ep,
			"greater than protocol supported version",
			"greater than protocol supported version")

		// check opcodes failures
		ops.Program[0] = byte(preVersion) // set version
		testLogicBytes(t, ops.Program, ep, "invalid global field")

		// check opcodes failures on 0 version
		ops.Program[0] = 0 // set version to 0
		testLogicBytes(t, ops.Program, ep, "invalid global field")
	}
}

// ensure v2+ fields error in programs of previous version, similarly to global fields test
func TestTxnFieldVersions(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	var fields []txnFieldSpec
	for _, fs := range txnFieldSpecs {
		if fs.version > 1 {
			fields = append(fields, fs)
		}
	}
	require.Greater(t, len(fields), 1)

	tests := []string{
		"txn %s",
		"gtxn 0 %s",
	}
	subs := map[string]string{
		tests[0]: "txna %s 0",
		tests[1]: "gtxna 0 %s 0",
	}
	txnaVersion := uint64(appsEnabledVersion)

	txn := makeSampleTxn()
	// We'll reject too early if we have a nonzero RekeyTo, because that
	// field must be zero for every txn in the group if this is an old
	// AVM version
	txn.Txn.RekeyTo = basics.Address{}
	txgroup := makeSampleTxnGroup(txn)
	asmDefaultError := "...was introduced in ..."
	for _, fs := range fields {
		field := fs.field.String()
		for _, command := range tests {
			text := fmt.Sprintf(command, field)
			asmError := asmDefaultError
			txnaMode := false
			if fs.array {
				text = fmt.Sprintf(subs[command], field)
				asmError = "...txna opcode was introduced in ..."
				txnaMode = true
			}

			// tack on a type check, and return a value (`int` gets compiled
			// differently in different versions, so use `txn FirstValid` to get
			// a positive integer)
			switch fs.ftype.AVMType {
			case avmUint64: // ensure the return type is uint64 by using !
				text += "; !; pop; txn FirstValid"
			case avmBytes: // ensure the return type is bytes by using len
				text += "; len; pop; txn FirstValid"
			case avmAny:
				text += "; pop; txn FirstValid"
			}

			// check assembler fails if version before introduction
			testLine(t, text, assemblerNoVersion, asmError)
			for v := uint64(0); v < fs.version; v++ {
				if txnaMode && v >= txnaVersion {
					asmError = asmDefaultError
				}
				testLine(t, text, v, asmError)
			}
			testLine(t, text, fs.version, "")

			ops := testProg(t, text, AssemblerMaxVersion)

			// check success in AssemblerMaxVersion, fs.version
			// also ensures the field returns the right type
			if !fs.effects {
				txgroup[0].Txn.ApprovalProgram = []byte("approve") // not in standard sample txn
				txgroup[0].Txn.ClearStateProgram = []byte("clear")
				ep := defaultAppParamsWithVersion(AssemblerMaxVersion, txgroup...)
				testAppBytes(t, ops.Program, ep)
				opsv := testProg(t, text, fs.version)
				ep = defaultAppParamsWithVersion(fs.version, txgroup...)
				testAppBytes(t, opsv.Program, ep)
			}

			preVersion := fs.version - 1
			ep := defaultSigParamsWithVersion(preVersion, txgroup...)

			// check failure with version check
			testLogicBytes(t, ops.Program, ep,
				"greater than protocol supported version", "greater than protocol supported version")

			// check opcodes failures
			ops.Program[0] = byte(preVersion) // set version
			checkErr := ""
			evalErr := "invalid txn field"
			if txnaMode && preVersion < txnaVersion {
				checkErr = "illegal opcode"
				evalErr = "illegal opcode"
			}
			testLogicBytes(t, ops.Program, ep, checkErr, evalErr)

			// check opcodes failures on 0 version
			ops.Program[0] = 0 // set version to 0
			checkErr = ""
			evalErr = "invalid txn field"
			if txnaMode {
				checkErr = "illegal opcode"
				evalErr = "illegal opcode"
			}
			testLogicBytes(t, ops.Program, ep, checkErr, evalErr)
		}
	}
}

// TestTxnEffectsAvailable ensures that LogicSigs can not use "effects" fields
// (ever). And apps can only use effects fields with `gtxn` after
// txnEffectsVersion. (itxn could use them earlier)
func TestTxnEffectsAvailable(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	for _, fs := range txnFieldSpecs {
		if !fs.effects {
			continue
		}
		source := fmt.Sprintf("gtxn 0 %s; pop; int 1", fs.field)
		if fs.array {
			source = fmt.Sprintf("gtxn 0 %s 0; pop; int 1", fs.field)
		}
		for v := fs.version; v <= AssemblerMaxVersion; v++ {
			ops := testProg(t, source, v)
			ep, _, _ := makeSampleEnv()
			ep.TxnGroup[1].Lsig.Logic = ops.Program
			_, err := EvalSignature(1, ep)
			require.Error(t, err)
			ep.Ledger = NewLedger(nil)
			_, err = EvalApp(ops.Program, 1, 888, ep)
			if v < txnEffectsVersion {
				require.Error(t, err, source)
			} else {
				if fs.array {
					continue // Array (Logs) will be 0 length, so will fail anyway
				}
				require.NoError(t, err, source)
			}
		}
	}
}

func TestAssetParamsFieldsVersions(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	var fields []assetParamsFieldSpec
	for _, fs := range assetParamsFieldSpecs {
		if fs.version > 2 {
			fields = append(fields, fs)
		}
	}
	require.Greater(t, len(fields), 0)

	for _, field := range fields {
		// Need to use intc so we can "backversion" the
		// program and not have it fail because of pushint.
		text := fmt.Sprintf("intcblock 55 1; intc_0; asset_params_get %s; bnz ok; err; ok: ", field.field.String())
		switch field.ftype.AVMType {
		case avmUint64: // ensure the return type is uint64 by adding
			text += " intc_1; +"
		case avmBytes: // ensure the return type is bytes by using len
			text += " len" // also happens to ensure that we get non empty - the params fields are fixed width
		}
		// check assembler fails if version before introduction
		for v := uint64(2); v <= AssemblerMaxVersion; v++ {
			ep, txn, ledger := makeSampleEnv()
			ledger.NewAsset(txn.Sender, 55, basics.AssetParams{})
			ep.Proto.LogicSigVersion = v
			if field.version > v {
				testProg(t, text, v, exp(1, "...was introduced in..."))
				ops := testProg(t, text, field.version) // assemble in the future
				ops.Program[0] = byte(v)
				testAppBytes(t, ops.Program, ep, "invalid asset_params_get field")
			} else {
				testProg(t, text, v)
				testApp(t, text, ep)
			}
		}

	}
}

func TestFieldVersions(t *testing.T) {
	// This test is weird, it confirms that we don't need to
	// bother with a "good" test for AssetHolding and AppParams
	// fields.  It will fail if we add a field that has a
	// different debut version, and then we'll need a test
	// like TestAssetParamsFieldsVersions that checks the field is
	// unavailable before its debut.

	partitiontest.PartitionTest(t)
	t.Parallel()

	for _, fs := range assetHoldingFieldSpecs {
		require.Equal(t, uint64(2), fs.version)
	}

	for _, fs := range appParamsFieldSpecs {
		require.Equal(t, uint64(5), fs.version)
	}
}

func TestAcctParamsFieldsVersions(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	for _, field := range acctParamsFieldSpecs {
		text := fmt.Sprintf("txn Sender; acct_params_get %s; assert;", field.field.String())
		if field.ftype.AVMType == avmBytes {
			text += "global ZeroAddress; concat; len" // use concat to prove we have bytes
		} else {
			text += "global ZeroAddress; len; +" // use + to prove we have an int
		}

		testLogicRange(t, 4, 0, func(t *testing.T, ep *EvalParams, txn *transactions.Transaction, ledger *Ledger) {
			v := ep.Proto.LogicSigVersion
			ledger.NewAccount(txn.Sender, 200_000)
			if field.version > v {
				// check assembler fails if version before introduction
				testProg(t, text, v, exp(1, "...was introduced in..."))
				ops := testProg(t, text, field.version) // assemble in the future
				ops.Program[0] = byte(v)                // but set version back to before intro
				if v < 6 {
					testAppBytes(t, ops.Program, ep, "illegal opcode", "illegal opcode")
				} else {
					testAppBytes(t, ops.Program, ep, "invalid acct_params_get field")
				}
			} else {
				testProg(t, text, v)
				testApp(t, text, ep)
			}
		})

	}
}

func TestBlockFieldsVersions(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	for _, field := range blockFieldSpecs {
		text := fmt.Sprintf("txn FirstValid; int 1; - ; block %s;", field.field)
		if field.ftype.AVMType == avmBytes {
			text += "global ZeroAddress; concat; len" // use concat to prove we have bytes
		} else {
			text += "global ZeroAddress; len; +" // use + to prove we have an int
		}

		testLogicRange(t, 4, 0, func(t *testing.T, ep *EvalParams, txn *transactions.Transaction, ledger *Ledger) {
			v := ep.Proto.LogicSigVersion
			if field.version > v {
				// check assembler fails if version before introduction
				testProg(t, text, v, exp(1, "...was introduced in..."))
				ops := testProg(t, text, field.version) // assemble in the future
				ops.Program[0] = byte(v)                // but set version back to before intro
				if v < randomnessVersion {
					testAppBytes(t, ops.Program, ep, "illegal opcode", "illegal opcode")
				} else {
					testAppBytes(t, ops.Program, ep, "invalid block field")
				}
			} else {
				testProg(t, text, v)
				testApp(t, text, ep)
			}
		})

	}
}