Skip to content

Commit

Permalink
Merge pull request #234 from getAlby/task-eos
Browse files Browse the repository at this point in the history
chore: dry up eos
  • Loading branch information
rolznz authored Jan 30, 2024
2 parents 7cf88fd + f146698 commit 78eec5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
22 changes: 8 additions & 14 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ import (

type Service struct {
// config from .env only. Fetch dynamic config from db
cfg *Config
db *gorm.DB
lnClient LNClient
ReceivedEOS bool
Logger *logrus.Logger
ctx context.Context
wg *sync.WaitGroup
cfg *Config
db *gorm.DB
lnClient LNClient
Logger *logrus.Logger
ctx context.Context
wg *sync.WaitGroup
}

// TODO: move to service.go
Expand Down Expand Up @@ -165,12 +164,11 @@ func (svc *Service) noticeHandler(notice string) {

func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error {
go func() {
// block till EOS is received
<-sub.EndOfStoredEvents
svc.ReceivedEOS = true
svc.Logger.Info("Received EOS")
}()

go func() {
// loop through incoming events
for event := range sub.Events {
go func(event *nostr.Event) {
resp, err := svc.HandleEvent(ctx, event)
Expand Down Expand Up @@ -255,10 +253,6 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
}

func (svc *Service) HandleEvent(ctx context.Context, event *nostr.Event) (result *nostr.Event, err error) {
//don't process historical events
if !svc.ReceivedEOS {
return nil, nil
}
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
Expand Down
17 changes: 4 additions & 13 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ func TestHandleEvent(t *testing.T) {
ctx := context.TODO()
svc, _ := createTestService(t)
defer os.Remove(testDB)
//test not yet receivedEOS
res, err := svc.HandleEvent(ctx, &nostr.Event{
Kind: NIP_47_REQUEST_KIND,
})
assert.Nil(t, res)
assert.Nil(t, err)
//now signal that we are ready to receive events
svc.ReceivedEOS = true

senderPrivkey := nostr.GeneratePrivateKey()
senderPubkey, err := nostr.GetPublicKey(senderPrivkey)
Expand All @@ -166,7 +158,7 @@ func TestHandleEvent(t *testing.T) {
assert.NoError(t, err)
payload, err := nip04.Encrypt(nip47PayJson, ss)
assert.NoError(t, err)
res, err = svc.HandleEvent(ctx, &nostr.Event{
res, err := svc.HandleEvent(ctx, &nostr.Event{
ID: "test_event_1",
Kind: NIP_47_REQUEST_KIND,
PubKey: senderPubkey,
Expand Down Expand Up @@ -646,10 +638,9 @@ func createTestService(t *testing.T) (svc *Service, ln LNClient) {
NostrSecretKey: sk,
NostrPublicKey: pk,
},
db: gormDb,
lnClient: ln,
ReceivedEOS: false,
Logger: logger,
db: gormDb,
lnClient: ln,
Logger: logger,
}, ln
}

Expand Down

0 comments on commit 78eec5c

Please sign in to comment.