-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
217 lines (184 loc) · 5.48 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/kchristidis/island/bidder"
"github.com/kchristidis/island/blockchain"
"github.com/kchristidis/island/blocknotifier"
"github.com/kchristidis/island/chaincode/schema"
"github.com/kchristidis/island/crypto"
"github.com/kchristidis/island/regulator"
"github.com/kchristidis/island/slotnotifier"
"github.com/kchristidis/island/stats"
"github.com/kchristidis/island/trace"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
func run() error {
writer = os.Stdout
iter++
outputPrefix = fmt.Sprintf("exp-%02d-run-%02d", schema.ExpNum, iter)
timeStart = time.Now()
startFromBlock = uint64(10)
statsBlockC = make(chan stats.Block, StatChannelBuffer)
statsSlotC = make(chan stats.Slot, StatChannelBuffer)
statsTranC = make(chan stats.Transaction, StatChannelBuffer)
doneC = make(chan struct{})
doneStatsC = make(chan struct{})
tracePath := filepath.Join("trace", trace.Filename)
traceMap, err = trace.Load(tracePath)
if err != nil {
return nil
}
privKeyPath := filepath.Join("crypto", "priv.pem")
privKey, err = crypto.LoadPrivate(privKeyPath)
if err != nil {
return nil
}
privKeyBytes := crypto.SerializePrivate(privKey)
// Begin initializations
println()
msg := fmt.Sprintf("Simulating experiment %d (running in prod: %t)...", schema.ExpNum, schema.StagingLevel == schema.Prod)
fmt.Fprintln(writer, msg)
println()
sdkctx = &blockchain.SDKContext{
SDKConfigFile: "config.yaml",
OrgName: "clark",
OrgAdmin: "Admin",
UserName: "User1",
OrdererID: "joe.example.com",
ChannelID: "clark-channel",
ChaincodeID: fmt.Sprintf("exp%d", schema.ExpNum),
ChannelConfigPath: os.Getenv("GOPATH") + "/src/github.com/kchristidis/island/fixtures/artifacts/clark-channel.tx",
ChaincodeGoPath: os.Getenv("GOPATH"),
ChaincodeSourcePath: "github.com/kchristidis/island/chaincode/",
}
if err = sdkctx.Setup(); err != nil {
return err
}
defer sdkctx.SDK.Close()
if err = sdkctx.Install(); err != nil {
return err
}
statsCollector = &stats.Collector{
BlockChan: statsBlockC,
SlotChan: statsSlotC,
TransactionChan: statsTranC,
Writer: writer,
DoneChan: doneStatsC,
}
wg3.Add(1)
go func() {
statsCollector.Run()
wg3.Done()
}()
switch schema.ExpNum {
case 1, 3:
for i := 0; i < 2; i++ {
// 1st for markend+bid calls
// 2nd one for postkey calls
slotCs = append(slotCs, make(chan int))
sNotifiers = append(sNotifiers, slotnotifier.New(slotCs[i], writer, doneC))
}
case 2:
slotCs = append(slotCs, make(chan int))
sNotifiers = []*slotnotifier.Notifier{slotnotifier.New(slotCs[0], writer, doneC), nil}
}
regtor = regulator.New(sdkctx, sNotifiers[0],
privKeyBytes,
statsSlotC, statsTranC, writer, doneC)
wg2.Add(1)
go func() {
if err := regtor.Run(); err != nil {
once.Do(func() {
msg := fmt.Sprint("regulator • closing donec")
fmt.Fprintln(writer, msg)
close(doneC)
})
}
wg2.Done()
}()
biddersList := trace.IDs
if schema.StagingLevel <= schema.Debug {
biddersList = biddersList[:schema.DebugBidderIDsCount] // We only care about the first `schema.DebugBidderIDsCount` bidders.
}
for i, ID := range biddersList {
bidders[i] = bidder.New(sdkctx, sNotifiers[0], sNotifiers[1],
ID, privKeyBytes, traceMap[ID],
statsSlotC, statsTranC, writer, doneC)
wg1.Add(1)
go func(i int) {
if err = bidders[i].Run(); err != nil {
once.Do(func() {
msg := fmt.Sprintf("bidder:%04d • closing donec", bidders[i].ID)
fmt.Fprintln(writer, msg)
close(doneC)
})
}
wg1.Done()
}(i)
}
// The size of the slotCs slice dictates how many block notifiers we need
bNotifiers = append(bNotifiers, blocknotifier.New(
schema.BlocksPerSlot, schema.ClockPeriod, schema.SleepDuration, startFromBlock,
statsBlockC, slotCs[0],
sdkctx, sdkctx.LedgerClient,
writer, doneC,
))
if len(slotCs) > 1 {
nilChan := make(chan stats.Block) // This ensures that only the first blockNotifier feeds the stats collector
bNotifiers = append(bNotifiers, blocknotifier.New(
schema.BlocksPerSlot, schema.ClockPeriod, schema.SleepDuration, startFromBlock+uint64(schema.BlockOffset),
nilChan, slotCs[1],
sdkctx, sdkctx.LedgerClient,
writer, doneC,
))
}
for i := range bNotifiers {
wg2.Add(1)
go func(i int) {
if err := bNotifiers[i].Run(); err != nil {
once.Do(func() {
msg := fmt.Sprintf("block-notifier:%d • closing donec", i)
fmt.Fprintln(writer, msg)
close(doneC)
})
}
wg2.Done()
}(i)
}
for i := range sNotifiers {
if sNotifiers[i] == nil {
continue
}
wg2.Add(1)
go func(i int) {
sNotifiers[i].Run()
wg2.Done()
}(i)
}
// Start the simulation
// In the green path, the *bidders* will run their entire trace, then exit.
// We then close `doneC`, and wait for all the other goroutines to conclude.
// This is why we use two separate waitgroups: one for bidders, and one for
// regulator/notifiers.
// An unfortunate side-effect of this design is that we do not let the regulator
// 'markEnd' the very last slot, since @ slot N the regulator closes slot N-1.
// That means we missing out on the stats of the very last slot, which is why
// we only output N-1 slots in our stats output ("cleared" slots).
wg1.Wait()
once.Do(func() {
msg := fmt.Sprint("main • closing donec")
fmt.Fprintln(writer, msg)
close(doneC)
})
wg2.Wait()
fmt.Fprintln(writer, "main • run completed")
return metrics()
}