summaryrefslogtreecommitdiff
path: root/ledger/apply/payment_test.go
blob: d377211a17a272d0e42fa2b493ea98f1369b0377 (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
// 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 apply

import (
	"math/rand"
	"testing"

	"github.com/stretchr/testify/require"

	"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"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
)

var poolAddr = basics.Address{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

var spec = transactions.SpecialAddresses{
	FeeSink:     feeSink,
	RewardsPool: poolAddr,
}

func keypair() *crypto.SignatureSecrets {
	var seed crypto.Seed
	crypto.RandBytes(seed[:])
	s := crypto.GenerateSignatureSecrets(seed)
	return s
}

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

	var a basics.MicroAlgos
	var b basics.MicroAlgos
	var i uint64

	a.Raw = 222233333
	err := protocol.Decode(protocol.Encode(&a), &b)
	if err != nil {
		panic(err)
	}
	require.Equal(t, a, b)

	a.Raw = 12345678
	err = protocol.DecodeReflect(protocol.Encode(a), &i)
	if err != nil {
		panic(err)
	}
	require.Equal(t, a.Raw, i)

	i = 87654321
	err = protocol.Decode(protocol.EncodeReflect(i), &a)
	if err != nil {
		panic(err)
	}
	require.Equal(t, a.Raw, i)

	x := true
	err = protocol.Decode(protocol.EncodeReflect(x), &a)
	if err == nil {
		panic("decode of bool into MicroAlgos succeeded")
	}
}

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

	mockBalV0 := makeMockBalances(protocol.ConsensusCurrentVersion)

	secretSrc := keypair()
	src := basics.Address(secretSrc.SignatureVerifier)

	secretDst := keypair()
	dst := basics.Address(secretDst.SignatureVerifier)

	tx := transactions.Transaction{
		Type: protocol.PaymentTx,
		Header: transactions.Header{
			Sender:     src,
			Fee:        basics.MicroAlgos{Raw: 1},
			FirstValid: basics.Round(100),
			LastValid:  basics.Round(1000),
		},
		PaymentTxnFields: transactions.PaymentTxnFields{
			Receiver: dst,
			Amount:   basics.MicroAlgos{Raw: uint64(50)},
		},
	}
	var ad transactions.ApplyData
	err := Payment(tx.PaymentTxnFields, tx.Header, mockBalV0, transactions.SpecialAddresses{FeeSink: feeSink}, &ad)
	require.NoError(t, err)
}

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

	mockBalV0 := makeMockBalances(protocol.ConsensusCurrentVersion)
	mockBalV7 := makeMockBalances(protocol.ConsensusV7)

	secretSrc := keypair()
	src := basics.Address(secretSrc.SignatureVerifier)

	secretDst := keypair()
	dst := basics.Address(secretDst.SignatureVerifier)

	tx := transactions.Transaction{
		Type: protocol.PaymentTx,
		Header: transactions.Header{
			Sender:     src,
			Fee:        basics.MicroAlgos{Raw: 1},
			FirstValid: basics.Round(100),
			LastValid:  basics.Round(1000),
		},
		PaymentTxnFields: transactions.PaymentTxnFields{
			Receiver: dst,
			Amount:   basics.MicroAlgos{Raw: uint64(50)},
		},
	}

	tx.Sender = basics.Address(feeSink)
	require.Error(t, checkSpender(tx.PaymentTxnFields, tx.Header, spec, mockBalV0.ConsensusParams()))

	poolAddr := basics.Address(poolAddr)
	tx.Receiver = poolAddr
	require.NoError(t, checkSpender(tx.PaymentTxnFields, tx.Header, spec, mockBalV0.ConsensusParams()))

	tx.CloseRemainderTo = poolAddr
	require.Error(t, checkSpender(tx.PaymentTxnFields, tx.Header, spec, mockBalV0.ConsensusParams()))
	require.Error(t, checkSpender(tx.PaymentTxnFields, tx.Header, spec, mockBalV7.ConsensusParams()))

	tx.Sender = src
	require.NoError(t, checkSpender(tx.PaymentTxnFields, tx.Header, spec, mockBalV7.ConsensusParams()))
}

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

	payments, _, _, _ := generateTestObjects(100, 50)
	genHash := crypto.Digest{0x42}
	for i, txn := range payments {
		txn.GenesisHash = genHash
		payments[i] = txn
	}
	tcpast := transactions.ExplicitTxnContext{
		Proto:   config.Consensus[protocol.ConsensusV27],
		GenHash: genHash,
	}
	tc := transactions.ExplicitTxnContext{
		Proto:   config.Consensus[protocol.ConsensusCurrentVersion],
		GenHash: genHash,
	}
	for _, txn := range payments {
		// Lifetime window
		tc.ExplicitRound = txn.First() + 1
		if txn.Alive(tc) != nil {
			t.Errorf("transaction not alive during lifetime %v", txn)
		}

		tc.ExplicitRound = txn.First()
		if txn.Alive(tc) != nil {
			t.Errorf("transaction not alive at issuance %v", txn)
		}

		tc.ExplicitRound = txn.Last()
		if txn.Alive(tc) != nil {
			t.Errorf("transaction not alive at expiry %v", txn)
		}

		tc.ExplicitRound = txn.First() - 1
		if txn.Alive(tc) == nil {
			t.Errorf("premature transaction alive %v", txn)
		}

		tc.ExplicitRound = txn.Last() + 1
		if txn.Alive(tc) == nil {
			t.Errorf("expired transaction alive %v", txn)
		}

		// Make a copy of txn, change some fields, be sure the TXID changes. This is not exhaustive.
		var txn2 transactions.Transaction
		txn2 = txn
		txn2.Note = []byte{42}
		if txn2.ID() == txn.ID() {
			t.Errorf("txid does not depend on note")
		}
		txn2 = txn
		txn2.Amount.Raw++
		if txn2.ID() == txn.ID() {
			t.Errorf("txid does not depend on amount")
		}
		txn2 = txn
		txn2.Fee.Raw++
		if txn2.ID() == txn.ID() {
			t.Errorf("txid does not depend on fee")
		}
		txn2 = txn
		txn2.LastValid++
		if txn2.ID() == txn.ID() {
			t.Errorf("txid does not depend on lastvalid")
		}

		// Check malformed transactions
		largeWindow := txn
		largeWindow.LastValid += basics.Round(tc.Proto.MaxTxnLife)
		if largeWindow.WellFormed(spec, tc.Proto) == nil {
			t.Errorf("transaction with large window %#v verified incorrectly", largeWindow)
		}

		badWindow := txn
		badWindow.LastValid = badWindow.FirstValid - 1
		if badWindow.WellFormed(spec, tc.Proto) == nil {
			t.Errorf("transaction with bad window %#v verified incorrectly", badWindow)
		}

		badFee := txn
		badFee.Fee = basics.MicroAlgos{}
		if badFee.WellFormed(spec, tcpast.Proto) == nil {
			t.Errorf("transaction with no fee %#v verified incorrectly", badFee)
		}
		require.Nil(t, badFee.WellFormed(spec, tc.Proto))

		badFee.Fee.Raw = 1
		if badFee.WellFormed(spec, tcpast.Proto) == nil {
			t.Errorf("transaction with low fee %#v verified incorrectly", badFee)
		}
		require.Nil(t, badFee.WellFormed(spec, tc.Proto))
	}
}

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

	secretSrc := keypair()
	src := basics.Address(secretSrc.SignatureVerifier)

	secretDst := keypair()
	dst := basics.Address(secretDst.SignatureVerifier)

	tx := transactions.Transaction{
		Type: protocol.PaymentTx,
		Header: transactions.Header{
			Sender:     src,
			Fee:        basics.MicroAlgos{Raw: config.Consensus[protocol.ConsensusCurrentVersion].MinTxnFee},
			FirstValid: basics.Round(100),
			LastValid:  basics.Round(1000),
		},
		PaymentTxnFields: transactions.PaymentTxnFields{
			Receiver:         dst,
			Amount:           basics.MicroAlgos{Raw: uint64(50)},
			CloseRemainderTo: src,
		},
	}
	require.Error(t, tx.WellFormed(spec, config.Consensus[protocol.ConsensusCurrentVersion]))
}

func generateTestObjects(numTxs, numAccs int) ([]transactions.Transaction, []transactions.SignedTxn, []*crypto.SignatureSecrets, []basics.Address) {
	txs := make([]transactions.Transaction, numTxs)
	signed := make([]transactions.SignedTxn, numTxs)
	secrets := make([]*crypto.SignatureSecrets, numAccs)
	addresses := make([]basics.Address, numAccs)

	for i := 0; i < numAccs; i++ {
		secret := keypair()
		addr := basics.Address(secret.SignatureVerifier)
		secrets[i] = secret
		addresses[i] = addr
	}

	for i := 0; i < numTxs; i++ {
		s := rand.Intn(numAccs)
		r := rand.Intn(numAccs)
		a := rand.Intn(1000)
		f := config.Consensus[protocol.ConsensusCurrentVersion].MinTxnFee + uint64(rand.Intn(10))
		iss := 50 + rand.Intn(30)
		exp := iss + 10

		txs[i] = transactions.Transaction{
			Type: protocol.PaymentTx,
			Header: transactions.Header{
				Sender:     addresses[s],
				Fee:        basics.MicroAlgos{Raw: f},
				FirstValid: basics.Round(iss),
				LastValid:  basics.Round(exp),
			},
			PaymentTxnFields: transactions.PaymentTxnFields{
				Receiver: addresses[r],
				Amount:   basics.MicroAlgos{Raw: uint64(a)},
			},
		}
		signed[i] = txs[i].Sign(secrets[s])
	}

	return txs, signed, secrets, addresses
}