From 33af2531b50666bc0ff65910c103629fc21aacf8 Mon Sep 17 00:00:00 2001 From: Anmol Sharma Date: Sat, 27 Jan 2024 21:12:40 +0530 Subject: [PATCH] test: added test for send functionality --- test/helpers/bitcoin-rpc-client.ts | 27 ++++--- test/helpers/docker-compose.yaml | 2 + test/helpers/nginx.conf.in | 112 +++++++++++++++++++++++++++++ test/wallet.spec.ts | 13 ++++ 4 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 test/helpers/nginx.conf.in diff --git a/test/helpers/bitcoin-rpc-client.ts b/test/helpers/bitcoin-rpc-client.ts index ac899b0..5ee28ca 100644 --- a/test/helpers/bitcoin-rpc-client.ts +++ b/test/helpers/bitcoin-rpc-client.ts @@ -22,12 +22,7 @@ export class BitcoinRpcClient { try { await this.createWallet('default'); } catch (e) { - if ( - e instanceof AxiosError && - e.response?.data.error.message.includes( - 'Database already exists.', - ) - ) { + if ((e as Error).message.includes('Database already exists.')) { loadWallet = true; } else { throw e; @@ -39,10 +34,7 @@ export class BitcoinRpcClient { loadWallet = false; } } catch (e) { - if ( - e instanceof AxiosError && - !e.response?.data.error.message.includes('No wallet is loaded.') - ) { + if (!(e as Error).message.includes('No wallet is loaded.')) { throw e; } } @@ -54,8 +46,7 @@ export class BitcoinRpcClient { } } catch (e) { if ( - e instanceof AxiosError && - !e.response?.data.error.message.includes( + !(e as Error).message.includes( 'Unable to obtain an exclusive lock on the database', ) ) { @@ -74,8 +65,6 @@ export class BitcoinRpcClient { } catch (e) { if (e instanceof AxiosError) { if (e.response?.data.error) { - // eslint-disable-next-line no-console - console.log(e.response.data); throw new Error(e.response.data.error.message); } else { throw new Error(e.message); @@ -145,4 +134,14 @@ export class BitcoinRpcClient { }, }); } + + async getMempoolEntry(txid: string) { + return await this.request({ + url: this.url, + data: { + method: 'getmempoolentry', + params: [txid], + }, + }); + } } diff --git a/test/helpers/docker-compose.yaml b/test/helpers/docker-compose.yaml index 365c1c1..fc70180 100644 --- a/test/helpers/docker-compose.yaml +++ b/test/helpers/docker-compose.yaml @@ -23,6 +23,8 @@ services: image: blockstream/esplora:5ec810278c737c78ed54e82749581f1a52ce1b54 environment: ELECTRS_ARGS: --jsonrpc-import --daemon-rpc-addr bitcoin:18443 --cookie alice:password + volumes: + - ./nginx.conf.in:/srv/explorer/source/contrib/nginx.conf.in ports: - "50001:50001" - "8094:80" diff --git a/test/helpers/nginx.conf.in b/test/helpers/nginx.conf.in new file mode 100644 index 0000000..1977b0f --- /dev/null +++ b/test/helpers/nginx.conf.in @@ -0,0 +1,112 @@ +geo $limit { + default 1; + 34.70.28.228/32 0; + 127.0.0.1/32 0; +} + +map $limit $limit_key { + 0 ""; + 1 $http_x_forwarded_for; +} + +limit_req_status 429; +limit_conn_status 429; + +upstream backend { + server unix:/var/electrs-rest.sock; + keepalive 300; +} + +upstream prerenderer { + server unix:/var/prerender-http.sock; + keepalive 300; +} + +upstream electrum_websocket { + server unix:/var/electrum-websocket.sock; + keepalive 300; +} + +server { + listen 80; + keepalive_requests 100000; + gzip on; + gzip_types application/json text/plain application/xml application/javascript; + gzip_proxied any; + gzip_vary on; + root {STATIC_DIR}; + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 650s; + {NGINX_LOGGING}; + server_tokens off; + + # client timeouts + client_body_timeout 5s; + client_header_timeout 5s; + + add_header X-Frame-Options SAMEORIGIN always; + add_header Content-Security-Policy "{NGINX_CSP}" always; + add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; + add_header X-XSS-Protection "1; mode=block" always; + add_header X-Content-Type-Options "nosniff" always; + add_header Referrer-Policy "no-referrer" always; + + location /{NGINX_PATH}api/ { + proxy_pass http://backend/; + add_header Access-Control-Allow-Origin * always; + add_header Access-Control-Expose-Headers 'x-total-results'; + proxy_http_version 1.1; + proxy_set_header Connection ""; + } + location /{NGINX_PATH}nojs/ { + proxy_pass http://prerenderer/; + proxy_http_version 1.1; + proxy_set_header Connection ""; + } + location /{NGINX_PATH}electrum-websocket/ { + client_body_timeout 60s; + proxy_pass http://electrum_websocket/; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_read_timeout 1d; + proxy_send_timeout 1d; + } + + location = /{NGINX_PATH}nojs { + return 301 " /{NGINX_PATH}nojs/"; + } + location ~ ^/{NGINX_PATH}((tx|block|block-height|address|assets?)/|[a-zA-Z0-9]+$) { + expires 60m; + if ($args = "nojs") { + {NGINX_REWRITE_NOJS}; + } + try_files $uri /index.html; + } + location /{NGINX_PATH} { + expires 60m; + if ($args = "nojs") { + {NGINX_REWRITE_NOJS}; + } + {NGINX_REWRITE} + } + location = /{NGINX_NOSLASH_PATH} { + if ($args = "nojs") { + {NGINX_REWRITE_NOJS}; + } + return 301 " /{NGINX_PATH}"; + } + error_page 404 /{NGINX_PATH}notfound.html; + location = /notfound.html { + internal; + } + location /{NGINX_PATH}api/fee-estimates { + return 200 '{"1": 1.0, "3": 1.0, "6": 1.0}'; + add_header Access-Control-Allow-Origin * always; + add_header Access-Control-Expose-Headers 'x-total-results'; + proxy_http_version 1.1; + proxy_set_header Connection ""; + } +} \ No newline at end of file diff --git a/test/wallet.spec.ts b/test/wallet.spec.ts index 9d90e92..3007b49 100644 --- a/test/wallet.spec.ts +++ b/test/wallet.spec.ts @@ -76,6 +76,19 @@ describe('Wallet', () => { expect(await wallet.getBalance()).toBe(10000000); }); + it('should send a payment to an address', async () => { + const txid = await wallet.send( + await bitcoinRpcClient.getNewAddress(), + 5000000, + ); + + expect(txid).toBeDefined(); + + // get the transaction from the node and check it has been broadcasted + const tx = await bitcoinRpcClient.getMempoolEntry(txid); + expect(tx).toBeDefined(); + }); + afterAll(async () => { await wallet.close(); fs.rmSync('./test/wallet', { recursive: true, force: true });