forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_tester.go
85 lines (70 loc) · 2.52 KB
/
simulation_tester.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
package simulation
import (
"fmt"
"math/rand"
"runtime"
"testing"
"time"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/integration/common/testlog"
"github.com/ethereum/go-ethereum"
"github.com/ten-protocol/go-ten/integration/simulation/network"
"github.com/ten-protocol/go-ten/integration/simulation/params"
simstats "github.com/ten-protocol/go-ten/integration/simulation/stats"
"github.com/google/uuid"
)
// testSimulation encapsulates the shared logic for simulating and testing various types of nodes.
func testSimulation(t *testing.T, netw network.Network, params *params.SimParams) {
defer func() {
// wait until clean up is complete before we log the lingering goroutine count
testlog.Logger().Info(fmt.Sprintf("goroutine leak monitor - simulation end - %d goroutines currently running", runtime.NumGoroutine()))
}()
testlog.Logger().Info(fmt.Sprintf("goroutine leak monitor - simulation start - %d goroutines currently running", runtime.NumGoroutine()))
rand.Seed(time.Now().UnixNano()) //nolint: staticcheck
uuid.EnableRandPool()
stats := simstats.NewStats(params.NumberOfNodes)
fmt.Printf("Creating network\n")
testlog.Logger().Info("Creating network")
defer netw.TearDown()
networkClients, err := netw.Create(params, stats)
// Return early if the network was not created
if err != nil {
fmt.Printf("Could not run test: %s\n", err)
return
}
txInjector := NewTransactionInjector(
params.AvgBlockDuration,
stats,
networkClients,
params.Wallets,
¶ms.L1TenData.MgmtContractAddress,
params.MgmtContractLib,
params.ERC20ContractLib,
0,
params,
)
simulation := Simulation{
RPCHandles: networkClients,
AvgBlockDuration: uint64(params.AvgBlockDuration),
TxInjector: txInjector,
SimulationTime: params.SimulationTime,
Stats: stats,
Params: params,
LogChannels: make(map[string][]chan types.Log),
Subscriptions: []ethereum.Subscription{},
}
// execute the simulation
fmt.Printf("Starting simulation\n")
testlog.Logger().Info("Starting simulation")
simulation.Start()
// run tests
fmt.Printf("Validating simulation results\n")
testlog.Logger().Info("Validating simulation results")
checkNetworkValidity(t, &simulation)
fmt.Printf("Stopping simulation\n")
testlog.Logger().Info("Stopping simulation")
simulation.Stop()
// generate and print the final stats
t.Logf("Simulation results:%+v", NewOutputStats(&simulation))
testlog.Logger().Info(fmt.Sprintf("Simulation results:%+v", NewOutputStats(&simulation)))
}