commit
14549af71f
30 changed files with 1320 additions and 57 deletions
|
|
@ -103,6 +103,7 @@ LSP_MAX_FEE_BPS=100
|
||||||
#METRICS_TOKEN=
|
#METRICS_TOKEN=
|
||||||
# Disable outbound payments aka honeypot mode
|
# Disable outbound payments aka honeypot mode
|
||||||
#DISABLE_EXTERNAL_PAYMENTS=false
|
#DISABLE_EXTERNAL_PAYMENTS=false
|
||||||
|
#ALLOW_RESET_METRICS_STORAGES=false
|
||||||
|
|
||||||
#WATCHDOG SECURITY
|
#WATCHDOG SECURITY
|
||||||
# A last line of defense against 0-day drainage attacks
|
# A last line of defense against 0-day drainage attacks
|
||||||
|
|
|
||||||
865
package-lock.json
generated
865
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -65,7 +65,8 @@
|
||||||
"websocket-polyfill": "^0.0.3",
|
"websocket-polyfill": "^0.0.3",
|
||||||
"why-is-node-running": "^3.2.0",
|
"why-is-node-running": "^3.2.0",
|
||||||
"wrtc": "^0.4.7",
|
"wrtc": "^0.4.7",
|
||||||
"ws": "^8.18.0"
|
"ws": "^8.18.0",
|
||||||
|
"zip-a-folder": "^3.1.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "^4.3.4",
|
"@types/chai": "^4.3.4",
|
||||||
|
|
|
||||||
|
|
@ -255,6 +255,11 @@ The nostr server will send back a message response, and inside the body there wi
|
||||||
- input: [DebitOperation](#DebitOperation)
|
- input: [DebitOperation](#DebitOperation)
|
||||||
- This methods has an __empty__ __response__ body
|
- This methods has an __empty__ __response__ body
|
||||||
|
|
||||||
|
- ResetMetricsStorages
|
||||||
|
- auth type: __Metrics__
|
||||||
|
- This methods has an __empty__ __request__ body
|
||||||
|
- This methods has an __empty__ __response__ body
|
||||||
|
|
||||||
- RespondToDebit
|
- RespondToDebit
|
||||||
- auth type: __User__
|
- auth type: __User__
|
||||||
- input: [DebitResponse](#DebitResponse)
|
- input: [DebitResponse](#DebitResponse)
|
||||||
|
|
@ -295,6 +300,11 @@ The nostr server will send back a message response, and inside the body there wi
|
||||||
- This methods has an __empty__ __request__ body
|
- This methods has an __empty__ __request__ body
|
||||||
- output: [UserHealthState](#UserHealthState)
|
- output: [UserHealthState](#UserHealthState)
|
||||||
|
|
||||||
|
- ZipMetricsStorages
|
||||||
|
- auth type: __Metrics__
|
||||||
|
- This methods has an __empty__ __request__ body
|
||||||
|
- output: [ZippedMetrics](#ZippedMetrics)
|
||||||
|
|
||||||
# HTTP API DEFINITION
|
# HTTP API DEFINITION
|
||||||
|
|
||||||
## Supported HTTP Auths
|
## Supported HTTP Auths
|
||||||
|
|
@ -794,6 +804,13 @@ The nostr server will send back a message response, and inside the body there wi
|
||||||
- input: [DebitOperation](#DebitOperation)
|
- input: [DebitOperation](#DebitOperation)
|
||||||
- This methods has an __empty__ __response__ body
|
- This methods has an __empty__ __response__ body
|
||||||
|
|
||||||
|
- ResetMetricsStorages
|
||||||
|
- auth type: __Metrics__
|
||||||
|
- http method: __post__
|
||||||
|
- http route: __/api/metrics/reset__
|
||||||
|
- This methods has an __empty__ __request__ body
|
||||||
|
- This methods has an __empty__ __response__ body
|
||||||
|
|
||||||
- ResetNPubLinkingToken
|
- ResetNPubLinkingToken
|
||||||
- auth type: __App__
|
- auth type: __App__
|
||||||
- http method: __post__
|
- http method: __post__
|
||||||
|
|
@ -892,6 +909,13 @@ The nostr server will send back a message response, and inside the body there wi
|
||||||
- This methods has an __empty__ __request__ body
|
- This methods has an __empty__ __request__ body
|
||||||
- output: [UserHealthState](#UserHealthState)
|
- output: [UserHealthState](#UserHealthState)
|
||||||
|
|
||||||
|
- ZipMetricsStorages
|
||||||
|
- auth type: __Metrics__
|
||||||
|
- http method: __post__
|
||||||
|
- http route: __/api/metrics/zip__
|
||||||
|
- This methods has an __empty__ __request__ body
|
||||||
|
- output: [ZippedMetrics](#ZippedMetrics)
|
||||||
|
|
||||||
# INPUTS AND OUTPUTS
|
# INPUTS AND OUTPUTS
|
||||||
|
|
||||||
## Messages
|
## Messages
|
||||||
|
|
@ -1465,6 +1489,9 @@ The nostr server will send back a message response, and inside the body there wi
|
||||||
|
|
||||||
### WebRtcMessage
|
### WebRtcMessage
|
||||||
- __message__: _[WebRtcMessage_message](#WebRtcMessage_message)_
|
- __message__: _[WebRtcMessage_message](#WebRtcMessage_message)_
|
||||||
|
|
||||||
|
### ZippedMetrics
|
||||||
|
- __path__: _string_
|
||||||
## Enums
|
## Enums
|
||||||
### The enumerators used in the messages
|
### The enumerators used in the messages
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,7 @@ type Client struct {
|
||||||
PayInvoice func(req PayInvoiceRequest) (*PayInvoiceResponse, error)
|
PayInvoice func(req PayInvoiceRequest) (*PayInvoiceResponse, error)
|
||||||
RequestNPubLinkingToken func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error)
|
RequestNPubLinkingToken func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error)
|
||||||
ResetDebit func(req DebitOperation) error
|
ResetDebit func(req DebitOperation) error
|
||||||
|
ResetMetricsStorages func() error
|
||||||
ResetNPubLinkingToken func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error)
|
ResetNPubLinkingToken func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error)
|
||||||
RespondToDebit func(req DebitResponse) error
|
RespondToDebit func(req DebitResponse) error
|
||||||
SendAppUserToAppPayment func(req SendAppUserToAppPaymentRequest) error
|
SendAppUserToAppPayment func(req SendAppUserToAppPaymentRequest) error
|
||||||
|
|
@ -132,6 +133,7 @@ type Client struct {
|
||||||
UpdateUserOffer func(req OfferConfig) error
|
UpdateUserOffer func(req OfferConfig) error
|
||||||
UseInviteLink func(req UseInviteLinkRequest) error
|
UseInviteLink func(req UseInviteLinkRequest) error
|
||||||
UserHealth func() (*UserHealthState, error)
|
UserHealth func() (*UserHealthState, error)
|
||||||
|
ZipMetricsStorages func() (*ZippedMetrics, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(params ClientParams) *Client {
|
func NewClient(params ClientParams) *Client {
|
||||||
|
|
@ -1752,6 +1754,27 @@ func NewClient(params ClientParams) *Client {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
ResetMetricsStorages: func() error {
|
||||||
|
auth, err := params.RetrieveMetricsAuth()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
finalRoute := "/api/metrics/reset"
|
||||||
|
body := []byte{}
|
||||||
|
resBody, err := doPostRequest(params.BaseURL+finalRoute, body, auth)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
result := ResultError{}
|
||||||
|
err = json.Unmarshal(resBody, &result)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if result.Status == "ERROR" {
|
||||||
|
return fmt.Errorf(result.Reason)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
ResetNPubLinkingToken: func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error) {
|
ResetNPubLinkingToken: func(req RequestNPubLinkingTokenRequest) (*RequestNPubLinkingTokenResponse, error) {
|
||||||
auth, err := params.RetrieveAppAuth()
|
auth, err := params.RetrieveAppAuth()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -2082,5 +2105,31 @@ func NewClient(params ClientParams) *Client {
|
||||||
}
|
}
|
||||||
return &res, nil
|
return &res, nil
|
||||||
},
|
},
|
||||||
|
ZipMetricsStorages: func() (*ZippedMetrics, error) {
|
||||||
|
auth, err := params.RetrieveMetricsAuth()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
finalRoute := "/api/metrics/zip"
|
||||||
|
body := []byte{}
|
||||||
|
resBody, err := doPostRequest(params.BaseURL+finalRoute, body, auth)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := ResultError{}
|
||||||
|
err = json.Unmarshal(resBody, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if result.Status == "ERROR" {
|
||||||
|
return nil, fmt.Errorf(result.Reason)
|
||||||
|
}
|
||||||
|
res := ZippedMetrics{}
|
||||||
|
err = json.Unmarshal(resBody, &res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &res, nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -665,6 +665,9 @@ type WebRtcCandidate struct {
|
||||||
type WebRtcMessage struct {
|
type WebRtcMessage struct {
|
||||||
Message *WebRtcMessage_message `json:"message"`
|
Message *WebRtcMessage_message `json:"message"`
|
||||||
}
|
}
|
||||||
|
type ZippedMetrics struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
type DebitResponse_response_type string
|
type DebitResponse_response_type string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -1632,6 +1632,25 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
|
||||||
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
})
|
})
|
||||||
|
if (!opts.allowNotImplementedMethods && !methods.ResetMetricsStorages) throw new Error('method: ResetMetricsStorages is not implemented')
|
||||||
|
app.post('/api/metrics/reset', async (req, res) => {
|
||||||
|
const info: Types.RequestInfo = { rpcName: 'ResetMetricsStorages', batch: false, nostr: false, batchSize: 0}
|
||||||
|
const stats: Types.RequestStats = { startMs:req.startTimeMs || 0, start:req.startTime || 0n, parse: process.hrtime.bigint(), guard: 0n, validate: 0n, handle: 0n }
|
||||||
|
let authCtx: Types.AuthContext = {}
|
||||||
|
try {
|
||||||
|
if (!methods.ResetMetricsStorages) throw new Error('method: ResetMetricsStorages is not implemented')
|
||||||
|
const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
|
||||||
|
authCtx = authContext
|
||||||
|
stats.guard = process.hrtime.bigint()
|
||||||
|
stats.validate = stats.guard
|
||||||
|
const query = req.query
|
||||||
|
const params = req.params
|
||||||
|
await methods.ResetMetricsStorages({rpcName:'ResetMetricsStorages', ctx:authContext })
|
||||||
|
stats.handle = process.hrtime.bigint()
|
||||||
|
res.json({status: 'OK'})
|
||||||
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
|
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
|
})
|
||||||
if (!opts.allowNotImplementedMethods && !methods.ResetNPubLinkingToken) throw new Error('method: ResetNPubLinkingToken is not implemented')
|
if (!opts.allowNotImplementedMethods && !methods.ResetNPubLinkingToken) throw new Error('method: ResetNPubLinkingToken is not implemented')
|
||||||
app.post('/api/app/user/npub/token/reset', async (req, res) => {
|
app.post('/api/app/user/npub/token/reset', async (req, res) => {
|
||||||
const info: Types.RequestInfo = { rpcName: 'ResetNPubLinkingToken', batch: false, nostr: false, batchSize: 0}
|
const info: Types.RequestInfo = { rpcName: 'ResetNPubLinkingToken', batch: false, nostr: false, batchSize: 0}
|
||||||
|
|
@ -1915,6 +1934,25 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
|
||||||
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
})
|
})
|
||||||
|
if (!opts.allowNotImplementedMethods && !methods.ZipMetricsStorages) throw new Error('method: ZipMetricsStorages is not implemented')
|
||||||
|
app.post('/api/metrics/zip', async (req, res) => {
|
||||||
|
const info: Types.RequestInfo = { rpcName: 'ZipMetricsStorages', batch: false, nostr: false, batchSize: 0}
|
||||||
|
const stats: Types.RequestStats = { startMs:req.startTimeMs || 0, start:req.startTime || 0n, parse: process.hrtime.bigint(), guard: 0n, validate: 0n, handle: 0n }
|
||||||
|
let authCtx: Types.AuthContext = {}
|
||||||
|
try {
|
||||||
|
if (!methods.ZipMetricsStorages) throw new Error('method: ZipMetricsStorages is not implemented')
|
||||||
|
const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
|
||||||
|
authCtx = authContext
|
||||||
|
stats.guard = process.hrtime.bigint()
|
||||||
|
stats.validate = stats.guard
|
||||||
|
const query = req.query
|
||||||
|
const params = req.params
|
||||||
|
const response = await methods.ZipMetricsStorages({rpcName:'ZipMetricsStorages', ctx:authContext })
|
||||||
|
stats.handle = process.hrtime.bigint()
|
||||||
|
res.json({status: 'OK', ...response})
|
||||||
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
|
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
|
})
|
||||||
if (opts.staticFiles) {
|
if (opts.staticFiles) {
|
||||||
app.use(express.static(opts.staticFiles))
|
app.use(express.static(opts.staticFiles))
|
||||||
app.get('*', function (_, res) { res.sendFile('index.html', { root: opts.staticFiles })})
|
app.get('*', function (_, res) { res.sendFile('index.html', { root: opts.staticFiles })})
|
||||||
|
|
|
||||||
|
|
@ -839,6 +839,17 @@ export default (params: ClientParams) => ({
|
||||||
}
|
}
|
||||||
return { status: 'ERROR', reason: 'invalid response' }
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
},
|
},
|
||||||
|
ResetMetricsStorages: async (): Promise<ResultError | ({ status: 'OK' })> => {
|
||||||
|
const auth = await params.retrieveMetricsAuth()
|
||||||
|
if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
|
||||||
|
let finalRoute = '/api/metrics/reset'
|
||||||
|
const { data } = await axios.post(params.baseUrl + finalRoute, {}, { headers: { 'authorization': auth } })
|
||||||
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
||||||
|
if (data.status === 'OK') {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
|
},
|
||||||
ResetNPubLinkingToken: async (request: Types.RequestNPubLinkingTokenRequest): Promise<ResultError | ({ status: 'OK' }& Types.RequestNPubLinkingTokenResponse)> => {
|
ResetNPubLinkingToken: async (request: Types.RequestNPubLinkingTokenRequest): Promise<ResultError | ({ status: 'OK' }& Types.RequestNPubLinkingTokenResponse)> => {
|
||||||
const auth = await params.retrieveAppAuth()
|
const auth = await params.retrieveAppAuth()
|
||||||
if (auth === null) throw new Error('retrieveAppAuth() returned null')
|
if (auth === null) throw new Error('retrieveAppAuth() returned null')
|
||||||
|
|
@ -995,4 +1006,18 @@ export default (params: ClientParams) => ({
|
||||||
}
|
}
|
||||||
return { status: 'ERROR', reason: 'invalid response' }
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
},
|
},
|
||||||
|
ZipMetricsStorages: async (): Promise<ResultError | ({ status: 'OK' }& Types.ZippedMetrics)> => {
|
||||||
|
const auth = await params.retrieveMetricsAuth()
|
||||||
|
if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
|
||||||
|
let finalRoute = '/api/metrics/zip'
|
||||||
|
const { data } = await axios.post(params.baseUrl + finalRoute, {}, { headers: { 'authorization': auth } })
|
||||||
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
||||||
|
if (data.status === 'OK') {
|
||||||
|
const result = data
|
||||||
|
if(!params.checkResult) return { status: 'OK', ...result }
|
||||||
|
const error = Types.ZippedMetricsValidate(result)
|
||||||
|
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
|
||||||
|
}
|
||||||
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -698,6 +698,17 @@ export default (params: NostrClientParams, send: (to:string, message: NostrRequ
|
||||||
}
|
}
|
||||||
return { status: 'ERROR', reason: 'invalid response' }
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
},
|
},
|
||||||
|
ResetMetricsStorages: async (): Promise<ResultError | ({ status: 'OK' })> => {
|
||||||
|
const auth = await params.retrieveNostrMetricsAuth()
|
||||||
|
if (auth === null) throw new Error('retrieveNostrMetricsAuth() returned null')
|
||||||
|
const nostrRequest: NostrRequest = {}
|
||||||
|
const data = await send(params.pubDestination, {rpcName:'ResetMetricsStorages',authIdentifier:auth, ...nostrRequest })
|
||||||
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
||||||
|
if (data.status === 'OK') {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
|
},
|
||||||
RespondToDebit: async (request: Types.DebitResponse): Promise<ResultError | ({ status: 'OK' })> => {
|
RespondToDebit: async (request: Types.DebitResponse): Promise<ResultError | ({ status: 'OK' })> => {
|
||||||
const auth = await params.retrieveNostrUserAuth()
|
const auth = await params.retrieveNostrUserAuth()
|
||||||
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')
|
if (auth === null) throw new Error('retrieveNostrUserAuth() returned null')
|
||||||
|
|
@ -805,4 +816,18 @@ export default (params: NostrClientParams, send: (to:string, message: NostrRequ
|
||||||
}
|
}
|
||||||
return { status: 'ERROR', reason: 'invalid response' }
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
},
|
},
|
||||||
|
ZipMetricsStorages: async (): Promise<ResultError | ({ status: 'OK' }& Types.ZippedMetrics)> => {
|
||||||
|
const auth = await params.retrieveNostrMetricsAuth()
|
||||||
|
if (auth === null) throw new Error('retrieveNostrMetricsAuth() returned null')
|
||||||
|
const nostrRequest: NostrRequest = {}
|
||||||
|
const data = await send(params.pubDestination, {rpcName:'ZipMetricsStorages',authIdentifier:auth, ...nostrRequest })
|
||||||
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
||||||
|
if (data.status === 'OK') {
|
||||||
|
const result = data
|
||||||
|
if(!params.checkResult) return { status: 'OK', ...result }
|
||||||
|
const error = Types.ZippedMetricsValidate(result)
|
||||||
|
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
|
||||||
|
}
|
||||||
|
return { status: 'ERROR', reason: 'invalid response' }
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1091,6 +1091,19 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
|
||||||
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
break
|
break
|
||||||
|
case 'ResetMetricsStorages':
|
||||||
|
try {
|
||||||
|
if (!methods.ResetMetricsStorages) throw new Error('method: ResetMetricsStorages is not implemented')
|
||||||
|
const authContext = await opts.NostrMetricsAuthGuard(req.appId, req.authIdentifier)
|
||||||
|
stats.guard = process.hrtime.bigint()
|
||||||
|
authCtx = authContext
|
||||||
|
stats.validate = stats.guard
|
||||||
|
await methods.ResetMetricsStorages({rpcName:'ResetMetricsStorages', ctx:authContext })
|
||||||
|
stats.handle = process.hrtime.bigint()
|
||||||
|
res({status: 'OK'})
|
||||||
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
|
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
|
break
|
||||||
case 'RespondToDebit':
|
case 'RespondToDebit':
|
||||||
try {
|
try {
|
||||||
if (!methods.RespondToDebit) throw new Error('method: RespondToDebit is not implemented')
|
if (!methods.RespondToDebit) throw new Error('method: RespondToDebit is not implemented')
|
||||||
|
|
@ -1213,6 +1226,19 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
|
||||||
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
break
|
break
|
||||||
|
case 'ZipMetricsStorages':
|
||||||
|
try {
|
||||||
|
if (!methods.ZipMetricsStorages) throw new Error('method: ZipMetricsStorages is not implemented')
|
||||||
|
const authContext = await opts.NostrMetricsAuthGuard(req.appId, req.authIdentifier)
|
||||||
|
stats.guard = process.hrtime.bigint()
|
||||||
|
authCtx = authContext
|
||||||
|
stats.validate = stats.guard
|
||||||
|
const response = await methods.ZipMetricsStorages({rpcName:'ZipMetricsStorages', ctx:authContext })
|
||||||
|
stats.handle = process.hrtime.bigint()
|
||||||
|
res({status: 'OK', ...response})
|
||||||
|
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
|
||||||
|
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
|
||||||
|
break
|
||||||
default: logger.error('unknown rpc call name from nostr event:'+req.rpcName)
|
default: logger.error('unknown rpc call name from nostr event:'+req.rpcName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,8 @@ export type MetricsContext = {
|
||||||
app_id: string
|
app_id: string
|
||||||
operator_id: string
|
operator_id: string
|
||||||
}
|
}
|
||||||
export type MetricsMethodInputs = GetAppsMetrics_Input | GetBundleMetrics_Input | GetErrorStats_Input | GetLndMetrics_Input | GetSingleBundleMetrics_Input | GetSingleUsageMetrics_Input | GetUsageMetrics_Input | SubmitWebRtcMessage_Input
|
export type MetricsMethodInputs = GetAppsMetrics_Input | GetBundleMetrics_Input | GetErrorStats_Input | GetLndMetrics_Input | GetSingleBundleMetrics_Input | GetSingleUsageMetrics_Input | GetUsageMetrics_Input | ResetMetricsStorages_Input | SubmitWebRtcMessage_Input | ZipMetricsStorages_Input
|
||||||
export type MetricsMethodOutputs = GetAppsMetrics_Output | GetBundleMetrics_Output | GetErrorStats_Output | GetLndMetrics_Output | GetSingleBundleMetrics_Output | GetSingleUsageMetrics_Output | GetUsageMetrics_Output | SubmitWebRtcMessage_Output
|
export type MetricsMethodOutputs = GetAppsMetrics_Output | GetBundleMetrics_Output | GetErrorStats_Output | GetLndMetrics_Output | GetSingleBundleMetrics_Output | GetSingleUsageMetrics_Output | GetUsageMetrics_Output | ResetMetricsStorages_Output | SubmitWebRtcMessage_Output | ZipMetricsStorages_Output
|
||||||
export type UserContext = {
|
export type UserContext = {
|
||||||
app_id: string
|
app_id: string
|
||||||
app_user_id: string
|
app_user_id: string
|
||||||
|
|
@ -253,6 +253,9 @@ export type RequestNPubLinkingToken_Output = ResultError | ({ status: 'OK' } & R
|
||||||
export type ResetDebit_Input = {rpcName:'ResetDebit', req: DebitOperation}
|
export type ResetDebit_Input = {rpcName:'ResetDebit', req: DebitOperation}
|
||||||
export type ResetDebit_Output = ResultError | { status: 'OK' }
|
export type ResetDebit_Output = ResultError | { status: 'OK' }
|
||||||
|
|
||||||
|
export type ResetMetricsStorages_Input = {rpcName:'ResetMetricsStorages'}
|
||||||
|
export type ResetMetricsStorages_Output = ResultError | { status: 'OK' }
|
||||||
|
|
||||||
export type ResetNPubLinkingToken_Input = {rpcName:'ResetNPubLinkingToken', req: RequestNPubLinkingTokenRequest}
|
export type ResetNPubLinkingToken_Input = {rpcName:'ResetNPubLinkingToken', req: RequestNPubLinkingTokenRequest}
|
||||||
export type ResetNPubLinkingToken_Output = ResultError | ({ status: 'OK' } & RequestNPubLinkingTokenResponse)
|
export type ResetNPubLinkingToken_Output = ResultError | ({ status: 'OK' } & RequestNPubLinkingTokenResponse)
|
||||||
|
|
||||||
|
|
@ -295,6 +298,9 @@ export type UseInviteLink_Output = ResultError | { status: 'OK' }
|
||||||
export type UserHealth_Input = {rpcName:'UserHealth'}
|
export type UserHealth_Input = {rpcName:'UserHealth'}
|
||||||
export type UserHealth_Output = ResultError | ({ status: 'OK' } & UserHealthState)
|
export type UserHealth_Output = ResultError | ({ status: 'OK' } & UserHealthState)
|
||||||
|
|
||||||
|
export type ZipMetricsStorages_Input = {rpcName:'ZipMetricsStorages'}
|
||||||
|
export type ZipMetricsStorages_Output = ResultError | ({ status: 'OK' } & ZippedMetrics)
|
||||||
|
|
||||||
export type ServerMethods = {
|
export type ServerMethods = {
|
||||||
AddApp?: (req: AddApp_Input & {ctx: AdminContext }) => Promise<AuthApp>
|
AddApp?: (req: AddApp_Input & {ctx: AdminContext }) => Promise<AuthApp>
|
||||||
AddAppInvoice?: (req: AddAppInvoice_Input & {ctx: AppContext }) => Promise<NewInvoiceResponse>
|
AddAppInvoice?: (req: AddAppInvoice_Input & {ctx: AppContext }) => Promise<NewInvoiceResponse>
|
||||||
|
|
@ -359,6 +365,7 @@ export type ServerMethods = {
|
||||||
PayInvoice?: (req: PayInvoice_Input & {ctx: UserContext }) => Promise<PayInvoiceResponse>
|
PayInvoice?: (req: PayInvoice_Input & {ctx: UserContext }) => Promise<PayInvoiceResponse>
|
||||||
RequestNPubLinkingToken?: (req: RequestNPubLinkingToken_Input & {ctx: AppContext }) => Promise<RequestNPubLinkingTokenResponse>
|
RequestNPubLinkingToken?: (req: RequestNPubLinkingToken_Input & {ctx: AppContext }) => Promise<RequestNPubLinkingTokenResponse>
|
||||||
ResetDebit?: (req: ResetDebit_Input & {ctx: UserContext }) => Promise<void>
|
ResetDebit?: (req: ResetDebit_Input & {ctx: UserContext }) => Promise<void>
|
||||||
|
ResetMetricsStorages?: (req: ResetMetricsStorages_Input & {ctx: MetricsContext }) => Promise<void>
|
||||||
ResetNPubLinkingToken?: (req: ResetNPubLinkingToken_Input & {ctx: AppContext }) => Promise<RequestNPubLinkingTokenResponse>
|
ResetNPubLinkingToken?: (req: ResetNPubLinkingToken_Input & {ctx: AppContext }) => Promise<RequestNPubLinkingTokenResponse>
|
||||||
RespondToDebit?: (req: RespondToDebit_Input & {ctx: UserContext }) => Promise<void>
|
RespondToDebit?: (req: RespondToDebit_Input & {ctx: UserContext }) => Promise<void>
|
||||||
SendAppUserToAppPayment?: (req: SendAppUserToAppPayment_Input & {ctx: AppContext }) => Promise<void>
|
SendAppUserToAppPayment?: (req: SendAppUserToAppPayment_Input & {ctx: AppContext }) => Promise<void>
|
||||||
|
|
@ -373,6 +380,7 @@ export type ServerMethods = {
|
||||||
UpdateUserOffer?: (req: UpdateUserOffer_Input & {ctx: UserContext }) => Promise<void>
|
UpdateUserOffer?: (req: UpdateUserOffer_Input & {ctx: UserContext }) => Promise<void>
|
||||||
UseInviteLink?: (req: UseInviteLink_Input & {ctx: GuestWithPubContext }) => Promise<void>
|
UseInviteLink?: (req: UseInviteLink_Input & {ctx: GuestWithPubContext }) => Promise<void>
|
||||||
UserHealth?: (req: UserHealth_Input & {ctx: UserContext }) => Promise<UserHealthState>
|
UserHealth?: (req: UserHealth_Input & {ctx: UserContext }) => Promise<UserHealthState>
|
||||||
|
ZipMetricsStorages?: (req: ZipMetricsStorages_Input & {ctx: MetricsContext }) => Promise<ZippedMetrics>
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum AddressType {
|
export enum AddressType {
|
||||||
|
|
@ -3816,6 +3824,24 @@ export const WebRtcMessageValidate = (o?: WebRtcMessage, opts: WebRtcMessageOpti
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ZippedMetrics = {
|
||||||
|
path: string
|
||||||
|
}
|
||||||
|
export const ZippedMetricsOptionalFields: [] = []
|
||||||
|
export type ZippedMetricsOptions = OptionsBaseMessage & {
|
||||||
|
checkOptionalsAreSet?: []
|
||||||
|
path_CustomCheck?: (v: string) => boolean
|
||||||
|
}
|
||||||
|
export const ZippedMetricsValidate = (o?: ZippedMetrics, opts: ZippedMetricsOptions = {}, path: string = 'ZippedMetrics::root.'): Error | null => {
|
||||||
|
if (opts.checkOptionalsAreSet && opts.allOptionalsAreSet) return new Error(path + ': only one of checkOptionalsAreSet or allOptionalNonDefault can be set for each message')
|
||||||
|
if (typeof o !== 'object' || o === null) return new Error(path + ': object is not an instance of an object or is null')
|
||||||
|
|
||||||
|
if (typeof o.path !== 'string') return new Error(`${path}.path: is not a string`)
|
||||||
|
if (opts.path_CustomCheck && !opts.path_CustomCheck(o.path)) return new Error(`${path}.path: custom check failed`)
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
export enum DebitResponse_response_type {
|
export enum DebitResponse_response_type {
|
||||||
DENIED = 'denied',
|
DENIED = 'denied',
|
||||||
INVOICE = 'invoice',
|
INVOICE = 'invoice',
|
||||||
|
|
|
||||||
|
|
@ -234,6 +234,20 @@ service LightningPub {
|
||||||
option (nostr) = true;
|
option (nostr) = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rpc ZipMetricsStorages(structs.Empty) returns (structs.ZippedMetrics) {
|
||||||
|
option (auth_type) = "Metrics";
|
||||||
|
option (http_method) = "post";
|
||||||
|
option (http_route) = "/api/metrics/zip";
|
||||||
|
option (nostr) = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
rpc ResetMetricsStorages(structs.Empty) returns (structs.Empty) {
|
||||||
|
option (auth_type) = "Metrics";
|
||||||
|
option (http_method) = "post";
|
||||||
|
option (http_route) = "/api/metrics/reset";
|
||||||
|
option (nostr) = true;
|
||||||
|
}
|
||||||
|
|
||||||
rpc CreateOneTimeInviteLink(structs.CreateOneTimeInviteLinkRequest) returns (structs.CreateOneTimeInviteLinkResponse) {
|
rpc CreateOneTimeInviteLink(structs.CreateOneTimeInviteLinkRequest) returns (structs.CreateOneTimeInviteLinkResponse) {
|
||||||
option (auth_type) = "Admin";
|
option (auth_type) = "Admin";
|
||||||
option (http_method) = "post";
|
option (http_method) = "post";
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ message LndSeed {
|
||||||
repeated string seed = 1;
|
repeated string seed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message ZippedMetrics {
|
||||||
|
string path = 1;
|
||||||
|
}
|
||||||
|
|
||||||
message EncryptionExchangeRequest {
|
message EncryptionExchangeRequest {
|
||||||
string publicKey = 1;
|
string publicKey = 1;
|
||||||
string deviceId = 2;
|
string deviceId = 2;
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,15 @@ import { NostrSend } from "../nostr/handler.js";
|
||||||
import { ProcessMetricsCollector } from "../storage/tlv/processMetricsCollector.js";
|
import { ProcessMetricsCollector } from "../storage/tlv/processMetricsCollector.js";
|
||||||
type UtilsSettings = {
|
type UtilsSettings = {
|
||||||
noCollector?: boolean
|
noCollector?: boolean
|
||||||
dataDir: string
|
dataDir: string,
|
||||||
|
allowResetMetricsStorages: boolean
|
||||||
}
|
}
|
||||||
export class Utils {
|
export class Utils {
|
||||||
tlvStorageFactory: TlvStorageFactory
|
tlvStorageFactory: TlvStorageFactory
|
||||||
stateBundler: StateBundler
|
stateBundler: StateBundler
|
||||||
settings: MainSettings
|
|
||||||
_nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
_nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
||||||
constructor({ noCollector, dataDir }: UtilsSettings) {
|
constructor({ noCollector, dataDir, allowResetMetricsStorages }: UtilsSettings) {
|
||||||
this.tlvStorageFactory = new TlvStorageFactory()
|
this.tlvStorageFactory = new TlvStorageFactory(allowResetMetricsStorages)
|
||||||
this.stateBundler = new StateBundler(dataDir, this.tlvStorageFactory)
|
this.stateBundler = new StateBundler(dataDir, this.tlvStorageFactory)
|
||||||
if (!noCollector) {
|
if (!noCollector) {
|
||||||
new ProcessMetricsCollector((metrics) => {
|
new ProcessMetricsCollector((metrics) => {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export type AppData = {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings) => {
|
export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings) => {
|
||||||
const utils = new Utils({ dataDir: mainSettings.storageSettings.dataDir })
|
const utils = new Utils({ dataDir: mainSettings.storageSettings.dataDir, allowResetMetricsStorages: mainSettings.allowResetMetricsStorages })
|
||||||
const storageManager = new Storage(mainSettings.storageSettings, utils)
|
const storageManager = new Storage(mainSettings.storageSettings, utils)
|
||||||
await storageManager.Connect(log)
|
await storageManager.Connect(log)
|
||||||
/* const manualMigration = await TypeOrmMigrationRunner(log, storageManager, mainSettings.storageSettings.dbSettings, process.argv[2])
|
/* const manualMigration = await TypeOrmMigrationRunner(log, storageManager, mainSettings.storageSettings.dbSettings, process.argv[2])
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ export default class SanityChecker {
|
||||||
return { type: fullData, data: fullData }
|
return { type: fullData, data: fullData }
|
||||||
} else if (LN_INVOICE_REGEX.test(fullData)) {
|
} else if (LN_INVOICE_REGEX.test(fullData)) {
|
||||||
return { type: 'invoice', data: fullData }
|
return { type: 'invoice', data: fullData }
|
||||||
} else if (BITCOIN_ADDRESS_REGEX.test(fullData)) {
|
} else if (BITCOIN_ADDRESS_REGEX.test(fullData) || fullData.startsWith("bcrt1")) {
|
||||||
return { type: 'address', data: fullData }
|
return { type: 'address', data: fullData }
|
||||||
} else {
|
} else {
|
||||||
return { type: 'u2u', data: fullData }
|
return { type: 'u2u', data: fullData }
|
||||||
|
|
@ -45,7 +45,7 @@ export default class SanityChecker {
|
||||||
const [prefix, data] = parts
|
const [prefix, data] = parts
|
||||||
if (prefix === 'routing_fee_refund' || prefix === 'payment_refund') {
|
if (prefix === 'routing_fee_refund' || prefix === 'payment_refund') {
|
||||||
return { type: prefix, data }
|
return { type: prefix, data }
|
||||||
} else if (BITCOIN_ADDRESS_REGEX.test(prefix)) {
|
} else if (BITCOIN_ADDRESS_REGEX.test(prefix) || prefix.startsWith("bcrt1")) {
|
||||||
return { type: 'address', data: prefix, txHash: data }
|
return { type: 'address', data: prefix, txHash: data }
|
||||||
} else {
|
} else {
|
||||||
return { type: 'u2u', data: prefix, serialId: +data }
|
return { type: 'u2u', data: prefix, serialId: +data }
|
||||||
|
|
@ -236,6 +236,7 @@ export default class SanityChecker {
|
||||||
this.decrementEvents = {}
|
this.decrementEvents = {}
|
||||||
for (let i = 0; i < this.events.length; i++) {
|
for (let i = 0; i < this.events.length; i++) {
|
||||||
const e = this.events[i]
|
const e = this.events[i]
|
||||||
|
this.log("checking event", e.type, e.data)
|
||||||
if (e.type === 'balance_decrement') {
|
if (e.type === 'balance_decrement') {
|
||||||
await this.verifyDecrementEvent(e)
|
await this.verifyDecrementEvent(e)
|
||||||
} else if (e.type === 'balance_increment') {
|
} else if (e.type === 'balance_increment') {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,8 @@ export type MainSettings = {
|
||||||
defaultAppName: string
|
defaultAppName: string
|
||||||
pushBackupsToNostr: boolean
|
pushBackupsToNostr: boolean
|
||||||
lnurlMetaText: string,
|
lnurlMetaText: string,
|
||||||
bridgeUrl: string
|
bridgeUrl: string,
|
||||||
|
allowResetMetricsStorages: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BitcoinCoreSettings = {
|
export type BitcoinCoreSettings = {
|
||||||
|
|
@ -74,7 +75,8 @@ export const LoadMainSettingsFromEnv = (): MainSettings => {
|
||||||
defaultAppName: process.env.DEFAULT_APP_NAME || "wallet",
|
defaultAppName: process.env.DEFAULT_APP_NAME || "wallet",
|
||||||
pushBackupsToNostr: process.env.PUSH_BACKUPS_TO_NOSTR === 'true' || false,
|
pushBackupsToNostr: process.env.PUSH_BACKUPS_TO_NOSTR === 'true' || false,
|
||||||
lnurlMetaText: process.env.LNURL_META_TEXT || "LNURL via Lightning.pub",
|
lnurlMetaText: process.env.LNURL_META_TEXT || "LNURL via Lightning.pub",
|
||||||
bridgeUrl: process.env.BRIDGE_URL || "https://shockwallet.app"
|
bridgeUrl: process.env.BRIDGE_URL || "https://shockwallet.app",
|
||||||
|
allowResetMetricsStorages: process.env.ALLOW_RESET_METRICS_STORAGES === 'true' || false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,13 @@ export default (mainHandler: Main): Types.ServerMethods => {
|
||||||
GetLndMetrics: async ({ ctx, req }) => {
|
GetLndMetrics: async ({ ctx, req }) => {
|
||||||
return mainHandler.metricsManager.GetLndMetrics(req)
|
return mainHandler.metricsManager.GetLndMetrics(req)
|
||||||
},
|
},
|
||||||
|
ResetMetricsStorages: async ({ ctx }) => {
|
||||||
|
return mainHandler.utils.tlvStorageFactory.ResetStorages()
|
||||||
|
},
|
||||||
|
ZipMetricsStorages: async ({ ctx }) => {
|
||||||
|
const path = await mainHandler.utils.tlvStorageFactory.ZipStorages()
|
||||||
|
return { path }
|
||||||
|
},
|
||||||
ListChannels: async ({ ctx }) => {
|
ListChannels: async ({ ctx }) => {
|
||||||
return mainHandler.adminManager.ListChannels()
|
return mainHandler.adminManager.ListChannels()
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -5,20 +5,28 @@ export type ProcessMetrics = {
|
||||||
memory_heap_total_kb?: number
|
memory_heap_total_kb?: number
|
||||||
memory_heap_used_kb?: number
|
memory_heap_used_kb?: number
|
||||||
memory_external_kb?: number
|
memory_external_kb?: number
|
||||||
|
cpu_user_ms?: number
|
||||||
|
cpu_system_ms?: number
|
||||||
}
|
}
|
||||||
export class ProcessMetricsCollector {
|
export class ProcessMetricsCollector {
|
||||||
reportLog = getLogger({ component: 'ProcessMetricsCollector' })
|
reportLog = getLogger({ component: 'ProcessMetricsCollector' })
|
||||||
prevValues: Record<string, number> = {}
|
prevValues: Record<string, number> = {}
|
||||||
interval: NodeJS.Timeout
|
interval: NodeJS.Timeout
|
||||||
|
cpuUsage: { user: number, system: number }
|
||||||
constructor(cb: (metrics: ProcessMetrics) => void) {
|
constructor(cb: (metrics: ProcessMetrics) => void) {
|
||||||
|
this.cpuUsage = process.cpuUsage()
|
||||||
this.interval = setInterval(() => {
|
this.interval = setInterval(() => {
|
||||||
const mem = process.memoryUsage()
|
const mem = process.memoryUsage()
|
||||||
|
const diff = process.cpuUsage(this.cpuUsage)
|
||||||
|
this.cpuUsage = process.cpuUsage()
|
||||||
const metrics: ProcessMetrics = {
|
const metrics: ProcessMetrics = {
|
||||||
memory_rss_kb: this.AddValue('memory_rss_kb', Math.ceil(mem.rss / 1000 || 0), true),
|
memory_rss_kb: this.AddValue('memory_rss_kb', Math.ceil(mem.rss / 1000 || 0), true),
|
||||||
memory_buffer_kb: this.AddValue('memory_buffer_kb', Math.ceil(mem.arrayBuffers / 1000 || 0), true),
|
memory_buffer_kb: this.AddValue('memory_buffer_kb', Math.ceil(mem.arrayBuffers / 1000 || 0), true),
|
||||||
memory_heap_total_kb: this.AddValue('memory_heap_total_kb', Math.ceil(mem.heapTotal / 1000 || 0), true),
|
memory_heap_total_kb: this.AddValue('memory_heap_total_kb', Math.ceil(mem.heapTotal / 1000 || 0), true),
|
||||||
memory_heap_used_kb: this.AddValue('memory_heap_used_kb', Math.ceil(mem.heapUsed / 1000 || 0), true),
|
memory_heap_used_kb: this.AddValue('memory_heap_used_kb', Math.ceil(mem.heapUsed / 1000 || 0), true),
|
||||||
memory_external_kb: this.AddValue('memory_external_kb', Math.ceil(mem.external / 1000 || 0), true),
|
memory_external_kb: this.AddValue('memory_external_kb', Math.ceil(mem.external / 1000 || 0), true),
|
||||||
|
cpu_user_ms: this.AddValue('cpu_user_ms', Math.ceil(diff.user / 1000), true),
|
||||||
|
cpu_system_ms: this.AddValue('cpu_system_ms', Math.ceil(diff.system / 1000), true),
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(metrics)
|
cb(metrics)
|
||||||
|
|
|
||||||
|
|
@ -9,20 +9,38 @@ export class TlvFilesStorage {
|
||||||
private meta: Record<string, Record<string, { chunks: number[] }>> = {}
|
private meta: Record<string, Record<string, { chunks: number[] }>> = {}
|
||||||
private pending: Record<string, Record<string, { tlvs: Uint8Array[] }>> = {}
|
private pending: Record<string, Record<string, { tlvs: Uint8Array[] }>> = {}
|
||||||
private metaReady = false
|
private metaReady = false
|
||||||
|
private interval: NodeJS.Timeout
|
||||||
constructor(storagePath: string) {
|
constructor(storagePath: string) {
|
||||||
this.storagePath = storagePath
|
this.storagePath = storagePath
|
||||||
if (!fs.existsSync(this.storagePath)) {
|
if (!fs.existsSync(this.storagePath)) {
|
||||||
fs.mkdirSync(this.storagePath, { recursive: true });
|
fs.mkdirSync(this.storagePath, { recursive: true });
|
||||||
}
|
}
|
||||||
|
this.init()
|
||||||
|
process.on('exit', () => {
|
||||||
|
this.persist()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
GetStoragePath = () => {
|
||||||
|
return this.storagePath
|
||||||
|
}
|
||||||
|
|
||||||
|
init = () => {
|
||||||
this.initMeta()
|
this.initMeta()
|
||||||
setInterval(() => {
|
this.interval = setInterval(() => {
|
||||||
if (Date.now() - this.lastPersisted > 1000 * 60 * 4) {
|
if (Date.now() - this.lastPersisted > 1000 * 60 * 4) {
|
||||||
this.persist()
|
this.persist()
|
||||||
}
|
}
|
||||||
}, 1000 * 60 * 5)
|
}, 1000 * 60 * 5)
|
||||||
process.on('exit', () => {
|
}
|
||||||
this.persist()
|
|
||||||
});
|
Reset = () => {
|
||||||
|
if (this.storagePath === "" && this.storagePath.startsWith("/")) {
|
||||||
|
throw new Error("cannot delete root storage path")
|
||||||
|
}
|
||||||
|
clearInterval(this.interval)
|
||||||
|
fs.rmSync(this.storagePath, { recursive: true, force: true })
|
||||||
|
this.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
LoadFile = (app: string, dataName: string, chunk: number): TlvFile => {
|
LoadFile = (app: string, dataName: string, chunk: number): TlvFile => {
|
||||||
|
|
@ -72,6 +90,10 @@ export class TlvFilesStorage {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PersistNow = () => {
|
||||||
|
this.persist()
|
||||||
|
}
|
||||||
|
|
||||||
private persist = () => {
|
private persist = () => {
|
||||||
if (!this.metaReady) {
|
if (!this.metaReady) {
|
||||||
throw new Error("meta metrics not ready")
|
throw new Error("meta metrics not ready")
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { ChildProcess, fork } from 'child_process';
|
import { ChildProcess, fork } from 'child_process';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { AddTlvOperation, ITlvStorageOperation, SuccessTlvOperationResponse, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, SerializableLatestData, SerializableTlvFile, TlvOperationResponse, TlvStorageSettings, WebRtcMessageOperation, ProcessMetricsTlvOperation } from './tlvFilesStorageProcessor';
|
import { AddTlvOperation, ITlvStorageOperation, SuccessTlvOperationResponse, LoadLatestTlvOperation, LoadTlvFileOperation, NewTlvStorageOperation, SerializableLatestData, SerializableTlvFile, TlvOperationResponse, TlvStorageSettings, WebRtcMessageOperation, ProcessMetricsTlvOperation, ZipStoragesOperation, ResetTlvStorageOperation } from './tlvFilesStorageProcessor';
|
||||||
import { LatestData, TlvFile } from './tlvFilesStorage';
|
import { LatestData, TlvFile } from './tlvFilesStorage';
|
||||||
import { NostrSend, SendData, SendInitiator } from '../../nostr/handler';
|
import { NostrSend, SendData, SendInitiator } from '../../nostr/handler';
|
||||||
import { WebRtcUserInfo } from '../../webRTC';
|
import { WebRtcUserInfo } from '../../webRTC';
|
||||||
|
|
@ -17,8 +17,10 @@ export class TlvStorageFactory extends EventEmitter {
|
||||||
private isConnected: boolean = false;
|
private isConnected: boolean = false;
|
||||||
private debug: boolean = false;
|
private debug: boolean = false;
|
||||||
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
||||||
constructor() {
|
private allowResetMetricsStorages: boolean
|
||||||
|
constructor(allowResetMetricsStorages: boolean) {
|
||||||
super();
|
super();
|
||||||
|
this.allowResetMetricsStorages = allowResetMetricsStorages
|
||||||
this.initializeSubprocess();
|
this.initializeSubprocess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,6 +63,21 @@ export class TlvStorageFactory extends EventEmitter {
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ZipStorages(): Promise<string> {
|
||||||
|
const opId = Math.random().toString()
|
||||||
|
const op: ZipStoragesOperation = { type: 'zipStorages', opId }
|
||||||
|
return this.handleOp<string>(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
ResetStorages(): Promise<void> {
|
||||||
|
if (!this.allowResetMetricsStorages) {
|
||||||
|
throw new Error('Resetting metrics storages is not allowed')
|
||||||
|
}
|
||||||
|
const opId = Math.random().toString()
|
||||||
|
const op: ResetTlvStorageOperation = { type: 'resetStorage', opId }
|
||||||
|
return this.handleOp<void>(op)
|
||||||
|
}
|
||||||
|
|
||||||
NewStorage(settings: TlvStorageSettings): TlvStorageInterface {
|
NewStorage(settings: TlvStorageSettings): TlvStorageInterface {
|
||||||
const opId = Math.random().toString()
|
const opId = Math.random().toString()
|
||||||
const op: NewTlvStorageOperation = { type: 'newStorage', opId, settings }
|
const op: NewTlvStorageOperation = { type: 'newStorage', opId, settings }
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import { SendData } from '../../nostr/handler.js';
|
||||||
import { SendInitiator } from '../../nostr/handler.js';
|
import { SendInitiator } from '../../nostr/handler.js';
|
||||||
import { ProcessMetrics, ProcessMetricsCollector } from './processMetricsCollector.js';
|
import { ProcessMetrics, ProcessMetricsCollector } from './processMetricsCollector.js';
|
||||||
import { integerToUint8Array } from '../../helpers/tlv.js';
|
import { integerToUint8Array } from '../../helpers/tlv.js';
|
||||||
|
import { zip } from 'zip-a-folder'
|
||||||
|
import crypto from 'crypto'
|
||||||
export type SerializableLatestData = Record<string, Record<string, { base64tlvs: string[], current_chunk: number, available_chunks: number[] }>>
|
export type SerializableLatestData = Record<string, Record<string, { base64tlvs: string[], current_chunk: number, available_chunks: number[] }>>
|
||||||
export type SerializableTlvFile = { base64fileData: string, chunks: number[] }
|
export type SerializableTlvFile = { base64fileData: string, chunks: number[] }
|
||||||
export const usageStorageName = 'usage'
|
export const usageStorageName = 'usage'
|
||||||
|
|
@ -15,6 +17,18 @@ export type TlvStorageSettings = {
|
||||||
name: typeof usageStorageName | typeof bundlerStorageName
|
name: typeof usageStorageName | typeof bundlerStorageName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ZipStoragesOperation = {
|
||||||
|
type: 'zipStorages'
|
||||||
|
opId: string
|
||||||
|
debug?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResetTlvStorageOperation = {
|
||||||
|
type: 'resetStorage'
|
||||||
|
opId: string
|
||||||
|
debug?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export type NewTlvStorageOperation = {
|
export type NewTlvStorageOperation = {
|
||||||
type: 'newStorage'
|
type: 'newStorage'
|
||||||
opId: string
|
opId: string
|
||||||
|
|
@ -74,7 +88,7 @@ export interface ITlvStorageOperation {
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TlvStorageOperation = NewTlvStorageOperation | AddTlvOperation | LoadLatestTlvOperation | LoadTlvFileOperation | WebRtcMessageOperation | ProcessMetricsTlvOperation
|
export type TlvStorageOperation = NewTlvStorageOperation | AddTlvOperation | LoadLatestTlvOperation | LoadTlvFileOperation | WebRtcMessageOperation | ProcessMetricsTlvOperation | ResetTlvStorageOperation | ZipStoragesOperation
|
||||||
|
|
||||||
export type SuccessTlvOperationResponse<T> = { success: true, type: string, data: T, opId: string }
|
export type SuccessTlvOperationResponse<T> = { success: true, type: string, data: T, opId: string }
|
||||||
export type TlvOperationResponse<T> = SuccessTlvOperationResponse<T> | ErrorTlvOperationResponse
|
export type TlvOperationResponse<T> = SuccessTlvOperationResponse<T> | ErrorTlvOperationResponse
|
||||||
|
|
@ -132,11 +146,15 @@ class TlvFilesStorageProcessor {
|
||||||
console.log('no bundle storage yet')
|
console.log('no bundle storage yet')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (pMetrics.memory_rss_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_rss_kb' + pName, this.serializeNowTlv(pMetrics.memory_rss_kb))
|
for (const key in pMetrics) {
|
||||||
|
const v = pMetrics[key as keyof ProcessMetrics]
|
||||||
|
if (v) this.storages[bundlerStorageName].AddTlv('_root', key + pName, this.serializeNowTlv(v))
|
||||||
|
}
|
||||||
|
/* if (pMetrics.memory_rss_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_rss_kb' + pName, this.serializeNowTlv(pMetrics.memory_rss_kb))
|
||||||
if (pMetrics.memory_buffer_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_buffer_kb' + pName, this.serializeNowTlv(pMetrics.memory_buffer_kb))
|
if (pMetrics.memory_buffer_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_buffer_kb' + pName, this.serializeNowTlv(pMetrics.memory_buffer_kb))
|
||||||
if (pMetrics.memory_heap_total_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_heap_total_kb' + pName, this.serializeNowTlv(pMetrics.memory_heap_total_kb))
|
if (pMetrics.memory_heap_total_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_heap_total_kb' + pName, this.serializeNowTlv(pMetrics.memory_heap_total_kb))
|
||||||
if (pMetrics.memory_heap_used_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_heap_used_kb' + pName, this.serializeNowTlv(pMetrics.memory_heap_used_kb))
|
if (pMetrics.memory_heap_used_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_heap_used_kb' + pName, this.serializeNowTlv(pMetrics.memory_heap_used_kb))
|
||||||
if (pMetrics.memory_external_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_external_kb' + pName, this.serializeNowTlv(pMetrics.memory_external_kb))
|
if (pMetrics.memory_external_kb) this.storages[bundlerStorageName].AddTlv('_root', 'memory_external_kb' + pName, this.serializeNowTlv(pMetrics.memory_external_kb)) */
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleOperation(operation: TlvStorageOperation) {
|
private async handleOperation(operation: TlvStorageOperation) {
|
||||||
|
|
@ -144,6 +162,9 @@ class TlvFilesStorageProcessor {
|
||||||
const opId = operation.opId;
|
const opId = operation.opId;
|
||||||
if (operation.debug) console.log('handleOperation', operation)
|
if (operation.debug) console.log('handleOperation', operation)
|
||||||
switch (operation.type) {
|
switch (operation.type) {
|
||||||
|
case 'resetStorage':
|
||||||
|
await this.handleResetStorage(operation);
|
||||||
|
break;
|
||||||
case 'newStorage':
|
case 'newStorage':
|
||||||
await this.handleNewStorage(operation);
|
await this.handleNewStorage(operation);
|
||||||
break;
|
break;
|
||||||
|
|
@ -162,6 +183,9 @@ class TlvFilesStorageProcessor {
|
||||||
case 'processMetrics':
|
case 'processMetrics':
|
||||||
await this.handleProcessMetrics(operation);
|
await this.handleProcessMetrics(operation);
|
||||||
break;
|
break;
|
||||||
|
case 'zipStorages':
|
||||||
|
await this.handleZipStorages(operation);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
this.sendResponse({
|
this.sendResponse({
|
||||||
success: false,
|
success: false,
|
||||||
|
|
@ -179,6 +203,19 @@ class TlvFilesStorageProcessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async handleResetStorage(operation: ResetTlvStorageOperation) {
|
||||||
|
for (const storageName in this.storages) {
|
||||||
|
this.storages[storageName].Reset()
|
||||||
|
}
|
||||||
|
this.sendResponse({
|
||||||
|
success: true,
|
||||||
|
type: 'resetStorage',
|
||||||
|
data: null,
|
||||||
|
opId: operation.opId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private async handleNewStorage(operation: NewTlvStorageOperation) {
|
private async handleNewStorage(operation: NewTlvStorageOperation) {
|
||||||
if (this.storages[operation.settings.name]) {
|
if (this.storages[operation.settings.name]) {
|
||||||
this.sendResponse({
|
this.sendResponse({
|
||||||
|
|
@ -279,6 +316,45 @@ class TlvFilesStorageProcessor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async handleZipStorages(operation: ZipStoragesOperation) {
|
||||||
|
let noAction = true
|
||||||
|
//const cwd = process.cwd()
|
||||||
|
const name = crypto.randomBytes(16).toString('hex') + '.zip'
|
||||||
|
for (const storageName in this.storages) {
|
||||||
|
noAction = false
|
||||||
|
this.storages[storageName].PersistNow()
|
||||||
|
//paths.push(cwd + '/' + this.storages[storageName].GetStoragePath())
|
||||||
|
const path = this.storages[storageName].GetStoragePath()
|
||||||
|
const err = await zip(path, name)
|
||||||
|
if (err) {
|
||||||
|
this.sendResponse({
|
||||||
|
success: false,
|
||||||
|
error: err.message,
|
||||||
|
opId: operation.opId
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (noAction) {
|
||||||
|
this.sendResponse({
|
||||||
|
success: false,
|
||||||
|
error: 'No storages to zip',
|
||||||
|
opId: operation.opId
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sendResponse<string>({
|
||||||
|
success: true,
|
||||||
|
type: 'zipStorages',
|
||||||
|
data: name,
|
||||||
|
opId: operation.opId
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private sendResponse<T>(response: TlvOperationResponse<T>) {
|
private sendResponse<T>(response: TlvOperationResponse<T>) {
|
||||||
if (process.send) {
|
if (process.send) {
|
||||||
process.send(response);
|
process.send(response);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
||||||
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
|
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, TestBase } from './testBase.js'
|
||||||
|
import * as Types from '../../proto/autogenerated/ts/types.js'
|
||||||
export const ignore = false
|
export const ignore = false
|
||||||
export const dev = false
|
export const dev = false
|
||||||
export default async (T: TestBase) => {
|
export default async (T: TestBase) => {
|
||||||
await safelySetUserBalance(T, T.user1, 2000)
|
await safelySetUserBalance(T, T.user1, 2000)
|
||||||
await testSuccessfulExternalPayment(T)
|
await testSuccessfulExternalPayment(T)
|
||||||
await testFailedExternalPayment(T)
|
await testFailedExternalPayment(T)
|
||||||
|
await testSuccesfulReceivedExternalChainPayment(T)
|
||||||
await runSanityCheck(T)
|
await runSanityCheck(T)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,3 +47,30 @@ const testFailedExternalPayment = async (T: TestBase) => {
|
||||||
expect(owner.balance_sats).to.be.equal(3)
|
expect(owner.balance_sats).to.be.equal(3)
|
||||||
T.d("app balance is still 3 sats")
|
T.d("app balance is still 3 sats")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const testSuccesfulReceivedExternalChainPayment = async (T: TestBase) => {
|
||||||
|
T.d("starting testSuccesfulReceivedExternalChainPayment")
|
||||||
|
/* const balanceBefore = await T.main.storage.userStorage.GetUser(T.user1.userId)
|
||||||
|
expect(balanceBefore.balance_sats).to.be.equal(0) */
|
||||||
|
const user2Address = await T.main.paymentManager.NewAddress({ app_id: T.app.appId, app_user_id: T.user2.appUserIdentifier, user_id: T.user2.userId }, { addressType: Types.AddressType.WITNESS_PUBKEY_HASH })
|
||||||
|
expect(user2Address.address).to.startWith("bcrt1")
|
||||||
|
T.d("generated external chain address for user2")
|
||||||
|
const payment = await T.externalAccessToOtherLnd.PayAddress(user2Address.address, 1000, 3, "test", { from: 'system', useProvider: false })
|
||||||
|
expect(payment.txid).to.not.be.undefined
|
||||||
|
T.d("paid 1000 sats to user2's external chain address")
|
||||||
|
await T.chainTools.mine(1)
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
T.d("mined 1 blocks to confirm the payment")
|
||||||
|
const u2 = await T.main.storage.userStorage.GetUser(T.user2.userId)
|
||||||
|
expect(u2.balance_sats).to.be.equal(1000)
|
||||||
|
T.d("user2 balance is now 1000")
|
||||||
|
const payment2 = await T.externalAccessToOtherLnd.PayAddress(user2Address.address, 1000, 3, "test", { from: 'system', useProvider: false })
|
||||||
|
expect(payment2.txid).to.not.be.undefined
|
||||||
|
T.d("paid 1000 sats to user2's external chain address again")
|
||||||
|
await T.chainTools.mine(1)
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
T.d("mined 1 blocks to confirm the payment")
|
||||||
|
const u2_2 = await T.main.storage.userStorage.GetUser(T.user2.userId)
|
||||||
|
expect(u2_2.balance_sats).to.be.equal(2000)
|
||||||
|
T.d("user2 balance is now 2000")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
||||||
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
|
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, TestBase } from './testBase.js'
|
||||||
export const ignore = false
|
export const ignore = false
|
||||||
|
|
||||||
export default async (T: TestBase) => {
|
export default async (T: TestBase) => {
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,17 @@ import { BitcoinCoreWrapper } from "./bitcoinCore.js"
|
||||||
import LND from '../services/lnd/lnd.js'
|
import LND from '../services/lnd/lnd.js'
|
||||||
import { LiquidityProvider } from "../services/main/liquidityProvider.js"
|
import { LiquidityProvider } from "../services/main/liquidityProvider.js"
|
||||||
import { Utils } from "../services/helpers/utilsWrapper.js"
|
import { Utils } from "../services/helpers/utilsWrapper.js"
|
||||||
import { TlvStorageFactory } from "../services/storage/tlv/tlvFilesStorageFactory.js"
|
|
||||||
export const setupNetwork = async () => {
|
export type ChainTools = {
|
||||||
|
mine: (amount: number) => Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setupNetwork = async (): Promise<ChainTools> => {
|
||||||
const settings = LoadTestSettingsFromEnv()
|
const settings = LoadTestSettingsFromEnv()
|
||||||
const core = new BitcoinCoreWrapper(settings)
|
const core = new BitcoinCoreWrapper(settings)
|
||||||
await core.InitAddress()
|
await core.InitAddress()
|
||||||
await core.Mine(1)
|
await core.Mine(1)
|
||||||
const tlvStorageFactory = new TlvStorageFactory()
|
const setupUtils = new Utils({ dataDir: settings.storageSettings.dataDir, allowResetMetricsStorages: settings.allowResetMetricsStorages })
|
||||||
const setupUtils = new Utils({ dataDir: settings.storageSettings.dataDir })
|
|
||||||
const alice = new LND(settings.lndSettings, new LiquidityProvider("", setupUtils, async () => { }, async () => { }), setupUtils, async () => { }, async () => { }, () => { }, () => { })
|
const alice = new LND(settings.lndSettings, new LiquidityProvider("", setupUtils, async () => { }, async () => { }), setupUtils, async () => { }, async () => { }, () => { }, () => { })
|
||||||
const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, new LiquidityProvider("", setupUtils, async () => { }, async () => { }), setupUtils, async () => { }, async () => { }, () => { }, () => { })
|
const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, new LiquidityProvider("", setupUtils, async () => { }, async () => { }), setupUtils, async () => { }, async () => { }, () => { }, () => { })
|
||||||
await tryUntil<void>(async i => {
|
await tryUntil<void>(async i => {
|
||||||
|
|
@ -53,6 +56,7 @@ export const setupNetwork = async () => {
|
||||||
|
|
||||||
alice.Stop()
|
alice.Stop()
|
||||||
bob.Stop()
|
bob.Stop()
|
||||||
|
return { mine: (amount: number) => core.Mine(amount) }
|
||||||
}
|
}
|
||||||
|
|
||||||
const tryUntil = async <T>(fn: (attempt: number) => Promise<T>, maxTries: number, interval: number) => {
|
const tryUntil = async <T>(fn: (attempt: number) => Promise<T>, maxTries: number, interval: number) => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { disableLoggers } from '../services/helpers/logger.js'
|
import { disableLoggers } from '../services/helpers/logger.js'
|
||||||
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
||||||
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
|
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, TestBase } from './testBase.js'
|
||||||
export const ignore = false
|
export const ignore = false
|
||||||
|
|
||||||
export default async (T: TestBase) => {
|
export default async (T: TestBase) => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { disableLoggers } from '../services/helpers/logger.js'
|
import { disableLoggers } from '../services/helpers/logger.js'
|
||||||
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
||||||
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
|
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, TestBase } from './testBase.js'
|
||||||
import * as Types from '../../proto/autogenerated/ts/types.js'
|
import * as Types from '../../proto/autogenerated/ts/types.js'
|
||||||
export const ignore = false
|
export const ignore = false
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import { LiquidityProvider } from '../services/main/liquidityProvider.js'
|
||||||
import { Utils } from '../services/helpers/utilsWrapper.js'
|
import { Utils } from '../services/helpers/utilsWrapper.js'
|
||||||
import { AdminManager } from '../services/main/adminManager.js'
|
import { AdminManager } from '../services/main/adminManager.js'
|
||||||
import { TlvStorageFactory } from '../services/storage/tlv/tlvFilesStorageFactory.js'
|
import { TlvStorageFactory } from '../services/storage/tlv/tlvFilesStorageFactory.js'
|
||||||
|
import { ChainTools } from './networkSetup.js'
|
||||||
chai.use(chaiString)
|
chai.use(chaiString)
|
||||||
export const expect = chai.expect
|
export const expect = chai.expect
|
||||||
export type Describe = (message: string, failure?: boolean) => void
|
export type Describe = (message: string, failure?: boolean) => void
|
||||||
|
|
@ -29,11 +30,12 @@ export type TestBase = {
|
||||||
app: AppData
|
app: AppData
|
||||||
user1: TestUserData
|
user1: TestUserData
|
||||||
user2: TestUserData
|
user2: TestUserData
|
||||||
externalAccessToMainLnd: LND
|
//externalAccessToMainLnd: LND
|
||||||
externalAccessToOtherLnd: LND
|
externalAccessToOtherLnd: LND
|
||||||
externalAccessToThirdLnd: LND
|
externalAccessToThirdLnd: LND
|
||||||
adminManager: AdminManager
|
adminManager: AdminManager
|
||||||
d: Describe
|
d: Describe
|
||||||
|
chainTools: ChainTools
|
||||||
}
|
}
|
||||||
|
|
||||||
export type StorageTestBase = {
|
export type StorageTestBase = {
|
||||||
|
|
@ -44,7 +46,7 @@ export type StorageTestBase = {
|
||||||
|
|
||||||
export const setupStorageTest = async (d: Describe): Promise<StorageTestBase> => {
|
export const setupStorageTest = async (d: Describe): Promise<StorageTestBase> => {
|
||||||
const settings = GetTestStorageSettings()
|
const settings = GetTestStorageSettings()
|
||||||
const utils = new Utils({ dataDir: settings.dataDir })
|
const utils = new Utils({ dataDir: settings.dataDir, allowResetMetricsStorages: true })
|
||||||
const storageManager = new Storage(settings, utils)
|
const storageManager = new Storage(settings, utils)
|
||||||
await storageManager.Connect(console.log)
|
await storageManager.Connect(console.log)
|
||||||
return {
|
return {
|
||||||
|
|
@ -58,7 +60,7 @@ export const teardownStorageTest = async (T: StorageTestBase) => {
|
||||||
T.storage.Stop()
|
T.storage.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SetupTest = async (d: Describe): Promise<TestBase> => {
|
export const SetupTest = async (d: Describe, chainTools: ChainTools): Promise<TestBase> => {
|
||||||
const settings = LoadTestSettingsFromEnv()
|
const settings = LoadTestSettingsFromEnv()
|
||||||
const initialized = await initMainHandler(getLogger({ component: "mainForTest" }), settings)
|
const initialized = await initMainHandler(getLogger({ component: "mainForTest" }), settings)
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
|
|
@ -71,9 +73,9 @@ export const SetupTest = async (d: Describe): Promise<TestBase> => {
|
||||||
const user1 = { userId: u1.info.userId, appUserIdentifier: u1.identifier, appId: app.appId }
|
const user1 = { userId: u1.info.userId, appUserIdentifier: u1.identifier, appId: app.appId }
|
||||||
const user2 = { userId: u2.info.userId, appUserIdentifier: u2.identifier, appId: app.appId }
|
const user2 = { userId: u2.info.userId, appUserIdentifier: u2.identifier, appId: app.appId }
|
||||||
|
|
||||||
const extermnalUtils = new Utils({ dataDir: settings.storageSettings.dataDir })
|
const extermnalUtils = new Utils({ dataDir: settings.storageSettings.dataDir, allowResetMetricsStorages: settings.allowResetMetricsStorages })
|
||||||
const externalAccessToMainLnd = new LND(settings.lndSettings, new LiquidityProvider("", extermnalUtils, async () => { }, async () => { }), extermnalUtils, async () => { }, async () => { }, () => { }, () => { })
|
/* const externalAccessToMainLnd = new LND(settings.lndSettings, new LiquidityProvider("", extermnalUtils, async () => { }, async () => { }), extermnalUtils, async () => { }, async () => { }, () => { }, () => { })
|
||||||
await externalAccessToMainLnd.Warmup()
|
await externalAccessToMainLnd.Warmup() */
|
||||||
|
|
||||||
const otherLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }
|
const otherLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }
|
||||||
const externalAccessToOtherLnd = new LND(otherLndSetting, new LiquidityProvider("", extermnalUtils, async () => { }, async () => { }), extermnalUtils, async () => { }, async () => { }, () => { }, () => { })
|
const externalAccessToOtherLnd = new LND(otherLndSetting, new LiquidityProvider("", extermnalUtils, async () => { }, async () => { }), extermnalUtils, async () => { }, async () => { }, () => { }, () => { })
|
||||||
|
|
@ -87,15 +89,16 @@ export const SetupTest = async (d: Describe): Promise<TestBase> => {
|
||||||
return {
|
return {
|
||||||
expect, main, app,
|
expect, main, app,
|
||||||
user1, user2,
|
user1, user2,
|
||||||
externalAccessToMainLnd, externalAccessToOtherLnd, externalAccessToThirdLnd,
|
/* externalAccessToMainLnd, */ externalAccessToOtherLnd, externalAccessToThirdLnd,
|
||||||
d,
|
d,
|
||||||
adminManager: initialized.adminManager
|
adminManager: initialized.adminManager,
|
||||||
|
chainTools
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const teardown = async (T: TestBase) => {
|
export const teardown = async (T: TestBase) => {
|
||||||
T.main.Stop()
|
T.main.Stop()
|
||||||
T.externalAccessToMainLnd.Stop()
|
/* T.externalAccessToMainLnd.Stop() */
|
||||||
T.externalAccessToOtherLnd.Stop()
|
T.externalAccessToOtherLnd.Stop()
|
||||||
T.externalAccessToThirdLnd.Stop()
|
T.externalAccessToThirdLnd.Stop()
|
||||||
T.adminManager.Stop()
|
T.adminManager.Stop()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//import whyIsNodeRunning from 'why-is-node-running'
|
//import whyIsNodeRunning from 'why-is-node-running'
|
||||||
import { globby } from 'globby'
|
import { globby } from 'globby'
|
||||||
import { setupNetwork } from './networkSetup.js'
|
import { ChainTools, setupNetwork } from './networkSetup.js'
|
||||||
import { Describe, SetupTest, teardown, TestBase, StorageTestBase, setupStorageTest, teardownStorageTest } from './testBase.js'
|
import { Describe, SetupTest, teardown, TestBase, StorageTestBase, setupStorageTest, teardownStorageTest } from './testBase.js'
|
||||||
type TestModule = {
|
type TestModule = {
|
||||||
ignore?: boolean
|
ignore?: boolean
|
||||||
|
|
@ -38,17 +38,18 @@ const start = async () => {
|
||||||
if (devModule !== -1) {
|
if (devModule !== -1) {
|
||||||
console.log("running dev module")
|
console.log("running dev module")
|
||||||
const { file, module } = modules[devModule]
|
const { file, module } = modules[devModule]
|
||||||
|
let chainTools: ChainTools | undefined
|
||||||
if (module.requires === 'storage') {
|
if (module.requires === 'storage') {
|
||||||
console.log("dev module requires only storage, skipping network setup")
|
console.log("dev module requires only storage, skipping network setup")
|
||||||
} else {
|
} else {
|
||||||
await setupNetwork()
|
chainTools = await setupNetwork()
|
||||||
}
|
}
|
||||||
await runTestFile(file, module)
|
await runTestFile(file, module, chainTools)
|
||||||
} else {
|
} else {
|
||||||
console.log("running all tests")
|
console.log("running all tests")
|
||||||
await setupNetwork()
|
const chainTools = await setupNetwork()
|
||||||
for (const { file, module } of modules) {
|
for (const { file, module } of modules) {
|
||||||
await runTestFile(file, module)
|
await runTestFile(file, module, chainTools)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(failures)
|
console.log(failures)
|
||||||
|
|
@ -62,7 +63,7 @@ const start = async () => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const runTestFile = async (fileName: string, mod: TestModule) => {
|
const runTestFile = async (fileName: string, mod: TestModule, chainTools?: ChainTools) => {
|
||||||
console.log(fileName)
|
console.log(fileName)
|
||||||
const d = getDescribe(fileName)
|
const d = getDescribe(fileName)
|
||||||
if (mod.ignore) {
|
if (mod.ignore) {
|
||||||
|
|
@ -78,7 +79,10 @@ const runTestFile = async (fileName: string, mod: TestModule) => {
|
||||||
T = await setupStorageTest(d)
|
T = await setupStorageTest(d)
|
||||||
} else {
|
} else {
|
||||||
d("-----requires all-----")
|
d("-----requires all-----")
|
||||||
T = await SetupTest(d)
|
if (!chainTools) {
|
||||||
|
throw new Error("chainTools are required for this test")
|
||||||
|
}
|
||||||
|
T = await SetupTest(d, chainTools)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
d("test starting")
|
d("test starting")
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
|
||||||
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, SetupTest, TestBase } from './testBase.js'
|
import { Describe, expect, expectThrowsAsync, runSanityCheck, safelySetUserBalance, TestBase } from './testBase.js'
|
||||||
export const ignore = false
|
export const ignore = false
|
||||||
export const dev = false
|
export const dev = false
|
||||||
export default async (T: TestBase) => {
|
export default async (T: TestBase) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue