summaryrefslogtreecommitdiff
path: root/daemon/algod/api/server/v2/test/handlers_resources_test.go
blob: adf187053aa36e72e2f656ceb70188fdce1d10bd (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
// Copyright (C) 2019-2023 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 test

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/algorand/go-algorand/data/transactions/logic"

	"github.com/labstack/echo/v4"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/config"
	v2 "github.com/algorand/go-algorand/daemon/algod/api/server/v2"
	"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated/model"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	ledgertesting "github.com/algorand/go-algorand/ledger/testing"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
)

type mockLedger struct {
	mock.Mock
	accounts map[basics.Address]basics.AccountData
	kvstore  map[string][]byte
	latest   basics.Round
	blocks   []bookkeeping.Block
	tracer   logic.EvalTracer
}

func (l *mockLedger) GetTracer() logic.EvalTracer {
	return l.tracer
}

func (l *mockLedger) GetStateDeltaForRound(rnd basics.Round) (ledgercore.StateDelta, error) {
	args := l.Called(rnd)
	return args.Get(0).(ledgercore.StateDelta), args.Error(1)
}

func (l *mockLedger) LookupAccount(round basics.Round, addr basics.Address) (ledgercore.AccountData, basics.Round, basics.MicroAlgos, error) {
	ad, ok := l.accounts[addr]
	if !ok { // return empty / not found
		return ledgercore.AccountData{}, l.latest, basics.MicroAlgos{Raw: 0}, nil
	}
	return ledgercore.ToAccountData(ad), l.latest, basics.MicroAlgos{Raw: 0}, nil
}
func (l *mockLedger) LookupLatest(addr basics.Address) (basics.AccountData, basics.Round, basics.MicroAlgos, error) {
	ad, ok := l.accounts[addr]
	if !ok {
		return basics.AccountData{}, l.latest, basics.MicroAlgos{Raw: 0}, nil
	}
	return ad, l.latest, basics.MicroAlgos{Raw: 0}, nil
}

func (l *mockLedger) LookupKv(round basics.Round, key string) ([]byte, error) {
	if value, ok := l.kvstore[key]; ok {
		return value, nil
	}
	return nil, fmt.Errorf("Key %v does not exist", key)
}

func (l *mockLedger) LookupKeysByPrefix(round basics.Round, keyPrefix string, maxKeyNum uint64) ([]string, error) {
	panic("not implemented")
}

func (l *mockLedger) ConsensusParams(r basics.Round) (config.ConsensusParams, error) {
	return config.Consensus[protocol.ConsensusFuture], nil
}

func (l *mockLedger) Latest() basics.Round { return l.latest }

func (l *mockLedger) LookupAsset(rnd basics.Round, addr basics.Address, aidx basics.AssetIndex) (ar ledgercore.AssetResource, err error) {
	ad, ok := l.accounts[addr]
	if !ok {
		return ledgercore.AssetResource{}, nil
	}
	if ap, ok := ad.AssetParams[basics.AssetIndex(aidx)]; ok {
		ar.AssetParams = &ap
	}
	if ah, ok := ad.Assets[basics.AssetIndex(aidx)]; ok {
		ar.AssetHolding = &ah
	}
	return ar, nil
}
func (l *mockLedger) LookupApplication(rnd basics.Round, addr basics.Address, aidx basics.AppIndex) (ar ledgercore.AppResource, err error) {
	ad, ok := l.accounts[addr]
	if !ok {
		return ledgercore.AppResource{}, nil
	}
	if ap, ok := ad.AppParams[basics.AppIndex(aidx)]; ok {
		ar.AppParams = &ap
	}
	if ls, ok := ad.AppLocalStates[basics.AppIndex(aidx)]; ok {
		ar.AppLocalState = &ls
	}
	return ar, nil
}
func (l *mockLedger) BlockCert(rnd basics.Round) (blk bookkeeping.Block, cert agreement.Certificate, err error) {
	panic("not implemented")
}
func (l *mockLedger) LatestTotals() (rnd basics.Round, at ledgercore.AccountTotals, err error) {
	panic("not implemented")
}
func (l *mockLedger) BlockHdr(rnd basics.Round) (bookkeeping.BlockHeader, error) {
	blk, err := l.Block(rnd)
	if err != nil {
		return bookkeeping.BlockHeader{}, err
	}
	return blk.BlockHeader, nil
}
func (l *mockLedger) Wait(r basics.Round) chan struct{} {
	panic("not implemented")
}
func (l *mockLedger) WaitWithCancel(r basics.Round) (chan struct{}, func()) {
	panic("not implemented")
}
func (l *mockLedger) GetCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (c basics.Address, ok bool, err error) {
	panic("not implemented")
}
func (l *mockLedger) EncodedBlockCert(rnd basics.Round) (blk []byte, cert []byte, err error) {
	panic("not implemented")
}
func (l *mockLedger) Block(rnd basics.Round) (blk bookkeeping.Block, err error) {
	if len(l.blocks) == 0 {
		err = fmt.Errorf("mockledger error: no block")
		return
	}
	return l.blocks[0], nil
}

func (l *mockLedger) AddressTxns(id basics.Address, r basics.Round) ([]transactions.SignedTxnWithAD, error) {
	blk := l.blocks[r]

	spec := transactions.SpecialAddresses{
		FeeSink:     blk.FeeSink,
		RewardsPool: blk.RewardsPool,
	}

	var res []transactions.SignedTxnWithAD

	for _, tx := range blk.Payset {
		if tx.Txn.MatchAddress(id, spec) {
			signedTxn := transactions.SignedTxnWithAD{SignedTxn: transactions.SignedTxn{Txn: tx.Txn}}
			res = append(res, signedTxn)
		}
	}
	return res, nil
}

func randomAccountWithResources(N int) basics.AccountData {
	a := ledgertesting.RandomAccountData(0)
	a.Assets = make(map[basics.AssetIndex]basics.AssetHolding)
	a.AssetParams = make(map[basics.AssetIndex]basics.AssetParams)
	a.AppLocalStates = make(map[basics.AppIndex]basics.AppLocalState)
	a.AppParams = make(map[basics.AppIndex]basics.AppParams)
	for i := 0; i < N; i++ {
		switch i % 4 {
		case 0:
			a.Assets[basics.AssetIndex(i)] = ledgertesting.RandomAssetHolding(false)
		case 1:
			a.AssetParams[basics.AssetIndex(i)] = ledgertesting.RandomAssetParams()
		case 2:
			a.AppLocalStates[basics.AppIndex(i)] = ledgertesting.RandomAppLocalState()
		case 3:
			a.AppParams[basics.AppIndex(i)] = ledgertesting.RandomAppParams()
		}
	}
	return a
}

func randomAccountWithAssets(N int) basics.AccountData {
	a := ledgertesting.RandomAccountData(0)
	a.Assets = make(map[basics.AssetIndex]basics.AssetHolding)
	for i := 0; i < N; i++ {
		a.Assets[basics.AssetIndex(i*4)] = ledgertesting.RandomAssetHolding(false)
	}
	return a
}

func randomAccountWithAssetParams(N int) basics.AccountData {
	a := ledgertesting.RandomAccountData(0)
	a.AssetParams = make(map[basics.AssetIndex]basics.AssetParams)
	for i := 0; i < N; i++ {
		a.AssetParams[basics.AssetIndex(i*4+1)] = ledgertesting.RandomAssetParams()
	}
	return a
}

func randomAccountWithAppLocalState(N int) basics.AccountData {
	a := ledgertesting.RandomAccountData(0)
	a.AppLocalStates = make(map[basics.AppIndex]basics.AppLocalState)
	for i := 0; i < N; i++ {
		a.AppLocalStates[basics.AppIndex(i*4+2)] = ledgertesting.RandomAppLocalState()
	}
	return a
}

func randomAccountWithAppParams(N int) basics.AccountData {
	a := ledgertesting.RandomAccountData(0)
	a.AppParams = make(map[basics.AppIndex]basics.AppParams)
	for i := 0; i < N; i++ {
		a.AppParams[basics.AppIndex(i*4+3)] = ledgertesting.RandomAppParams()
	}
	return a
}

func setupTestForLargeResources(t *testing.T, acctSize, maxResults int, accountMaker func(int) basics.AccountData) (handlers v2.Handlers, fakeAddr basics.Address, acctData basics.AccountData) {
	ml := mockLedger{
		accounts: make(map[basics.Address]basics.AccountData),
		latest:   basics.Round(10),
	}
	fakeAddr = ledgertesting.RandomAddress()

	acctData = accountMaker(acctSize)
	ml.accounts[fakeAddr] = acctData

	mockNode := makeMockNode(&ml, t.Name(), nil, cannedStatusReportGolden, false)
	mockNode.config.MaxAPIResourcesPerAccount = uint64(maxResults)
	dummyShutdownChan := make(chan struct{})
	handlers = v2.Handlers{
		Node:     mockNode,
		Log:      logging.Base(),
		Shutdown: dummyShutdownChan,
	}
	return
}

func newReq(t *testing.T) (ctx echo.Context, rec *httptest.ResponseRecorder) {
	e := echo.New()
	req := httptest.NewRequest(http.MethodGet, "/", nil)
	rec = httptest.NewRecorder()
	ctx = e.NewContext(req, rec)
	return
}

func accountInformationResourceLimitsTest(t *testing.T, accountMaker func(int) basics.AccountData, acctSize, maxResults int, exclude string, expectedCode int) {
	handlers, addr, acctData := setupTestForLargeResources(t, acctSize, maxResults, accountMaker)
	params := model.AccountInformationParams{}
	if exclude != "" {
		params.Exclude = (*model.AccountInformationParamsExclude)(&exclude)
	}
	ctx, rec := newReq(t)
	err := handlers.AccountInformation(ctx, addr.String(), params)
	require.NoError(t, err)
	require.Equal(t, expectedCode, rec.Code)

	var ret struct {
		TotalApps          int           `json:"total-apps-opted-in"`
		TotalAssets        int           `json:"total-assets-opted-in"`
		TotalCreatedApps   int           `json:"total-created-apps"`
		TotalCreatedAssets int           `json:"total-created-assets"`
		MaxResults         int           `json:"max-results"`
		Apps               []interface{} `json:"apps-local-state"`
		Assets             []interface{} `json:"assets"`
		CreatedApps        []interface{} `json:"createdApps"`
		CreatedAssets      []interface{} `json:"createdAssets"`
	}

	var errRet struct {
		Data struct {
			TotalApps          int `json:"total-apps-opted-in"`
			TotalAssets        int `json:"total-assets-opted-in"`
			TotalCreatedApps   int `json:"total-created-apps"`
			TotalCreatedAssets int `json:"total-created-assets"`
			MaxResults         int `json:"max-results"`
		} `json:"data,omitempty"`
	}

	// check totals
	switch rec.Code {
	case 400:
		err = json.Unmarshal(rec.Body.Bytes(), &errRet)
		require.NoError(t, err)
		require.Equal(t, maxResults, errRet.Data.MaxResults)
		// totals should be present in both 200 and 400
		require.Equal(t, acctSize, errRet.Data.TotalApps+errRet.Data.TotalAssets+errRet.Data.TotalCreatedApps+errRet.Data.TotalCreatedAssets, "totals incorrect: %+v", ret)
	case 200:
		err = json.Unmarshal(rec.Body.Bytes(), &ret)
		require.NoError(t, err)
		// totals should be present in both 200 and 400
		require.Equal(t, acctSize, ret.TotalApps+ret.TotalAssets+ret.TotalCreatedApps+ret.TotalCreatedAssets, "totals incorrect: %+v", ret)

		if exclude == "all" {
			require.Nil(t, ret.Apps)
			require.Nil(t, ret.Assets)
			require.Nil(t, ret.CreatedApps)
			require.Nil(t, ret.CreatedAssets)
		} else if exclude == "none" {
			require.Equal(t, acctSize, len(ret.Apps)+len(ret.Assets)+len(ret.CreatedApps)+len(ret.CreatedAssets))
		}
	}

	// check individual assets/apps
	for i := 0; i < ret.TotalAssets; i++ {
		ctx, rec = newReq(t)
		aidx := basics.AssetIndex(i * 4)
		err = handlers.AccountAssetInformation(ctx, addr.String(), uint64(aidx), model.AccountAssetInformationParams{})
		require.NoError(t, err)
		require.Equal(t, 200, rec.Code)
		var ret model.AccountAssetResponse
		err = json.Unmarshal(rec.Body.Bytes(), &ret)
		require.NoError(t, err)
		assert.Nil(t, ret.CreatedAsset)
		assert.Equal(t, ret.AssetHolding, &model.AssetHolding{
			Amount:   acctData.Assets[aidx].Amount,
			AssetID:  uint64(aidx),
			IsFrozen: acctData.Assets[aidx].Frozen,
		})
	}
	for i := 0; i < ret.TotalCreatedAssets; i++ {
		ctx, rec = newReq(t)
		aidx := basics.AssetIndex(i*4 + 1)
		err = handlers.AccountAssetInformation(ctx, addr.String(), uint64(aidx), model.AccountAssetInformationParams{})
		require.NoError(t, err)
		require.Equal(t, 200, rec.Code)
		var ret model.AccountAssetResponse
		err = json.Unmarshal(rec.Body.Bytes(), &ret)
		require.NoError(t, err)
		assert.Nil(t, ret.AssetHolding)
		ap := acctData.AssetParams[aidx]
		assetParams := v2.AssetParamsToAsset(addr.String(), aidx, &ap)
		assert.Equal(t, ret.CreatedAsset, &assetParams.Params)
	}
	for i := 0; i < ret.TotalApps; i++ {
		ctx, rec = newReq(t)
		aidx := basics.AppIndex(i*4 + 2)
		err = handlers.AccountApplicationInformation(ctx, addr.String(), uint64(aidx), model.AccountApplicationInformationParams{})
		require.NoError(t, err)
		require.Equal(t, 200, rec.Code)
		var ret model.AccountApplicationResponse
		err = json.Unmarshal(rec.Body.Bytes(), &ret)
		require.NoError(t, err)
		assert.Nil(t, ret.CreatedApp)
		require.NotNil(t, ret.AppLocalState)
		assert.Equal(t, uint64(aidx), ret.AppLocalState.Id)
		ls := acctData.AppLocalStates[aidx]
		assert.Equal(t, ls.Schema.NumByteSlice, ret.AppLocalState.Schema.NumByteSlice)
		assert.Equal(t, ls.Schema.NumUint, ret.AppLocalState.Schema.NumUint)
	}
	for i := 0; i < ret.TotalCreatedApps; i++ {
		ctx, rec = newReq(t)
		aidx := basics.AppIndex(i*4 + 3)
		err = handlers.AccountApplicationInformation(ctx, addr.String(), uint64(aidx), model.AccountApplicationInformationParams{})
		require.NoError(t, err)
		require.Equal(t, 200, rec.Code)
		var ret model.AccountApplicationResponse
		err = json.Unmarshal(rec.Body.Bytes(), &ret)
		require.NoError(t, err)
		assert.Nil(t, ret.AppLocalState)
		ap := acctData.AppParams[aidx]
		expAp := v2.AppParamsToApplication(addr.String(), aidx, &ap)
		assert.EqualValues(t, expAp.Params.ApprovalProgram, ret.CreatedApp.ApprovalProgram)
		assert.EqualValues(t, expAp.Params.ClearStateProgram, ret.CreatedApp.ClearStateProgram)
		assert.EqualValues(t, expAp.Params.Creator, ret.CreatedApp.Creator)
	}
}

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

	for _, tc := range []struct {
		name         string
		accountMaker func(int) basics.AccountData
	}{
		{name: "all", accountMaker: randomAccountWithResources},
		{name: "assets", accountMaker: randomAccountWithAssets},
		{name: "applocal", accountMaker: randomAccountWithAppLocalState},
		{name: "assetparams", accountMaker: randomAccountWithAssetParams},
		{name: "appparams", accountMaker: randomAccountWithAppParams},
	} {
		t.Run(tc.name, func(t *testing.T) {
			accountInformationResourceLimitsTest(t, tc.accountMaker, 99, 100, "", 200)      // under limit
			accountInformationResourceLimitsTest(t, tc.accountMaker, 101, 100, "", 400)     // over limit
			accountInformationResourceLimitsTest(t, tc.accountMaker, 100, 100, "", 200)     // at limit
			accountInformationResourceLimitsTest(t, tc.accountMaker, 101, 100, "all", 200)  // over limit with exclude=all
			accountInformationResourceLimitsTest(t, tc.accountMaker, 101, 100, "none", 400) // over limit with exclude=none
		})
	}
}