mlabs-haskell / the-plutus-scaffold

Up-to-data version of plutus-scaffold. It's a fuller example utilizing ctl, see the overview in README. This project contains the build systems and scripts needed to scaffold a plutus project quickly.
10 stars 0 forks source link

Error in connections to ctl-runtime when app served with http-server #34

Open zmrocze opened 1 year ago

zmrocze commented 1 year ago

Context

The flake provides two convenience applications for serving the webapp:

nix run .#run-frontend-app-local
nix run .#run-frontend-app-deployment

which differ only in the URLs defined for kupo/ogmios in ContractParams, and then in turn in the final js bundles for the app. See section About ContractParams of README and browse for tags SwitchContractParams in source to find all related bits of code.

The applications are simple calls of http-server on the app bundles. See flake.nix/run-frontend-app.

Problem description

In both instances application errors when connecting to ctl-runtime, but differently.

Local

I defined a branch debug-connections(debug-connections) that points Kupo to the default ctl-runtime localhost:1442. (thats only difference to main, which instead points to "localhost:4008/kupo"). Running first

nix run .#ctl-runtime

then

nix run .#run-frontend-app-local

and interacting with the application yields in the browser console:

Access to XMLHttpRequest at 'http://localhost:1442/matches/addr_test1qpc8jarze80escum2mq4lvche53cm5hdk2q26h89cdqxhzfulzsv3cm695q8sp3yaenrmt608vhayktj8e2x9wpqvusq008xl4?unspent' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Offchain.js:7432          GET http://localhost:1442/matches/addr_test1qpc8jarze80escum2mq4lvche53cm5hdk2q26h89cdqxhzfulzsv3cm695q8sp3yaenrmt608vhayktj8e2x9wpqvusq008xl4?unspent net::ERR_FAILED 200
...
Offchain.js:5504 Uncaught (in promise) Error: CouldNotGetUtxos
...

So is this the cors policy from kupo, run by ctl-runtime that blocks it?

Deployment

Deployment targets the public mlabs ctl-runtime instances

Running

nix run .#run-frontend-app-deployment

and interacting with the application yields in the browser console:

WebSocket connection to 'ws://ogmios.preview.ctl-runtime.staging.mlabs.city:1337/' failed: WebSocket is closed before the connection is established.
Offchain.js:13429 [ERROR] 2023-05-22T18:18:42.748Z First connection to ogmios WebSocket failed. Terminating. Error: [object Object]
Offchain.js:5504 Uncaught (in promise) Error: [object Object]
[...]

Is it actually a server timeout? I doesn't look like because of the warning: "WebSocket is closed before the connection is established".

Github pages

Another problem happens with hosting the public mlabs instances version on github pages (https://mlabs-haskell.github.io/the-plutus-scaffold/):

Mixed Content: The page at 'https://mlabs-haskell.github.io/the-plutus-scaffold/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://ogmios.preview.ctl-runtime.staging.mlabs.city:1337/'. This request has been blocked; this endpoint must be available over WSS
...
reconnecting-websocket-mjs.js:518 Uncaught (in promise) DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
    at https://mlabs-haskell.github.io/the-plutus-scaffold/317.main.js:2:492550

Is there easy fix?

So there's the proxy with the setting changeOrigin in the dev-server from ctl-scaffold, is it related to this issue? How to replicate that with http-server and without modifying ctl-runtime?

I think the fix would be easy if the project provides full deployment server, then ctl-runtime can just be pinged at different url's served by the same server (if the problem is related to cors). But is there a fix to just provide a convenience flake app server, keeping the website fully static?

klntsky commented 1 year ago

Local

Add a proxy that sets Access-Control-Allow-Origin: * (or better, the origin you will be using, i.e. the hostname)

Deployment

I think it must be wss (secure: true) - do you have the full parameters for this deployment for me to check if that's the problem?

Github pages

Same

zmrocze commented 1 year ago

Local

Do you mean a proxy on ctl-runtime (or even just on kupo)?

Deployment, Gh Pages

Note that deploy and gh-pages above had different error messasages. Now after setting secure: true in ContractParams here BOTH return the same error (the same as deployment previously):

[DEBUG] 2023-05-23T18:49:48.591Z Created a new WebSocket
reconnecting-websocket-mjs.js:537 WebSocket connection to 'wss://ogmios.preview.ctl-runtime.staging.mlabs.city:1337/' failed: WebSocket is closed before the connection is established.
t._disconnect @ reconnecting-websocket-mjs.js:537
_handleError @ reconnecting-websocket-mjs.js:178
t._handleTimeout @ reconnecting-websocket-mjs.js:527
(anonymous) @ reconnecting-websocket-mjs.js:522
setTimeout (async)
(anonymous) @ reconnecting-websocket-mjs.js:522
Promise.then (async)
t._connect @ reconnecting-websocket-mjs.js:510
t @ reconnecting-websocket-mjs.js:203
(anonymous) @ Offchain.js:13677
(anonymous) @ Offchain.js:29444
I @ Offchain.js:8242
V @ Offchain.js:8477
run @ Offchain.js:8786
(anonymous) @ Offchain.js:9107
k @ Offchain.js:9146
(anonymous) @ Offchain.js:9160
I @ Offchain.js:8242
V @ Offchain.js:8477
(anonymous) @ Offchain.js:8783
a @ Offchain.js:8264
enqueue @ Offchain.js:8285
run @ Offchain.js:8782
(anonymous) @ Offchain.js:9795
(anonymous) @ Offchain.js:5448
(anonymous) @ Offchain.js:5448
(anonymous) @ Offchain.js:35252
(anonymous) @ Offchain.js:35247
e.unsafePerformEffect @ Offchain.js:46746
(anonymous) @ Offchain.js:47318
n.payToPassword @ Offchain.js:47502
(anonymous) @ App.tsx:146
(anonymous) @ 917.main.js:1
(anonymous) @ 917.main.js:1
(anonymous) @ 917.main.js:1
onClick @ 917.main.js:1
Ie @ react-dom.production.min.js:54
$e @ react-dom.production.min.js:54
(anonymous) @ react-dom.production.min.js:55
Mr @ react-dom.production.min.js:105
Dr @ react-dom.production.min.js:106
(anonymous) @ react-dom.production.min.js:117
fc @ react-dom.production.min.js:274
Te @ react-dom.production.min.js:52
qr @ react-dom.production.min.js:109
Gt @ react-dom.production.min.js:74
Ht @ react-dom.production.min.js:73
Show 15 more frames
Offchain.js:13429 [ERROR] 2023-05-23T18:49:52.605Z First connection to ogmios WebSocket failed. Terminating. Error: [object Object]
Offchain.js:5504 Uncaught (in promise) Error: [object Object]
    at e.error (Offchain.js:5504:12)
    at Offchain.js:29450:83
    at t (Offchain.js:13701:7)
    at t._callEventListener (reconnecting-websocket-mjs.js:555:13)
    at reconnecting-websocket-mjs.js:183:79
    at Array.forEach (<anonymous>)
    at t._handleError (reconnecting-websocket-mjs.js:183:36)
    at t._handleTimeout (reconnecting-websocket-mjs.js:527:14)
    at reconnecting-websocket-mjs.js:522:75