1
- package engine
1
+ package godvm
2
2
3
3
import (
4
4
"context"
@@ -7,15 +7,13 @@ import (
7
7
"sync"
8
8
9
9
goNostr "github.com/nbd-wtf/go-nostr"
10
- "github.com/sebdeveloper6952/go-dvm/domain"
11
- "github.com/sebdeveloper6952/go-dvm/lightning"
12
- "github.com/sebdeveloper6952/go-dvm/nostr"
10
+ "github.com/sebdeveloper6952/godvm/lightning"
13
11
"github.com/sirupsen/logrus"
14
12
)
15
13
16
14
type Engine struct {
17
- dvmsByKind map [int ][]domain. Dvmer
18
- nostrSvc nostr. Service
15
+ dvmsByKind map [int ][]Dvmer
16
+ nostrSvc NostrService
19
17
lnSvc lightning.Service
20
18
log * logrus.Logger
21
19
waitingForEvent map [string ][]chan * goNostr.Event
@@ -29,15 +27,15 @@ func NewEngine() (*Engine, error) {
29
27
})
30
28
logger .SetLevel (logrus .TraceLevel )
31
29
32
- nostrSvc , err := nostr . NewNostr (
30
+ nostrSvc , err := NewNostrService (
33
31
logger ,
34
32
)
35
33
if err != nil {
36
34
return nil , err
37
35
}
38
36
39
37
e := & Engine {
40
- dvmsByKind : make (map [int ][]domain. Dvmer ),
38
+ dvmsByKind : make (map [int ][]Dvmer ),
41
39
waitingForEvent : make (map [string ][]chan * goNostr.Event ),
42
40
nostrSvc : nostrSvc ,
43
41
log : logger ,
@@ -50,10 +48,10 @@ func (e *Engine) SetLogLevel(level logrus.Level) {
50
48
e .log .SetLevel (level )
51
49
}
52
50
53
- func (e * Engine ) RegisterDVM (dvm domain. Dvmer ) {
51
+ func (e * Engine ) RegisterDVM (dvm Dvmer ) {
54
52
kindSupported := dvm .KindSupported ()
55
53
if _ , ok := e .dvmsByKind [kindSupported ]; ! ok {
56
- e .dvmsByKind [kindSupported ] = make ([]domain. Dvmer , 0 , 2 )
54
+ e .dvmsByKind [kindSupported ] = make ([]Dvmer , 0 , 2 )
57
55
}
58
56
e .dvmsByKind [kindSupported ] = append (e .dvmsByKind [kindSupported ], dvm )
59
57
}
@@ -90,7 +88,7 @@ func (e *Engine) Run(
90
88
continue
91
89
}
92
90
93
- nip90Input , err := nostr . Nip90InputFromJobRequestEvent (event )
91
+ nip90Input , err := Nip90InputFromJobRequestEvent (event )
94
92
if err != nil {
95
93
e .log .Errorf ("[engine] nip90Input from event %+v\n " , err )
96
94
continue
@@ -99,10 +97,10 @@ func (e *Engine) Run(
99
97
// if the inputs are asking for events/jobs, we fetch them here before proceeding
100
98
var wg sync.WaitGroup
101
99
for inputIdx := range nip90Input .Inputs {
102
- if nip90Input .Inputs [inputIdx ].Type == nostr . InputTypeEvent ||
103
- nip90Input .Inputs [inputIdx ].Type == nostr . InputTypeJob {
100
+ if nip90Input .Inputs [inputIdx ].Type == InputTypeEvent ||
101
+ nip90Input .Inputs [inputIdx ].Type == InputTypeJob {
104
102
wg .Add (1 )
105
- go func (input * nostr. Input ) {
103
+ go func (input * Input ) {
106
104
defer wg .Done ()
107
105
108
106
// TODO: must handle when the event is not found, only when the input type is "event".
@@ -124,7 +122,7 @@ func (e *Engine) Run(
124
122
e .log .Tracef ("[engine] finished waiting for input events" )
125
123
126
124
for i := range dvmsForKind {
127
- go func (dvm domain. Dvmer , input * nostr. Nip90Input ) {
125
+ go func (dvm Dvmer , input * Nip90Input ) {
128
126
if dvm .AcceptJob (input ) {
129
127
e .runDvm (ctx , dvm , input )
130
128
}
@@ -139,13 +137,13 @@ func (e *Engine) Run(
139
137
return nil
140
138
}
141
139
142
- func (e * Engine ) runDvm (ctx context.Context , dvm domain. Dvmer , input * nostr. Nip90Input ) {
140
+ func (e * Engine ) runDvm (ctx context.Context , dvm Dvmer , input * Nip90Input ) {
143
141
chanToDvm , chanToEngine , chanErr := dvm .Run (ctx , input )
144
142
145
143
for {
146
144
select {
147
145
case update := <- chanToEngine :
148
- if update .Status == domain . StatusError {
146
+ if update .Status == StatusError {
149
147
if err := e .sendFeedbackEvent (
150
148
ctx ,
151
149
dvm ,
@@ -155,7 +153,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
155
153
e .log .Errorf ("[nostr] sendEventFeedback %+v\n " , err )
156
154
}
157
155
return
158
- } else if update .Status == domain . StatusPaymentRequired {
156
+ } else if update .Status == StatusPaymentRequired {
159
157
invoice , err := e .addInvoiceAndTrack (ctx , chanToDvm , int64 (update .AmountSats ))
160
158
if err != nil {
161
159
e .log .Tracef ("[nostr] addInvoice %+v\n " , err )
@@ -171,7 +169,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
171
169
); err != nil {
172
170
e .log .Errorf ("[nostr] sendEventFeedback %+v\n " , err )
173
171
}
174
- } else if update .Status == domain . StatusProcessing {
172
+ } else if update .Status == StatusProcessing {
175
173
if err := e .sendFeedbackEvent (
176
174
ctx ,
177
175
dvm ,
@@ -180,7 +178,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
180
178
); err != nil {
181
179
e .log .Errorf ("[nostr] sendEventFeedback %+v\n " , err )
182
180
}
183
- } else if update .Status == domain . StatusSuccess {
181
+ } else if update .Status == StatusSuccess {
184
182
if err := e .sendFeedbackEvent (
185
183
ctx ,
186
184
dvm ,
@@ -201,7 +199,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
201
199
202
200
e .log .Tracef ("[engine] job completed %+v" , update )
203
201
return
204
- } else if update .Status == domain . StatusSuccessWithPayment {
202
+ } else if update .Status == StatusSuccessWithPayment {
205
203
if err := e .sendFeedbackEvent (
206
204
ctx ,
207
205
dvm ,
@@ -249,7 +247,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
249
247
func (e * Engine ) advertiseDvms (ctx context.Context ) {
250
248
for kind , dvms := range e .dvmsByKind {
251
249
for i := range dvms {
252
- ev := nostr . NewHandlerInformationEvent (
250
+ ev := NewHandlerInformationEvent (
253
251
dvms [i ].Pk (),
254
252
dvms [i ].Profile (),
255
253
[]int {kind },
@@ -260,7 +258,7 @@ func (e *Engine) advertiseDvms(ctx context.Context) {
260
258
e .log .Errorf ("[engine] publish nip-89 %s %+v" , dvms [i ].Pk (), err )
261
259
}
262
260
263
- profileEv := nostr . NewProfileMetadataEvent (
261
+ profileEv := NewProfileMetadataEvent (
264
262
dvms [i ].Pk (),
265
263
dvms [i ].Profile (),
266
264
)
@@ -274,13 +272,13 @@ func (e *Engine) advertiseDvms(ctx context.Context) {
274
272
275
273
func (e * Engine ) addInvoiceAndTrack (
276
274
ctx context.Context ,
277
- chanToDvm chan * domain. JobUpdate ,
275
+ chanToDvm chan * JobUpdate ,
278
276
amountSats int64 ,
279
277
) (* lightning.Invoice , error ) {
280
278
invoice , err := e .lnSvc .AddInvoice (ctx , amountSats )
281
279
if err != nil {
282
- chanToDvm <- & domain. JobUpdate {
283
- Status : domain . StatusError ,
280
+ chanToDvm <- & JobUpdate {
281
+ Status : StatusError ,
284
282
}
285
283
return nil , err
286
284
}
@@ -292,14 +290,14 @@ func (e *Engine) addInvoiceAndTrack(
292
290
select {
293
291
case invoiceUpdate := <- u :
294
292
if invoiceUpdate .Settled {
295
- chanToDvm <- & domain. JobUpdate {
296
- Status : domain . StatusPaymentCompleted ,
293
+ chanToDvm <- & JobUpdate {
294
+ Status : StatusPaymentCompleted ,
297
295
}
298
296
break trackInvoiceLoop
299
297
}
300
298
case <- e :
301
- chanToDvm <- & domain. JobUpdate {
302
- Status : domain . StatusError ,
299
+ chanToDvm <- & JobUpdate {
300
+ Status : StatusError ,
303
301
}
304
302
return
305
303
}
@@ -311,22 +309,28 @@ func (e *Engine) addInvoiceAndTrack(
311
309
312
310
func (e * Engine ) sendFeedbackEvent (
313
311
ctx context.Context ,
314
- dvm domain. Dvmer ,
315
- input * nostr. Nip90Input ,
316
- update * domain. JobUpdate ,
312
+ dvm Dvmer ,
313
+ input * Nip90Input ,
314
+ update * JobUpdate ,
317
315
) error {
318
316
feedbackEvent := & goNostr.Event {
319
317
PubKey : dvm .Pk (),
320
318
CreatedAt : goNostr .Now (),
321
- Kind : nostr . KindJobFeedback ,
319
+ Kind : KindJobFeedback ,
322
320
Tags : goNostr.Tags {
323
321
{"e" , input .JobRequestId },
324
322
{"p" , input .CustomerPubkey },
325
- {"status" , domain . JobStatusToString [update .Status ]},
323
+ {"status" , JobStatusToString [update .Status ]},
326
324
},
327
325
}
328
326
329
- if update .Status == domain .StatusPaymentRequired {
327
+ if update .ExtraTags != nil && len (update .ExtraTags ) > 0 {
328
+ for i := range update .ExtraTags {
329
+ feedbackEvent .Tags = append (feedbackEvent .Tags , update .ExtraTags [i ])
330
+ }
331
+ }
332
+
333
+ if update .Status == StatusPaymentRequired {
330
334
tag := goNostr.Tag {
331
335
"amount" ,
332
336
fmt .Sprintf ("%d" , update .AmountSats * 1000 ),
@@ -346,16 +350,22 @@ func (e *Engine) sendFeedbackEvent(
346
350
347
351
func (e * Engine ) sendJobResultEvent (
348
352
ctx context.Context ,
349
- dvm domain. Dvmer ,
350
- input * nostr. Nip90Input ,
351
- update * domain. JobUpdate ,
353
+ dvm Dvmer ,
354
+ input * Nip90Input ,
355
+ update * JobUpdate ,
352
356
) error {
353
357
tags := goNostr.Tags {
354
358
{"request" , input .JobRequestEventJSON },
355
359
{"e" , input .JobRequestId },
356
360
{"p" , input .CustomerPubkey },
357
361
}
358
362
363
+ if update .ExtraTags != nil && len (update .ExtraTags ) > 0 {
364
+ for i := range update .ExtraTags {
365
+ tags = append (tags , update .ExtraTags [i ])
366
+ }
367
+ }
368
+
359
369
for i := range input .Inputs {
360
370
tag := goNostr.Tag {
361
371
"i" ,
@@ -377,7 +387,7 @@ func (e *Engine) sendJobResultEvent(
377
387
tags = append (tags , tag )
378
388
}
379
389
380
- if update .Status == domain . StatusSuccessWithPayment && update .PaymentRequest != "" {
390
+ if update .Status == StatusSuccessWithPayment && update .PaymentRequest != "" {
381
391
tags = append (
382
392
tags ,
383
393
goNostr.Tag {
@@ -396,7 +406,7 @@ func (e *Engine) sendJobResultEvent(
396
406
Tags : tags ,
397
407
}
398
408
399
- if update .Status == domain . StatusPaymentRequired {
409
+ if update .Status == StatusPaymentRequired {
400
410
tag := goNostr.Tag {
401
411
"amount" ,
402
412
fmt .Sprintf ("%d" , update .AmountSats * 1000 ),
0 commit comments