forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_manager.go
296 lines (253 loc) · 7.76 KB
/
job_manager.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
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
package jobs
import (
"fmt"
"path/filepath"
"strings"
compilers "github.com/hyperledger/burrow/deploy/compile"
"github.com/hyperledger/burrow/deploy/def"
"github.com/hyperledger/burrow/deploy/util"
log "github.com/sirupsen/logrus"
)
type intermediateJob struct {
contractName string
compilerResp *compilers.Response
err error
done chan struct{}
}
func intermediateJobRunner(jobs chan *intermediateJob) {
for {
intermediate, ok := <-jobs
if !ok {
break
}
resp, err := compilers.Compile(intermediate.contractName, false, nil)
(*intermediate).compilerResp = resp
(*intermediate).err = err
close(intermediate.done)
}
}
func queueCompilerWork(job *def.Job, jobs chan *intermediateJob) error {
payload, err := job.Payload()
if err != nil {
return fmt.Errorf("could not get Job payload: %v", payload)
}
// Do compilation first
switch payload.(type) {
case *def.Build:
intermediate := intermediateJob{done: make(chan struct{}), contractName: job.Build.Contract}
job.Intermediate = &intermediate
jobs <- &intermediate
case *def.Deploy:
if filepath.Ext(job.Deploy.Contract) == ".sol" {
intermediate := intermediateJob{done: make(chan struct{}), contractName: job.Deploy.Contract}
job.Intermediate = &intermediate
jobs <- &intermediate
}
case *def.Proposal:
for _, job := range job.Proposal.Jobs {
err = queueCompilerWork(job, jobs)
if err != nil {
return err
}
}
}
return nil
}
func getCompilerWork(intermediate interface{}) (*compilers.Response, error) {
if intermediate, ok := intermediate.(*intermediateJob); ok {
<-intermediate.done
return intermediate.compilerResp, intermediate.err
}
return nil, fmt.Errorf("internal error: no compiler work queued")
}
func DoJobs(do *def.DeployArgs, client *def.Client) error {
// ADD DefaultAddr and DefaultSet to jobs array....
// These work in reverse order and the addendums to the
// the ordering from the loading process is lifo
if len(do.DefaultSets) >= 1 {
defaultSetJobs(do)
}
if do.Address != "" {
defaultAddrJob(do)
}
err := do.Validate()
if err != nil {
return fmt.Errorf("error validating Burrow deploy file at %s: %v", do.YAMLPath, err)
}
// Ensure we have a queue large enough so that we don't have to wait for more work to be queued
jobs := make(chan *intermediateJob, do.Jobs*2)
defer close(jobs)
for i := 0; i < do.Jobs; i++ {
go intermediateJobRunner(jobs)
}
for _, job := range do.Package.Jobs {
queueCompilerWork(job, jobs)
}
for _, job := range do.Package.Jobs {
payload, err := job.Payload()
if err != nil {
return fmt.Errorf("could not get Job payload: %v", payload)
}
err = util.PreProcessFields(payload, do, client)
if err != nil {
return err
}
// Revalidate with possible replacements
err = payload.Validate()
if err != nil {
return fmt.Errorf("error validating job %s after pre-processing variables: %v", job.Name, err)
}
switch payload.(type) {
case *def.Proposal:
announce(job.Name, "Proposal")
job.Result, err = ProposalJob(job.Proposal, do, client)
// Meta Job
case *def.Meta:
announce(job.Name, "Meta")
do.CurrentOutput = fmt.Sprintf("%s.output.json", job.Name)
job.Result, err = MetaJob(job.Meta, do, client)
// Governance
case *def.UpdateAccount:
announce(job.Name, "UpdateAccount")
job.Result, job.Variables, err = UpdateAccountJob(job.UpdateAccount, do.Package.Account, client)
// Util jobs
case *def.Account:
announce(job.Name, "Account")
job.Result, err = SetAccountJob(job.Account, do)
case *def.Set:
announce(job.Name, "Set")
job.Result, err = SetValJob(job.Set, do)
// Transaction jobs
case *def.Send:
announce(job.Name, "Sent")
job.Result, err = SendJob(job.Send, do.Package.Account, client)
case *def.RegisterName:
announce(job.Name, "RegisterName")
job.Result, err = RegisterNameJob(job.RegisterName, do, client)
case *def.Permission:
announce(job.Name, "Permission")
job.Result, err = PermissionJob(job.Permission, do.Package.Account, client)
// Contracts jobs
case *def.Deploy:
announce(job.Name, "Deploy")
job.Result, err = DeployJob(job.Deploy, do, client, job.Intermediate)
case *def.Call:
announce(job.Name, "Call")
CallTx, ferr := FormulateCallJob(job.Call, do, client)
if ferr != nil {
return ferr
}
job.Result, job.Variables, err = CallJob(job.Call, CallTx, do, client)
case *def.Build:
announce(job.Name, "Build")
var resp *compilers.Response
resp, err = getCompilerWork(job.Intermediate)
if err != nil {
return err
}
job.Result, err = BuildJob(job.Build, do.BinPath, resp)
// State jobs
case *def.RestoreState:
announce(job.Name, "RestoreState")
job.Result, err = RestoreStateJob(job.RestoreState)
case *def.DumpState:
announce(job.Name, "DumpState")
job.Result, err = DumpStateJob(job.DumpState)
// Test jobs
case *def.QueryAccount:
announce(job.Name, "QueryAccount")
job.Result, err = QueryAccountJob(job.QueryAccount, client)
case *def.QueryContract:
announce(job.Name, "QueryContract")
job.Result, job.Variables, err = QueryContractJob(job.QueryContract, do, client)
case *def.QueryName:
announce(job.Name, "QueryName")
job.Result, err = QueryNameJob(job.QueryName, client)
case *def.QueryVals:
announce(job.Name, "QueryVals")
job.Result, err = QueryValsJob(job.QueryVals, client)
case *def.Assert:
announce(job.Name, "Assert")
job.Result, err = AssertJob(job.Assert)
default:
log.Error("")
return fmt.Errorf("the Job specified in deploy.yaml and parsed as '%v' is not recognised as a valid job",
job)
}
if len(job.Variables) != 0 {
for _, theJob := range job.Variables {
log.WithField("=>", fmt.Sprintf("%s,%s", theJob.Name, theJob.Value)).Info("Job Vars")
}
}
if err != nil {
return err
}
}
postProcess(do)
return nil
}
func announce(job, typ string) {
log.Warn("*****Executing Job*****\n")
log.WithField("=>", job).Warn("Job Name")
log.WithField("=>", typ).Info("Type")
log.Warn("\n")
}
func announceProposalJob(job, typ string) {
log.Warn("*****Capturing Proposal Job*****\n")
log.WithField("=>", job).Warn("Job Name")
log.WithField("=>", typ).Info("Type")
log.Warn("\n")
}
func defaultAddrJob(do *def.DeployArgs) {
oldJobs := do.Package.Jobs
newJob := &def.Job{
Name: "defaultAddr",
Account: &def.Account{
Address: do.Address,
},
}
do.Package.Jobs = append([]*def.Job{newJob}, oldJobs...)
}
func defaultSetJobs(do *def.DeployArgs) {
oldJobs := do.Package.Jobs
newJobs := []*def.Job{}
for _, setr := range do.DefaultSets {
blowdUp := strings.Split(setr, "=")
if blowdUp[0] != "" {
newJobs = append(newJobs, &def.Job{
Name: blowdUp[0],
Set: &def.Set{
Value: blowdUp[1],
},
})
}
}
do.Package.Jobs = append(newJobs, oldJobs...)
}
func postProcess(do *def.DeployArgs) error {
// Formulate the results map
results := make(map[string]interface{})
for _, job := range do.Package.Jobs {
results[job.Name] = job.Result
}
// check do.YAMLPath and do.DefaultOutput
var yaml string
yamlName := strings.LastIndexByte(do.YAMLPath, '.')
if yamlName >= 0 {
yaml = do.YAMLPath[:yamlName]
} else {
return fmt.Errorf("invalid jobs file path (%s)", do.YAMLPath)
}
// if do.YAMLPath is not default and do.DefaultOutput is default, over-ride do.DefaultOutput
if yaml != "deploy" && do.DefaultOutput == "deploy.output.json" {
do.DefaultOutput = fmt.Sprintf("%s.output.json", yaml)
}
// if CurrentOutput set, we're in a meta job
if do.CurrentOutput != "" {
log.Warn(fmt.Sprintf("Writing meta output of [%s] to current directory", do.CurrentOutput))
return WriteJobResultJSON(results, do.CurrentOutput)
}
// Write the output
log.Warn(fmt.Sprintf("Writing [%s] to current directory", do.DefaultOutput))
return WriteJobResultJSON(results, do.DefaultOutput)
}