forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_test.go
263 lines (231 loc) · 9.66 KB
/
simple_test.go
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
// 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 ledger
import (
"context"
"fmt"
"strings"
"testing"
"github.com/algorand/go-algorand/agreement"
"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/bookkeeping"
"github.com/algorand/go-algorand/data/committee"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/verify"
"github.com/algorand/go-algorand/data/txntest"
"github.com/algorand/go-algorand/ledger/eval"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
"github.com/stretchr/testify/require"
)
type simpleLedgerCfg struct {
onDisk bool // default is in-memory
notArchival bool // default is archival
}
type simpleLedgerOption func(*simpleLedgerCfg)
func simpleLedgerOnDisk() simpleLedgerOption {
return func(cfg *simpleLedgerCfg) { cfg.onDisk = true }
}
func simpleLedgerNotArchival() simpleLedgerOption {
return func(cfg *simpleLedgerCfg) { cfg.notArchival = true }
}
func newSimpleLedgerWithConsensusVersion(t testing.TB, balances bookkeeping.GenesisBalances, cv protocol.ConsensusVersion, cfg config.Local, opts ...simpleLedgerOption) *Ledger {
var genHash crypto.Digest
crypto.RandBytes(genHash[:])
return newSimpleLedgerFull(t, balances, cv, genHash, cfg, opts...)
}
func newSimpleLedgerFull(t testing.TB, balances bookkeeping.GenesisBalances, cv protocol.ConsensusVersion, genHash crypto.Digest, cfg config.Local, opts ...simpleLedgerOption) *Ledger {
var slCfg simpleLedgerCfg
for _, opt := range opts {
opt(&slCfg)
}
genBlock, err := bookkeeping.MakeGenesisBlock(cv, balances, "test", genHash)
require.NoError(t, err)
require.False(t, genBlock.FeeSink.IsZero())
require.False(t, genBlock.RewardsPool.IsZero())
dbName := fmt.Sprintf("%s.%d", t.Name(), crypto.RandUint64())
dbName = strings.Replace(dbName, "/", "_", -1)
cfg.Archival = !slCfg.notArchival
l, err := OpenLedger(logging.Base(), dbName, !slCfg.onDisk, ledgercore.InitState{
Block: genBlock,
Accounts: balances.Balances,
GenesisHash: genHash,
}, cfg)
require.NoError(t, err)
return l
}
// nextBlock begins evaluation of a new block, after ledger creation or endBlock()
func nextBlock(t testing.TB, ledger *Ledger) *eval.BlockEvaluator {
rnd := ledger.Latest()
hdr, err := ledger.BlockHdr(rnd)
require.NoError(t, err)
nextHdr := bookkeeping.MakeBlock(hdr).BlockHeader
nextHdr.TimeStamp = hdr.TimeStamp + 1 // ensure deterministic tests
eval, err := eval.StartEvaluator(ledger, nextHdr, eval.EvaluatorOptions{
Generate: true,
Validate: true, // Do the complete checks that a new txn would be subject to
})
require.NoError(t, err)
return eval
}
func fillDefaults(t testing.TB, ledger *Ledger, eval *eval.BlockEvaluator, txn *txntest.Txn) {
if txn.GenesisHash.IsZero() && ledger.GenesisProto().SupportGenesisHash {
txn.GenesisHash = ledger.GenesisHash()
}
if txn.FirstValid == 0 {
txn.FirstValid = eval.Round()
}
if txn.Type == protocol.KeyRegistrationTx && txn.VoteFirst == 0 {
txn.VoteFirst = eval.Round()
}
txn.FillDefaults(ledger.GenesisProto())
}
func txns(t testing.TB, ledger *Ledger, eval *eval.BlockEvaluator, txns ...*txntest.Txn) {
t.Helper()
for _, txn1 := range txns {
txn(t, ledger, eval, txn1)
}
}
func txn(t testing.TB, ledger *Ledger, eval *eval.BlockEvaluator, txn *txntest.Txn, problem ...string) {
t.Helper()
fillDefaults(t, ledger, eval, txn)
err := eval.Transaction(txn.SignedTxn(), transactions.ApplyData{})
if err != nil {
if len(problem) == 1 && problem[0] != "" {
require.Contains(t, err.Error(), problem[0])
} else {
require.NoError(t, err) // Will obviously fail
}
return
}
require.True(t, len(problem) == 0 || problem[0] == "")
}
func txgroup(t testing.TB, ledger *Ledger, eval *eval.BlockEvaluator, txns ...*txntest.Txn) error {
t.Helper()
for _, txn := range txns {
fillDefaults(t, ledger, eval, txn)
}
txgroup := txntest.Group(txns...)
return eval.TransactionGroup(transactions.WrapSignedTxnsWithAD(txgroup))
}
// endBlock completes the block being created, returning the ValidatedBlock for
// inspection. Proposer is optional - if unset, blocks will be finished with
// ZeroAddress proposer.
func endBlock(t testing.TB, ledger *Ledger, eval *eval.BlockEvaluator, proposer ...basics.Address) *ledgercore.ValidatedBlock {
ub, err := eval.GenerateBlock(nil)
require.NoError(t, err)
// We fake some thigns that agreement would do, like setting proposer
validatedBlock := ledgercore.MakeValidatedBlock(ub.UnfinishedBlock(), ub.UnfinishedDeltas())
gvb := &validatedBlock
// Making the proposer the feesink unless specified causes less disruption
// to existing tests. (Because block payouts don't change balances.)
prp := gvb.Block().BlockHeader.FeeSink
if len(proposer) > 0 {
prp = proposer[0]
}
// Since we can't do agreement, we have this backdoor way to install a
// proposer or seed into the header for tests. Doesn't matter that it makes
// them both the same. Since this can't call the agreement code, the
// eligibility of the prp is not considered.
if ledger.GenesisProto().Payouts.Enabled {
*gvb = ledgercore.MakeValidatedBlock(gvb.Block().WithProposer(committee.Seed(prp), prp, true), gvb.Delta())
} else {
// To more closely mimic the agreement code, we don't
// write the proposer when !Payouts.Enabled.
*gvb = ledgercore.MakeValidatedBlock(gvb.Block().WithProposer(committee.Seed(prp), basics.Address{}, false), gvb.Delta())
}
vvb, err := validateWithoutSignatures(t, ledger, gvb.Block())
require.NoError(t, err)
// we could add some checks that ensure gvb and vvb are quite similar, but
// they will differ a bit, as noted above.
err = ledger.AddValidatedBlock(*vvb, agreement.Certificate{})
require.NoError(t, err)
// `rndBQ` gives the latest known block round added to the ledger
// we should wait until `rndBQ` block to be committed to blockQueue,
// in case there is a data race, noted in
// https://github.com/algorand/go-algorand/issues/4349
// where writing to `callTxnGroup` after `dl.fullBlock` caused data race,
// because the underlying async goroutine `go bq.syncer()` is reading `callTxnGroup`.
// A solution here would be wait until all new added blocks are committed,
// then we return the result and continue the execution.
rndBQ := ledger.Latest()
ledger.WaitForCommit(rndBQ)
return vvb
}
func validateWithoutSignatures(t testing.TB, ledger *Ledger, blk bookkeeping.Block) (*ledgercore.ValidatedBlock, error) {
save := ledger.verifiedTxnCache
defer func() { ledger.verifiedTxnCache = save }()
ledger.verifiedTxnCache = verify.GetMockedCache(true) // validate the txns, but not signatures
return ledger.Validate(context.Background(), blk, nil)
}
// main wraps up some TEAL source in a header and footer so that it is
// an app that does nothing at create time, but otherwise runs source,
// then approves, if the source avoids panicing and leaves the stack
// empty.
func main(source string) string {
return strings.Replace(fmt.Sprintf(`txn ApplicationID
bz end
%s
end: int 1`, source), ";", "\n", -1)
}
// lookup gets the current accountdata for an address
func lookup(t testing.TB, ledger *Ledger, addr basics.Address) basics.AccountData {
ad, _, _, err := ledger.LookupLatest(addr)
require.NoError(t, err)
return ad
}
// micros gets the current microAlgo balance for an address
func micros(t testing.TB, ledger *Ledger, addr basics.Address) uint64 {
return lookup(t, ledger, addr).MicroAlgos.Raw
}
// holding gets the current balance and optin status for some asa for an address
func holding(t testing.TB, ledger *Ledger, addr basics.Address, asset basics.AssetIndex) (uint64, bool) {
if holding, ok := lookup(t, ledger, addr).Assets[asset]; ok {
return holding.Amount, true
}
return 0, false
}
// asaParams gets the asset params for a given asa index
func asaParams(t testing.TB, ledger *Ledger, asset basics.AssetIndex) (basics.AssetParams, error) {
creator, ok, err := ledger.GetCreator(basics.CreatableIndex(asset), basics.AssetCreatable)
if err != nil {
return basics.AssetParams{}, err
}
if !ok {
return basics.AssetParams{}, fmt.Errorf("no asset (%d)", asset)
}
if params, ok := lookup(t, ledger, creator).AssetParams[asset]; ok {
return params, nil
}
return basics.AssetParams{}, fmt.Errorf("bad lookup (%d)", asset)
}
// globals gets the AppParams for an address, app index pair (only works if addr is the creator)
func globals(t testing.TB, ledger *Ledger, addr basics.Address, app basics.AppIndex) (basics.AppParams, bool) {
if globals, ok := lookup(t, ledger, addr).AppParams[app]; ok {
return globals, true
}
return basics.AppParams{}, false
}
// locals gets the AppLocalState for an address, app index pair
func locals(t testing.TB, ledger *Ledger, addr basics.Address, app basics.AppIndex) (basics.AppLocalState, bool) {
if locals, ok := lookup(t, ledger, addr).AppLocalStates[app]; ok {
return locals, true
}
return basics.AppLocalState{}, false
}