Skip to content

Commit 98168d0

Browse files
reformat as library for easier usage
1 parent 011385e commit 98168d0

16 files changed

+69
-245
lines changed

domain/dvm.go renamed to dvm.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
package domain
1+
package godvm
22

33
import (
44
"context"
55

66
goNostr "github.com/nbd-wtf/go-nostr"
7-
8-
"github.com/sebdeveloper6952/go-dvm/nostr"
97
)
108

119
type Dvmer interface {
1210
Pk() string
1311
Sign(e *goNostr.Event) error
1412
KindSupported() int
1513
Version() string
16-
AcceptJob(input *nostr.Nip90Input) bool
17-
Run(ctx context.Context, input *nostr.Nip90Input) (chan *JobUpdate, chan *JobUpdate, chan error)
18-
Profile() *nostr.ProfileMetadata
14+
AcceptJob(input *Nip90Input) bool
15+
Run(ctx context.Context, input *Nip90Input) (chan *JobUpdate, chan *JobUpdate, chan error)
16+
Profile() *ProfileMetadata
1917
}
2018

2119
type Dvm struct {

engine/engine.go renamed to engine.go

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package engine
1+
package godvm
22

33
import (
44
"context"
@@ -7,15 +7,13 @@ import (
77
"sync"
88

99
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"
1311
"github.com/sirupsen/logrus"
1412
)
1513

1614
type Engine struct {
17-
dvmsByKind map[int][]domain.Dvmer
18-
nostrSvc nostr.Service
15+
dvmsByKind map[int][]Dvmer
16+
nostrSvc NostrService
1917
lnSvc lightning.Service
2018
log *logrus.Logger
2119
waitingForEvent map[string][]chan *goNostr.Event
@@ -29,15 +27,15 @@ func NewEngine() (*Engine, error) {
2927
})
3028
logger.SetLevel(logrus.TraceLevel)
3129

32-
nostrSvc, err := nostr.NewNostr(
30+
nostrSvc, err := NewNostrService(
3331
logger,
3432
)
3533
if err != nil {
3634
return nil, err
3735
}
3836

3937
e := &Engine{
40-
dvmsByKind: make(map[int][]domain.Dvmer),
38+
dvmsByKind: make(map[int][]Dvmer),
4139
waitingForEvent: make(map[string][]chan *goNostr.Event),
4240
nostrSvc: nostrSvc,
4341
log: logger,
@@ -50,10 +48,10 @@ func (e *Engine) SetLogLevel(level logrus.Level) {
5048
e.log.SetLevel(level)
5149
}
5250

53-
func (e *Engine) RegisterDVM(dvm domain.Dvmer) {
51+
func (e *Engine) RegisterDVM(dvm Dvmer) {
5452
kindSupported := dvm.KindSupported()
5553
if _, ok := e.dvmsByKind[kindSupported]; !ok {
56-
e.dvmsByKind[kindSupported] = make([]domain.Dvmer, 0, 2)
54+
e.dvmsByKind[kindSupported] = make([]Dvmer, 0, 2)
5755
}
5856
e.dvmsByKind[kindSupported] = append(e.dvmsByKind[kindSupported], dvm)
5957
}
@@ -90,7 +88,7 @@ func (e *Engine) Run(
9088
continue
9189
}
9290

93-
nip90Input, err := nostr.Nip90InputFromJobRequestEvent(event)
91+
nip90Input, err := Nip90InputFromJobRequestEvent(event)
9492
if err != nil {
9593
e.log.Errorf("[engine] nip90Input from event %+v\n", err)
9694
continue
@@ -99,10 +97,10 @@ func (e *Engine) Run(
9997
// if the inputs are asking for events/jobs, we fetch them here before proceeding
10098
var wg sync.WaitGroup
10199
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 {
104102
wg.Add(1)
105-
go func(input *nostr.Input) {
103+
go func(input *Input) {
106104
defer wg.Done()
107105

108106
// TODO: must handle when the event is not found, only when the input type is "event".
@@ -124,7 +122,7 @@ func (e *Engine) Run(
124122
e.log.Tracef("[engine] finished waiting for input events")
125123

126124
for i := range dvmsForKind {
127-
go func(dvm domain.Dvmer, input *nostr.Nip90Input) {
125+
go func(dvm Dvmer, input *Nip90Input) {
128126
if dvm.AcceptJob(input) {
129127
e.runDvm(ctx, dvm, input)
130128
}
@@ -139,13 +137,13 @@ func (e *Engine) Run(
139137
return nil
140138
}
141139

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) {
143141
chanToDvm, chanToEngine, chanErr := dvm.Run(ctx, input)
144142

145143
for {
146144
select {
147145
case update := <-chanToEngine:
148-
if update.Status == domain.StatusError {
146+
if update.Status == StatusError {
149147
if err := e.sendFeedbackEvent(
150148
ctx,
151149
dvm,
@@ -155,7 +153,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
155153
e.log.Errorf("[nostr] sendEventFeedback %+v\n", err)
156154
}
157155
return
158-
} else if update.Status == domain.StatusPaymentRequired {
156+
} else if update.Status == StatusPaymentRequired {
159157
invoice, err := e.addInvoiceAndTrack(ctx, chanToDvm, int64(update.AmountSats))
160158
if err != nil {
161159
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
171169
); err != nil {
172170
e.log.Errorf("[nostr] sendEventFeedback %+v\n", err)
173171
}
174-
} else if update.Status == domain.StatusProcessing {
172+
} else if update.Status == StatusProcessing {
175173
if err := e.sendFeedbackEvent(
176174
ctx,
177175
dvm,
@@ -180,7 +178,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
180178
); err != nil {
181179
e.log.Errorf("[nostr] sendEventFeedback %+v\n", err)
182180
}
183-
} else if update.Status == domain.StatusSuccess {
181+
} else if update.Status == StatusSuccess {
184182
if err := e.sendFeedbackEvent(
185183
ctx,
186184
dvm,
@@ -201,7 +199,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
201199

202200
e.log.Tracef("[engine] job completed %+v", update)
203201
return
204-
} else if update.Status == domain.StatusSuccessWithPayment {
202+
} else if update.Status == StatusSuccessWithPayment {
205203
if err := e.sendFeedbackEvent(
206204
ctx,
207205
dvm,
@@ -249,7 +247,7 @@ func (e *Engine) runDvm(ctx context.Context, dvm domain.Dvmer, input *nostr.Nip9
249247
func (e *Engine) advertiseDvms(ctx context.Context) {
250248
for kind, dvms := range e.dvmsByKind {
251249
for i := range dvms {
252-
ev := nostr.NewHandlerInformationEvent(
250+
ev := NewHandlerInformationEvent(
253251
dvms[i].Pk(),
254252
dvms[i].Profile(),
255253
[]int{kind},
@@ -260,7 +258,7 @@ func (e *Engine) advertiseDvms(ctx context.Context) {
260258
e.log.Errorf("[engine] publish nip-89 %s %+v", dvms[i].Pk(), err)
261259
}
262260

263-
profileEv := nostr.NewProfileMetadataEvent(
261+
profileEv := NewProfileMetadataEvent(
264262
dvms[i].Pk(),
265263
dvms[i].Profile(),
266264
)
@@ -274,13 +272,13 @@ func (e *Engine) advertiseDvms(ctx context.Context) {
274272

275273
func (e *Engine) addInvoiceAndTrack(
276274
ctx context.Context,
277-
chanToDvm chan *domain.JobUpdate,
275+
chanToDvm chan *JobUpdate,
278276
amountSats int64,
279277
) (*lightning.Invoice, error) {
280278
invoice, err := e.lnSvc.AddInvoice(ctx, amountSats)
281279
if err != nil {
282-
chanToDvm <- &domain.JobUpdate{
283-
Status: domain.StatusError,
280+
chanToDvm <- &JobUpdate{
281+
Status: StatusError,
284282
}
285283
return nil, err
286284
}
@@ -292,14 +290,14 @@ func (e *Engine) addInvoiceAndTrack(
292290
select {
293291
case invoiceUpdate := <-u:
294292
if invoiceUpdate.Settled {
295-
chanToDvm <- &domain.JobUpdate{
296-
Status: domain.StatusPaymentCompleted,
293+
chanToDvm <- &JobUpdate{
294+
Status: StatusPaymentCompleted,
297295
}
298296
break trackInvoiceLoop
299297
}
300298
case <-e:
301-
chanToDvm <- &domain.JobUpdate{
302-
Status: domain.StatusError,
299+
chanToDvm <- &JobUpdate{
300+
Status: StatusError,
303301
}
304302
return
305303
}
@@ -311,22 +309,28 @@ func (e *Engine) addInvoiceAndTrack(
311309

312310
func (e *Engine) sendFeedbackEvent(
313311
ctx context.Context,
314-
dvm domain.Dvmer,
315-
input *nostr.Nip90Input,
316-
update *domain.JobUpdate,
312+
dvm Dvmer,
313+
input *Nip90Input,
314+
update *JobUpdate,
317315
) error {
318316
feedbackEvent := &goNostr.Event{
319317
PubKey: dvm.Pk(),
320318
CreatedAt: goNostr.Now(),
321-
Kind: nostr.KindJobFeedback,
319+
Kind: KindJobFeedback,
322320
Tags: goNostr.Tags{
323321
{"e", input.JobRequestId},
324322
{"p", input.CustomerPubkey},
325-
{"status", domain.JobStatusToString[update.Status]},
323+
{"status", JobStatusToString[update.Status]},
326324
},
327325
}
328326

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 {
330334
tag := goNostr.Tag{
331335
"amount",
332336
fmt.Sprintf("%d", update.AmountSats*1000),
@@ -346,16 +350,22 @@ func (e *Engine) sendFeedbackEvent(
346350

347351
func (e *Engine) sendJobResultEvent(
348352
ctx context.Context,
349-
dvm domain.Dvmer,
350-
input *nostr.Nip90Input,
351-
update *domain.JobUpdate,
353+
dvm Dvmer,
354+
input *Nip90Input,
355+
update *JobUpdate,
352356
) error {
353357
tags := goNostr.Tags{
354358
{"request", input.JobRequestEventJSON},
355359
{"e", input.JobRequestId},
356360
{"p", input.CustomerPubkey},
357361
}
358362

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+
359369
for i := range input.Inputs {
360370
tag := goNostr.Tag{
361371
"i",
@@ -377,7 +387,7 @@ func (e *Engine) sendJobResultEvent(
377387
tags = append(tags, tag)
378388
}
379389

380-
if update.Status == domain.StatusSuccessWithPayment && update.PaymentRequest != "" {
390+
if update.Status == StatusSuccessWithPayment && update.PaymentRequest != "" {
381391
tags = append(
382392
tags,
383393
goNostr.Tag{
@@ -396,7 +406,7 @@ func (e *Engine) sendJobResultEvent(
396406
Tags: tags,
397407
}
398408

399-
if update.Status == domain.StatusPaymentRequired {
409+
if update.Status == StatusPaymentRequired {
400410
tag := goNostr.Tag{
401411
"amount",
402412
fmt.Sprintf("%d", update.AmountSats*1000),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/sebdeveloper6952/go-dvm
1+
module github.com/sebdeveloper6952/godvm
22

33
go 1.21.5
44

domain/job.go renamed to job.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package domain
1+
package godvm
22

33
type Job struct {
44
ID string
@@ -32,5 +32,6 @@ type JobUpdate struct {
3232
AmountSats int
3333
PaymentRequest string
3434
Result string
35+
ExtraTags [][]string
3536
FailureMsg string
3637
}

lightning/lnbits/lnbits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/lightningnetwork/lnd/lntypes"
1111

12-
"github.com/sebdeveloper6952/go-dvm/lightning"
12+
"github.com/sebdeveloper6952/godvm/lightning"
1313
)
1414

1515
type lnbits struct {

lightning/lnd/lnd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/lightningnetwork/lnd/lntypes"
1818
"github.com/lightningnetwork/lnd/lnwire"
1919

20-
"github.com/sebdeveloper6952/go-dvm/lightning"
20+
"github.com/sebdeveloper6952/godvm/lightning"
2121
)
2222

2323
type lnd struct {

nostr/nip01.go renamed to nip01.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nostr
1+
package godvm
22

33
import (
44
"encoding/json"

nostr/nip89.go renamed to nip89.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nostr
1+
package godvm
22

33
import (
44
"encoding/json"

nostr/nip90.go renamed to nip90.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nostr
1+
package godvm
22

33
import (
44
"encoding/json"
@@ -20,6 +20,7 @@ const (
2020
KindReqNpubDiscovery = 5301
2121
KindReqNostrEventCount = 5400
2222
KindReqMalwareScan = 5500
23+
KindReqAppAnalysis = 5501
2324
KindReqEventTimestamping = 5900
2425
KindReqBitcoinOpReturn = 5901
2526

@@ -35,6 +36,7 @@ const (
3536
KindResNpubDiscovery = 6301
3637
KindResNostrEventCount = 6400
3738
KindResMalwareScan = 6500
39+
KindResAppAnalysis = 6501
3840
KindResEventTimestamping = 6900
3941
KindResBitcoinOpReturn = 6901
4042

nostr/service.go renamed to nostr.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nostr
1+
package godvm
22

33
import (
44
"context"
@@ -10,7 +10,7 @@ import (
1010
"github.com/sirupsen/logrus"
1111
)
1212

13-
type Service interface {
13+
type NostrService interface {
1414
Run(
1515
ctx context.Context,
1616
dvmSupportedKinds []int,
@@ -39,9 +39,9 @@ type svc struct {
3939
log *logrus.Logger
4040
}
4141

42-
func NewNostr(
42+
func NewNostrService(
4343
log *logrus.Logger,
44-
) (Service, error) {
44+
) (NostrService, error) {
4545
return &svc{
4646
jobRequestEvents: make(chan *goNostr.Event),
4747
inputEvents: make(chan *goNostr.Event),

0 commit comments

Comments
 (0)