-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcommon_test.go
More file actions
554 lines (477 loc) · 16.7 KB
/
Copy pathcommon_test.go
File metadata and controls
554 lines (477 loc) · 16.7 KB
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
package gossip
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math"
"math/big"
"sync"
"testing"
"time"
"github.com/Fantom-foundation/lachesis-base/abft"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/dag"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/Fantom-foundation/go-opera/evmcore"
"github.com/Fantom-foundation/go-opera/gossip/blockproc"
"github.com/Fantom-foundation/go-opera/gossip/emitter"
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/inter/iblockproc"
"github.com/Fantom-foundation/go-opera/inter/state"
"github.com/Fantom-foundation/go-opera/inter/validatorpk"
"github.com/Fantom-foundation/go-opera/opera"
"github.com/Fantom-foundation/go-opera/utils"
"github.com/Fantom-foundation/go-opera/utils/adapters/vecmt2dagidx"
"github.com/Fantom-foundation/go-opera/valkeystore"
"github.com/Fantom-foundation/go-opera/vecmt"
)
const (
gasLimit = uint64(21000)
maxGasLimit = uint64(6000000)
genesisBalance = 1e18
genesisStake = 2 * 4e6
maxEpochDuration = time.Hour
sameEpoch = maxEpochDuration / 1000
nextEpoch = maxEpochDuration
)
type callbacks struct {
buildEvent func(e *inter.MutableEventPayload)
onEventConfirmed func(e inter.EventI)
}
type testEnv struct {
t time.Time
nonces map[common.Address]uint64
callback callbacks
*Service
signer valkeystore.SignerI
pubkeys []validatorpk.PubKey
}
func panics(name string) func(error) {
return func(err error) {
log.Crit(fmt.Sprintf("%s error", name), "err", err)
}
}
type testGossipStoreAdapter struct {
*Store
}
func (g *testGossipStoreAdapter) GetEvent(id hash.Event) dag.Event {
e := g.Store.GetEvent(id)
if e == nil {
return nil
}
return e
}
func makeTestEngine(gdb *Store) (*abft.Lachesis, *vecmt.Index) {
cdb := abft.NewMemStore()
_ = cdb.ApplyGenesis(&abft.Genesis{
Epoch: gdb.GetEpoch(),
Validators: gdb.GetValidators(),
})
vecClock := vecmt.NewIndex(panics("Vector clock"), vecmt.LiteConfig())
engine := abft.NewLachesis(cdb, &testGossipStoreAdapter{gdb}, vecmt2dagidx.Wrap(vecClock), panics("Lachesis"), abft.LiteConfig())
return engine, vecClock
}
type testEmitterWorldExternal struct {
emitter.External
env *testEnv
}
func (em testEmitterWorldExternal) Build(e *inter.MutableEventPayload, onIndexed func()) error {
e.SetCreationTime(inter.Timestamp(em.env.t.UnixNano()))
if em.env.callback.buildEvent != nil {
em.env.callback.buildEvent(e)
}
return em.External.Build(e, onIndexed)
}
func (em testEmitterWorldExternal) Broadcast(*inter.EventPayload) {}
type testConfirmedEventsProcessor struct {
blockproc.ConfirmedEventsProcessor
env *testEnv
}
func (p testConfirmedEventsProcessor) ProcessConfirmedEvent(e inter.EventI) {
if p.env.callback.onEventConfirmed != nil {
p.env.callback.onEventConfirmed(e)
}
p.ConfirmedEventsProcessor.ProcessConfirmedEvent(e)
}
type testConfirmedEventsModule struct {
blockproc.ConfirmedEventsModule
env *testEnv
}
func (m testConfirmedEventsModule) Start(bs iblockproc.BlockState, es iblockproc.EpochState) blockproc.ConfirmedEventsProcessor {
p := m.ConfirmedEventsModule.Start(bs, es)
return testConfirmedEventsProcessor{p, m.env}
}
func newTestEnv(firstEpoch idx.Epoch, validatorsNum idx.Validator, tb testing.TB) *testEnv {
rules := opera.FakeNetRules()
rules.Epochs.MaxEpochDuration = inter.Timestamp(maxEpochDuration)
rules.Blocks.MaxEmptyBlockSkipPeriod = 0
genStore := makefakegenesis.FakeGenesisStoreWithRulesAndStart(validatorsNum, utils.ToFtm(genesisBalance), utils.ToFtm(genesisStake), rules, firstEpoch, 2)
genesis := genStore.Genesis()
store, err := NewMemStore(tb)
if err != nil {
panic(fmt.Errorf("NewMemStore failed; %w", err))
}
err = store.ApplyGenesis(genesis)
if err != nil {
panic(fmt.Errorf("ApplyGenesis failed; %w", err))
}
// install blockProc callbacks
env := &testEnv{
t: store.GetGenesisTime().Time(),
nonces: make(map[common.Address]uint64),
}
blockProc := DefaultBlockProc()
blockProc.EventsModule = testConfirmedEventsModule{blockProc.EventsModule, env}
engine, vecClock := makeTestEngine(store)
// create the service
txPool := &dummyTxPool{}
env.Service, err = newService(DefaultConfig(cachescale.Identity), store, blockProc, engine, vecClock, func(_ evmcore.StateReader) TxPool {
return txPool
})
if err != nil {
panic(err)
}
txPool.signer = env.EthAPI.signer
err = engine.Bootstrap(env.GetConsensusCallbacks())
if err != nil {
panic(err)
}
valKeystore := valkeystore.NewDefaultMemKeystore()
env.signer = valkeystore.NewSigner(valKeystore)
// register emitters
for i := idx.Validator(0); i < validatorsNum; i++ {
cfg := emitter.DefaultConfig()
vid := store.GetValidators().GetID(i)
pubkey := store.GetEpochState().ValidatorProfiles[vid].PubKey
cfg.Validator = emitter.ValidatorConfig{
ID: vid,
PubKey: pubkey,
}
cfg.EmitIntervals = emitter.EmitIntervals{}
cfg.MaxParents = idx.Event(validatorsNum/2 + 1)
cfg.MaxTxsPerAddress = 10000000
_ = valKeystore.Add(pubkey, crypto.FromECDSA(makefakegenesis.FakeKey(vid)), validatorpk.FakePassword)
_ = valKeystore.Unlock(pubkey, validatorpk.FakePassword)
world := env.EmitterWorld(env.signer)
world.External = testEmitterWorldExternal{world.External, env}
em := emitter.NewEmitter(cfg, world)
env.RegisterEmitter(em)
env.pubkeys = append(env.pubkeys, pubkey)
em.Start()
}
env.feed.Start(store.evm)
env.blockProcTasks.Start(1)
env.verWatcher.Start()
return env
}
func (env *testEnv) Close() {
env.feed.Stop()
env.verWatcher.Stop()
env.store.Close()
env.tflusher.Stop()
}
func (env *testEnv) GetEvmStateReader() *EvmStateReader {
return &EvmStateReader{
store: env.store,
}
}
func (env *testEnv) ApplyTxs(spent time.Duration, txs ...*types.Transaction) (types.Receipts, error) {
env.t = env.t.Add(spent)
externalReceipts := make(types.Receipts, 0, len(txs))
newBlocks := make(chan evmcore.ChainHeadNotify)
chainHeadSub := env.feed.SubscribeNewBlock(newBlocks)
mu := &sync.Mutex{}
go func() {
for b := range newBlocks {
if len(b.Block.Transactions) == 0 {
continue
}
receipts := env.store.evm.GetReceipts(idx.Block(b.Block.Number.Uint64()), env.EthAPI.signer, b.Block.Hash, b.Block.Transactions)
for i, tx := range b.Block.Transactions {
if r, _, _ := tx.RawSignatureValues(); r.Sign() != 0 {
mu.Lock()
externalReceipts = append(externalReceipts, receipts[i])
mu.Unlock()
env.txpool.(*dummyTxPool).Delete(tx.Hash())
}
}
if externalReceipts.Len() == len(txs) {
chainHeadSub.Unsubscribe()
close(newBlocks)
break
}
}
}()
env.txpool.AddRemotes(txs)
defer env.txpool.(*dummyTxPool).Clear()
err := env.EmitUntil(func() bool {
mu.Lock()
defer mu.Unlock()
return externalReceipts.Len() == len(txs)
})
return externalReceipts, err
}
func (env *testEnv) ApplyMPs(spent time.Duration, mps ...inter.MisbehaviourProof) error {
env.t = env.t.Add(spent)
// all callbacks are non-async
lastEpoch := idx.Epoch(0)
env.callback.buildEvent = func(e *inter.MutableEventPayload) {
if e.Epoch() > lastEpoch {
e.SetMisbehaviourProofs(mps)
lastEpoch = e.Epoch()
}
}
confirmed := false
env.callback.onEventConfirmed = func(_e inter.EventI) {
// ensure that not only MPs were confirmed, but also no new MPs will be confirmed in future
if _e.AnyMisbehaviourProofs() && _e.Epoch() == lastEpoch {
confirmed = true
// sanity check for gas used
e := env.store.GetEventPayload(_e.ID())
rule := env.store.GetRules().Economy.Gas
if e.GasPowerUsed() < rule.EventGas+uint64(len(e.MisbehaviourProofs()))*rule.MisbehaviourProofGas {
panic("GasPowerUsed calculation doesn't include MisbehaviourProofGas")
}
}
}
defer func() {
env.callback.buildEvent = nil
}()
return env.EmitUntil(func() bool {
return confirmed
})
}
func (env *testEnv) EmitUntil(stop func() bool) error {
t := time.Now()
for !stop() {
for _, em := range env.emitters {
_, err := em.EmitEvent()
if err != nil {
return err
}
}
env.WaitBlockEnd()
env.t = env.t.Add(time.Second)
if time.Since(t) > 30*time.Second {
panic("block doesn't get processed")
}
}
return nil
}
func (env *testEnv) Transfer(from, to idx.ValidatorID, amount *big.Int) *types.Transaction {
sender := env.Address(from)
nonce, _ := env.PendingNonceAt(nil, sender)
env.incNonce(sender)
key := env.privateKey(from)
receiver := env.Address(to)
gp := new(big.Int).SetUint64(1e12)
tx := types.NewTransaction(nonce, receiver, amount, gasLimit, gp, nil)
tx, err := types.SignTx(tx, env.EthAPI.signer, key)
if err != nil {
panic(err)
}
return tx
}
func (env *testEnv) Contract(from idx.ValidatorID, amount *big.Int, hex string) *types.Transaction {
sender := env.Address(from)
nonce, _ := env.PendingNonceAt(nil, sender)
env.incNonce(sender)
key := env.privateKey(from)
gp := new(big.Int).SetUint64(1e12)
data := hexutil.MustDecode(hex)
tx := types.NewContractCreation(nonce, amount, maxGasLimit, gp, data)
tx, err := types.SignTx(tx, env.EthAPI.signer, key)
if err != nil {
panic(err)
}
return tx
}
func (env *testEnv) privateKey(n idx.ValidatorID) *ecdsa.PrivateKey {
key := makefakegenesis.FakeKey(n)
return key
}
func (env *testEnv) Address(n idx.ValidatorID) common.Address {
key := makefakegenesis.FakeKey(n)
addr := crypto.PubkeyToAddress(key.PublicKey)
return addr
}
func (env *testEnv) Payer(n idx.ValidatorID, amounts ...*big.Int) *bind.TransactOpts {
key := env.privateKey(n)
t, _ := bind.NewKeyedTransactorWithChainID(key, new(big.Int).SetUint64(env.store.GetRules().NetworkID))
nonce, _ := env.PendingNonceAt(nil, env.Address(n))
t.Nonce = big.NewInt(int64(nonce))
t.Value = big.NewInt(0)
for _, amount := range amounts {
t.Value.Add(t.Value, amount)
}
t.GasLimit = env.GetEvmStateReader().MaxGasLimit()
t.GasPrice = new(big.Int).SetUint64(1e12)
return t
}
func (env *testEnv) Pay(n idx.ValidatorID, amounts ...*big.Int) *bind.TransactOpts {
t := env.Payer(n, amounts...)
env.incNonce(t.From)
return t
}
func (env *testEnv) ReadOnly() *bind.CallOpts {
return &bind.CallOpts{}
}
func (env *testEnv) State() state.StateDB {
statedb, err := env.store.evm.GetTxPoolStateDB()
if err != nil {
panic(err)
}
return statedb
}
func (env *testEnv) incNonce(account common.Address) {
env.nonces[account] += 1
}
/*
* bind.ContractCaller interface
*/
var (
errBlockNumberUnsupported = errors.New("simulatedBackend cannot access blocks other than the latest block")
)
// CodeAt returns the code of the given account. This is needed to differentiate
// between contract internal errors and the local chain being out of sync.
func (env *testEnv) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && idx.Block(blockNumber.Uint64()) != env.store.GetLatestBlockIndex() {
return nil, errBlockNumberUnsupported
}
code := env.State().GetCode(contract)
return code, nil
}
// ContractCall executes an Ethereum contract call with the specified data as the
// input.
func (env *testEnv) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && idx.Block(blockNumber.Uint64()) != env.store.GetLatestBlockIndex() {
return nil, errBlockNumberUnsupported
}
h := env.GetEvmStateReader().GetHeader(common.Hash{}, uint64(env.store.GetLatestBlockIndex()))
block := &evmcore.EvmBlock{
EvmHeader: *h,
}
rval, _, _, err := env.callContract(ctx, call, block, env.State())
return rval, err
}
func (env *testEnv) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
var num64 uint64
if number == nil {
num64 = uint64(env.store.GetLatestBlockIndex())
} else {
num64 = number.Uint64()
}
return env.GetEvmStateReader().GetHeader(common.Hash{}, num64).EthHeader(), nil
}
// callContract implements common code between normal and pending contract calls.
// state is modified during execution, make sure to copy it if necessary.
func (env *testEnv) callContract(
ctx context.Context, call ethereum.CallMsg, block *evmcore.EvmBlock, state state.StateDB,
) (
ret []byte, usedGas uint64, failed bool, err error,
) {
// Ensure message is initialized properly.
if call.GasPrice == nil {
call.GasPrice = big.NewInt(1)
}
if call.Gas == 0 {
call.Gas = 50000000
}
if call.Value == nil {
call.Value = new(big.Int)
}
// Set infinite balance to the fake caller account.
state.AddBalance(call.From, big.NewInt(math.MaxInt64))
msg := callmsg{call}
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
txContext := evmcore.NewEVMTxContext(msg)
context := evmcore.NewEVMBlockContext(block.Header(), env.GetEvmStateReader(), nil)
vmenv := vm.NewEVM(context, txContext, state, env.store.GetEvmChainConfig(), opera.DefaultVMConfig)
gaspool := new(evmcore.GasPool).AddGas(math.MaxUint64)
res, err := evmcore.NewStateTransition(vmenv, msg, gaspool).TransitionDb()
ret, usedGas, failed = res.Return(), res.UsedGas, res.Failed()
return
}
/*
* bind.ContractTransactor interface
*/
// PendingCodeAt returns the code of the given account in the pending state.
func (env *testEnv) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
code := env.State().GetCode(account)
return code, nil
}
// PendingNonceAt retrieves the current pending nonce associated with an account.
func (env *testEnv) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
nonce := env.nonces[account]
return nonce, nil
}
// SuggestGasTipCap retrieves the currently suggested gas price tip to allow a timely
// execution of a transaction.
func (env *testEnv) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return new(big.Int), nil
}
// SuggestGasTipCap retrieves the currently suggested gas price to allow a timely
// execution of a transaction.
func (env *testEnv) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return env.store.GetRules().Economy.MinGasPrice, nil
}
// EstimateGas tries to estimate the gas needed to execute a specific
// transaction based on the current pending state of the backend blockchain.
// There is no guarantee that this is the true gas limit requirement as other
// transactions may be added or removed by miners, but it should provide a basis
// for setting a reasonable default.
func (env *testEnv) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
if call.To == nil {
gas = gasLimit * 10000
} else {
gas = gasLimit * 10
}
return
}
// SendTransaction injects the transaction into the pending pool for execution.
func (env *testEnv) SendTransaction(ctx context.Context, tx *types.Transaction) error {
// do nothing to avoid executing by transactor, only generating needed
return nil
}
/*
* bind.ContractFilterer interface
*/
// FilterLogs executes a log filter operation, blocking during execution and
// returning all the results in one batch.
func (env *testEnv) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
panic("not implemented yet")
return nil, nil
}
// SubscribeFilterLogs creates a background log filtering operation, returning
// a subscription immediately, which can be used to stream the found events.
func (env *testEnv) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
panic("not implemented yet")
return nil, nil
}
// callmsg implements evmcore.Message to allow passing it as a transaction simulator.
type callmsg struct {
ethereum.CallMsg
}
func (m callmsg) From() common.Address { return m.CallMsg.From }
func (m callmsg) To() *common.Address { return m.CallMsg.To }
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
func (m callmsg) GasTipCap() *big.Int { return m.CallMsg.GasTipCap }
func (m callmsg) GasFeeCap() *big.Int { return m.CallMsg.GasFeeCap }
func (m callmsg) Gas() uint64 { return m.CallMsg.Gas }
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
func (m callmsg) Nonce() uint64 { return 0 }
func (m callmsg) IsFake() bool { return true }
func (m callmsg) Data() []byte { return m.CallMsg.Data }
func (m callmsg) AccessList() types.AccessList { return nil }