summaryrefslogtreecommitdiff
path: root/data/transactions/teal_test.go
blob: 92dbe0330cd53933d134de5bf20612334d9d4af7 (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
// 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 transactions

import (
	"fmt"
	"testing"

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

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

	a := require.New(t)

	d1 := EvalDelta{}
	d2 := EvalDelta{}
	a.True(d1.Equal(d2))

	d2 = EvalDelta{
		GlobalDelta: nil,
		LocalDeltas: nil,
		Logs:        nil,
	}
	a.True(d1.Equal(d2))

	d2 = EvalDelta{
		GlobalDelta: basics.StateDelta{},
		LocalDeltas: map[uint64]basics.StateDelta{},
		Logs:        []string{},
	}
	a.True(d1.Equal(d2))

	d2 = EvalDelta{
		GlobalDelta: basics.StateDelta{"test": {Action: basics.SetUintAction, Uint: 0}},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		GlobalDelta: basics.StateDelta{"test": {Action: basics.SetUintAction, Uint: 0}},
	}
	a.True(d1.Equal(d2))

	d2 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetUintAction, Uint: 0}},
		},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetUintAction, Uint: 1}},
		},
	}
	a.False(d1.Equal(d2))

	d2 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetUintAction, Uint: 1}},
		},
	}
	a.True(d1.Equal(d2))

	d1 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetBytesAction, Bytes: "val"}},
		},
	}
	d2 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetBytesAction, Bytes: "val"}},
		},
	}
	a.True(d1.Equal(d2))

	d2 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			0: {"test": {Action: basics.SetBytesAction, Bytes: "val1"}},
		},
	}
	a.False(d1.Equal(d2))

	d2 = EvalDelta{
		LocalDeltas: map[uint64]basics.StateDelta{
			1: {"test": {Action: basics.SetBytesAction, Bytes: "val"}},
		},
	}
	a.False(d1.Equal(d2))

	d2 = EvalDelta{
		Logs: []string{"val"},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		Logs: []string{"val2"},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		Logs: []string{"val", "val2"},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		Logs: []string{"val"},
	}
	a.True(d1.Equal(d2))

	// Test inner transaction equality
	d1 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{},
	}
	d2 = EvalDelta{
		InnerTxns: nil,
	}
	a.True(d1.Equal(d2))

	// Test inner transaction equality
	d1 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{{
			SignedTxn: SignedTxn{
				Lsig: LogicSig{
					Logic: []byte{0x01},
				},
			},
		}},
	}
	d2 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{{
			SignedTxn: SignedTxn{
				Lsig: LogicSig{
					Logic: []byte{0x01},
				},
			},
		}},
	}
	a.True(d1.Equal(d2))
	d2 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{{
			SignedTxn: SignedTxn{
				Lsig: LogicSig{
					Logic: []byte{0x02},
					Args:  [][]byte{},
				},
			},
		}},
	}
	a.False(d1.Equal(d2))

	d1 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{{
			SignedTxn: SignedTxn{
				Txn: Transaction{
					Type: protocol.TxType("pay"),
				},
			},
		}},
	}
	d2 = EvalDelta{
		InnerTxns: []SignedTxnWithAD{{
			SignedTxn: SignedTxn{
				Txn: Transaction{
					Type: protocol.TxType("axfer"),
				},
			},
		}},
	}
	a.False(d1.Equal(d2))

}

// TestUnchangedAllocBounds ensure that the allocbounds on EvalDelta have not
// changed.  If they change, EvalDelta.checkAllocBounds must be changed, or at
// least reconsidered, as well. We must give plenty of thought to whether a new
// allocbound, used by new versions, is compatible with old code. If the change
// can only show up in new protocol versions, it should be ok. But if we change
// a bound, it will go into effect immediately, not with Protocol upgrade. So we
// must be extremely careful that old protocol versions can not emit messages
// that take advnatage of a new, bigger bound. (Or, if the bound is *lowered* it
// had better be the case that such messages cannot be emitted in old code.)
func TestUnchangedAllocBounds(t *testing.T) {
	partitiontest.PartitionTest(t)

	delta := &EvalDelta{}
	max := 256 // Hardcodes config.MaxEvalDeltaAccounts
	for i := 0; i < max; i++ {
		delta.InnerTxns = append(delta.InnerTxns, SignedTxnWithAD{})
		msg := delta.MarshalMsg(nil)
		_, err := delta.UnmarshalMsg(msg)
		require.NoError(t, err)
	}
	delta.InnerTxns = append(delta.InnerTxns, SignedTxnWithAD{})
	msg := delta.MarshalMsg(nil)
	_, err := delta.UnmarshalMsg(msg)
	require.Error(t, err)

	delta = &EvalDelta{}
	max = 2048 // Hardcodes config.MaxLogCalls, currently MaxAppProgramLen
	for i := 0; i < max; i++ {
		delta.Logs = append(delta.Logs, "junk")
		msg := delta.MarshalMsg(nil)
		_, err := delta.UnmarshalMsg(msg)
		require.NoError(t, err)
	}
	delta.Logs = append(delta.Logs, "junk")
	msg = delta.MarshalMsg(nil)
	_, err = delta.UnmarshalMsg(msg)
	require.Error(t, err)

	delta = &EvalDelta{}
	max = 256 // Hardcodes config.MaxInnerTransactionsPerDelta
	for i := 0; i < max; i++ {
		delta.InnerTxns = append(delta.InnerTxns, SignedTxnWithAD{})
		msg := delta.MarshalMsg(nil)
		_, err := delta.UnmarshalMsg(msg)
		require.NoError(t, err)
	}
	delta.InnerTxns = append(delta.InnerTxns, SignedTxnWithAD{})
	msg = delta.MarshalMsg(nil)
	_, err = delta.UnmarshalMsg(msg)
	require.Error(t, err)

	// This one appears wildly conservative. The real max is something like
	// MaxAppTxnAccounts (4) + 1, since the key must be an index in the static
	// array of touchable accounts.
	delta = &EvalDelta{LocalDeltas: make(map[uint64]basics.StateDelta)}
	max = 2048 // Hardcodes config.MaxEvalDeltaAccounts
	for i := 0; i < max; i++ {
		delta.LocalDeltas[uint64(i)] = basics.StateDelta{}
		msg := delta.MarshalMsg(nil)
		_, err := delta.UnmarshalMsg(msg)
		require.NoError(t, err)
	}
	delta.LocalDeltas[uint64(max)] = basics.StateDelta{}
	msg = delta.MarshalMsg(nil)
	_, err = delta.UnmarshalMsg(msg)
	require.Error(t, err)

	// This one *might* be wildly conservative. Only 64 keys can be set in
	// globals, but I don't know what happens if you set and delete 65 (or way
	// more) keys in a single transaction.
	delta = &EvalDelta{GlobalDelta: make(basics.StateDelta)}
	max = 2048 // Hardcodes config.MaxStateDeltaKeys
	for i := 0; i < max; i++ {
		delta.GlobalDelta[fmt.Sprintf("%d", i)] = basics.ValueDelta{}
		msg := delta.MarshalMsg(nil)
		_, err := delta.UnmarshalMsg(msg)
		require.NoError(t, err)
	}
	delta.GlobalDelta[fmt.Sprintf("%d", max)] = basics.ValueDelta{}
	msg = delta.MarshalMsg(nil)
	_, err = delta.UnmarshalMsg(msg)
	require.Error(t, err)

}