Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nocap ts #674

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/trawler/src/parsers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse } from 'dotenv'
import { normalizeRelays } from './sanitizers.js'

export const parseRelayList = (note) => {
Expand All @@ -8,6 +9,8 @@ export const parseRelayList = (note) => {
parsed = parseRelayListFromKind3(note)
if(note.kind === 10002)
parsed = parseRelayListFromKind10002(note)
if(note.kind === 30002)
parsed = parseRelayListFromKind30002(note)

return normalizeRelays(parsed)
}
Expand All @@ -34,4 +37,14 @@ export const parseRelayListFromKind10002 = (note) => {
.map( t => t[1] )
} catch(e) {""}
return dirtyRelayList ? dirtyRelayList : []
}

export const parseRelayListFromKind30002 = (note) => {
let dirtyRelayList
try {
dirtyRelayList = note?.tags
.filter( t => t[0] === 'relay' )
.map( t => t[1] )
} catch(e) {""}
return dirtyRelayList ? dirtyRelayList : []
}
2 changes: 1 addition & 1 deletion apps/trawler/src/trawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const trawl = async function($job){

const it = await fetcher.allEventsIterator(
[ relay ],
{ kinds: [ 2, 10002 ] },
{ kinds: [ 10002 ] },
{ since },
{ sort: true }
)
Expand Down
79 changes: 79 additions & 0 deletions daemons/nocapd/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
monitor:
slug: budapest
owner: "e771af0b05c8e95fcdf6feb3500544d2fb1ccd384788e9f490bb3ee28e8ed66f"
info:
name: Test Monitor
about: NIP-66 Network Monitor operating out of my living room
nip05: [email protected]
# lud06:
# lud16:
# website:
# zapService:
geo:
cityName: Budapest
country: Hungary
countryCode: HU
lat: 47.4979
lon: 19.0402
regionCode: BU
regionName: Budapest
continentName: Europe

publisher:
to_relays:
- 'wss://history.nostr.watch'
kinds:
- 30166
- 30066

nocapd:
loglevel: info
networks:
- clearnet
retry:
expiry: [
{ max: 3, delay: 1000 * 60 * 1},
{ max: 5, delay: 1000 * 60 * 20},
{ max: 7, delay: 1000 * 60 * 60 * 1},
{ max: 11, delay: 1000 * 60 * 60 * 3},
{ max: 15, delay: 1000 * 60 * 60 * 6 },
{ max: 22, delay: 1000 * 60 * 60 * 24 },
{ max: 107, delay: 1000 * 60 * 60 * 24 * 7 }
]

seed:
sources:
- events
- api
options:
events:
interval: 15m
relays:
- 'wss://history.nostr.watch'
pubkeys:
- '6cd206fb5517a77497b53a4c64219fd8b5bce845231ecd271e74a96b03afdcda'
remotes:
rest_api: 'https://api.nostr.watch/v1'
checks:
enabled:
- all
options:
enabled: true
expires: 1h
interval: 15s
priority: 10
timeout: {
open: 15000,
read: 15000,
write: 15000,
dns: 3000,
geo: 3000,
info: 6000,
ssl: 3000,
}
max: "Math.ceil(relays.length/60)"

bullmq:
type: remote # is redis backend for bullmq local or remote? If local, 'sync' via 'bullmq' will not work.
worker:
concurrency: 8
5 changes: 5 additions & 0 deletions daemons/nwd/plugins/worker/nocapd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WorkerBase } from '@nostrwatch/nwd/PluginBase'

export class Nocapd extends WorkerBase {

}
7 changes: 7 additions & 0 deletions daemons/nwd/plugins/worker/trawler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WorkerBase } from '@nostrwatch/nwd/PluginBase'

export class Trawler extends WorkerBase {
constructor(){
super()
}
}
3 changes: 3 additions & 0 deletions daemons/nwd/src/helpers/Backoff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class NWDBackoff {
constructor(){}
}
31 changes: 31 additions & 0 deletions daemons/nwd/src/helpers/Cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CronJob } from 'cron';

type Crons = { [key: string]: CronJob }

export class Cron {
private _jobs: Crons;

constructor(){}

add(name: string, onTick: Function, cronTime: string){
this._jobs[name] = new CronJob({ onTick, cronTime });
}

stop(name: string){
this._jobs[name].stop();
}

stopAll(){
Object.keys(this._jobs).forEach( this.stop.bind(this) );
}

remove(name: string){
this.stop(name)
delete this._jobs[name]
}

removeAll(){
this.stopAll()
this._jobs = {} as Crons
}
}
78 changes: 78 additions & 0 deletions daemons/nwd/src/models/NWD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

import { Config } from '../types/Config'

import { Cron } from '../helpers/Cron'

import { PluginService, CacheService, IdentityService, SigningService, QueueService, WorkerService, SyncService } from '../services/All'

export type NWDServices = {
plugin: PluginService,
identity: IdentityService,
signing: SigningService,
queue: QueueService,
worker: WorkerService,
cache: CacheService,
sync: SyncService
}

export class NWD {

private _config: Config;
private _service: NWDServices;
private _services: any[] = [PluginService, IdentityService, SigningService, QueueService, WorkerService, CacheService, SyncService]

protected cron: Cron

constructor(config: Config){
this.config = config;
}

private _init(){
this.cron = new Cron();
this._init_services();
this._init_plugins();
}

private _init_services(){
this._services.forEach((Service: any) => {
this.service[Service.type] = new Service(this);
})
this.populate = this.service.plugin.populate.bind(this.service)
this.syncIn = this.service.sync.in.bind(this.service)
}

private _init_plugins(){
this.service.plugin.init();
}
public run(){
this._init();
this.schedule();
this.populate();
this.syncIn();
this.start();
}

private schedule(){
const populateInterval = this.service.plugin.plugins.queue.populateInterval
const syncInInterval = this.service.plugin.sync.inInterval
this.cron.add(`*/${populateInterval} * * * * *`, populator)
this.cron.add(`*${syncInInterval} * * * * *`, syncIn)
}

private set service(service: any){
this._service = service
}

private get service(): any {
return this._service
}

protected set config (config: Config){
this._config = config
}

public get config(): Config {
return this._config
}
}

86 changes: 86 additions & 0 deletions daemons/nwd/src/models/NWDCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { NWDPlugin, PluginDataAccess } from './NWDPlugin'
import { PluginService } from '../services/PluginService'
import { IdentityService } from '../services/IdentityService'

export class NWDCache extends NWDPlugin {

protected static _limit: number = 1;
protected static _type: string = "cache";

protected static _access: PluginDataAccess = { "identity": [ "pubkey" ] };
// protected static _limit: number;
// protected static _name: string;
// protected static _version: string;

constructor($PluginService: PluginService, $IdentityService: IdentityService) {
super($PluginService, $IdentityService)
}

private _id(key: string): string{
return `${this.service?.identity.pubkey.length(12)}:${this._class().type}:${key}`
}

public getLastCache(key: string): any {
return this._getLastCache(key)
}

public setLastCache(key: string, value: any) {
this._setLastCache(key, value)
}

public relayExists(relayUrl: string): boolean {
const _record: string | undefined = this.relayGet(relayUrl)
if(typeof _record !== 'undefined' ) return true
return false
}

public relayGet(relay: string): string | undefined {
return this._relayGet(relay)
}

public relayGetOnline(relay: string): string[] | undefined[] {
return this._relayGetOnline(relay)
}

public relayGetMeta(relay: string, key: string): string | undefined {
return this._relayGetMeta(relay, key)
}

public relayInsert(relay: string, value: any){
return this._relayInsert(relay)
}

public relayInsertIfNotExists( relay: string, value: any ): string | number | undefined {
return !this.relayExists(relay)? this.relayInsert(relay, value): undefined
}

public relayUpsert(relay: string, value: any){
if(this.relayExists(relay))
return this?._relayUpdate(relay, value)
else
return this?._relayInsert(relay, value)
}

public relayUpdate(){
return this?._relayUpdate(relay, value)
}

public relayPatch(relay: string, value: any){
return this?._relayPatch(relay, value)
}

//overloads
private _relayExists(relayUrl: string): boolean {}
private _relayGet(relay: string): string | undefined {}
private _relayGetOnline(relay: string): string[] | undefined[] {}
private _relayGetMeta(relay: string): any | undefined {}
private _relayInsert(relay: string, value: any): string | number | undefined {}
private _relayInsertIfNotExists( relay: string, value: any ): string | number | undefined {}
private _relayUpdate(): boolean {}
private _relayPatch(relay: string, value: any): boolean {}
private _relayDelete(): boolean {}
private _relayDeleteIfExists(relay: string): boolean {}

private _getLastCache(key: string): any {}
private _setLastCache(key: string, value: any): any {}
}
Loading