This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
128 lines (107 loc) · 2.92 KB
/
main.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
package simulator
import (
"context"
"time"
"github.com/BOPR/common"
"github.com/BOPR/core"
)
const (
SimulatorService = "simulator"
)
type Simulator struct {
// Base service
core.BaseService
// DB instance
DB core.DB
// contract caller to interact with contracts
LoadedBazooka core.Bazooka
// header listener subscription
cancelSimulator context.CancelFunc
toSwap bool
}
// NewSimulator returns new simulator object
func NewSimulator() *Simulator {
logger := common.Logger.With("module", SimulatorService)
sim := &Simulator{}
sim.BaseService = *core.NewBaseService(logger, SimulatorService, sim)
db, err := core.NewDB()
if err != nil {
panic(err)
}
sim.LoadedBazooka, err = core.NewPreLoadedBazooka()
if err != nil {
panic(err)
}
sim.DB = db
return sim
}
// OnStart starts new block subscription
func (s *Simulator) OnStart() error {
s.BaseService.OnStart() // Always call the overridden method.
ctx, cancelSimulator := context.WithCancel(context.Background())
s.cancelSimulator = cancelSimulator
go s.SimulationStart(ctx, 10*time.Second)
s.toSwap = false
return nil
}
// OnStop stops all necessary go routines
func (s *Simulator) OnStop() {
s.BaseService.OnStop() // Always call the overridden method.
s.DB.Close()
s.cancelSimulator()
}
// SimulationStart starts the simulator
func (s *Simulator) SimulationStart(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
// stop ticker when everything done
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.sendTxsToAndFro()
// pick batch from DB
case <-ctx.Done():
ticker.Stop()
return
}
}
}
// tries sending transactins to and fro accounts to the rollup node
func (s *Simulator) sendTxsToAndFro() {
transferAmount := 1
FromID := uint64(2)
ToID := uint64(3)
if s.toSwap {
tempID := FromID
FromID = ToID
ToID = tempID
s.toSwap = !s.toSwap
}
for i := 0; i < 3; i++ {
latestFromAcc, err := s.DB.GetAccountByID(FromID)
if err != nil {
s.Logger.Error("unable to fetch latest account", "error", err)
return
}
_, _, nonce, token, err := s.LoadedBazooka.DecodeAccount(latestFromAcc.Data)
if err != nil {
s.Logger.Error("unable to decode account", "error", err)
return
}
txBytes, err := s.LoadedBazooka.EncodeTransferTx(int64(FromID), int64(ToID), int64(token.Uint64()), int64(nonce.Uint64())+1, int64(transferAmount), 1)
if err != nil {
s.Logger.Error("unable to encode tx", "error", err)
return
}
txCore := core.NewPendingTx(FromID, ToID, core.TX_TRANSFER_TYPE, "0x1ad4773ace8ee65b8f1d94a3ca7adba51ee2ca0bdb550907715b3b65f1e3ad9f69e610383dc9ceb8a50c882da4b1b98b96500bdf308c1bdce2187cb23b7d736f1b", txBytes)
err = s.DB.InsertTx(&txCore)
if err != nil {
s.Logger.Error("unable to insert tx", "error", err)
return
}
s.Logger.Info("Sent a tx!", "TxHash", txCore.TxHash, "From", txCore.From, "To", txCore.To)
if txCore.From == uint64(2) {
s.toSwap = true
}
}
}