summaryrefslogtreecommitdiff
path: root/data/transactions/logic/backwardCompat_test.go
blob: b56531cc0afe3459b8d27c2a70ed45275fcf1976 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright (C) 2019-2022 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 (
	"encoding/hex"
	"fmt"
	"strings"
	"testing"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/transactions/logictest"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

// This test ensures a program compiled with by pre-TEAL v2 go-algorand
// that includes all the opcodes from TEAL v1 runs in TEAL v2 runModeSignature well
var sourceTEALv1 = `byte 0x41 // A
sha256
byte 0x559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd
==
byte 0x42
keccak256
byte 0x1f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a69111
==
&&
byte 0x43
sha512_256
byte 0x34b99f8dde1ba273c0a28cf5b2e4dbe497f8cb2453de0c8ba6d578c9431a62cb
==
&&
arg_0
arg_1
arg_2
ed25519verify
&&
// should be a single 1 on the stack
int 0
+
int 0
-
int 1
/
int 1
*
// should be a single 1 on the stack
int 2
<
int 0
>
int 1
<=
int 1
>=
int 1
&&
int 0
||
int 1
==
int 1
!=
!
// should be a single 1 on the stack
arg_3
len
int 32
==
itob
btoi
% // 1 % 1 = 0
int 1
|
int 1
&
int 0
^
int 0xffffffffffffffff
~
mulw
// should be a two zeros on the stack
==
intc_0
intc_1
==
intc_2
intc_3
==
&&
intc 4
int 1
==
&&
pop  // consume intc_N comparisons and repeat for bytec_N
bytec_0
bytec_1
==
bytec_2
bytec_3
==
&&
bytec 4
byte 0x00
==
&&
pop
// test all txn fields
txn Sender
txn Receiver
!=
txn Fee
txn FirstValid
==
&&
// disabled
// txn FirstValidTime
int 0
txn LastValid
!=
&&
txn Note
txn Lease
!=
&&
txn Amount
txn GroupIndex
!=
&&
txn CloseRemainderTo
txn VotePK
==
&&
txn SelectionPK
txn Type
!=
&&
txn VoteFirst
txn VoteLast
==
&&
txn VoteKeyDilution
txn TypeEnum
!=
&&
txn XferAsset
txn AssetAmount
!=
&&
txn AssetSender
txn AssetReceiver
==
&&
txn AssetCloseTo
txn TxID
==
&&
pop
// repeat for gtxn
gtxn 0 Sender
gtxn 0 Receiver
!=
gtxn 0 Fee
gtxn 0 FirstValid
==
&&
// disabled
// gtxn 0 FirstValidTime
int 0
gtxn 0 LastValid
!=
&&
gtxn 0 Note
gtxn 0 Lease
!=
&&
gtxn 0 Amount
gtxn 0 GroupIndex
!=
&&
gtxn 0 CloseRemainderTo
gtxn 0 VotePK
==
&&
gtxn 0 SelectionPK
gtxn 0 Type
!=
&&
gtxn 0 VoteFirst
gtxn 0 VoteLast
==
&&
gtxn 0 VoteKeyDilution
gtxn 0 TypeEnum
!=
&&
gtxn 0 XferAsset
gtxn 0 AssetAmount
!=
&&
gtxn 0 AssetSender
gtxn 0 AssetReceiver
==
&&
gtxn 0 AssetCloseTo
gtxn 0 TxID
==
&&
pop
// check global (these are set equal in defaultEvalProto())
global MinTxnFee
global MinBalance
==
global MaxTxnLife
global GroupSize
!=
&&
global ZeroAddress
byte 0x0000000000000000000000000000000000000000000000000000000000000000
==
&&
store 0
load 0
&&

// wrap up, should be a two zeros on the stack
bnz ok
err
ok:
int 1
dup
==
`

var programTEALv1 = "01200500010220ffffffffffffffffff012608014120559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd0142201f675bff07515f5df96737194ea945c36c41e7b4fcef307b7cd4d0e602a6911101432034b99f8dde1ba273c0a28cf5b2e4dbe497f8cb2453de0c8ba6d578c9431a62cb0100200000000000000000000000000000000000000000000000000000000000000000280129122a022b1210270403270512102d2e2f041022082209230a230b240c220d230e230f231022112312231314301525121617182319231a221b21041c1d12222312242512102104231210482829122a2b121027042706121048310031071331013102121022310413103105310613103108311613103109310a1210310b310f1310310c310d1210310e31101310311131121310311331141210311531171210483300003300071333000133000212102233000413103300053300061310330008330016131033000933000a121033000b33000f131033000c33000d121033000e3300101310330011330012131033001333001412103300153300171210483200320112320232041310320327071210350034001040000100234912"

func TestBackwardCompatTEALv1(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	var s crypto.Seed
	crypto.RandBytes(s[:])
	c := crypto.GenerateSignatureSecrets(s)
	msg := "62fdfc072182654f163f5f0f9a621d729566c74d0aa413bf009c9800418c19cd"
	data, err := hex.DecodeString(msg)
	require.NoError(t, err)
	pk := basics.Address(c.SignatureVerifier)

	program, err := hex.DecodeString(programTEALv1)
	require.NoError(t, err)

	// ensure old program is the same as a new one when assembling without version
	ops, err := AssembleString(sourceTEALv1)
	require.NoError(t, err)
	require.Equal(t, program, ops.Program)
	// ensure the old program is the same as a new one except TEAL version byte
	opsV2, err := AssembleStringWithVersion(sourceTEALv1, 2)
	require.NoError(t, err)
	require.Equal(t, program[1:], opsV2.Program[1:])

	sig := c.Sign(Msg{
		ProgramHash: crypto.HashObj(Program(program)),
		Data:        data[:],
	})

	txn := makeSampleTxn()
	// RekeyTo disallowed on TEAL v0/v1
	txn.Txn.RekeyTo = basics.Address{}
	txgroup := makeSampleTxnGroup(txn)
	txn.Lsig.Logic = program
	txn.Lsig.Args = [][]byte{data[:], sig[:], pk[:], txn.Txn.Sender[:], txn.Txn.Note}
	txn.Txn.RekeyTo = basics.Address{} // RekeyTo not allowed in TEAL v1

	sb := strings.Builder{}
	ep := defaultEvalParamsWithVersion(&sb, &txn, 1)
	ep.TxnGroup = txgroup

	// ensure v1 program runs well on latest TEAL evaluator
	require.Equal(t, uint8(1), program[0])

	// Cost should stay exactly 2140
	ep.Proto.LogicSigMaxCost = 2139
	err = Check(program, ep)
	require.Error(t, err)
	require.Contains(t, err.Error(), "static cost")

	ep.Proto.LogicSigMaxCost = 2140
	err = Check(program, ep)
	require.NoError(t, err)

	pass, err := Eval(program, ep)
	if err != nil || !pass {
		t.Log(hex.EncodeToString(program))
		t.Log(sb.String())
	}
	require.NoError(t, err)
	require.True(t, pass)

	// Costs for v2 should be higher because of hash opcode cost changes
	ep2 := defaultEvalParamsWithVersion(&sb, &txn, 2)
	ep2.TxnGroup = txgroup
	ep2.Proto.LogicSigMaxCost = 2307
	err = Check(opsV2.Program, ep2)
	require.Error(t, err)
	require.Contains(t, err.Error(), "static cost")

	ep2.Proto.LogicSigMaxCost = 2308
	err = Check(opsV2.Program, ep2)
	require.NoError(t, err)

	pass, err = Eval(opsV2.Program, ep2)
	if err != nil || !pass {
		t.Log(hex.EncodeToString(ops.Program))
		t.Log(sb.String())
	}
	require.NoError(t, err)
	require.True(t, pass)

	// ensure v0 program runs well on latest TEAL evaluator
	ep = defaultEvalParams(&sb, &txn)
	ep.TxnGroup = txgroup
	program[0] = 0
	sig = c.Sign(Msg{
		ProgramHash: crypto.HashObj(Program(program)),
		Data:        data[:],
	})
	txn.Lsig.Logic = program
	txn.Lsig.Args = [][]byte{data[:], sig[:], pk[:], txn.Txn.Sender[:], txn.Txn.Note}

	// Cost remains the same, because v0 does not get dynamic treatment
	ep.Proto.LogicSigMaxCost = 2139
	err = Check(program, ep)
	require.Error(t, err)

	ep.Proto.LogicSigMaxCost = 2140
	err = Check(program, ep)
	require.NoError(t, err)
	pass, err = Eval(program, ep)
	require.NoError(t, err)
	require.True(t, pass)

	// But in v4, cost is now dynamic and exactly 1 less than v2/v3,
	// because bnz skips "err". It's caught during Eval
	program[0] = 4
	ep.Proto.LogicSigMaxCost = 2306
	err = Check(program, ep)
	require.NoError(t, err)
	_, err = Eval(program, ep)
	require.Error(t, err)

	ep.Proto.LogicSigMaxCost = 2307
	err = Check(program, ep)
	require.NoError(t, err)
	pass, err = Eval(program, ep)
	require.NoError(t, err)
	require.True(t, pass)

}

// ensure v2 fields error on pre TEAL v2 logicsig version
// ensure v2 fields error in v1 program
func TestBackwardCompatGlobalFields(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)

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

		ops := testProg(t, text, AssemblerMaxVersion)

		proto := config.Consensus[protocol.ConsensusV23]
		require.False(t, proto.Application)
		ep := defaultEvalParams(nil, nil)
		ep.Proto = &proto
		ep.Ledger = ledger

		// check failure with version check
		_, err := Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "greater than protocol supported version")
		_, err = Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "greater than protocol supported version")

		// check opcodes failures
		ops.Program[0] = 1 // set version to 1
		_, err = Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "invalid global field")
		_, err = Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "invalid global field")

		// check opcodes failures
		ops.Program[0] = 0 // set version to 0
		_, err = Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "invalid global field")
		_, err = Eval(ops.Program, ep)
		require.Error(t, err)
		require.Contains(t, err.Error(), "invalid global field")
	}
}

// ensure v2 fields error in v1 program
func TestBackwardCompatTxnFields(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",
	}

	ledger := logictest.MakeLedger(nil)
	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
	// TEAL version
	txn.Txn.RekeyTo = basics.Address{}
	txgroup := makeSampleTxnGroup(txn)
	for _, fs := range fields {
		field := fs.field.String()
		for _, command := range tests {
			text := fmt.Sprintf(command, field)
			asmError := "...available in version ..."
			if _, ok := txnaFieldSpecByField[fs.field]; ok {
				parts := strings.Split(text, " ")
				op := parts[0]
				asmError = fmt.Sprintf("found array field %#v in %s op", field, op)
			}
			// check assembler fails if version before introduction
			testLine(t, text, assemblerNoVersion, asmError)
			for v := uint64(0); v < fs.version; v++ {
				testLine(t, text, v, asmError)
			}

			ops, err := AssembleStringWithVersion(text, AssemblerMaxVersion)
			if _, ok := txnaFieldSpecByField[fs.field]; ok {
				// "txn Accounts" is invalid, so skip evaluation
				require.Error(t, err, asmError)
				continue
			} else {
				require.NoError(t, err)
			}

			proto := config.Consensus[protocol.ConsensusV23]
			require.False(t, proto.Application)
			ep := defaultEvalParams(nil, nil)
			ep.Proto = &proto
			ep.Ledger = ledger
			ep.TxnGroup = txgroup

			// check failure with version check
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "greater than protocol supported version")
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "greater than protocol supported version")

			// check opcodes failures
			ops.Program[0] = 1 // set version to 1
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "invalid txn field")
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "invalid txn field")

			// check opcodes failures
			ops.Program[0] = 0 // set version to 0
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "invalid txn field")
			_, err = Eval(ops.Program, ep)
			require.Error(t, err)
			require.Contains(t, err.Error(), "invalid txn field")
		}
	}
}

func TestBackwardCompatAssemble(t *testing.T) {
	partitiontest.PartitionTest(t)

	// TEAL v1 does not allow branching to the last line
	// TEAL v2 makes such programs legal
	t.Parallel()
	source := `int 0
int 1
bnz done
done:`

	t.Run("v=default", func(t *testing.T) {
		testProg(t, source, assemblerNoVersion, expect{4, "label \"done\" is too far away"})
	})

	t.Run("v=default", func(t *testing.T) {
		testProg(t, source, 0, expect{4, "label \"done\" is too far away"})
	})

	t.Run("v=default", func(t *testing.T) {
		testProg(t, source, 1, expect{4, "label \"done\" is too far away"})
	})

	for v := uint64(2); v <= AssemblerMaxVersion; v++ {
		t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
			ops, err := AssembleStringWithVersion(source, v)
			require.NoError(t, err)
			ep := defaultEvalParams(nil, nil)
			_, err = Eval(ops.Program, ep)
			require.NoError(t, err)
		})
	}
}

func TestExplicitConstants(t *testing.T) {
	partitiontest.PartitionTest(t)

	require.Equal(t, 4096, MaxStringSize, "constant changed, move it to consensus params")
	require.Equal(t, 64, MaxByteMathSize, "constant changed, move it to consensus params")
	require.Equal(t, 1024, MaxLogSize, "constant changed, move it to consensus params")
	require.Equal(t, 32, MaxLogCalls, "constant changed, move it to consensus params")
}