From 5108125008d28de0fc6c183fac8ab286d35e71d1 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 13:37:22 +0530 Subject: [PATCH 001/153] feat: add integration tests to actions --- .github/workflows/tests.yml | 79 +++++++++++++++++++++++++++++++++++++ docker-compose-test.yml | 53 +++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 docker-compose-test.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..33799e1f --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,79 @@ +name: Integration Tests + +on: + push: + branches: + - dev + - dev2 + pull_request: + branches: + - dev + - dev2 + +jobs: + test: + runs-on: ubuntu-latest + + services: + docker: + image: docker:19.03.12 + options: --privileged + ports: + - 5432:5432 + - 18443:18443 + - 50001:50001 + - 8080:8080 + - 18080:18080 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Compose + run: | + docker-compose -f docker-compose-test.yml up --build -d + - name: Wait for services to be ready + run: | + sleep 5 # Adjust time as necessary for services to initialize + - name: Add Mnemonic + run: | + docker exec $(docker ps -qf "name=enclave") \ + curl -X POST http://0.0.0.0:18080/add_mnemonic \ + -H "Content-Type: application/json" \ + -d '{ + "mnemonic": "achieve merry hidden lyrics element brand student armed dismiss vague fury avocado grief crazy garlic gallery blur spider bag bless motor crawl surround copper", + "password": "b1gHKyfXTzs6", + "index": 0, + "threshold": 2 + }' + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '20.12.2' + + - name: Install Node.js dependencies for client + run: | + cd clients/nodejs + npm install + - name: Install Node.js dependencies for lib + run: | + cd clients/libs/nodejs + npm install + - name: Run npm link + run: | + cd clients/libs/nodejs + npm link + + - name: Link mercurynodejslib + run: | + cd clients/nodejs + npm link mercurynodejslib + - name: Run Client-Side Tests + run: | + cd clients/apps/nodejs + node test_basic_workflow2.js + - name: Tear Down + run: | + docker-compose down diff --git a/docker-compose-test.yml b/docker-compose-test.yml new file mode 100644 index 00000000..a80f1add --- /dev/null +++ b/docker-compose-test.yml @@ -0,0 +1,53 @@ +version: '3.8' + +services: + postgres: + image: postgres + environment: + POSTGRES_PASSWORD: pgpassword + ports: + - "5432:5432" + + bitcoin: + image: ruimarinho/bitcoin-core:latest + command: ["-regtest", "-server", "-rpcallowip=::/0", "-rpcbind=0.0.0.0", "-rpcuser=user", "-rpcpassword=pass"] + ports: + - "18443:18443" + volumes: + - bitcoin_data:/root/.bitcoin + + electrumx: + image: lukechilds/electrumx:latest + environment: + DAEMON_URL: http://user:pass@bitcoin:18443/ + COIN: BitcoinSegwit + NET: regtest + ports: + - "50001:50001" + + enclave-sgx: + build: + context: enclave + dockerfile: Dockerfiles/SIM/Dockerfile + depends_on: + - postgres + environment: + ENCLAVE_DATABASE_URL: postgres://postgres:pgpassword@postgres:5432/postgres + ENCLAVE_PORT: 18080 + SEED_DIR: ./seed + + mercury: + image: commerceblockx/mercury-server:3a686af + environment: + LOCKBOX: http://enclave-sgx:18080 + NETWORK: regtest + LOCKHEIGHT_INIT: 1000 + LH_DECREMENT: 10 + CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres + ports: + - "8080:8080" + depends_on: + - postgres + +volumes: + bitcoin_data: \ No newline at end of file From 758bf73e71bfb2052656a255edb6879f24a111ef Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 13:49:40 +0530 Subject: [PATCH 002/153] fix: nodejs dependency installation --- .github/workflows/tests.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 33799e1f..6034ac93 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -53,10 +53,6 @@ jobs: with: node-version: '20.12.2' - - name: Install Node.js dependencies for client - run: | - cd clients/nodejs - npm install - name: Install Node.js dependencies for lib run: | cd clients/libs/nodejs From 8603248c8621f9a419d75f956802feaf74a907ef Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 15:37:54 +0530 Subject: [PATCH 003/153] fix: path in dependency installation for client --- .github/workflows/tests.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6034ac93..15b55d49 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -53,6 +53,10 @@ jobs: with: node-version: '20.12.2' + - name: Install Node.js dependencies for client + run: | + cd clients/apps/nodejs + npm install - name: Install Node.js dependencies for lib run: | cd clients/libs/nodejs @@ -64,7 +68,7 @@ jobs: - name: Link mercurynodejslib run: | - cd clients/nodejs + cd clients/libs/nodejs npm link mercurynodejslib - name: Run Client-Side Tests run: | From b047a3997d06eca3e06f944e5414366f7e9d861e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 15:56:50 +0530 Subject: [PATCH 004/153] fix: linking of dependency mercurynodejslib --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 15b55d49..91c3cbeb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,11 +64,11 @@ jobs: - name: Run npm link run: | cd clients/libs/nodejs - npm link + npm link --save - name: Link mercurynodejslib run: | - cd clients/libs/nodejs + cd clients/apps/nodejs npm link mercurynodejslib - name: Run Client-Side Tests run: | From a6109137beaf5a30fe77a2b0d9cb069a186d800b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 16:32:21 +0530 Subject: [PATCH 005/153] fix: port issue with mercury server --- .github/workflows/tests.yml | 2 +- docker-compose-test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 91c3cbeb..acdff56f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -22,7 +22,7 @@ jobs: - 5432:5432 - 18443:18443 - 50001:50001 - - 8080:8080 + - 8000:8000 - 18080:18080 volumes: - /var/run/docker.sock:/var/run/docker.sock diff --git a/docker-compose-test.yml b/docker-compose-test.yml index a80f1add..6a00c02b 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -45,7 +45,7 @@ services: LH_DECREMENT: 10 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: - - "8080:8080" + - "8000:8000" depends_on: - postgres From 42a89390995acc4a6fe4b1b1b9f0bb1a2814b292 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 16:42:49 +0530 Subject: [PATCH 006/153] fix: host of mercury server --- clients/apps/nodejs/config/default.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 5379e8c0..480eaa4c 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,5 +1,5 @@ { - "statechainEntity": "http://127.0.0.1:8000", + "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", From 6e8e7815e02b111cc0da24aed9575560ea804b28 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 16:58:49 +0530 Subject: [PATCH 007/153] debug: services on actions --- .github/workflows/tests.yml | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index acdff56f..611585c0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,40 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 5 # Adjust time as necessary for services to initialize + sleep 15 # Adjust time as necessary for services to initialize + - name: Health check for services + run: | + echo "Checking if services are up and running..." + # Check PostgreSQL + until nc -z -v -w30 127.0.0.1 5432 + do + echo "Waiting for PostgreSQL..." + sleep 5 + done + # Check Bitcoin + until nc -z -v -w30 127.0.0.1 18443 + do + echo "Waiting for Bitcoin..." + sleep 5 + done + # Check ElectrumX + until nc -z -v -w30 127.0.0.1 50001 + do + echo "Waiting for ElectrumX..." + sleep 5 + done + # Check Enclave + until nc -z -v -w30 127.0.0.1 18080 + do + echo "Waiting for Enclave..." + sleep 5 + done + # Check Mercury + until nc -z -v -w30 0.0.0.0 8000 + do + echo "Waiting for Mercury..." + sleep 5 + done - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 610e3a857e42bd0e4db351c061f7dab298ad7181 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 17:22:53 +0530 Subject: [PATCH 008/153] debug: mercury server availability --- .github/workflows/tests.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 611585c0..87ba43f0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,11 +59,11 @@ jobs: sleep 5 done # Check Enclave - until nc -z -v -w30 127.0.0.1 18080 - do - echo "Waiting for Enclave..." - sleep 5 - done + # until nc -z -v -w30 127.0.0.1 18080 + # do + # echo "Waiting for Enclave..." + # sleep 5 + # done # Check Mercury until nc -z -v -w30 0.0.0.0 8000 do From 456d937ac888ffc8a03eb62b49b36d65ae655b79 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 17:55:28 +0530 Subject: [PATCH 009/153] debug: mercury server through curl --- .github/workflows/tests.yml | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 87ba43f0..a224b609 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,39 +37,9 @@ jobs: - name: Wait for services to be ready run: | sleep 15 # Adjust time as necessary for services to initialize - - name: Health check for services + - name: Verify Mercury Service with Curl run: | - echo "Checking if services are up and running..." - # Check PostgreSQL - until nc -z -v -w30 127.0.0.1 5432 - do - echo "Waiting for PostgreSQL..." - sleep 5 - done - # Check Bitcoin - until nc -z -v -w30 127.0.0.1 18443 - do - echo "Waiting for Bitcoin..." - sleep 5 - done - # Check ElectrumX - until nc -z -v -w30 127.0.0.1 50001 - do - echo "Waiting for ElectrumX..." - sleep 5 - done - # Check Enclave - # until nc -z -v -w30 127.0.0.1 18080 - # do - # echo "Waiting for Enclave..." - # sleep 5 - # done - # Check Mercury - until nc -z -v -w30 0.0.0.0 8000 - do - echo "Waiting for Mercury..." - sleep 5 - done + docker exec $(docker ps -qf "name=mercury-server") curl -v http://0.0.0.1:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 683f754bc57de48f347c10567e88520f70297a44 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 17:56:30 +0530 Subject: [PATCH 010/153] debug: mercury server though curl --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a224b609..588f0343 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: sleep 15 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - docker exec $(docker ps -qf "name=mercury-server") curl -v http://0.0.0.1:8000/info/config + docker exec $(docker ps -qf "name=mercury-server") curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 69527e66b9f33e5aa552a96cc3a841e5ad39cf72 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 18:06:42 +0530 Subject: [PATCH 011/153] debug: running containers --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 588f0343..3303229a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: sleep 15 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - docker exec $(docker ps -qf "name=mercury-server") curl -v http://0.0.0.0:8000/info/config + docker ps - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 58a1d96fdf2fbf841f2711eef84d8dcb1988415c Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 18:14:37 +0530 Subject: [PATCH 012/153] debug: mercury server through exec and curl --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3303229a..647d1d61 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: sleep 15 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - docker ps + docker exec $(docker ps -qf "name=commerceblockx/mercury-server") curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From bf7ced18ced94f55937c4be0297e92e851fc4174 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 18:25:57 +0530 Subject: [PATCH 013/153] debug: mercury server through curl --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 647d1d61..652fc6a5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: sleep 15 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - docker exec $(docker ps -qf "name=commerceblockx/mercury-server") curl -v http://0.0.0.0:8000/info/config + curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From b91be75db38bb7e0137d686f748a6c6e869159b2 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 18:59:32 +0530 Subject: [PATCH 014/153] debug: test with mercurylayer.com --- .github/workflows/tests.yml | 6 +++--- clients/apps/nodejs/config/default.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 652fc6a5..141c3d59 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,9 +37,9 @@ jobs: - name: Wait for services to be ready run: | sleep 15 # Adjust time as necessary for services to initialize - - name: Verify Mercury Service with Curl - run: | - curl -v http://0.0.0.0:8000/info/config + # - name: Verify Mercury Service with Curl + # run: | + # curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 480eaa4c..6c51c6fa 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,5 +1,5 @@ { - "statechainEntity": "http://0.0.0.0:8000", + "statechainEntity": "http://api.mercurylayer.com/info/config", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", From 7ee3ff0df9c9a7fc5d031544fa04427022bcbcc9 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 19:08:48 +0530 Subject: [PATCH 015/153] fix: mercurylayer api url --- .github/workflows/tests.yml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 141c3d59..c6d3b2b5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,10 +36,14 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 15 # Adjust time as necessary for services to initialize - # - name: Verify Mercury Service with Curl - # run: | - # curl -v http://0.0.0.0:8000/info/config + sleep 120 # Adjust time as necessary for services to initialize + - name: Check Mercury Service Logs + run: | + docker-compose logs $(docker ps -qf "name=commerceblockx/mercury-server") + + - name: Verify Mercury Service with Curl + run: | + curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 49394cb95bda6dc9b9762e6602eb7aa0625b15dc Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 19:18:05 +0530 Subject: [PATCH 016/153] fix: remove mercury logs --- .github/workflows/tests.yml | 12 ++++-------- clients/apps/nodejs/config/default.json | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c6d3b2b5..16576ff3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,14 +36,10 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 120 # Adjust time as necessary for services to initialize - - name: Check Mercury Service Logs - run: | - docker-compose logs $(docker ps -qf "name=commerceblockx/mercury-server") - - - name: Verify Mercury Service with Curl - run: | - curl -v http://0.0.0.0:8000/info/config + sleep 10 # Adjust time as necessary for services to initialize + # - name: Verify Mercury Service with Curl + # run: | + # curl -v http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 6c51c6fa..f5359827 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,5 +1,5 @@ { - "statechainEntity": "http://api.mercurylayer.com/info/config", + "statechainEntity": "http://api.mercurylayer.com", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", From 66ed164e8c0d09b4efa68015272abfa557df774b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 21:18:44 +0530 Subject: [PATCH 017/153] feat: add test for iterative self transfer --- .github/workflows/tests.yml | 9 ++- clients/apps/nodejs/config/default.json | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 89 +++++++++++++++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 16576ff3..828ea707 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,10 +36,11 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 10 # Adjust time as necessary for services to initialize - # - name: Verify Mercury Service with Curl - # run: | - # curl -v http://0.0.0.0:8000/info/config + sleep 30 # Adjust time as necessary for services to initialize + - name: Verify Mercury Service with Curl + run: | + docker exec $(docker ps -qf "name=commerceblockx/mercury-server") \ + curl http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index f5359827..480eaa4c 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,5 +1,5 @@ { - "statechainEntity": "http://api.mercurylayer.com", + "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index c239cd29..bf8320fa 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -93,6 +93,80 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { } +async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { + const amount = 10000; + + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + deposit_info["amount"] = amount; + console.log("deposit_coin: ", deposit_info); + + let coin = undefined; + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + + console.log("coin: ", coin); + + let block_header = client_config.electrum_client.block_headers_subscribe_raw(); + let currentBlockHeight = block_header.height; + console.log("Current block height: ", currentBlockHeight); + + while (coin.locktime < currentBlockHeight) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); + + console.log("transfer_address: ", transfer_address); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("coin transferSend: ", coin); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + + // Fetch the coin again to get the updated locktime + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + coin = list_coins.find(c => c.statechain_id === coin.statechain_id); + + console.log("Updated coin: ", coin); + } + + let withdraw_address = "tb1qwrujs6f4gyexsextpf9p50smjtht7p7ypknteu"; + + let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); + + console.log("txid: ", txid); +} + async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { const amount = 10000; @@ -170,5 +244,20 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + await removeDatabase(); +})(); + +// Deposit, iterative self transfer +(async () => { + + const clientConfig = client_config.load(); + + let wallet_1_name = "w1"; + + await removeDatabase(); + await createWallet(clientConfig, wallet_1_name); + + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); + await removeDatabase(); })(); \ No newline at end of file From 558d37dff0527f8b20ea31406a9dbaf2091f68cb Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 21:28:41 +0530 Subject: [PATCH 018/153] debug: mercury server through curl --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 828ea707..0f65f6ea 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,8 +39,8 @@ jobs: sleep 30 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - docker exec $(docker ps -qf "name=commerceblockx/mercury-server") \ - curl http://0.0.0.0:8000/info/config + container_id=$(docker ps -qf "ancestor=commerceblockx/mercury-server") + docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From e6d607df2668875419864721aa51ff75ca0da8ec Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 30 May 2024 21:54:00 +0530 Subject: [PATCH 019/153] debug: mercury server container --- .github/workflows/tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0f65f6ea..6b226d27 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,8 @@ jobs: sleep 30 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - container_id=$(docker ps -qf "ancestor=commerceblockx/mercury-server") + container_id=$(docker ps -qf "name=mercury") + echo "Container ID: $container_id" docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | From 6340d46d147cd4d827e6c30a307666832e58befe Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 10:13:11 +0530 Subject: [PATCH 020/153] debug: mercury server --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6b226d27..88dd87d5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: sleep 30 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | - container_id=$(docker ps -qf "name=mercury") + container_id=$(docker ps -qf "ancestor=commerceblockx/mercury-server" -q | head -n 1) echo "Container ID: $container_id" docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic From e77b67ca0169eb4460c547c8f458952fa770aca6 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 10:27:15 +0530 Subject: [PATCH 021/153] debug: list all containers --- .github/workflows/tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 88dd87d5..9b1fcc19 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,6 +37,9 @@ jobs: - name: Wait for services to be ready run: | sleep 30 # Adjust time as necessary for services to initialize + - name: List all running containers + run: | + docker ps -a - name: Verify Mercury Service with Curl run: | container_id=$(docker ps -qf "ancestor=commerceblockx/mercury-server" -q | head -n 1) From 90c8c7a05b0cb5fc4e0dfb24c679a4a4b011042d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 10:36:35 +0530 Subject: [PATCH 022/153] debug: mercury server through container id --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9b1fcc19..d6dcc2dc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,7 +42,7 @@ jobs: docker ps -a - name: Verify Mercury Service with Curl run: | - container_id=$(docker ps -qf "ancestor=commerceblockx/mercury-server" -q | head -n 1) + container_id=$(docker ps -qf "name=commerceblockx/mercury-server:3a686af") echo "Container ID: $container_id" docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic From 75adfeca9d0e8380038d19ed9678a2d061fc345e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 10:48:03 +0530 Subject: [PATCH 023/153] debug: fix mercury server container name --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d6dcc2dc..26cf72d4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,7 +42,7 @@ jobs: docker ps -a - name: Verify Mercury Service with Curl run: | - container_id=$(docker ps -qf "name=commerceblockx/mercury-server:3a686af") + container_id=$(docker ps -qf "name=mercurylayer_mercury_1") echo "Container ID: $container_id" docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic From 3dcb0d3101f395e0261538d261e9092b2ff57102 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 10:56:57 +0530 Subject: [PATCH 024/153] debug: docker logs --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 26cf72d4..0fe980e6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,6 +44,7 @@ jobs: run: | container_id=$(docker ps -qf "name=mercurylayer_mercury_1") echo "Container ID: $container_id" + docker logs $container_id docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | From be69652df1fade3098ae24b37ae964c0f7e958ae Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 11:06:10 +0530 Subject: [PATCH 025/153] debug: increase wait time for service to be ready --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0fe980e6..9df7b03e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 30 # Adjust time as necessary for services to initialize + sleep 150 # Adjust time as necessary for services to initialize - name: List all running containers run: | docker ps -a From cb8aa7b8c4327fe4b93b2ebaeb439574bf18f254 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 11:32:06 +0530 Subject: [PATCH 026/153] debug: sql error --- .github/workflows/tests.yml | 11 +---------- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9df7b03e..65769686 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,16 +36,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 150 # Adjust time as necessary for services to initialize - - name: List all running containers - run: | - docker ps -a - - name: Verify Mercury Service with Curl - run: | - container_id=$(docker ps -qf "name=mercurylayer_mercury_1") - echo "Container ID: $container_id" - docker logs $container_id - docker exec $container_id curl http://0.0.0.0:8000/info/config + sleep 90 # Adjust time as necessary for services to initialize - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index bf8320fa..33c5a61e 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -248,16 +248,16 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle })(); // Deposit, iterative self transfer -(async () => { +// (async () => { - const clientConfig = client_config.load(); +// const clientConfig = client_config.load(); - let wallet_1_name = "w1"; +// let wallet_1_name = "w1"; - await removeDatabase(); - await createWallet(clientConfig, wallet_1_name); +// await removeDatabase(); +// await createWallet(clientConfig, wallet_1_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); +// await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); - await removeDatabase(); -})(); \ No newline at end of file +// await removeDatabase(); +// })(); \ No newline at end of file From fa83139846da73d6fc248c570db7fb9683b1d767 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 12:01:31 +0530 Subject: [PATCH 027/153] debug: mercury server post request --- .github/workflows/tests.yml | 8 +++++++- clients/apps/nodejs/config/default.json | 6 +++--- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 65769686..0dcda323 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,13 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 90 # Adjust time as necessary for services to initialize + sleep 100 # Adjust time as necessary for services to initialize + - name: Verify Mercury Service with Curl + run: | + container_id=$(docker ps -qf "name=mercurylayer_mercury_1") + echo "Container ID: $container_id" + docker logs $container_id + docker exec $container_id curl http://0.0.0.0:8000/info/config - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 480eaa4c..a2c574f9 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -2,11 +2,11 @@ "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", - "electrumServer": "ssl://mempool.space:60602", + "__electrumServer": "tcp://localhost:50001", + "electrumServer": "tcp://localhost:50001", "electrumType": "electrs", "_electrumServer": "tcp://localhost:50001", - "network": "signet", + "network": "regtest", "feeRateTolerance": 5, "databaseFile": "wallet.db", "confirmationTarget": 2, diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 33c5a61e..50798a59 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -248,16 +248,16 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle })(); // Deposit, iterative self transfer -// (async () => { +(async () => { -// const clientConfig = client_config.load(); + const clientConfig = client_config.load(); -// let wallet_1_name = "w1"; + let wallet_name = "w3"; -// await removeDatabase(); -// await createWallet(clientConfig, wallet_1_name); + await removeDatabase(); + await createWallet(clientConfig, wallet_name); -// await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); -// await removeDatabase(); -// })(); \ No newline at end of file + await removeDatabase(); +})(); \ No newline at end of file From 82eb65e1fc95c9727f2da8af00183e6a01f33374 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 12:15:50 +0530 Subject: [PATCH 028/153] debug: electrum client --- clients/apps/nodejs/config/default.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index a2c574f9..480eaa4c 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -2,11 +2,11 @@ "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://localhost:50001", - "electrumServer": "tcp://localhost:50001", + "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", + "electrumServer": "ssl://mempool.space:60602", "electrumType": "electrs", "_electrumServer": "tcp://localhost:50001", - "network": "regtest", + "network": "signet", "feeRateTolerance": 5, "databaseFile": "wallet.db", "confirmationTarget": 2, From e4b365fd6d809fa47496665181ba27ba9ff50e84 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 12:29:24 +0530 Subject: [PATCH 029/153] debug: test failure --- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 50798a59..80c5ffb7 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -248,16 +248,16 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle })(); // Deposit, iterative self transfer -(async () => { +// (async () => { - const clientConfig = client_config.load(); +// const clientConfig = client_config.load(); - let wallet_name = "w3"; +// let wallet_name = "w3"; - await removeDatabase(); - await createWallet(clientConfig, wallet_name); +// await removeDatabase(); +// await createWallet(clientConfig, wallet_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); +// await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); - await removeDatabase(); -})(); \ No newline at end of file +// await removeDatabase(); +// })(); \ No newline at end of file From 3b31ed2188db743e11d8ebc46265a94e384e65c7 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 12:49:51 +0530 Subject: [PATCH 030/153] debug: post request /deposit/init/pod --- .github/workflows/tests.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0dcda323..354b2784 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,7 +42,14 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_mercury_1") echo "Container ID: $container_id" docker logs $container_id - docker exec $container_id curl http://0.0.0.0:8000/info/config + docker exec $container_id \ + curl -X POST http://0.0.0.0:8000/deposit/init/pod \ + -H "Content-Type: application/json" \ + -d '{ + "auth_key": "f461775606ffc86e3f6e3115ff425d371b0f68cc59ad8cf71375c0e08c2ee8e9", + "token_id": "616f505d-b94c-45cf-b251-833e4fa14fa1", + "signed_token_id": "7401ac1f792f56d5357997f9846b5045656758f4afece4c51b73472bd338e97da3d167733502cf62d77c1169bd89bfaa0c9c5fcc26d75190e7a3a3fd2f83ae0a" + }' - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From 198a12b0b757a45671043916f9f573e3bd20739f Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 13:11:36 +0530 Subject: [PATCH 031/153] debug: internal server error for mercury server --- clients/apps/nodejs/config/default.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 480eaa4c..14d14f56 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,5 +1,5 @@ { - "statechainEntity": "http://0.0.0.0:8000", + "statechainEntity": "https://api.mercurylayer.com", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", From f62bcd977d377da9090354c6a4618f192a13feea Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 16:39:09 +0530 Subject: [PATCH 032/153] fix: expose port for ssl of electrum server --- .github/workflows/tests.yml | 1 + clients/apps/nodejs/config/default.json | 10 +++++----- docker-compose-test.yml | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 354b2784..3d02dc77 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,6 +21,7 @@ jobs: ports: - 5432:5432 - 18443:18443 + - 50002:50002 - 50001:50001 - 8000:8000 - 18080:18080 diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 14d14f56..b8497809 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -1,12 +1,12 @@ { - "statechainEntity": "https://api.mercurylayer.com", + "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", - "electrumServer": "ssl://mempool.space:60602", + "__electrumServer": "tcp://127.0.0.1:50001", + "electrumServer": "ssl://127.0.0.1:50002", "electrumType": "electrs", - "_electrumServer": "tcp://localhost:50001", - "network": "signet", + "_electrumServer": "tcp://127.0.0.1:50001", + "network": "regtest", "feeRateTolerance": 5, "databaseFile": "wallet.db", "confirmationTarget": 2, diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 6a00c02b..648d0d32 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -23,6 +23,7 @@ services: COIN: BitcoinSegwit NET: regtest ports: + - "50002:50002" - "50001:50001" enclave-sgx: From 67665b6a14e3bddbe24b2a607502ddc2465c22d9 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 17:28:01 +0530 Subject: [PATCH 033/153] debug: electrum server --- .github/workflows/tests.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3d02dc77..713dc1d9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 100 # Adjust time as necessary for services to initialize + sleep 90 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_mercury_1") @@ -51,6 +51,11 @@ jobs: "token_id": "616f505d-b94c-45cf-b251-833e4fa14fa1", "signed_token_id": "7401ac1f792f56d5357997f9846b5045656758f4afece4c51b73472bd338e97da3d167733502cf62d77c1169bd89bfaa0c9c5fcc26d75190e7a3a3fd2f83ae0a" }' + - name: Verify ElectrumX Service with Curl + run: | + container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") + echo "Container ID: $container_id" + docker logs $container_id - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From c11e64272462077b9683d9e13c49bd41911ad066 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 17:40:58 +0530 Subject: [PATCH 034/153] debug: bitcoin core --- .github/workflows/tests.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 713dc1d9..b293f7de 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,6 +51,11 @@ jobs: "token_id": "616f505d-b94c-45cf-b251-833e4fa14fa1", "signed_token_id": "7401ac1f792f56d5357997f9846b5045656758f4afece4c51b73472bd338e97da3d167733502cf62d77c1169bd89bfaa0c9c5fcc26d75190e7a3a3fd2f83ae0a" }' + - name: Verify Bitcoin core Service with Curl + run: | + container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") + echo "Container ID: $container_id" + docker logs $container_id - name: Verify ElectrumX Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") From 413e4be3c9d6179bf63a0297cca117ede97fdba5 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 17:53:31 +0530 Subject: [PATCH 035/153] fix: increase wait time to service ready for bitcoin core --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b293f7de..75478e36 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 90 # Adjust time as necessary for services to initialize + sleep 200 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_mercury_1") From 7dabdb00e130d55810b1f16e31867a5ce8869365 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 18:06:36 +0530 Subject: [PATCH 036/153] fix: command args for bitcoin core --- docker-compose-test.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 648d0d32..66d8350e 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -10,7 +10,15 @@ services: bitcoin: image: ruimarinho/bitcoin-core:latest - command: ["-regtest", "-server", "-rpcallowip=::/0", "-rpcbind=0.0.0.0", "-rpcuser=user", "-rpcpassword=pass"] + command: + -printtoconsole + -regtest=1 + -rpcallowip=0.0.0.0/0 + -rpcbind=0.0.0.0 + -rpcuser=user + -rpcpassword=pass + -txindex=1 + -server=1 ports: - "18443:18443" volumes: From 81756d996448f89908ff4d8fd2080554832ec9fa Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 18:25:21 +0530 Subject: [PATCH 037/153] debug: enclave --- .github/workflows/tests.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 75478e36..1dd27f15 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -61,6 +61,11 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") echo "Container ID: $container_id" docker logs $container_id + - name: Verify Enclave Service with Curl + run: | + container_id=$(docker ps -qf "name=mercurylayer_enclave-sgx_1") + echo "Container ID: $container_id" + docker logs $container_id - name: Add Mnemonic run: | docker exec $(docker ps -qf "name=enclave") \ From a2efc6dd3d5e154fff4715543ad0e9743d9e98e8 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 19:20:12 +0530 Subject: [PATCH 038/153] debug: enclave /get_public_key --- .github/workflows/tests.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1dd27f15..c41c7995 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 200 # Adjust time as necessary for services to initialize + sleep 90 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_mercury_1") @@ -77,6 +77,10 @@ jobs: "index": 0, "threshold": 2 }' + docker exec $(docker ps -qf "name=enclave") \ + curl -X POST http://0.0.0.0:18080/get_public_key \ + -H "Content-Type: application/json" \ + -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' - name: Set up Node.js uses: actions/setup-node@v2 with: From 47adaf6a225cba0be75cb9d6bdd0645ada2178ee Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 31 May 2024 19:40:56 +0530 Subject: [PATCH 039/153] fix: add port for enclave to docker-compose --- docker-compose-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 66d8350e..46085fce 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -44,6 +44,8 @@ services: ENCLAVE_DATABASE_URL: postgres://postgres:pgpassword@postgres:5432/postgres ENCLAVE_PORT: 18080 SEED_DIR: ./seed + ports: + - "18080:18080" mercury: image: commerceblockx/mercury-server:3a686af From 3800f295eb3308413cd4c836fb0a97b5859cf94a Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 11:10:20 +0530 Subject: [PATCH 040/153] debug: get public key from enclave --- .github/workflows/tests.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c41c7995..46479ea6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -77,6 +77,11 @@ jobs: "index": 0, "threshold": 2 }' + - name: Wait for setting up mnemonic + run: | + sleep 10 # Adjust time as necessary for services to initialize + - name: Get Public Key + run: | docker exec $(docker ps -qf "name=enclave") \ curl -X POST http://0.0.0.0:18080/get_public_key \ -H "Content-Type: application/json" \ From eabb3501edfdac7f5f4e40a39118d6506ad488ff Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 13:25:38 +0530 Subject: [PATCH 041/153] debug: use lockbox url of testnet --- clients/apps/nodejs/test_basic_workflow2.js | 77 ++++++++++++++++++++- docker-compose-test.yml | 2 +- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 80c5ffb7..a2cf8e75 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -228,6 +228,63 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log("txid: ", txid); } +async function depositAndRepeatSend(clientConfig, wallet_1_name) { + const amount = 10000; + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + deposit_info["amount"] = amount; + console.log("deposit_coin: ", deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + for (let i = 0; i < 1000; i++) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + } + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); +} (async () => { @@ -260,4 +317,22 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle // await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); // await removeDatabase(); -// })(); \ No newline at end of file +// })(); + +// Deposit, repeat send +// (async () => { +// const clientConfig = client_config.load(); + +// let wallet_1_name = "w1"; +// let wallet_2_name = "w2"; + +// await removeDatabase(); +// await createWallet(clientConfig, wallet_1_name); +// await createWallet(clientConfig, wallet_2_name); + +// await depositAndRepeatSend(clientConfig, wallet_1_name); + +// await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + +// await removeDatabase(); +// })(); diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 46085fce..3e187f02 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -50,7 +50,7 @@ services: mercury: image: commerceblockx/mercury-server:3a686af environment: - LOCKBOX: http://enclave-sgx:18080 + LOCKBOX: http://209.250.247.84:18080 NETWORK: regtest LOCKHEIGHT_INIT: 1000 LH_DECREMENT: 10 From a1ec824b87a9e166acc5065b399bf285e630d6f8 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 13:56:49 +0530 Subject: [PATCH 042/153] fix: enclave dockerfile cmd --- docker-compose-test.yml | 2 +- enclave/Dockerfiles/SIM/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 3e187f02..46085fce 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -50,7 +50,7 @@ services: mercury: image: commerceblockx/mercury-server:3a686af environment: - LOCKBOX: http://209.250.247.84:18080 + LOCKBOX: http://enclave-sgx:18080 NETWORK: regtest LOCKHEIGHT_INIT: 1000 LH_DECREMENT: 10 diff --git a/enclave/Dockerfiles/SIM/Dockerfile b/enclave/Dockerfiles/SIM/Dockerfile index 42a77598..38bc106a 100644 --- a/enclave/Dockerfiles/SIM/Dockerfile +++ b/enclave/Dockerfiles/SIM/Dockerfile @@ -69,4 +69,4 @@ RUN rm -rf Dockerfiles # Use the SGX_MODE argument in the make command RUN make SGX_MODE=SIM INCLUDE_SECP256K1_ZKP=1 -CMD ["/home/lockbox-sgx/app"] +CMD ["/home/lockbox-sgx/app -g"] From 37609d0f9fd0f14fd14e55721223c62eb808467b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 14:14:25 +0530 Subject: [PATCH 043/153] fix: cmd for enclave server --- enclave/Dockerfiles/SIM/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enclave/Dockerfiles/SIM/Dockerfile b/enclave/Dockerfiles/SIM/Dockerfile index 38bc106a..7af80dcd 100644 --- a/enclave/Dockerfiles/SIM/Dockerfile +++ b/enclave/Dockerfiles/SIM/Dockerfile @@ -69,4 +69,4 @@ RUN rm -rf Dockerfiles # Use the SGX_MODE argument in the make command RUN make SGX_MODE=SIM INCLUDE_SECP256K1_ZKP=1 -CMD ["/home/lockbox-sgx/app -g"] +CMD ["/home/lockbox-sgx/app", "-g"] From 1d39b208d06305ed08706502db87b72da1b5ab56 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 14:30:41 +0530 Subject: [PATCH 044/153] debug: deposit msg --- clients/apps/react-app/src/main/deposit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/react-app/src/main/deposit.js b/clients/apps/react-app/src/main/deposit.js index cbaa89b1..b5df3b8d 100644 --- a/clients/apps/react-app/src/main/deposit.js +++ b/clients/apps/react-app/src/main/deposit.js @@ -120,7 +120,7 @@ const initPod = async (depositMsg1) => { if (torProxy) { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) } } - + console.log('depositMsg1 ->', depositMsg1) const response = await axios.post(url, depositMsg1, socksAgent) if (response.status != 200) { From 4a8e0a5a07d76f4fa4087ec74f7ad1e0efdc6bda Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 14:39:05 +0530 Subject: [PATCH 045/153] debug: depositMsg for /deposit/init/pod --- clients/apps/react-app/src/main/deposit.js | 2 +- clients/libs/nodejs/deposit.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/apps/react-app/src/main/deposit.js b/clients/apps/react-app/src/main/deposit.js index b5df3b8d..cbaa89b1 100644 --- a/clients/apps/react-app/src/main/deposit.js +++ b/clients/apps/react-app/src/main/deposit.js @@ -120,7 +120,7 @@ const initPod = async (depositMsg1) => { if (torProxy) { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) } } - console.log('depositMsg1 ->', depositMsg1) + const response = await axios.post(url, depositMsg1, socksAgent) if (response.status != 200) { diff --git a/clients/libs/nodejs/deposit.js b/clients/libs/nodejs/deposit.js index 46978656..eedea6fc 100644 --- a/clients/libs/nodejs/deposit.js +++ b/clients/libs/nodejs/deposit.js @@ -93,7 +93,7 @@ const init = async (clientConfig, db, wallet, token_id) => { if (torProxy) { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - + console.log('depositMsg1 ->', depositMsg1) const response = await axios.post(url, depositMsg1, socksAgent); if (response.status != 200) { From 81b1cc18caa5c041d77e5c790675d5a88a3cea2b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 16:32:00 +0530 Subject: [PATCH 046/153] debug: statechain_id in pod_deposit --- server/src/endpoints/deposit.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/endpoints/deposit.rs b/server/src/endpoints/deposit.rs index aff61f94..636a1ab6 100644 --- a/server/src/endpoints/deposit.rs +++ b/server/src/endpoints/deposit.rs @@ -148,7 +148,8 @@ pub async fn post_deposit(statechain_entity: &State, deposit_m Err(err) => { let response_body = json!({ "error": "Internal Server Error", - "message": err.to_string() + "message": err.to_string(), + "payload": statechain_id.clone(), }); return status::Custom(Status::InternalServerError, Json(response_body)); From ca6041fd89b6826835452921b58d2ed1e29c0f58 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 19:16:00 +0530 Subject: [PATCH 047/153] debug: enclave logs --- .github/workflows/tests.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 46479ea6..63092b4d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -86,6 +86,7 @@ jobs: curl -X POST http://0.0.0.0:18080/get_public_key \ -H "Content-Type: application/json" \ -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' + docker logs $(docker ps -qf "name=enclave") - name: Set up Node.js uses: actions/setup-node@v2 with: @@ -112,6 +113,13 @@ jobs: run: | cd clients/apps/nodejs node test_basic_workflow2.js + - name: Get Public Key + run: | + docker exec $(docker ps -qf "name=enclave") \ + curl -X POST http://0.0.0.0:18080/get_public_key \ + -H "Content-Type: application/json" \ + -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' + docker logs $(docker ps -qf "name=enclave") - name: Tear Down run: | docker-compose down From e31cd5085621f31b170fc4193ac7d5ab0d8b2735 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 19:40:58 +0530 Subject: [PATCH 048/153] debig: network connectivity between containers --- .github/workflows/tests.yml | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 63092b4d..0fb3febe 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -87,6 +87,22 @@ jobs: -H "Content-Type: application/json" \ -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' docker logs $(docker ps -qf "name=enclave") + - name: Check connectivity between containers + run: | + # Get container IDs + enclave_container=$(docker ps -qf "name=mercurylayer_enclave-sgx_1") + mercury_container=$(docker ps -qf "name=mercurylayer_mercury_1") + + # Check if mercurylayer_mercury_1 can reach mercurylayer_enclave-sgx_1 + docker exec $mercury_container curl -v http://mercurylayer_enclave-sgx_1:18080/get_public_key \ + -H "Content-Type: application/json" \ + -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' + + # Alternatively, using IP address if service name resolution fails + enclave_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $enclave_container) + docker exec $mercury_container curl -v http://$enclave_ip:18080/get_public_key \ + -H "Content-Type: application/json" \ + -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' - name: Set up Node.js uses: actions/setup-node@v2 with: @@ -113,13 +129,6 @@ jobs: run: | cd clients/apps/nodejs node test_basic_workflow2.js - - name: Get Public Key - run: | - docker exec $(docker ps -qf "name=enclave") \ - curl -X POST http://0.0.0.0:18080/get_public_key \ - -H "Content-Type: application/json" \ - -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' - docker logs $(docker ps -qf "name=enclave") - name: Tear Down run: | docker-compose down From 7f9213ec76b7f4ce114597a62805bc300015ff9b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 19:52:47 +0530 Subject: [PATCH 049/153] debug: lockbox url --- docker-compose-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 46085fce..2a71f4d9 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -50,7 +50,7 @@ services: mercury: image: commerceblockx/mercury-server:3a686af environment: - LOCKBOX: http://enclave-sgx:18080 + LOCKBOX: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest LOCKHEIGHT_INIT: 1000 LH_DECREMENT: 10 From 6d8d2103828e0196a96479ac4a0bfcde10085b7d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 20:17:58 +0530 Subject: [PATCH 050/153] fix: mercury server in docker compose --- docker-compose-test.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 2a71f4d9..2ba98894 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -48,7 +48,9 @@ services: - "18080:18080" mercury: - image: commerceblockx/mercury-server:3a686af + build: + context: server + dockerfile: Dockerfile environment: LOCKBOX: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest From 5b33c3d99d40551f3fe3816cb3c005509a7bacfc Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 20:28:45 +0530 Subject: [PATCH 051/153] fix: dockerfile location in docker compose --- docker-compose-test.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 2ba98894..63b77508 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -36,8 +36,8 @@ services: enclave-sgx: build: - context: enclave - dockerfile: Dockerfiles/SIM/Dockerfile + context: . + dockerfile: ./enclave/Dockerfiles/SIM/Dockerfile depends_on: - postgres environment: @@ -49,8 +49,8 @@ services: mercury: build: - context: server - dockerfile: Dockerfile + context: . + dockerfile: ./server/Dockerfile environment: LOCKBOX: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest From 3771341654fd78973bbbb7d289f352dfbeca4306 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 20:34:57 +0530 Subject: [PATCH 052/153] fix: dockerfile location in mercury server docker compose --- docker-compose-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 63b77508..573c9458 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -36,8 +36,8 @@ services: enclave-sgx: build: - context: . - dockerfile: ./enclave/Dockerfiles/SIM/Dockerfile + context: enclave + dockerfile: Dockerfiles/SIM/Dockerfile depends_on: - postgres environment: From 1ccdebcb4f4f0fbb0dfd72352bd96523801c4185 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 20:56:23 +0530 Subject: [PATCH 053/153] fix: lockbox env name --- server/src/server_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/server_config.rs b/server/src/server_config.rs index e0ca67f1..9d72036d 100644 --- a/server/src/server_config.rs +++ b/server/src/server_config.rs @@ -67,7 +67,7 @@ impl ServerConfig { }; ServerConfig { - lockbox: Some(get_env_or_config("lockbox", "LOCKBOX_URL")), + lockbox: Some(get_env_or_config("lockbox", "LOCKBOX")), network: get_env_or_config("network", "BITCOIN_NETWORK"), lockheight_init: get_env_or_config("lockheight_init", "LOCKHEIGHT_INIT").parse::().unwrap(), lh_decrement: get_env_or_config("lh_decrement", "LH_DECREMENT").parse::().unwrap(), From 931eee0316969a5a0a62c99805e6f5f3ccf586b8 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 21:04:29 +0530 Subject: [PATCH 054/153] fix: env name for lockbox --- docker-compose-test.yml | 2 +- server/.env_example | 2 +- server/src/server_config.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 573c9458..06521d0c 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -52,7 +52,7 @@ services: context: . dockerfile: ./server/Dockerfile environment: - LOCKBOX: http://mercurylayer_enclave-sgx_1:18080 + LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest LOCKHEIGHT_INIT: 1000 LH_DECREMENT: 10 diff --git a/server/.env_example b/server/.env_example index 28584a0d..f584779e 100644 --- a/server/.env_example +++ b/server/.env_example @@ -1,4 +1,4 @@ -LOCKBOX = +LOCKBOX_URL = NETWORK = LOCKHEIGHT_INIT = LH_DECREMENT = diff --git a/server/src/server_config.rs b/server/src/server_config.rs index 9d72036d..e0ca67f1 100644 --- a/server/src/server_config.rs +++ b/server/src/server_config.rs @@ -67,7 +67,7 @@ impl ServerConfig { }; ServerConfig { - lockbox: Some(get_env_or_config("lockbox", "LOCKBOX")), + lockbox: Some(get_env_or_config("lockbox", "LOCKBOX_URL")), network: get_env_or_config("network", "BITCOIN_NETWORK"), lockheight_init: get_env_or_config("lockheight_init", "LOCKHEIGHT_INIT").parse::().unwrap(), lh_decrement: get_env_or_config("lh_decrement", "LH_DECREMENT").parse::().unwrap(), From d9358e0d556d1b76545456cf294d27c84ebb81cc Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 3 Jun 2024 21:38:41 +0530 Subject: [PATCH 055/153] feat: add deposit using bitcoin-cli --- .github/workflows/tests.yml | 14 -------------- clients/apps/nodejs/test_basic_workflow2.js | 7 +++++++ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0fb3febe..128ed965 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -66,20 +66,6 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_enclave-sgx_1") echo "Container ID: $container_id" docker logs $container_id - - name: Add Mnemonic - run: | - docker exec $(docker ps -qf "name=enclave") \ - curl -X POST http://0.0.0.0:18080/add_mnemonic \ - -H "Content-Type: application/json" \ - -d '{ - "mnemonic": "achieve merry hidden lyrics element brand student armed dismiss vague fury avocado grief crazy garlic gallery blur spider bag bless motor crawl surround copper", - "password": "b1gHKyfXTzs6", - "index": 0, - "threshold": 2 - }' - - name: Wait for setting up mnemonic - run: | - sleep 10 # Adjust time as necessary for services to initialize - name: Get Public Key run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index a2cf8e75..afb165b6 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -45,6 +45,13 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); + const amountInBtc = 0.0001; + + // Sending Bitcoin using bitcoin-cli + const sendBitcoinCommand = `bitcoin-cli sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + const execSync = require('child_process').execSync; + execSync(sendBitcoinCommand); + let coin = undefined; while (!coin) { From b49db0bba89c7c9d477c085a8b62d6415c3d6744 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 11:33:13 +0530 Subject: [PATCH 056/153] debug: bitcoin-cli for deposit --- clients/apps/nodejs/test_basic_workflow2.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index afb165b6..b16cbadd 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -48,9 +48,14 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { const amountInBtc = 0.0001; // Sending Bitcoin using bitcoin-cli - const sendBitcoinCommand = `bitcoin-cli sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - const execSync = require('child_process').execSync; - execSync(sendBitcoinCommand); + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + execSync(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } let coin = undefined; From 22401376c7aa614809156756e12f88bb58016805 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 11:53:29 +0530 Subject: [PATCH 057/153] fix: bitcoin-cli cmd execution --- .github/workflows/tests.yml | 1 + clients/apps/nodejs/test_basic_workflow2.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 128ed965..0da06422 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,6 +56,7 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") echo "Container ID: $container_id" docker logs $container_id + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index b16cbadd..08fc5860 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -50,7 +50,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { // Sending Bitcoin using bitcoin-cli try { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - execSync(sendBitcoinCommand); + exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); } catch (error) { console.error('Error sending Bitcoin:', error.message); From 17c2fb5397939ece0949e300b986aabfde9ab03d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 12:14:49 +0530 Subject: [PATCH 058/153] fix: create wallet before send to address --- .github/workflows/tests.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0da06422..17fc7b91 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,6 +56,10 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") echo "Container ID: $container_id" docker logs $container_id + wallet_name="new_wallet" + address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name | jq -r '.warning') + echo "New Wallet Address: $address" + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 $address docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | From 9daa8845af0b2010e4d022463cd5b895f15d1b6d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 12:30:37 +0530 Subject: [PATCH 059/153] fix: address in deposit --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 17fc7b91..324a11a0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,7 +59,7 @@ jobs: wallet_name="new_wallet" address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name | jq -r '.warning') echo "New Wallet Address: $address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 $address + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | From a47f194fae16bc468a1de7dc3f38698a331ad384 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 12:53:39 +0530 Subject: [PATCH 060/153] fix: address in generatetoaddress --- .github/workflows/tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 324a11a0..abda5d87 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -57,7 +57,8 @@ jobs: echo "Container ID: $container_id" docker logs $container_id wallet_name="new_wallet" - address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name | jq -r '.warning') + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name + address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 From d7e4b6a4c4583522a3aa2d2f162c1d28148501ea Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 13:07:24 +0530 Subject: [PATCH 061/153] fix: add rpcfee field to sendtoaddress cmd --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index abda5d87..783d0ecd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -61,7 +61,7 @@ jobs: address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 -rpcfee=0.00001 - name: Verify ElectrumX Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") From 5d6d3c2fed48d1a8e1a636a6493724846f695169 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 13:22:25 +0530 Subject: [PATCH 062/153] fix: increase rpcfee --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 783d0ecd..5f11f8fa 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -61,7 +61,7 @@ jobs: address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 -rpcfee=0.00001 + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 -rpcfee=0.001 - name: Verify ElectrumX Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") From 80a85062ab97cb0b38c0ecb3875ced9e7256fd09 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 13:47:11 +0530 Subject: [PATCH 063/153] fix: add fallbackfee to bitcoin core --- .github/workflows/tests.yml | 2 +- docker-compose-test.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5f11f8fa..abda5d87 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -61,7 +61,7 @@ jobs: address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 -rpcfee=0.001 + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 06521d0c..b94e6d6c 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -19,6 +19,7 @@ services: -rpcpassword=pass -txindex=1 -server=1 + -fallbackfee=0.0001 ports: - "18443:18443" volumes: From 69a1183123adc1a7070db8359340540da96c5bd3 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 14:28:04 +0530 Subject: [PATCH 064/153] fix: generate block after deposit --- clients/apps/nodejs/test_basic_workflow2.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 08fc5860..93b1c088 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -52,6 +52,9 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 1 "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated a block`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; From 211fa4028d94f19eb24de1c7bfdad491e684dce3 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 14:45:27 +0530 Subject: [PATCH 065/153] fix: increase generate block to confirmation target --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 93b1c088..8333f416 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -52,7 +52,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 1 "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); console.log(`Generated a block`); } catch (error) { From b1daf77db933743e8b4e39fd40ac32b891eb9553 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 15:55:15 +0530 Subject: [PATCH 066/153] fix: change withdraw address to regtest --- .github/workflows/tests.yml | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index abda5d87..a75be86a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 90 # Adjust time as necessary for services to initialize + sleep 60 # Adjust time as necessary for services to initialize - name: Verify Mercury Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_mercury_1") diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 8333f416..2d3daa69 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -100,7 +100,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { assert(received_statechain_ids[0] == coin.statechain_id); } - let withdraw_address = "tb1qwrujs6f4gyexsextpf9p50smjtht7p7ypknteu"; + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); From 03c9048759e4298079c2df95b39b7e24a3366f66 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 16:09:46 +0530 Subject: [PATCH 067/153] fix: adjust sleep time for services to be ready --- .github/workflows/tests.yml | 28 ++++++------ clients/apps/nodejs/test_basic_workflow2.js | 49 ++++++++++++++++++++- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a75be86a..014b8889 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,20 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 60 # Adjust time as necessary for services to initialize - - name: Verify Mercury Service with Curl - run: | - container_id=$(docker ps -qf "name=mercurylayer_mercury_1") - echo "Container ID: $container_id" - docker logs $container_id - docker exec $container_id \ - curl -X POST http://0.0.0.0:8000/deposit/init/pod \ - -H "Content-Type: application/json" \ - -d '{ - "auth_key": "f461775606ffc86e3f6e3115ff425d371b0f68cc59ad8cf71375c0e08c2ee8e9", - "token_id": "616f505d-b94c-45cf-b251-833e4fa14fa1", - "signed_token_id": "7401ac1f792f56d5357997f9846b5045656758f4afece4c51b73472bd338e97da3d167733502cf62d77c1169bd89bfaa0c9c5fcc26d75190e7a3a3fd2f83ae0a" - }' + sleep 80 # Adjust time as necessary for services to initialize - name: Verify Bitcoin core Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") @@ -72,6 +59,19 @@ jobs: container_id=$(docker ps -qf "name=mercurylayer_enclave-sgx_1") echo "Container ID: $container_id" docker logs $container_id + - name: Verify Mercury Service with Curl + run: | + container_id=$(docker ps -qf "name=mercurylayer_mercury_1") + echo "Container ID: $container_id" + docker logs $container_id + docker exec $container_id \ + curl -X POST http://0.0.0.0:8000/deposit/init/pod \ + -H "Content-Type: application/json" \ + -d '{ + "auth_key": "f461775606ffc86e3f6e3115ff425d371b0f68cc59ad8cf71375c0e08c2ee8e9", + "token_id": "616f505d-b94c-45cf-b251-833e4fa14fa1", + "signed_token_id": "7401ac1f792f56d5357997f9846b5045656758f4afece4c51b73472bd338e97da3d167733502cf62d77c1169bd89bfaa0c9c5fcc26d75190e7a3a3fd2f83ae0a" + }' - name: Get Public Key run: | docker exec $(docker ps -qf "name=enclave") \ diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 2d3daa69..63b8cd7d 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -125,6 +125,21 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); + const amountInBtc = 0.0001; + + // Sending Bitcoin using bitcoin-cli + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + exec(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated a block`); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } + let coin = undefined; while (!coin) { @@ -175,7 +190,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("Updated coin: ", coin); } - let withdraw_address = "tb1qwrujs6f4gyexsextpf9p50smjtht7p7ypknteu"; + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); @@ -200,6 +215,21 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); + const amountInBtc = 0.0001; + + // Sending Bitcoin using bitcoin-cli + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + exec(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated a block`); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } + let coin = undefined; console.log("coin: ", coin); @@ -236,7 +266,7 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle assert(received_statechain_ids.length > 0); assert(received_statechain_ids[0] == coin.statechain_id); - let withdraw_address = "tb1qwrujs6f4gyexsextpf9p50smjtht7p7ypknteu"; + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; let txid = await mercurynodejslib.broadcastBackupTransaction(clientConfig, wallet_2_name, coin.statechain_id, withdraw_address, null); @@ -259,6 +289,21 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); + const amountInBtc = 0.0001; + + // Sending Bitcoin using bitcoin-cli + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + exec(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated a block`); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } + let coin = undefined; console.log("coin: ", coin); From c65dfccbed398fa7cc719d0479c0c80654642bbf Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 16:33:12 +0530 Subject: [PATCH 068/153] debug: wallet database deletion --- .github/workflows/tests.yml | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 38 ++++++++++----------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 014b8889..2179a0bf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 80 # Adjust time as necessary for services to initialize + sleep 75 # Adjust time as necessary for services to initialize - name: Verify Bitcoin core Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 63b8cd7d..93e42b9e 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -365,34 +365,34 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { })(); // Deposit, iterative self transfer -// (async () => { +(async () => { -// const clientConfig = client_config.load(); + const clientConfig = client_config.load(); -// let wallet_name = "w3"; + let wallet_name = "w3"; -// await removeDatabase(); -// await createWallet(clientConfig, wallet_name); + await removeDatabase(); + await createWallet(clientConfig, wallet_name); -// await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); -// await removeDatabase(); -// })(); + await removeDatabase(); +})(); // Deposit, repeat send -// (async () => { -// const clientConfig = client_config.load(); +(async () => { + const clientConfig = client_config.load(); -// let wallet_1_name = "w1"; -// let wallet_2_name = "w2"; + let wallet_1_name = "w1"; + let wallet_2_name = "w2"; -// await removeDatabase(); -// await createWallet(clientConfig, wallet_1_name); -// await createWallet(clientConfig, wallet_2_name); + await removeDatabase(); + await createWallet(clientConfig, wallet_1_name); + await createWallet(clientConfig, wallet_2_name); -// await depositAndRepeatSend(clientConfig, wallet_1_name); + await depositAndRepeatSend(clientConfig, wallet_1_name); -// await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); -// await removeDatabase(); -// })(); + await removeDatabase(); +})(); From 050d008604c9238e30b2c4b28da56c55faeb876b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 16:50:48 +0530 Subject: [PATCH 069/153] fix: change wallet name to fix sql error --- clients/apps/nodejs/test_basic_workflow2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 93e42b9e..16346f03 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -383,8 +383,8 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { (async () => { const clientConfig = client_config.load(); - let wallet_1_name = "w1"; - let wallet_2_name = "w2"; + let wallet_1_name = "w4"; + let wallet_2_name = "w5"; await removeDatabase(); await createWallet(clientConfig, wallet_1_name); From 0617e7813c0b5868b1c83701ffea88c79b736f08 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 17:24:35 +0530 Subject: [PATCH 070/153] fix: db lock error --- clients/apps/nodejs/test_basic_workflow2.js | 34 ++++----------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 16346f03..07226232 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -8,10 +8,10 @@ const client_config = require('./client_config'); async function removeDatabase() { try { const { stdout, stderr } = await exec('rm wallet.db'); - // console.log('stdout:', stdout); - // console.error('stderr:', stderr); + console.log('stdout:', stdout); + console.error('stderr:', stderr); } catch (e) { - // console.error(e); + console.error(e); } } @@ -356,43 +356,21 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { await removeDatabase(); await createWallet(clientConfig, wallet_1_name); await createWallet(clientConfig, wallet_2_name); - await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - await removeDatabase(); -})(); - -// Deposit, iterative self transfer -(async () => { - - const clientConfig = client_config.load(); - - let wallet_name = "w3"; + // Deposit, iterative self transfer await removeDatabase(); - await createWallet(clientConfig, wallet_name); - + await createWallet(clientConfig, wallet_1_name); await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); - await removeDatabase(); -})(); - -// Deposit, repeat send -(async () => { - const clientConfig = client_config.load(); - - let wallet_1_name = "w4"; - let wallet_2_name = "w5"; + // Deposit, repeat send await removeDatabase(); await createWallet(clientConfig, wallet_1_name); await createWallet(clientConfig, wallet_2_name); - await depositAndRepeatSend(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - await removeDatabase(); })(); From 7bda2988d5e1bea4ddd6c8de740f9667609d86bb Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 17:56:43 +0530 Subject: [PATCH 071/153] fix: path in removeDatabase fn --- clients/apps/nodejs/test_basic_workflow2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 07226232..f53fd801 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -7,7 +7,7 @@ const client_config = require('./client_config'); async function removeDatabase() { try { - const { stdout, stderr } = await exec('rm wallet.db'); + const { stdout, stderr } = await exec(`rm ../../apps/nodejs/${client_config.databaseFile}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { @@ -363,7 +363,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { // Deposit, iterative self transfer await removeDatabase(); await createWallet(clientConfig, wallet_1_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); await removeDatabase(); // Deposit, repeat send From a54ccd1e3baf040735dc2415d3f0ec04b701b0d8 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 18:10:46 +0530 Subject: [PATCH 072/153] fix: databaseFile variable --- clients/apps/nodejs/test_basic_workflow2.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index f53fd801..31f41244 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -6,8 +6,9 @@ const { CoinStatus } = require('mercurynodejslib/coin_enum'); const client_config = require('./client_config'); async function removeDatabase() { - try { - const { stdout, stderr } = await exec(`rm ../../apps/nodejs/${client_config.databaseFile}`); + try { + const clientConfig = client_config.load(); + const { stdout, stderr } = await exec(`rm ../../apps/nodejs/${clientConfig.databaseFile}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { From 7e5b3af3f82cfb874964d317658445eba60befbe Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 20:58:53 +0530 Subject: [PATCH 073/153] fix: blockheight from electrum server --- .github/workflows/tests.yml | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2179a0bf..014b8889 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: docker-compose -f docker-compose-test.yml up --build -d - name: Wait for services to be ready run: | - sleep 75 # Adjust time as necessary for services to initialize + sleep 80 # Adjust time as necessary for services to initialize - name: Verify Bitcoin core Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 31f41244..1a02c14e 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -164,7 +164,9 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("coin: ", coin); - let block_header = client_config.electrum_client.block_headers_subscribe_raw(); + const electrumClient = await getElectrumClient(clientConfig); + + let block_header = await electrumClient.request('blockchain.headers.subscribe'); let currentBlockHeight = block_header.height; console.log("Current block height: ", currentBlockHeight); From 03401cc857c97dca93e963efa08e8ef20360ad40 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 4 Jun 2024 21:31:32 +0530 Subject: [PATCH 074/153] fix: getElectrumClient import --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- clients/libs/nodejs/index.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 1a02c14e..440f8639 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -164,7 +164,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("coin: ", coin); - const electrumClient = await getElectrumClient(clientConfig); + const electrumClient = await mercurynodejslib.getElectrumClient(clientConfig); let block_header = await electrumClient.request('blockchain.headers.subscribe'); let currentBlockHeight = block_header.height; diff --git a/clients/libs/nodejs/index.js b/clients/libs/nodejs/index.js index 180cb912..f1813be3 100644 --- a/clients/libs/nodejs/index.js +++ b/clients/libs/nodejs/index.js @@ -196,5 +196,6 @@ module.exports = { withdrawCoin, newTransferAddress, transferSend, - transferReceive + transferReceive, + getElectrumClient }; \ No newline at end of file From afc3bd48549d5197def45a9182fd7e60cb6dc40b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 12:52:53 +0530 Subject: [PATCH 075/153] debug: db file location --- clients/apps/nodejs/test_basic_workflow2.js | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 440f8639..c7704d33 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -8,7 +8,7 @@ const client_config = require('./client_config'); async function removeDatabase() { try { const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`rm ../../apps/nodejs/${clientConfig.databaseFile}`); + const { stdout, stderr } = await exec(`ls`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { @@ -55,7 +55,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); - console.log(`Generated a block`); + console.log(`Generated ${clientConfig.confirmationTarget} blocks`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -135,7 +135,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); - console.log(`Generated a block`); + console.log(`Generated ${clientConfig.confirmationTarget} blocks`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -227,7 +227,7 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); - console.log(`Generated a block`); + console.log(`Generated ${clientConfig.confirmationTarget} blocks`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -301,7 +301,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); - console.log(`Generated a block`); + console.log(`Generated ${clientConfig.confirmationTarget} blocks`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -363,17 +363,17 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); await removeDatabase(); - // Deposit, iterative self transfer - await removeDatabase(); - await createWallet(clientConfig, wallet_1_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); - await removeDatabase(); - - // Deposit, repeat send - await removeDatabase(); - await createWallet(clientConfig, wallet_1_name); - await createWallet(clientConfig, wallet_2_name); - await depositAndRepeatSend(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - await removeDatabase(); + // // Deposit, iterative self transfer + // await removeDatabase(); + // await createWallet(clientConfig, wallet_1_name); + // await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); + // await removeDatabase(); + + // // Deposit, repeat send + // await removeDatabase(); + // await createWallet(clientConfig, wallet_1_name); + // await createWallet(clientConfig, wallet_2_name); + // await depositAndRepeatSend(clientConfig, wallet_1_name); + // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + // await removeDatabase(); })(); From 7af8e7916d2cd313b2eade991651f4515a3180f5 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 13:09:35 +0530 Subject: [PATCH 076/153] debug: find wallet.db file --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index c7704d33..e11d08c4 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -8,7 +8,7 @@ const client_config = require('./client_config'); async function removeDatabase() { try { const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`ls`); + const { stdout, stderr } = await exec(`find . -type f -name wallet.db -print -o -path .. -prune -o -print`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { From d8fb7e3d947885999ef18f1eb3030facd36ddad0 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 13:25:40 +0530 Subject: [PATCH 077/153] fix: removeDb fn --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index e11d08c4..ba9b1cde 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -8,7 +8,7 @@ const client_config = require('./client_config'); async function removeDatabase() { try { const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`find . -type f -name wallet.db -print -o -path .. -prune -o -print`); + const { stdout, stderr } = await exec(`rm ${clientConfig.databaseFile}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { From cfaf3ce325d3f054093cee6d71b99726758669f6 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 13:39:39 +0530 Subject: [PATCH 078/153] fix: wallet names in tests --- clients/apps/nodejs/test_basic_workflow2.js | 29 ++++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index ba9b1cde..10363394 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -8,7 +8,7 @@ const client_config = require('./client_config'); async function removeDatabase() { try { const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`rm ${clientConfig.databaseFile}`); + const { stdout, stderr } = await exec(`rm ./${clientConfig.databaseFile}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { @@ -364,16 +364,19 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { await removeDatabase(); // // Deposit, iterative self transfer - // await removeDatabase(); - // await createWallet(clientConfig, wallet_1_name); - // await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_1_name); - // await removeDatabase(); - - // // Deposit, repeat send - // await removeDatabase(); - // await createWallet(clientConfig, wallet_1_name); - // await createWallet(clientConfig, wallet_2_name); - // await depositAndRepeatSend(clientConfig, wallet_1_name); - // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - // await removeDatabase(); + let wallet_3_name = "w3"; + await removeDatabase(); + await createWallet(clientConfig, wallet_3_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_3_name); + await removeDatabase(); + + // Deposit, repeat send + let wallet_4_name = "w4"; + let wallet_5_name = "w5"; + await removeDatabase(); + await createWallet(clientConfig, wallet_4_name); + await createWallet(clientConfig, wallet_5_name); + await depositAndRepeatSend(clientConfig, wallet_4_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_4_name, wallet_5_name); + await removeDatabase(); })(); From 101058054bb6375f1e3a1c2b75d903747b56a999 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 15:10:19 +0530 Subject: [PATCH 079/153] fix: lockheight of mercury server --- .github/workflows/tests.yml | 8 +++++++- clients/libs/nodejs/index.js | 3 +++ docker-compose-test.yml | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 014b8889..1c58f469 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -47,7 +47,7 @@ jobs: docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 100 "$address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | @@ -121,6 +121,12 @@ jobs: run: | cd clients/apps/nodejs node test_basic_workflow2.js + - name: Print environment variables + run: printenv + - name: Print working directory + run: pwd + - name: List files in working directory + run: ls -al - name: Tear Down run: | docker-compose down diff --git a/clients/libs/nodejs/index.js b/clients/libs/nodejs/index.js index f1813be3..add8de25 100644 --- a/clients/libs/nodejs/index.js +++ b/clients/libs/nodejs/index.js @@ -18,6 +18,9 @@ const wallet_manager = require('./wallet'); const getDatabase = async (clientConfig) => { const databaseFile = clientConfig.databaseFile; + const absolutePath = path.resolve(databaseFile); + console.log(`Database file will be created at: ${absolutePath}`); + const db = new sqlite3.Database(databaseFile); await sqlite_manager.createTables(db); return db; diff --git a/docker-compose-test.yml b/docker-compose-test.yml index b94e6d6c..04cb86ed 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -55,7 +55,7 @@ services: environment: LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest - LOCKHEIGHT_INIT: 1000 + LOCKHEIGHT_INIT: 100 LH_DECREMENT: 10 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: From 3b411077781ff9b2fdbfee9cb8bbd3528a539349 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 15:26:12 +0530 Subject: [PATCH 080/153] fix: initial blocks generation --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1c58f469..49a9584d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -47,7 +47,7 @@ jobs: docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass createwallet $wallet_name address=$(docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass getnewaddress $wallet_name) echo "New Wallet Address: $address" - docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 100 "$address" + docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress 101 "$address" docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | From 1c729c6840c70e67db4ad2e3b88a93356d308512 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 16:49:49 +0530 Subject: [PATCH 081/153] fix: path import --- clients/libs/nodejs/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/libs/nodejs/index.js b/clients/libs/nodejs/index.js index add8de25..5abcbb19 100644 --- a/clients/libs/nodejs/index.js +++ b/clients/libs/nodejs/index.js @@ -15,6 +15,7 @@ const sqlite_manager = require('./sqlite_manager'); const { v4: uuidv4 } = require('uuid'); const wallet_manager = require('./wallet'); +const path = require('path'); const getDatabase = async (clientConfig) => { const databaseFile = clientConfig.databaseFile; From 5277ea88b8badd92be9002143cf41e170eda5c4c Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 17:06:37 +0530 Subject: [PATCH 082/153] fix: path in removeDatabase fn --- clients/apps/nodejs/test_basic_workflow2.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 10363394..a07da0ea 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -7,8 +7,10 @@ const client_config = require('./client_config'); async function removeDatabase() { try { - const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`rm ./${clientConfig.databaseFile}`); + const clientConfig = client_config.load(); + const absolutePath = path.resolve(clientConfig.databaseFile); + console.log(`Removing database file at: ${absolutePath}`); + const { stdout, stderr } = await exec(`rm ${absolutePath}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { From d972f1b216b8f67e909efa530424deb5ef45fd4a Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 17:18:53 +0530 Subject: [PATCH 083/153] fix: path import in removeDatabase fn --- clients/apps/nodejs/test_basic_workflow2.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index a07da0ea..d477f64f 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -4,6 +4,7 @@ const assert = require('node:assert/strict'); const mercurynodejslib = require('mercurynodejslib'); const { CoinStatus } = require('mercurynodejslib/coin_enum'); const client_config = require('./client_config'); +const path = require('path'); async function removeDatabase() { try { From f906c18a36a210e88b126538f9b00decadc93204 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 17:46:48 +0530 Subject: [PATCH 084/153] fix: unit test for iterative self transfer --- clients/apps/nodejs/test_basic_workflow2.js | 18 +++++------------- clients/libs/nodejs/index.js | 3 --- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index d477f64f..15c6edae 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -4,14 +4,11 @@ const assert = require('node:assert/strict'); const mercurynodejslib = require('mercurynodejslib'); const { CoinStatus } = require('mercurynodejslib/coin_enum'); const client_config = require('./client_config'); -const path = require('path'); async function removeDatabase() { try { - const clientConfig = client_config.load(); - const absolutePath = path.resolve(clientConfig.databaseFile); - console.log(`Removing database file at: ${absolutePath}`); - const { stdout, stderr } = await exec(`rm ${absolutePath}`); + const clientConfig = client_config.load(); + const { stdout, stderr } = await exec(`rm ./${clientConfig.databaseFile}`); console.log('stdout:', stdout); console.error('stderr:', stderr); } catch (e) { @@ -173,7 +170,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( let currentBlockHeight = block_header.height; console.log("Current block height: ", currentBlockHeight); - while (coin.locktime < currentBlockHeight) { + while (coin.locktime <= currentBlockHeight) { let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); console.log("transfer_address: ", transfer_address); @@ -358,28 +355,23 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { let wallet_1_name = "w1"; let wallet_2_name = "w2"; - - await removeDatabase(); await createWallet(clientConfig, wallet_1_name); await createWallet(clientConfig, wallet_2_name); await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - await removeDatabase(); + // // Deposit, iterative self transfer let wallet_3_name = "w3"; - await removeDatabase(); await createWallet(clientConfig, wallet_3_name); await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_3_name); - await removeDatabase(); + // Deposit, repeat send let wallet_4_name = "w4"; let wallet_5_name = "w5"; - await removeDatabase(); await createWallet(clientConfig, wallet_4_name); await createWallet(clientConfig, wallet_5_name); await depositAndRepeatSend(clientConfig, wallet_4_name); await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_4_name, wallet_5_name); - await removeDatabase(); })(); diff --git a/clients/libs/nodejs/index.js b/clients/libs/nodejs/index.js index 5abcbb19..31c8e654 100644 --- a/clients/libs/nodejs/index.js +++ b/clients/libs/nodejs/index.js @@ -19,9 +19,6 @@ const path = require('path'); const getDatabase = async (clientConfig) => { const databaseFile = clientConfig.databaseFile; - const absolutePath = path.resolve(databaseFile); - console.log(`Database file will be created at: ${absolutePath}`); - const db = new sqlite3.Database(databaseFile); await sqlite_manager.createTables(db); return db; From 7191335037c94e6ada6bd224bb6bdb07febeb66b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 18:19:58 +0530 Subject: [PATCH 085/153] fix: locktime for mercury server --- docker-compose-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 04cb86ed..09d55f74 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -55,7 +55,7 @@ services: environment: LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest - LOCKHEIGHT_INIT: 100 + LOCKHEIGHT_INIT: 1 LH_DECREMENT: 10 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: From 6e8edeb22aac3fc61c9cdea59c0e183b45d239c1 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 19:33:08 +0530 Subject: [PATCH 086/153] fix: lockheight_init for mercury server --- docker-compose-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 09d55f74..80770205 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -55,7 +55,7 @@ services: environment: LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest - LOCKHEIGHT_INIT: 1 + LOCKHEIGHT_INIT: 10 LH_DECREMENT: 10 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: From e8d4a52835eb31f7dd21a69a67e5663ac2d7ee44 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 19:56:21 +0530 Subject: [PATCH 087/153] debug: lockheight_init for mercury server --- docker-compose-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 80770205..b94e6d6c 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -55,7 +55,7 @@ services: environment: LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest - LOCKHEIGHT_INIT: 10 + LOCKHEIGHT_INIT: 1000 LH_DECREMENT: 10 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: From 196c47f616db3023489d3354638d998b9d75eaaf Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 5 Jun 2024 21:25:22 +0530 Subject: [PATCH 088/153] fix: lh_decrement and lockheight_init --- clients/apps/nodejs/test_basic_workflow2.js | 14 +++++++------- docker-compose-test.yml | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 15c6edae..3d8ca645 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -361,17 +361,17 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - // // Deposit, iterative self transfer + // Deposit, repeat send let wallet_3_name = "w3"; + let wallet_4_name = "w4"; await createWallet(clientConfig, wallet_3_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_3_name); + await createWallet(clientConfig, wallet_4_name); + await depositAndRepeatSend(clientConfig, wallet_3_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - // Deposit, repeat send - let wallet_4_name = "w4"; + // Deposit, iterative self transfer let wallet_5_name = "w5"; - await createWallet(clientConfig, wallet_4_name); await createWallet(clientConfig, wallet_5_name); - await depositAndRepeatSend(clientConfig, wallet_4_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_4_name, wallet_5_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_5_name); })(); diff --git a/docker-compose-test.yml b/docker-compose-test.yml index b94e6d6c..ee4ac5af 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -55,8 +55,8 @@ services: environment: LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest - LOCKHEIGHT_INIT: 1000 - LH_DECREMENT: 10 + LOCKHEIGHT_INIT: 1100 + LH_DECREMENT: 1 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres ports: - "8000:8000" From 9ad4c15892de6b6b47b9a01e464eed949054b397 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 12:24:12 +0530 Subject: [PATCH 089/153] chore: add test for transfer sender after transfer receiver --- clients/apps/nodejs/test_basic_workflow2.js | 90 ++++++++++++++++++++- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 3d8ca645..2aea5c99 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -331,7 +331,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log("coin: ", coin); - for (let i = 0; i < 1000; i++) { + for (let i = 0; i < 10; i++) { let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); @@ -349,6 +349,82 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { assert(received_statechain_ids[0] == coin.statechain_id); } +async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { + const amount = 10000; + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + deposit_info["amount"] = amount; + console.log("deposit_coin: ", deposit_info); + + const amountInBtc = 0.0001; + + // Sending Bitcoin using bitcoin-cli + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + exec(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated ${clientConfig.confirmationTarget} blocks`); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + + try { + transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet_ again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Expected error message"), "Unexpected error message"); + } +} + (async () => { const clientConfig = client_config.load(); @@ -369,9 +445,15 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { await depositAndRepeatSend(clientConfig, wallet_3_name); await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - - // Deposit, iterative self transfer + // Transfer-sender after transfer-receiver let wallet_5_name = "w5"; + let wallet_6_name = "w6"; await createWallet(clientConfig, wallet_5_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_5_name); + await createWallet(clientConfig, wallet_6_name); + await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + + // Deposit, iterative self transfer + let wallet_7_name = "w7"; + await createWallet(clientConfig, wallet_7_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_7_name); })(); From c4101771558d858d1460294218666490ccdff51d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 13:18:53 +0530 Subject: [PATCH 090/153] chore: refactor depositCoin --- clients/apps/nodejs/test_basic_workflow2.js | 142 +++----------------- 1 file changed, 18 insertions(+), 124 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 2aea5c99..ccf1beee 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -28,8 +28,7 @@ async function createWallet(clientConfig, walletName) { // TODO: add more assertions } -async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { - +async function depositCoin(clientConfig, wallet_name) { const amount = 10000; const token = await mercurynodejslib.newToken(clientConfig, wallet_name); @@ -53,13 +52,18 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget + 1} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget} blocks`); + console.log(`Generated ${clientConfig.confirmationTarget + 1} blocks`); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; } +} + +async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { + + await depositCoin(clientConfig, wallet_name); let coin = undefined; @@ -110,36 +114,8 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { } async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { - const amount = 10000; - - const token = await mercurynodejslib.newToken(clientConfig, wallet_name); - const tokenId = token.token_id; - - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - deposit_info["amount"] = amount; - console.log("deposit_coin: ", deposit_info); - - const amountInBtc = 0.0001; - - // Sending Bitcoin using bitcoin-cli - try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - exec(sendBitcoinCommand); - console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget} blocks`); - } catch (error) { - console.error('Error sending Bitcoin:', error.message); - return; - } + + await depositCoin(clientConfig, wallet_name); let coin = undefined; @@ -202,36 +178,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { - const amount = 10000; - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - deposit_info["amount"] = amount; - console.log("deposit_coin: ", deposit_info); - - const amountInBtc = 0.0001; - - // Sending Bitcoin using bitcoin-cli - try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - exec(sendBitcoinCommand); - console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget} blocks`); - } catch (error) { - console.error('Error sending Bitcoin:', error.message); - return; - } + await depositCoin(clientConfig, wallet_1_name); let coin = undefined; @@ -277,35 +224,8 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle } async function depositAndRepeatSend(clientConfig, wallet_1_name) { - const amount = 10000; - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - deposit_info["amount"] = amount; - console.log("deposit_coin: ", deposit_info); - - const amountInBtc = 0.0001; - - // Sending Bitcoin using bitcoin-cli - try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - exec(sendBitcoinCommand); - console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget} blocks`); - } catch (error) { - console.error('Error sending Bitcoin:', error.message); - return; - } + + await depositCoin(clientConfig, wallet_1_name); let coin = undefined; @@ -350,35 +270,8 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { } async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { - const amount = 10000; - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - deposit_info["amount"] = amount; - console.log("deposit_coin: ", deposit_info); - - const amountInBtc = 0.0001; - - // Sending Bitcoin using bitcoin-cli - try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - exec(sendBitcoinCommand); - console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget} blocks`); - } catch (error) { - console.error('Error sending Bitcoin:', error.message); - return; - } + + await depositCoin(clientConfig, wallet_1_name); let coin = undefined; @@ -418,10 +311,11 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, try { transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet_ again, but no error was thrown"); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Expected error message"), "Unexpected error message"); + assert(error.message.includes("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is TRANSFERRED"), + `Unexpected error message: ${error.message}`); } } From e085e20bc927a800fb5a9b5c52b7fa01855e6d45 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 14:06:59 +0530 Subject: [PATCH 091/153] chore: add test for deposit and transfer --- clients/apps/nodejs/test_basic_workflow2.js | 86 ++++++++++++++++++--- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index ccf1beee..581906a8 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -28,14 +28,11 @@ async function createWallet(clientConfig, walletName) { // TODO: add more assertions } -async function depositCoin(clientConfig, wallet_name) { - const amount = 10000; +async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { const token = await mercurynodejslib.newToken(clientConfig, wallet_name); const tokenId = token.token_id; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); let usedToken = tokenList.find(token => token.token_id === tokenId); @@ -45,7 +42,7 @@ async function depositCoin(clientConfig, wallet_name) { deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); - const amountInBtc = 0.0001; + const amountInBtc = amount / 100000000; // Sending Bitcoin using bitcoin-cli try { @@ -63,7 +60,9 @@ async function depositCoin(clientConfig, wallet_name) { async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { - await depositCoin(clientConfig, wallet_name); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -115,7 +114,9 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { - await depositCoin(clientConfig, wallet_name); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -178,7 +179,9 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { - await depositCoin(clientConfig, wallet_1_name); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -225,7 +228,9 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle async function depositAndRepeatSend(clientConfig, wallet_1_name) { - await depositCoin(clientConfig, wallet_1_name); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -271,7 +276,9 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { - await depositCoin(clientConfig, wallet_1_name); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -319,6 +326,56 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, } } +async function depositAndTransfer(clientConfig, wallet_name) { + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + + for (let i = 0; i < 10; i++) { + await depositCoin(clientConfig, wallet_name, amount, deposit_info); + + let coin = undefined; + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + } + + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + for (const coin of list_coins) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); + + console.log("transfer_address: ", transfer_address); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("coin transferSend: ", coin); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + } +} + (async () => { const clientConfig = client_config.load(); @@ -346,8 +403,13 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, await createWallet(clientConfig, wallet_6_name); await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); - // Deposit, iterative self transfer + // Deposit of 1000 coins in same wallet, and transfer each one 1000 times let wallet_7_name = "w7"; await createWallet(clientConfig, wallet_7_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_7_name); + await depositAndTransfer(clientConfig, wallet_7_name); + + // Deposit, iterative self transfer + let wallet_8_name = "w8"; + await createWallet(clientConfig, wallet_8_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_8_name); })(); From 56ed7e287c3b62e7fbb5bc9f2d52d03cfed21111 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 15:32:35 +0530 Subject: [PATCH 092/153] fix: token generation --- clients/apps/nodejs/test_basic_workflow2.js | 63 ++++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 581906a8..057894d2 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -30,15 +30,6 @@ async function createWallet(clientConfig, walletName) { async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { - const token = await mercurynodejslib.newToken(clientConfig, wallet_name); - const tokenId = token.token_id; - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - deposit_info["amount"] = amount; console.log("deposit_coin: ", deposit_info); @@ -60,6 +51,15 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); await depositCoin(clientConfig, wallet_name, amount, deposit_info); @@ -113,6 +113,15 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { } async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); @@ -179,6 +188,15 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); @@ -227,6 +245,15 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle } async function depositAndRepeatSend(clientConfig, wallet_1_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); @@ -275,6 +302,15 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { } async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); @@ -328,6 +364,15 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, async function depositAndTransfer(clientConfig, wallet_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + const amount = 10000; const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); From 1eed92db89ec73630a9540d18a000fc052625012 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 17:02:43 +0530 Subject: [PATCH 093/153] fix: assert error --- clients/apps/nodejs/test_basic_workflow2.js | 31 ++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 057894d2..18136e3a 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -54,14 +54,15 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { const token = await mercurynodejslib.newToken(clientConfig, wallet_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -117,14 +118,15 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( const token = await mercurynodejslib.newToken(clientConfig, wallet_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -191,14 +193,15 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -249,14 +252,15 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -306,14 +310,15 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); let coin = undefined; @@ -367,14 +372,14 @@ async function depositAndTransfer(clientConfig, wallet_name) { const token = await mercurynodejslib.newToken(clientConfig, wallet_name); const tokenId = token.token_id; + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); let usedToken = tokenList.find(token => token.token_id === tokenId); assert(usedToken.spent); - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); for (let i = 0; i < 10; i++) { await depositCoin(clientConfig, wallet_name, amount, deposit_info); From 5d5303d80047c6803c111a8e0cc47bf4d1dfea6b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 17:33:46 +0530 Subject: [PATCH 094/153] fix: typos for wallet name --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 18136e3a..e625cf37 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -406,7 +406,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { } } - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); for (const coin of list_coins) { let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); From 761e00e3edb5738bfd6c68fabf2aeeb5b4f67830 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 6 Jun 2024 17:50:20 +0530 Subject: [PATCH 095/153] fix: variable type --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index e625cf37..55ed3aef 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -408,7 +408,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - for (const coin of list_coins) { + for (let coin of list_coins) { let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); console.log("transfer_address: ", transfer_address); From 08b164ad1da1ed3df7db52eec1f1cf966b62f8fc Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 7 Jun 2024 12:52:54 +0530 Subject: [PATCH 096/153] chore: add test for interruption of transfer-send in sign first --- clients/apps/nodejs/test_basic_workflow2.js | 86 +++++++++++++++++++-- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 55ed3aef..a574e1a1 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -28,6 +28,12 @@ async function createWallet(clientConfig, walletName) { // TODO: add more assertions } +async function generateBlock(numBlocks) { + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated ${numBlocks} blocks`); +} + async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { deposit_info["amount"] = amount; @@ -40,9 +46,7 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${clientConfig.confirmationTarget + 1} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${clientConfig.confirmationTarget + 1} blocks`); + generateBlock(2); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -78,6 +82,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -142,6 +147,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -219,6 +225,7 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -278,6 +285,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -336,6 +344,7 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -397,6 +406,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -426,6 +436,61 @@ async function depositAndTransfer(clientConfig, wallet_name) { } } +async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + generateBlock(1); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + console.log("Pausing container: mercurylayer_mercury_1"); + await exec("docker pause mercurylayer_mercury_1"); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); +} + (async () => { const clientConfig = client_config.load(); @@ -445,6 +510,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { await createWallet(clientConfig, wallet_4_name); await depositAndRepeatSend(clientConfig, wallet_3_name); await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); + console.log("Completed test for Deposit, repeat send"); // Transfer-sender after transfer-receiver let wallet_5_name = "w5"; @@ -452,14 +518,24 @@ async function depositAndTransfer(clientConfig, wallet_name) { await createWallet(clientConfig, wallet_5_name); await createWallet(clientConfig, wallet_6_name); await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + console.log("Completed test for Transfer-sender after transfer-receiver"); // Deposit of 1000 coins in same wallet, and transfer each one 1000 times let wallet_7_name = "w7"; await createWallet(clientConfig, wallet_7_name); await depositAndTransfer(clientConfig, wallet_7_name); + console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); - // Deposit, iterative self transfer + // Test for interruption of transferSend before sign first let wallet_8_name = "w8"; + let wallet_9_name = "w9"; await createWallet(clientConfig, wallet_8_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_8_name); + await createWallet(clientConfig, wallet_9_name); + await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); + console.log("Completed test for interruption of transferSend before sign first"); + + // Deposit, iterative self transfer + let wallet_10_name = "w10"; + await createWallet(clientConfig, wallet_10_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_10_name); })(); From 6f7398c1ce679a2558d76b1463403907f1b8f894 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 7 Jun 2024 13:36:33 +0530 Subject: [PATCH 097/153] fix: increase block generation for deposit coin step --- clients/apps/nodejs/test_basic_workflow2.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index a574e1a1..475ce474 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -46,7 +46,7 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - generateBlock(2); + generateBlock(3); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -483,6 +483,9 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + console.log("Unpausing container: mercurylayer_mercury_1"); + await exec("docker unpause mercurylayer_mercury_1"); + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); console.log("received_statechain_ids: ", received_statechain_ids); From 70acfb78bad6b6d27309dce6be51f579858a3cb6 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 7 Jun 2024 16:37:02 +0530 Subject: [PATCH 098/153] fix: generateBlock --- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 475ce474..697fdf5a 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -46,7 +46,7 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - generateBlock(3); + await generateBlock(2); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; @@ -82,7 +82,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -147,7 +147,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -225,7 +225,7 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -285,7 +285,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -344,7 +344,7 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -406,7 +406,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } @@ -466,7 +466,7 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); + await generateBlock(1); continue; } From 8e97af5352719475aa2583eb67925f8eb9ecab48 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 7 Jun 2024 16:55:33 +0530 Subject: [PATCH 099/153] fix: wait for coin confirmed --- clients/apps/nodejs/test_basic_workflow2.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 697fdf5a..f139bb59 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -82,7 +82,6 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -147,7 +146,6 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -225,7 +223,6 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -285,7 +282,6 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -344,7 +340,6 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -406,7 +401,6 @@ async function depositAndTransfer(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } @@ -466,7 +460,6 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - await generateBlock(1); continue; } From 5867699eb44a5552f3c0754c56f174c6231e0edd Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 7 Jun 2024 17:16:02 +0530 Subject: [PATCH 100/153] fix: increase block after coin deposit --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index f139bb59..8779c6c7 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -46,7 +46,7 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - await generateBlock(2); + await generateBlock(3); } catch (error) { console.error('Error sending Bitcoin:', error.message); return; From 82cae28a671d00d1c999d39ba367064abed6b14c Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 10 Jun 2024 21:00:09 +0530 Subject: [PATCH 101/153] chore: add test for transferSend signSecond interruption --- clients/apps/nodejs/test_basic_workflow2.js | 243 +++++++++++++++++--- 1 file changed, 211 insertions(+), 32 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 8779c6c7..b8c6d473 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -487,40 +487,212 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na assert(received_statechain_ids[0] == coin.statechain_id); } +const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { + let coin_nonce = mercury_wasm.createAndCommitNonces(coin); + + let server_pubnonce = await signFirst(clientConfig, coin_nonce.sign_first_request_payload); + + coin.secret_nonce = coin_nonce.secret_nonce; + coin.public_nonce = coin_nonce.public_nonce; + coin.server_public_nonce = server_pubnonce; + coin.blinding_factor = coin_nonce.blinding_factor; + + const serverInfo = await utils.infoConfig(clientConfig, electrumClient); + + let new_block_height = 0; + if (block_height == null) { + const block_header = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) + new_block_height = block_header.height; + } else { + new_block_height = block_height; + } + + const initlock = serverInfo.initlock; + const interval = serverInfo.interval; + const feeRateSatsPerByte = serverInfo.fee_rate_sats_per_byte; + + let partialSigRequest = mercury_wasm.getPartialSigRequest( + coin, + new_block_height, + initlock, + interval, + feeRateSatsPerByte, + qtBackupTx, + toAddress, + network, + isWithdrawal); + + const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; + + console.log("Pausing container: mercurylayer_mercury_1"); + await exec("docker pause mercurylayer_mercury_1"); + + const serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); + + console.log("Unpausing container: mercurylayer_mercury_1"); + await exec("docker unpause mercurylayer_mercury_1"); + + const clientPartialSig = partialSigRequest.client_partial_sig; + const msg = partialSigRequest.msg; + const session = partialSigRequest.encoded_session; + const outputPubkey = partialSigRequest.output_pubkey; + + const signature = mercury_wasm.createSignature(msg, clientPartialSig, serverPartialSig, session, outputPubkey); + + const encodedUnsignedTx = partialSigRequest.encoded_unsigned_tx; + + const signed_tx = mercury_wasm.newBackupTransaction(encodedUnsignedTx, signature); + + return signed_tx; +} + +async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coinDeposited = undefined; + + console.log("coin: ", coinDeposited); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + generateBlock(1); + continue; + } + + coinDeposited = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coinDeposited); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + const db = await getDatabase(clientConfig); + + const electrumClient = await getElectrumClient(clientConfig); + + let batchId = (options && options.batchId) || null; + + let wallet = await sqlite_manager.getWallet(db, walletName); + + const backupTxs = await sqlite_manager.getBackupTxs(db, statechainId); + + if (backupTxs.length === 0) { + throw new Error(`There is no backup transaction for the statechain id ${statechainId}`); + } + + const new_tx_n = backupTxs.length + 1; + + let coinsWithStatechainId = wallet.coins.filter(c => { + return c.statechain_id === statechainId + }); + + if (!coinsWithStatechainId || coinsWithStatechainId.length === 0) { + throw new Error(`There is no coin for the statechain id ${statechainId}`); + } + + // If the user sends to himself, he will have two coins with same statechain_id + // In this case, we need to find the one with the lowest locktime + // Sort the coins by locktime in ascending order and pick the first one + let coin = coinsWithStatechainId.sort((a, b) => a.locktime - b.locktime)[0]; + + if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) { + throw new Error(`Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}`); + } + + if (coin.locktime == null) { + throw new Error("Coin.locktime is null"); + } + + const blockHeader = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) + const currentBlockheight = blockHeader.height; + + if (currentBlockheight > coin.locktime) { + throw new Error(`The coin is expired. Coin locktime is ${coin.locktime} and current blockheight is ${currentBlockheight}`); + } + + const statechain_id = coin.statechain_id; + const signed_statechain_id = coin.signed_statechain_id; + + const isWithdrawal = false; + const qtBackupTx = backupTxs.length; + + backupTxs.sort((a, b) => a.tx_n - b.tx_n); + + const bkp_tx1 = backupTxs[0]; + + const block_height = mercury_wasm.getBlockheight(bkp_tx1); + + const decodedTransferAddress = mercury_wasm.decodeTransferAddress(toAddress); + const new_auth_pubkey = decodedTransferAddress.auth_pubkey; + + const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); + + coin = await new_transaction(clientConfig, electrumClient, coin, transfer_address.transfer_receive, isWithdrawal, qtBackupTx, block_height, wallet.network); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); +} + (async () => { const clientConfig = client_config.load(); - let wallet_1_name = "w1"; - let wallet_2_name = "w2"; - await createWallet(clientConfig, wallet_1_name); - await createWallet(clientConfig, wallet_2_name); - await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - - - // Deposit, repeat send - let wallet_3_name = "w3"; - let wallet_4_name = "w4"; - await createWallet(clientConfig, wallet_3_name); - await createWallet(clientConfig, wallet_4_name); - await depositAndRepeatSend(clientConfig, wallet_3_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - console.log("Completed test for Deposit, repeat send"); - - // Transfer-sender after transfer-receiver - let wallet_5_name = "w5"; - let wallet_6_name = "w6"; - await createWallet(clientConfig, wallet_5_name); - await createWallet(clientConfig, wallet_6_name); - await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); - console.log("Completed test for Transfer-sender after transfer-receiver"); - - // Deposit of 1000 coins in same wallet, and transfer each one 1000 times - let wallet_7_name = "w7"; - await createWallet(clientConfig, wallet_7_name); - await depositAndTransfer(clientConfig, wallet_7_name); - console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); + // let wallet_1_name = "w1"; + // let wallet_2_name = "w2"; + // await createWallet(clientConfig, wallet_1_name); + // await createWallet(clientConfig, wallet_2_name); + // await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); + // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + + + // // Deposit, repeat send + // let wallet_3_name = "w3"; + // let wallet_4_name = "w4"; + // await createWallet(clientConfig, wallet_3_name); + // await createWallet(clientConfig, wallet_4_name); + // await depositAndRepeatSend(clientConfig, wallet_3_name); + // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); + // console.log("Completed test for Deposit, repeat send"); + + // // Transfer-sender after transfer-receiver + // let wallet_5_name = "w5"; + // let wallet_6_name = "w6"; + // await createWallet(clientConfig, wallet_5_name); + // await createWallet(clientConfig, wallet_6_name); + // await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + // console.log("Completed test for Transfer-sender after transfer-receiver"); + + // // Deposit of 1000 coins in same wallet, and transfer each one 1000 times + // let wallet_7_name = "w7"; + // await createWallet(clientConfig, wallet_7_name); + // await depositAndTransfer(clientConfig, wallet_7_name); + // console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); // Test for interruption of transferSend before sign first let wallet_8_name = "w8"; @@ -530,8 +702,15 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); console.log("Completed test for interruption of transferSend before sign first"); - // Deposit, iterative self transfer let wallet_10_name = "w10"; + let wallet_11_name = "w11"; await createWallet(clientConfig, wallet_10_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_10_name); + await createWallet(clientConfig, wallet_11_name); + await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); + console.log("Completed test for interruption of transferSend before sign second"); + + // Deposit, iterative self transfer + let wallet_12_name = "w12"; + await createWallet(clientConfig, wallet_12_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_12_name); })(); From 6ee4ce747f5fae00392fea896d03a21eb466e60d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 10 Jun 2024 21:46:54 +0530 Subject: [PATCH 102/153] fix: block unblock port of mercury server for test --- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index b8c6d473..06fd8ce3 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -471,13 +471,13 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - console.log("Pausing container: mercurylayer_mercury_1"); - await exec("docker pause mercurylayer_mercury_1"); + console.log("Blocking port 8000 of mercurylayer_mercury_1"); + await exec("docker exec mercurylayer_mercury_1 iptables -A OUTPUT -p tcp --dport 8000 -j DROP"); coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - console.log("Unpausing container: mercurylayer_mercury_1"); - await exec("docker unpause mercurylayer_mercury_1"); + console.log("Unblocking port 8000 of mercurylayer_mercury_1"); + await exec("docker exec mercurylayer_mercury_1 iptables -D OUTPUT -p tcp --dport 8000 -j DROP"); let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -524,13 +524,13 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; - console.log("Pausing container: mercurylayer_mercury_1"); - await exec("docker pause mercurylayer_mercury_1"); + console.log("Blocking port 8000 of mercurylayer_mercury_1"); + await exec("docker exec mercurylayer_mercury_1 iptables -A OUTPUT -p tcp --dport 8080 -j DROP"); const serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); - console.log("Unpausing container: mercurylayer_mercury_1"); - await exec("docker unpause mercurylayer_mercury_1"); + console.log("Unblocking port 8000 of mercurylayer_mercury_1"); + await exec("docker exec mercurylayer_mercury_1 iptables -D OUTPUT -p tcp --dport 8080 -j DROP"); const clientPartialSig = partialSigRequest.client_partial_sig; const msg = partialSigRequest.msg; From d218a87e85da663102e98de49dfa50b2ea1cb71c Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 11:18:33 +0530 Subject: [PATCH 103/153] debug: network name for mercury server --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 49a9584d..4f572608 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -95,6 +95,8 @@ jobs: docker exec $mercury_container curl -v http://$enclave_ip:18080/get_public_key \ -H "Content-Type: application/json" \ -d '{"statechain_id":"550e8400e29b41d4a716446655440000"}' + + docker inspect mercurylayer_mercury_1 - name: Set up Node.js uses: actions/setup-node@v2 with: From 89e98749e8966fa31d94b9fd59b0300600a6234d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 11:39:14 +0530 Subject: [PATCH 104/153] fix: disconnect network for test --- clients/apps/nodejs/test_basic_workflow2.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 06fd8ce3..9a670134 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -471,13 +471,13 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - console.log("Blocking port 8000 of mercurylayer_mercury_1"); - await exec("docker exec mercurylayer_mercury_1 iptables -A OUTPUT -p tcp --dport 8000 -j DROP"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - console.log("Unblocking port 8000 of mercurylayer_mercury_1"); - await exec("docker exec mercurylayer_mercury_1 iptables -D OUTPUT -p tcp --dport 8000 -j DROP"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -524,13 +524,13 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; - console.log("Blocking port 8000 of mercurylayer_mercury_1"); - await exec("docker exec mercurylayer_mercury_1 iptables -A OUTPUT -p tcp --dport 8080 -j DROP"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); const serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); - console.log("Unblocking port 8000 of mercurylayer_mercury_1"); - await exec("docker exec mercurylayer_mercury_1 iptables -D OUTPUT -p tcp --dport 8080 -j DROP"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); const clientPartialSig = partialSigRequest.client_partial_sig; const msg = partialSigRequest.msg; From eeeae6cdee83f441b7301ae21b9a6fbd98be7a3a Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 12:10:23 +0530 Subject: [PATCH 105/153] fix: enclose post request of sign into try catch --- .../apps/react-app/src/main/transaction.js | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/clients/apps/react-app/src/main/transaction.js b/clients/apps/react-app/src/main/transaction.js index 968d2084..3f281d73 100644 --- a/clients/apps/react-app/src/main/transaction.js +++ b/clients/apps/react-app/src/main/transaction.js @@ -19,8 +19,16 @@ const signFirst = async (signFirstRequestPayload) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - let response = await axios.post(url, signFirstRequestPayload, socksAgent); - + try { + let response = await axios.post(url, signFirstRequestPayload, socksAgent); + console.log('Response:', response.data); + } catch (error) { + if (error.code === 'ECONNREFUSED') { + console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); + } else { + console.error('An error occurred:', error.message); + } + } let server_pubnonce_hex = response.data.server_pubnonce; return server_pubnonce_hex; @@ -40,8 +48,16 @@ const signSecond = async (partialSigRequest) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - let response = await axios.post(url, partialSigRequest, socksAgent); - + try { + let response = await axios.post(url, partialSigRequest, socksAgent); + console.log('Response:', response.data); + } catch (error) { + if (error.code === 'ECONNREFUSED') { + console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); + } else { + console.error('An error occurred:', error.message); + } + } let server_partial_sig_hex = response.data.partial_sig; return server_partial_sig_hex; From 7c09e9646d33bbb85adef8208b7d09b8d04bd987 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 12:36:34 +0530 Subject: [PATCH 106/153] fix: enclose transfer/sender request in try catch --- clients/libs/nodejs/transfer_send.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/clients/libs/nodejs/transfer_send.js b/clients/libs/nodejs/transfer_send.js index afa4222c..60ed473b 100644 --- a/clients/libs/nodejs/transfer_send.js +++ b/clients/libs/nodejs/transfer_send.js @@ -143,8 +143,16 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - const response = await axios.post(url, transferSenderRequestPayload, socksAgent); - + try { + const response = await axios.post(url, transferSenderRequestPayload, socksAgent); + console.log('Response:', response.data); + } catch (error) { + if (error.code === 'ECONNREFUSED') { + console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); + } else { + console.error('An error occurred:', error.message); + } + } return response.data.x1; } From 433da97ba7a4617b666abbc87e9522b7cf01f325 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 12:56:01 +0530 Subject: [PATCH 107/153] fix: get_new_x1 fn --- clients/libs/nodejs/transfer_send.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clients/libs/nodejs/transfer_send.js b/clients/libs/nodejs/transfer_send.js index 60ed473b..3a14956b 100644 --- a/clients/libs/nodejs/transfer_send.js +++ b/clients/libs/nodejs/transfer_send.js @@ -146,6 +146,7 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new try { const response = await axios.post(url, transferSenderRequestPayload, socksAgent); console.log('Response:', response.data); + return response.data.x1; } catch (error) { if (error.code === 'ECONNREFUSED') { console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); @@ -153,7 +154,7 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new console.error('An error occurred:', error.message); } } - return response.data.x1; + return null; } module.exports = { execute }; \ No newline at end of file From 0a90a70a4db05f8df1fccff769967960b06d5f28 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 23:32:07 +0530 Subject: [PATCH 108/153] fix: error handling for mercury server post request --- .../apps/react-app/src/main/transaction.js | 24 ++++--------------- clients/libs/nodejs/transaction.js | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/clients/apps/react-app/src/main/transaction.js b/clients/apps/react-app/src/main/transaction.js index 3f281d73..968d2084 100644 --- a/clients/apps/react-app/src/main/transaction.js +++ b/clients/apps/react-app/src/main/transaction.js @@ -19,16 +19,8 @@ const signFirst = async (signFirstRequestPayload) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - try { - let response = await axios.post(url, signFirstRequestPayload, socksAgent); - console.log('Response:', response.data); - } catch (error) { - if (error.code === 'ECONNREFUSED') { - console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); - } else { - console.error('An error occurred:', error.message); - } - } + let response = await axios.post(url, signFirstRequestPayload, socksAgent); + let server_pubnonce_hex = response.data.server_pubnonce; return server_pubnonce_hex; @@ -48,16 +40,8 @@ const signSecond = async (partialSigRequest) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - try { - let response = await axios.post(url, partialSigRequest, socksAgent); - console.log('Response:', response.data); - } catch (error) { - if (error.code === 'ECONNREFUSED') { - console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); - } else { - console.error('An error occurred:', error.message); - } - } + let response = await axios.post(url, partialSigRequest, socksAgent); + let server_partial_sig_hex = response.data.partial_sig; return server_partial_sig_hex; diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 1594b88b..58f6ca01 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -70,8 +70,16 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - let response = await axios.post(url, signFirstRequestPayload, socksAgent); - + try { + let response = await axios.post(url, signFirstRequestPayload, socksAgent); + console.log('Response:', response.data); + } catch (error) { + if (error.code === 'ECONNREFUSED') { + console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); + } else { + console.error('An error occurred:', error.message); + } + } let server_pubnonce_hex = response.data.server_pubnonce; if (server_pubnonce_hex.startsWith("0x")) { @@ -95,8 +103,16 @@ const signSecond = async (clientConfig, partialSigRequest) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - let response = await axios.post(url, partialSigRequest, socksAgent); - + try { + let response = await axios.post(url, partialSigRequest, socksAgent); + console.log('Response:', response.data); + } catch (error) { + if (error.code === 'ECONNREFUSED') { + console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); + } else { + console.error('An error occurred:', error.message); + } + } let server_partial_sig_hex = response.data.partial_sig; if (server_partial_sig_hex.startsWith("0x")) { From 62ad35508263d75aef8186701f1f74e31d6403bd Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 11 Jun 2024 23:47:54 +0530 Subject: [PATCH 109/153] fix: variable error --- clients/libs/nodejs/transaction.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 58f6ca01..44c8b4bc 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -70,8 +70,9 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } + let response; try { - let response = await axios.post(url, signFirstRequestPayload, socksAgent); + response = await axios.post(url, signFirstRequestPayload, socksAgent); console.log('Response:', response.data); } catch (error) { if (error.code === 'ECONNREFUSED') { @@ -103,8 +104,9 @@ const signSecond = async (clientConfig, partialSigRequest) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } + let response; try { - let response = await axios.post(url, partialSigRequest, socksAgent); + response = await axios.post(url, partialSigRequest, socksAgent); console.log('Response:', response.data); } catch (error) { if (error.code === 'ECONNREFUSED') { From 32d7cb15f515beeb5bc3949ea2d680fc23fc36f9 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 00:22:50 +0530 Subject: [PATCH 110/153] fix: handle the case if response undefined --- clients/libs/nodejs/transaction.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 44c8b4bc..69bd19ad 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -81,6 +81,11 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { console.error('An error occurred:', error.message); } } + + // Check if response or response.data is undefined + if (!response || !response.data || !response.data.server_pubnonce) { + throw new Error('Server public nonce is not available.'); + } let server_pubnonce_hex = response.data.server_pubnonce; if (server_pubnonce_hex.startsWith("0x")) { @@ -115,6 +120,11 @@ const signSecond = async (clientConfig, partialSigRequest) => { console.error('An error occurred:', error.message); } } + + // Check if response or response data is undefined + if (!response || !response.data || !response.data.partial_sig) { + throw new Error('Server partial signature is not available.'); + } let server_partial_sig_hex = response.data.partial_sig; if (server_partial_sig_hex.startsWith("0x")) { From e1e2f7080156bc4bc2118b83616828d69e573e3f Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 00:41:13 +0530 Subject: [PATCH 111/153] fix: error handling for signFirst and signSecond --- clients/libs/nodejs/transaction.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 69bd19ad..6d353194 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -84,7 +84,8 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { // Check if response or response.data is undefined if (!response || !response.data || !response.data.server_pubnonce) { - throw new Error('Server public nonce is not available.'); + console.error('Server public nonce is not available.'); + return null; } let server_pubnonce_hex = response.data.server_pubnonce; @@ -123,7 +124,8 @@ const signSecond = async (clientConfig, partialSigRequest) => { // Check if response or response data is undefined if (!response || !response.data || !response.data.partial_sig) { - throw new Error('Server partial signature is not available.'); + console.error('Server partial signature is not available.'); + return null; } let server_partial_sig_hex = response.data.partial_sig; From 88a7ddf73e1aab77c1b6703ae5bf649265d5ce7e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 12:13:58 +0530 Subject: [PATCH 112/153] fix: assert errors in test --- clients/apps/nodejs/test_basic_workflow2.js | 21 ++++++++++++++++++--- clients/libs/nodejs/transaction.js | 6 ++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 9a670134..6261db6f 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -474,8 +474,14 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na console.log("Disconnect mercurylayer_mercury_1 from network"); await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - + try { + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Server public nonce is not available."), + `Unexpected error message: ${error.message}`); + } console.log("Connect mercurylayer_mercury_1 from network"); await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); @@ -527,7 +533,16 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW console.log("Disconnect mercurylayer_mercury_1 from network"); await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); - const serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); + let serverPartialSig; + + try { + serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Server partial signature is not available."), + `Unexpected error message: ${error.message}`); + } console.log("Connect mercurylayer_mercury_1 from network"); await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 6d353194..69bd19ad 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -84,8 +84,7 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { // Check if response or response.data is undefined if (!response || !response.data || !response.data.server_pubnonce) { - console.error('Server public nonce is not available.'); - return null; + throw new Error('Server public nonce is not available.'); } let server_pubnonce_hex = response.data.server_pubnonce; @@ -124,8 +123,7 @@ const signSecond = async (clientConfig, partialSigRequest) => { // Check if response or response data is undefined if (!response || !response.data || !response.data.partial_sig) { - console.error('Server partial signature is not available.'); - return null; + throw new Error('Server partial signature is not available.'); } let server_partial_sig_hex = response.data.partial_sig; From 8567f77c110a298fd1572658eec09cca3a12f603 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 12:58:06 +0530 Subject: [PATCH 113/153] fix: add test for electrum unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 88 +++++++++++++++------ clients/libs/nodejs/utils.js | 7 +- 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 6261db6f..19aa466a 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -484,13 +484,6 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na } console.log("Connect mercurylayer_mercury_1 from network"); await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); } const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { @@ -546,19 +539,6 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW console.log("Connect mercurylayer_mercury_1 from network"); await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); - - const clientPartialSig = partialSigRequest.client_partial_sig; - const msg = partialSigRequest.msg; - const session = partialSigRequest.encoded_session; - const outputPubkey = partialSigRequest.output_pubkey; - - const signature = mercury_wasm.createSignature(msg, clientPartialSig, serverPartialSig, session, outputPubkey); - - const encodedUnsignedTx = partialSigRequest.encoded_unsigned_tx; - - const signed_tx = mercury_wasm.newBackupTransaction(encodedUnsignedTx, signature); - - return signed_tx; } async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { @@ -665,13 +645,62 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); coin = await new_transaction(clientConfig, electrumClient, coin, transfer_address.transfer_receive, isWithdrawal, qtBackupTx, block_height, wallet.network); +} - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); +async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; - console.log("received_statechain_ids: ", received_statechain_ids); + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + + try { + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Error getting fee rate from electrum server"), + `Unexpected error message: ${error.message}`); + } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } (async () => { @@ -724,8 +753,15 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); console.log("Completed test for interruption of transferSend before sign second"); - // Deposit, iterative self transfer let wallet_12_name = "w12"; + let wallet_13_name = "w13"; await createWallet(clientConfig, wallet_12_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_12_name); + await createWallet(clientConfig, wallet_13_name); + await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); + console.log("Completed test for interruption of sign with Electrum unavailability"); + + // Deposit, iterative self transfer + let wallet_14_name = "w14"; + await createWallet(clientConfig, wallet_14_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_14_name); })(); diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index cdd9430c..bd9eb229 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -7,7 +7,12 @@ const infoConfig = async (clientConfig, ecl) => { const statechain_entity_url = clientConfig.statechainEntity; const path = "info/config"; - let fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) + let fee_rate_btc_per_kb; + try { + fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) + } catch (error) { + throw new Error("Error getting fee rate from electrum server"); + } // console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); From 63b68dedb0ca2a73800ce4a7a61b98bbf63b70d3 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 14:22:45 +0530 Subject: [PATCH 114/153] chore: add test for transfer receive for electrum unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 70 ++++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 19aa466a..9ffed568 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -560,7 +560,7 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n console.log("coin: ", coinDeposited); - while (!coin) { + while (!coinDeposited) { const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); let coinsWithStatechainId = list_coins.filter(c => { @@ -703,6 +703,60 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } +async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + try { + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Error getting fee rate from electrum server"), + `Unexpected error message: ${error.message}`); + } +} + (async () => { const clientConfig = client_config.load(); @@ -746,6 +800,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); console.log("Completed test for interruption of transferSend before sign first"); + // Test for interruption of transferSend before sign second let wallet_10_name = "w10"; let wallet_11_name = "w11"; await createWallet(clientConfig, wallet_10_name); @@ -753,6 +808,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); console.log("Completed test for interruption of transferSend before sign second"); + // Test for interruption of sign with Electrum unavailability let wallet_12_name = "w12"; let wallet_13_name = "w13"; await createWallet(clientConfig, wallet_12_name); @@ -760,8 +816,16 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); console.log("Completed test for interruption of sign with Electrum unavailability"); - // Deposit, iterative self transfer + // Test for interruption of transfer receive with mercury server unavailability let wallet_14_name = "w14"; + let wallet_15_name = "w15"; await createWallet(clientConfig, wallet_14_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_14_name); + await createWallet(clientConfig, wallet_15_name); + await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); + console.log("Completed test for interruption of transfer receive with mercury server unavailability"); + + // Deposit, iterative self transfer + let wallet_16_name = "w16"; + await createWallet(clientConfig, wallet_16_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_16_name); })(); From 0d12e1afa1472c601b2e21f9184040e165fe11ec Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 15:09:32 +0530 Subject: [PATCH 115/153] chore: add test for transfer receive for mercury server unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 79 ++++++++++++++++++--- clients/libs/nodejs/transfer_receive.js | 7 +- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 9ffed568..8340d21c 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -476,7 +476,7 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + assert.fail("Expected error when transferring from wallet one, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); assert(error.message.includes("Server public nonce is not available."), @@ -530,7 +530,7 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW try { serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); - assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + assert.fail("Expected error when signing second transaction, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); assert(error.message.includes("Server partial signature is not available."), @@ -571,7 +571,6 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); - generateBlock(1); continue; } @@ -693,7 +692,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + assert.fail("Expected error when transferring from wallet one, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); assert(error.message.includes("Error getting fee rate from electrum server"), @@ -749,7 +748,7 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + assert.fail("Expected error when receiving into wallet two, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); assert(error.message.includes("Error getting fee rate from electrum server"), @@ -757,6 +756,60 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, } } +async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + try { + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + assert.fail("Expected error when receiving into wallet two, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Failed to get message address from mercury server"), + `Unexpected error message: ${error.message}`); + } +} + (async () => { const clientConfig = client_config.load(); @@ -816,16 +869,24 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); console.log("Completed test for interruption of sign with Electrum unavailability"); - // Test for interruption of transfer receive with mercury server unavailability + // Test for interruption of transfer receive with Electrum unavailability let wallet_14_name = "w14"; let wallet_15_name = "w15"; await createWallet(clientConfig, wallet_14_name); await createWallet(clientConfig, wallet_15_name); await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); - console.log("Completed test for interruption of transfer receive with mercury server unavailability"); + console.log("Completed test for interruption of transfer receive with Electrum unavailability"); - // Deposit, iterative self transfer + // Test for interruption of transfer receive with mercury server unavailability let wallet_16_name = "w16"; + let wallet_17_name = "w17"; await createWallet(clientConfig, wallet_16_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_16_name); + await createWallet(clientConfig, wallet_17_name); + await interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_16_name, wallet_17_name); + console.log("Completed test for interruption of transfer receive with mercury server unavailability"); + + // Deposit, iterative self transfer + let wallet_18_name = "w18"; + await createWallet(clientConfig, wallet_18_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); })(); diff --git a/clients/libs/nodejs/transfer_receive.js b/clients/libs/nodejs/transfer_receive.js index aa57c7e7..d67d5ef5 100644 --- a/clients/libs/nodejs/transfer_receive.js +++ b/clients/libs/nodejs/transfer_receive.js @@ -113,7 +113,12 @@ const getMsgAddr = async (clientConfig, auth_pubkey) => { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - const response = await axios.get(url, socksAgent); + let response; + try { + response = await axios.get(url, socksAgent); + } catch (error) { + throw new Error('Failed to get message address from mercury server'); + } return response.data.list_enc_transfer_msg; } From 67e0c2dfbdc810a2279c2045de68ec56459cf69b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 20:00:44 +0530 Subject: [PATCH 116/153] fix: mercury and electrum unavailability tests --- clients/apps/nodejs/test_basic_workflow2.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 8340d21c..23a71a20 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -746,6 +746,9 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); assert.fail("Expected error when receiving into wallet two, but no error was thrown"); @@ -754,6 +757,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -800,6 +805,9 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); assert.fail("Expected error when receiving into wallet two, but no error was thrown"); @@ -808,6 +816,8 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon assert(error.message.includes("Failed to get message address from mercury server"), `Unexpected error message: ${error.message}`); } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } (async () => { From 1539c9fc204e0e8ad089d780bb6831da1438ac02 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 12 Jun 2024 21:23:01 +0530 Subject: [PATCH 117/153] debug: coin deposition --- clients/apps/nodejs/test_basic_workflow2.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 23a71a20..099a238e 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -850,10 +850,10 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon // console.log("Completed test for Transfer-sender after transfer-receiver"); // // Deposit of 1000 coins in same wallet, and transfer each one 1000 times - // let wallet_7_name = "w7"; - // await createWallet(clientConfig, wallet_7_name); - // await depositAndTransfer(clientConfig, wallet_7_name); - // console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); + let wallet_7_name = "w7"; + await createWallet(clientConfig, wallet_7_name); + await depositAndTransfer(clientConfig, wallet_7_name); + console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); // Test for interruption of transferSend before sign first let wallet_8_name = "w8"; From 1078092310e5bcebfd4fbc5ccad8403b15c015bc Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 12:36:12 +0530 Subject: [PATCH 118/153] debug: coin deposit verification --- clients/apps/nodejs/test_basic_workflow2.js | 52 ++++++++++----------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 099a238e..25fc7b40 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -824,32 +824,32 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon const clientConfig = client_config.load(); - // let wallet_1_name = "w1"; - // let wallet_2_name = "w2"; - // await createWallet(clientConfig, wallet_1_name); - // await createWallet(clientConfig, wallet_2_name); - // await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); - // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - - - // // Deposit, repeat send - // let wallet_3_name = "w3"; - // let wallet_4_name = "w4"; - // await createWallet(clientConfig, wallet_3_name); - // await createWallet(clientConfig, wallet_4_name); - // await depositAndRepeatSend(clientConfig, wallet_3_name); - // await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - // console.log("Completed test for Deposit, repeat send"); - - // // Transfer-sender after transfer-receiver - // let wallet_5_name = "w5"; - // let wallet_6_name = "w6"; - // await createWallet(clientConfig, wallet_5_name); - // await createWallet(clientConfig, wallet_6_name); - // await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); - // console.log("Completed test for Transfer-sender after transfer-receiver"); - - // // Deposit of 1000 coins in same wallet, and transfer each one 1000 times + let wallet_1_name = "w1"; + let wallet_2_name = "w2"; + await createWallet(clientConfig, wallet_1_name); + await createWallet(clientConfig, wallet_2_name); + await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + + + // Deposit, repeat send + let wallet_3_name = "w3"; + let wallet_4_name = "w4"; + await createWallet(clientConfig, wallet_3_name); + await createWallet(clientConfig, wallet_4_name); + await depositAndRepeatSend(clientConfig, wallet_3_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); + console.log("Completed test for Deposit, repeat send"); + + // Transfer-sender after transfer-receiver + let wallet_5_name = "w5"; + let wallet_6_name = "w6"; + await createWallet(clientConfig, wallet_5_name); + await createWallet(clientConfig, wallet_6_name); + await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + console.log("Completed test for Transfer-sender after transfer-receiver"); + + // Deposit of 1000 coins in same wallet, and transfer each one 1000 times let wallet_7_name = "w7"; await createWallet(clientConfig, wallet_7_name); await depositAndTransfer(clientConfig, wallet_7_name); From 3b464ba15e473bed2217742211ff85c6fcd66437 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 13:10:41 +0530 Subject: [PATCH 119/153] debug: listStatecoins --- clients/apps/nodejs/test_basic_workflow2.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 25fc7b40..43cb6d46 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -73,6 +73,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { while (!coin) { const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + console.log(list_coins); let coinsWithStatechainId = list_coins.filter(c => { return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; From daa4076b1d6fefef9a6c1b90069249138d113533 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 13:21:02 +0530 Subject: [PATCH 120/153] fix: get_new_x1 --- clients/libs/nodejs/transfer_send.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clients/libs/nodejs/transfer_send.js b/clients/libs/nodejs/transfer_send.js index 3a14956b..b916b9ca 100644 --- a/clients/libs/nodejs/transfer_send.js +++ b/clients/libs/nodejs/transfer_send.js @@ -143,10 +143,10 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } + let response; try { - const response = await axios.post(url, transferSenderRequestPayload, socksAgent); + response = await axios.post(url, transferSenderRequestPayload, socksAgent); console.log('Response:', response.data); - return response.data.x1; } catch (error) { if (error.code === 'ECONNREFUSED') { console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); @@ -154,7 +154,7 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new console.error('An error occurred:', error.message); } } - return null; + return response.data.x1; } module.exports = { execute }; \ No newline at end of file From 0d5b68f87f78eda13898b6addaa9c36963f9b568 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 13:40:43 +0530 Subject: [PATCH 121/153] fix: add generateBlock if coin status in mempool --- clients/apps/nodejs/test_basic_workflow2.js | 903 -------------------- 1 file changed, 903 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 43cb6d46..e69de29b 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -1,903 +0,0 @@ -const util = require('node:util'); -const exec = util.promisify(require('node:child_process').exec); -const assert = require('node:assert/strict'); -const mercurynodejslib = require('mercurynodejslib'); -const { CoinStatus } = require('mercurynodejslib/coin_enum'); -const client_config = require('./client_config'); - -async function removeDatabase() { - try { - const clientConfig = client_config.load(); - const { stdout, stderr } = await exec(`rm ./${clientConfig.databaseFile}`); - console.log('stdout:', stdout); - console.error('stderr:', stderr); - } catch (e) { - console.error(e); - } -} - -const sleep = (ms) => { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -async function createWallet(clientConfig, walletName) { - - let wallet = await mercurynodejslib.createWallet(clientConfig, walletName); - assert.equal(wallet.name, walletName); - - // TODO: add more assertions -} - -async function generateBlock(numBlocks) { - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; - exec(generateBlockCommand); - console.log(`Generated ${numBlocks} blocks`); -} - -async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { - - deposit_info["amount"] = amount; - console.log("deposit_coin: ", deposit_info); - - const amountInBtc = amount / 100000000; - - // Sending Bitcoin using bitcoin-cli - try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; - exec(sendBitcoinCommand); - console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); - await generateBlock(3); - } catch (error) { - console.error('Error sending Bitcoin:', error.message); - return; - } -} - -async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_name, amount, deposit_info); - - let coin = undefined; - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - console.log(list_coins); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - - break; - } - - console.log("coin: ", coin); - - for (let i = 0; i < 10; i++) { - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); - - console.log("transfer_address: ", transfer_address); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); - - console.log("coin transferSend: ", coin); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - } - - let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; - - let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); - - console.log("txid: ", txid); - -} - -async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_name, amount, deposit_info); - - let coin = undefined; - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - - break; - } - - console.log("coin: ", coin); - - const electrumClient = await mercurynodejslib.getElectrumClient(clientConfig); - - let block_header = await electrumClient.request('blockchain.headers.subscribe'); - let currentBlockHeight = block_header.height; - console.log("Current block height: ", currentBlockHeight); - - while (coin.locktime <= currentBlockHeight) { - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); - - console.log("transfer_address: ", transfer_address); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); - - console.log("coin transferSend: ", coin); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - - // Fetch the coin again to get the updated locktime - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - coin = list_coins.find(c => c.statechain_id === coin.statechain_id); - - console.log("Updated coin: ", coin); - } - - let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; - - let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); - - console.log("txid: ", txid); -} - -async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - - let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; - - let txid = await mercurynodejslib.broadcastBackupTransaction(clientConfig, wallet_2_name, coin.statechain_id, withdraw_address, null); - - console.log("txid: ", txid); -} - -async function depositAndRepeatSend(clientConfig, wallet_1_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - for (let i = 0; i < 10; i++) { - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - } - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); -} - -async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - - try { - transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is TRANSFERRED"), - `Unexpected error message: ${error.message}`); - } -} - -async function depositAndTransfer(clientConfig, wallet_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - for (let i = 0; i < 10; i++) { - await depositCoin(clientConfig, wallet_name, amount, deposit_info); - - let coin = undefined; - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - - break; - } - } - - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); - - for (let coin of list_coins) { - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); - - console.log("transfer_address: ", transfer_address); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); - - console.log("coin transferSend: ", coin); - - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); - - console.log("received_statechain_ids: ", received_statechain_ids); - - assert(received_statechain_ids.length > 0); - assert(received_statechain_ids[0] == coin.statechain_id); - } -} - -async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_name) { - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); - - try { - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet one, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Server public nonce is not available."), - `Unexpected error message: ${error.message}`); - } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); -} - -const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { - let coin_nonce = mercury_wasm.createAndCommitNonces(coin); - - let server_pubnonce = await signFirst(clientConfig, coin_nonce.sign_first_request_payload); - - coin.secret_nonce = coin_nonce.secret_nonce; - coin.public_nonce = coin_nonce.public_nonce; - coin.server_public_nonce = server_pubnonce; - coin.blinding_factor = coin_nonce.blinding_factor; - - const serverInfo = await utils.infoConfig(clientConfig, electrumClient); - - let new_block_height = 0; - if (block_height == null) { - const block_header = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) - new_block_height = block_header.height; - } else { - new_block_height = block_height; - } - - const initlock = serverInfo.initlock; - const interval = serverInfo.interval; - const feeRateSatsPerByte = serverInfo.fee_rate_sats_per_byte; - - let partialSigRequest = mercury_wasm.getPartialSigRequest( - coin, - new_block_height, - initlock, - interval, - feeRateSatsPerByte, - qtBackupTx, - toAddress, - network, - isWithdrawal); - - const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; - - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); - - let serverPartialSig; - - try { - serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); - assert.fail("Expected error when signing second transaction, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Server partial signature is not available."), - `Unexpected error message: ${error.message}`); - } - - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); -} - -async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coinDeposited = undefined; - - console.log("coin: ", coinDeposited); - - while (!coinDeposited) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coinDeposited = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coinDeposited); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - const db = await getDatabase(clientConfig); - - const electrumClient = await getElectrumClient(clientConfig); - - let batchId = (options && options.batchId) || null; - - let wallet = await sqlite_manager.getWallet(db, walletName); - - const backupTxs = await sqlite_manager.getBackupTxs(db, statechainId); - - if (backupTxs.length === 0) { - throw new Error(`There is no backup transaction for the statechain id ${statechainId}`); - } - - const new_tx_n = backupTxs.length + 1; - - let coinsWithStatechainId = wallet.coins.filter(c => { - return c.statechain_id === statechainId - }); - - if (!coinsWithStatechainId || coinsWithStatechainId.length === 0) { - throw new Error(`There is no coin for the statechain id ${statechainId}`); - } - - // If the user sends to himself, he will have two coins with same statechain_id - // In this case, we need to find the one with the lowest locktime - // Sort the coins by locktime in ascending order and pick the first one - let coin = coinsWithStatechainId.sort((a, b) => a.locktime - b.locktime)[0]; - - if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) { - throw new Error(`Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}`); - } - - if (coin.locktime == null) { - throw new Error("Coin.locktime is null"); - } - - const blockHeader = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) - const currentBlockheight = blockHeader.height; - - if (currentBlockheight > coin.locktime) { - throw new Error(`The coin is expired. Coin locktime is ${coin.locktime} and current blockheight is ${currentBlockheight}`); - } - - const statechain_id = coin.statechain_id; - const signed_statechain_id = coin.signed_statechain_id; - - const isWithdrawal = false; - const qtBackupTx = backupTxs.length; - - backupTxs.sort((a, b) => a.tx_n - b.tx_n); - - const bkp_tx1 = backupTxs[0]; - - const block_height = mercury_wasm.getBlockheight(bkp_tx1); - - const decodedTransferAddress = mercury_wasm.decodeTransferAddress(toAddress); - const new_auth_pubkey = decodedTransferAddress.auth_pubkey; - - const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); - - coin = await new_transaction(clientConfig, electrumClient, coin, transfer_address.transfer_receive, isWithdrawal, qtBackupTx, block_height, wallet.network); -} - -async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); - - try { - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - assert.fail("Expected error when transferring from wallet one, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting fee rate from electrum server"), - `Unexpected error message: ${error.message}`); - } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); -} - -async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); - - try { - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - assert.fail("Expected error when receiving into wallet two, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting fee rate from electrum server"), - `Unexpected error message: ${error.message}`); - } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); -} - -async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { - - const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); - const tokenId = token.token_id; - - const amount = 10000; - const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); - - let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); - - let usedToken = tokenList.find(token => token.token_id === tokenId); - - assert(usedToken.spent); - - await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); - - let coin = undefined; - - console.log("coin: ", coin); - - while (!coin) { - const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); - - let coinsWithStatechainId = list_coins.filter(c => { - return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; - }); - - if (coinsWithStatechainId.length === 0) { - console.log("Waiting for coin to be confirmed..."); - console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await sleep(5000); - continue; - } - - coin = coinsWithStatechainId[0]; - break; - } - - console.log("coin: ", coin); - - let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - - coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); - - try { - let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); - assert.fail("Expected error when receiving into wallet two, but no error was thrown"); - } catch (error) { - console.log("Expected error received: ", error.message); - assert(error.message.includes("Failed to get message address from mercury server"), - `Unexpected error message: ${error.message}`); - } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); -} - -(async () => { - - const clientConfig = client_config.load(); - - let wallet_1_name = "w1"; - let wallet_2_name = "w2"; - await createWallet(clientConfig, wallet_1_name); - await createWallet(clientConfig, wallet_2_name); - await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - - - // Deposit, repeat send - let wallet_3_name = "w3"; - let wallet_4_name = "w4"; - await createWallet(clientConfig, wallet_3_name); - await createWallet(clientConfig, wallet_4_name); - await depositAndRepeatSend(clientConfig, wallet_3_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - console.log("Completed test for Deposit, repeat send"); - - // Transfer-sender after transfer-receiver - let wallet_5_name = "w5"; - let wallet_6_name = "w6"; - await createWallet(clientConfig, wallet_5_name); - await createWallet(clientConfig, wallet_6_name); - await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); - console.log("Completed test for Transfer-sender after transfer-receiver"); - - // Deposit of 1000 coins in same wallet, and transfer each one 1000 times - let wallet_7_name = "w7"; - await createWallet(clientConfig, wallet_7_name); - await depositAndTransfer(clientConfig, wallet_7_name); - console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); - - // Test for interruption of transferSend before sign first - let wallet_8_name = "w8"; - let wallet_9_name = "w9"; - await createWallet(clientConfig, wallet_8_name); - await createWallet(clientConfig, wallet_9_name); - await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); - console.log("Completed test for interruption of transferSend before sign first"); - - // Test for interruption of transferSend before sign second - let wallet_10_name = "w10"; - let wallet_11_name = "w11"; - await createWallet(clientConfig, wallet_10_name); - await createWallet(clientConfig, wallet_11_name); - await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); - console.log("Completed test for interruption of transferSend before sign second"); - - // Test for interruption of sign with Electrum unavailability - let wallet_12_name = "w12"; - let wallet_13_name = "w13"; - await createWallet(clientConfig, wallet_12_name); - await createWallet(clientConfig, wallet_13_name); - await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); - console.log("Completed test for interruption of sign with Electrum unavailability"); - - // Test for interruption of transfer receive with Electrum unavailability - let wallet_14_name = "w14"; - let wallet_15_name = "w15"; - await createWallet(clientConfig, wallet_14_name); - await createWallet(clientConfig, wallet_15_name); - await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); - console.log("Completed test for interruption of transfer receive with Electrum unavailability"); - - // Test for interruption of transfer receive with mercury server unavailability - let wallet_16_name = "w16"; - let wallet_17_name = "w17"; - await createWallet(clientConfig, wallet_16_name); - await createWallet(clientConfig, wallet_17_name); - await interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_16_name, wallet_17_name); - console.log("Completed test for interruption of transfer receive with mercury server unavailability"); - - // Deposit, iterative self transfer - let wallet_18_name = "w18"; - await createWallet(clientConfig, wallet_18_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); -})(); From dcc24cbe4718c16dce7fa9cb6013ceb540ea84d5 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 15:01:50 +0530 Subject: [PATCH 122/153] fix: generateBlock for coins in mempool --- clients/apps/nodejs/test_basic_workflow2.js | 913 ++++++++++++++++++++ 1 file changed, 913 insertions(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index e69de29b..97c0cf6f 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -0,0 +1,913 @@ +const util = require('node:util'); +const exec = util.promisify(require('node:child_process').exec); +const assert = require('node:assert/strict'); +const mercurynodejslib = require('mercurynodejslib'); +const { CoinStatus } = require('mercurynodejslib/coin_enum'); +const client_config = require('./client_config'); + +async function removeDatabase() { + try { + const clientConfig = client_config.load(); + const { stdout, stderr } = await exec(`rm ./${clientConfig.databaseFile}`); + console.log('stdout:', stdout); + console.error('stderr:', stderr); + } catch (e) { + console.error(e); + } +} + +const sleep = (ms) => { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function createWallet(clientConfig, walletName) { + + let wallet = await mercurynodejslib.createWallet(clientConfig, walletName); + assert.equal(wallet.name, walletName); + + // TODO: add more assertions +} + +async function generateBlock(numBlocks) { + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + exec(generateBlockCommand); + console.log(`Generated ${numBlocks} blocks`); +} + +async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { + + deposit_info["amount"] = amount; + console.log("deposit_coin: ", deposit_info); + + const amountInBtc = amount / 100000000; + + // Sending Bitcoin using bitcoin-cli + try { + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + exec(sendBitcoinCommand); + console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); + await generateBlock(3); + } catch (error) { + console.error('Error sending Bitcoin:', error.message); + return; + } +} + +async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_name, amount, deposit_info); + + let coin = undefined; + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + console.log(list_coins); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + + console.log("coin: ", coin); + + for (let i = 0; i < 10; i++) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); + + console.log("transfer_address: ", transfer_address); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("coin transferSend: ", coin); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + } + + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; + + let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); + + console.log("txid: ", txid); + +} + +async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_name, amount, deposit_info); + + let coin = undefined; + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + + console.log("coin: ", coin); + + const electrumClient = await mercurynodejslib.getElectrumClient(clientConfig); + + let block_header = await electrumClient.request('blockchain.headers.subscribe'); + let currentBlockHeight = block_header.height; + console.log("Current block height: ", currentBlockHeight); + + while (coin.locktime <= currentBlockHeight) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); + + console.log("transfer_address: ", transfer_address); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("coin transferSend: ", coin); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + + // Fetch the coin again to get the updated locktime + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + coin = list_coins.find(c => c.statechain_id === coin.statechain_id); + + console.log("Updated coin: ", coin); + } + + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; + + let txid = await mercurynodejslib.withdrawCoin(clientConfig, wallet_name, coin.statechain_id, withdraw_address, null); + + console.log("txid: ", txid); +} + +async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + + let withdraw_address = "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"; + + let txid = await mercurynodejslib.broadcastBackupTransaction(clientConfig, wallet_2_name, coin.statechain_id, withdraw_address, null); + + console.log("txid: ", txid); +} + +async function depositAndRepeatSend(clientConfig, wallet_1_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + for (let i = 0; i < 10; i++) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + } + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null); + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); +} + +async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + + try { + transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet one again, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is TRANSFERRED"), + `Unexpected error message: ${error.message}`); + } +} + +async function depositAndTransfer(clientConfig, wallet_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + for (let i = 0; i < 10; i++) { + await depositCoin(clientConfig, wallet_name, amount, deposit_info); + + let coin = undefined; + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + + break; + } + } + + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_name); + + for (let coin of list_coins) { + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_name, null); + + console.log("transfer_address: ", transfer_address); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("coin transferSend: ", coin); + + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_name); + + console.log("received_statechain_ids: ", received_statechain_ids); + + assert(received_statechain_ids.length > 0); + assert(received_statechain_ids[0] == coin.statechain_id); + } +} + +async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + + try { + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet one, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Server public nonce is not available."), + `Unexpected error message: ${error.message}`); + } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); +} + +const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { + let coin_nonce = mercury_wasm.createAndCommitNonces(coin); + + let server_pubnonce = await signFirst(clientConfig, coin_nonce.sign_first_request_payload); + + coin.secret_nonce = coin_nonce.secret_nonce; + coin.public_nonce = coin_nonce.public_nonce; + coin.server_public_nonce = server_pubnonce; + coin.blinding_factor = coin_nonce.blinding_factor; + + const serverInfo = await utils.infoConfig(clientConfig, electrumClient); + + let new_block_height = 0; + if (block_height == null) { + const block_header = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) + new_block_height = block_header.height; + } else { + new_block_height = block_height; + } + + const initlock = serverInfo.initlock; + const interval = serverInfo.interval; + const feeRateSatsPerByte = serverInfo.fee_rate_sats_per_byte; + + let partialSigRequest = mercury_wasm.getPartialSigRequest( + coin, + new_block_height, + initlock, + interval, + feeRateSatsPerByte, + qtBackupTx, + toAddress, + network, + isWithdrawal); + + const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + + let serverPartialSig; + + try { + serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); + assert.fail("Expected error when signing second transaction, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Server partial signature is not available."), + `Unexpected error message: ${error.message}`); + } + + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); +} + +async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coinDeposited = undefined; + + console.log("coin: ", coinDeposited); + + while (!coinDeposited) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await sleep(5000); + continue; + } + + coinDeposited = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coinDeposited); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + const db = await getDatabase(clientConfig); + + const electrumClient = await getElectrumClient(clientConfig); + + let batchId = (options && options.batchId) || null; + + let wallet = await sqlite_manager.getWallet(db, walletName); + + const backupTxs = await sqlite_manager.getBackupTxs(db, statechainId); + + if (backupTxs.length === 0) { + throw new Error(`There is no backup transaction for the statechain id ${statechainId}`); + } + + const new_tx_n = backupTxs.length + 1; + + let coinsWithStatechainId = wallet.coins.filter(c => { + return c.statechain_id === statechainId + }); + + if (!coinsWithStatechainId || coinsWithStatechainId.length === 0) { + throw new Error(`There is no coin for the statechain id ${statechainId}`); + } + + // If the user sends to himself, he will have two coins with same statechain_id + // In this case, we need to find the one with the lowest locktime + // Sort the coins by locktime in ascending order and pick the first one + let coin = coinsWithStatechainId.sort((a, b) => a.locktime - b.locktime)[0]; + + if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) { + throw new Error(`Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}`); + } + + if (coin.locktime == null) { + throw new Error("Coin.locktime is null"); + } + + const blockHeader = await electrumClient.request('blockchain.headers.subscribe'); // request(promise) + const currentBlockheight = blockHeader.height; + + if (currentBlockheight > coin.locktime) { + throw new Error(`The coin is expired. Coin locktime is ${coin.locktime} and current blockheight is ${currentBlockheight}`); + } + + const statechain_id = coin.statechain_id; + const signed_statechain_id = coin.signed_statechain_id; + + const isWithdrawal = false; + const qtBackupTx = backupTxs.length; + + backupTxs.sort((a, b) => a.tx_n - b.tx_n); + + const bkp_tx1 = backupTxs[0]; + + const block_height = mercury_wasm.getBlockheight(bkp_tx1); + + const decodedTransferAddress = mercury_wasm.decodeTransferAddress(toAddress); + const new_auth_pubkey = decodedTransferAddress.auth_pubkey; + + const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); + + coin = await new_transaction(clientConfig, electrumClient, coin, transfer_address.transfer_receive, isWithdrawal, qtBackupTx, block_height, wallet.network); +} + +async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + + try { + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + assert.fail("Expected error when transferring from wallet one, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Error getting fee rate from electrum server"), + `Unexpected error message: ${error.message}`); + } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); +} + +async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + + try { + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + assert.fail("Expected error when receiving into wallet two, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Error getting fee rate from electrum server"), + `Unexpected error message: ${error.message}`); + } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); +} + +async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const deposit_info = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + let tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + + let usedToken = tokenList.find(token => token.token_id === tokenId); + + assert(usedToken.spent); + + await depositCoin(clientConfig, wallet_1_name, amount, deposit_info); + + let coin = undefined; + + console.log("coin: ", coin); + + while (!coin) { + const list_coins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + let coinsWithStatechainId = list_coins.filter(c => { + return c.statechain_id === deposit_info.statechain_id && c.status === CoinStatus.CONFIRMED; + }); + + if (coinsWithStatechainId.length === 0) { + console.log("Waiting for coin to be confirmed..."); + console.log(`Check the address ${deposit_info.deposit_address} ...\n`); + await generateBlock(3); + await sleep(5000); + continue; + } + + coin = coinsWithStatechainId[0]; + break; + } + + console.log("coin: ", coin); + + let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); + + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + + try { + let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + assert.fail("Expected error when receiving into wallet two, but no error was thrown"); + } catch (error) { + console.log("Expected error received: ", error.message); + assert(error.message.includes("Failed to get message address from mercury server"), + `Unexpected error message: ${error.message}`); + } + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); +} + +(async () => { + + const clientConfig = client_config.load(); + + let wallet_1_name = "w1"; + let wallet_2_name = "w2"; + await createWallet(clientConfig, wallet_1_name); + await createWallet(clientConfig, wallet_2_name); + await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + + + // Deposit, repeat send + let wallet_3_name = "w3"; + let wallet_4_name = "w4"; + await createWallet(clientConfig, wallet_3_name); + await createWallet(clientConfig, wallet_4_name); + await depositAndRepeatSend(clientConfig, wallet_3_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); + console.log("Completed test for Deposit, repeat send"); + + // Transfer-sender after transfer-receiver + let wallet_5_name = "w5"; + let wallet_6_name = "w6"; + await createWallet(clientConfig, wallet_5_name); + await createWallet(clientConfig, wallet_6_name); + await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + console.log("Completed test for Transfer-sender after transfer-receiver"); + + // Deposit of 1000 coins in same wallet, and transfer each one 1000 times + let wallet_7_name = "w7"; + await createWallet(clientConfig, wallet_7_name); + await depositAndTransfer(clientConfig, wallet_7_name); + console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); + + // Test for interruption of transferSend before sign first + let wallet_8_name = "w8"; + let wallet_9_name = "w9"; + await createWallet(clientConfig, wallet_8_name); + await createWallet(clientConfig, wallet_9_name); + await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); + console.log("Completed test for interruption of transferSend before sign first"); + + // Test for interruption of transferSend before sign second + let wallet_10_name = "w10"; + let wallet_11_name = "w11"; + await createWallet(clientConfig, wallet_10_name); + await createWallet(clientConfig, wallet_11_name); + await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); + console.log("Completed test for interruption of transferSend before sign second"); + + // Test for interruption of sign with Electrum unavailability + let wallet_12_name = "w12"; + let wallet_13_name = "w13"; + await createWallet(clientConfig, wallet_12_name); + await createWallet(clientConfig, wallet_13_name); + await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); + console.log("Completed test for interruption of sign with Electrum unavailability"); + + // Test for interruption of transfer receive with Electrum unavailability + let wallet_14_name = "w14"; + let wallet_15_name = "w15"; + await createWallet(clientConfig, wallet_14_name); + await createWallet(clientConfig, wallet_15_name); + await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); + console.log("Completed test for interruption of transfer receive with Electrum unavailability"); + + // Test for interruption of transfer receive with mercury server unavailability + let wallet_16_name = "w16"; + let wallet_17_name = "w17"; + await createWallet(clientConfig, wallet_16_name); + await createWallet(clientConfig, wallet_17_name); + await interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_16_name, wallet_17_name); + console.log("Completed test for interruption of transfer receive with mercury server unavailability"); + + // Deposit, iterative self transfer + let wallet_18_name = "w18"; + await createWallet(clientConfig, wallet_18_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); +})(); From 7c0995f6e1ab23652940446da98a2d61448c2d09 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 17:01:36 +0530 Subject: [PATCH 123/153] debug: fee rate --- clients/libs/nodejs/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index bd9eb229..31b3908d 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -14,7 +14,7 @@ const infoConfig = async (clientConfig, ecl) => { throw new Error("Error getting fee rate from electrum server"); } - // console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); + console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); // Why does it happen? if (fee_rate_btc_per_kb <= 0) { @@ -22,7 +22,7 @@ const infoConfig = async (clientConfig, ecl) => { } const fee_rate_sats_per_byte = (fee_rate_btc_per_kb * 100000.0); - // console.log("fee_rate_sats_per_byte: " + fee_rate_sats_per_byte); + console.log("fee_rate_sats_per_byte: " + fee_rate_sats_per_byte); const torProxy = clientConfig.torProxy; From 175eb144be13639a47e5918902ebc7983b6332ca Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 18:40:56 +0530 Subject: [PATCH 124/153] debug: coin confirmation --- clients/libs/rust/src/coin_status.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clients/libs/rust/src/coin_status.rs b/clients/libs/rust/src/coin_status.rs index 71284d6a..09d3c502 100644 --- a/clients/libs/rust/src/coin_status.rs +++ b/clients/libs/rust/src/coin_status.rs @@ -78,6 +78,11 @@ async fn check_deposit(client_config: &ClientConfig, coin: &mut Coin, wallet_net coin.status = CoinStatus::UNCONFIRMED; + console.log("Blockheight ", blockheight); + console.log("UTXO height ", utxo.height); + console.log("Confirmations ", confirmations); + console.log("Confirmation target ", client_config.confirmation_target); + if confirmations as u32 >= client_config.confirmation_target { coin.status = CoinStatus::CONFIRMED; } From 1d3ed9a69592ee16b74719437f059c72e30181ac Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 19:03:47 +0530 Subject: [PATCH 125/153] debug: coin status --- clients/libs/nodejs/coin_status.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/libs/nodejs/coin_status.js b/clients/libs/nodejs/coin_status.js index 26e453f2..d348f86b 100644 --- a/clients/libs/nodejs/coin_status.js +++ b/clients/libs/nodejs/coin_status.js @@ -177,7 +177,7 @@ const updateCoins = async (clientConfig, electrumClient, db, wallet_name) => { let coin = wallet.coins[i]; if (coin.status == CoinStatus.INITIALISED || coin.status == CoinStatus.IN_MEMPOOL || coin.status == CoinStatus.UNCONFIRMED) { - + console.log("COIN STATUS ", coin.status); let depositResult = await checkDeposit(clientConfig, electrumClient, coin, network); if (depositResult) { From ce47ac2bf7e6542cdccaea928cbb8ff73574a6ee Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 19:35:56 +0530 Subject: [PATCH 126/153] debug: checkDeposit --- clients/libs/nodejs/coin_status.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clients/libs/nodejs/coin_status.js b/clients/libs/nodejs/coin_status.js index d348f86b..c8f5ad78 100644 --- a/clients/libs/nodejs/coin_status.js +++ b/clients/libs/nodejs/coin_status.js @@ -73,6 +73,11 @@ const checkDeposit = async (clientConfig, electrumClient, coin, wallet_network) const confirmations = blockheight - utxo.height + 1; const confirmationTarget = clientConfig.confirmationTarget; + console.log("CONFIRMATIONS ", confirmations); + console.log("CONFIRMATION TARGET ", confirmationTarget); + console.log("COIN STATUS ", coin.status); + console.log("BLOCK HEIGHT ", block_height); + console.log("UTXO HEIGHT ", utxo.height); coin.status = CoinStatus.UNCONFIRMED; @@ -180,6 +185,8 @@ const updateCoins = async (clientConfig, electrumClient, db, wallet_name) => { console.log("COIN STATUS ", coin.status); let depositResult = await checkDeposit(clientConfig, electrumClient, coin, network); + console.log("DEPOSIT RESULT ", depositResult); + if (depositResult) { wallet.activities.push(depositResult.activity); await sqlite_manager.insertTransaction(db, coin.statechain_id, [depositResult.backup_tx]); From 4dd599bc41b92e7bb17e3f02d42558229b8418ea Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 13 Jun 2024 20:40:55 +0530 Subject: [PATCH 127/153] fix: typos --- clients/libs/nodejs/coin_status.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/libs/nodejs/coin_status.js b/clients/libs/nodejs/coin_status.js index c8f5ad78..68a0ae4e 100644 --- a/clients/libs/nodejs/coin_status.js +++ b/clients/libs/nodejs/coin_status.js @@ -76,7 +76,7 @@ const checkDeposit = async (clientConfig, electrumClient, coin, wallet_network) console.log("CONFIRMATIONS ", confirmations); console.log("CONFIRMATION TARGET ", confirmationTarget); console.log("COIN STATUS ", coin.status); - console.log("BLOCK HEIGHT ", block_height); + console.log("BLOCK HEIGHT ", blockheight); console.log("UTXO HEIGHT ", utxo.height); coin.status = CoinStatus.UNCONFIRMED; From 75fe69fc0166b47ddd2c3aa711214fc35b4ce86e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 10:38:54 +0530 Subject: [PATCH 128/153] debug: network disconnect --- clients/apps/nodejs/test_basic_workflow2.js | 40 ++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 97c0cf6f..6834c659 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -479,8 +479,8 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Disconnect mercurylayer_mercury_1 from network"); + // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); @@ -490,8 +490,8 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na assert(error.message.includes("Server public nonce is not available."), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Connect mercurylayer_mercury_1 from network"); + // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { @@ -531,8 +531,8 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Disconnect mercurylayer_mercury_1 from network"); + // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); let serverPartialSig; @@ -545,8 +545,8 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Connect mercurylayer_mercury_1 from network"); + // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { @@ -696,8 +696,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + // console.log("Disconnect mercurylayer_mercury_1 from network"); + // await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); @@ -707,8 +707,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + // console.log("Connect mercurylayer_mercury_1 from network"); + // await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -756,8 +756,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + // console.log("Disconnect mercurylayer_mercury_1 from network"); + // await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -767,8 +767,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + // console.log("Connect mercurylayer_mercury_1 from network"); + // await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -816,8 +816,8 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Disconnect mercurylayer_mercury_1 from network"); + // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -827,8 +827,8 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon assert(error.message.includes("Failed to get message address from mercury server"), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + // console.log("Connect mercurylayer_mercury_1 from network"); + // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } (async () => { From 88f035fc1ff07205ed51182468a6ce383f77fcc3 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 11:22:42 +0530 Subject: [PATCH 129/153] debug: fee_rate --- clients/libs/nodejs/utils.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index 31b3908d..c30daead 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -7,12 +7,14 @@ const infoConfig = async (clientConfig, ecl) => { const statechain_entity_url = clientConfig.statechainEntity; const path = "info/config"; - let fee_rate_btc_per_kb; - try { - fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) - } catch (error) { - throw new Error("Error getting fee rate from electrum server"); - } + let fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) + + // let fee_rate_btc_per_kb; + // try { + // fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) + // } catch (error) { + // throw new Error("Error getting fee rate from electrum server"); + // } console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); From 83b3d2bf6ff7f7cce1a859ed5a61593dc451742e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 12:07:26 +0530 Subject: [PATCH 130/153] debug: blockheight --- clients/apps/nodejs/test_basic_workflow2.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 6834c659..d6363924 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -32,6 +32,12 @@ async function generateBlock(numBlocks) { const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); console.log(`Generated ${numBlocks} blocks`); + + const clientConfig = client_config.load(); + const electrumClient = await getElectrumClient(clientConfig); + const block_header = await electrumClient.request('blockchain.headers.subscribe'); + const blockheight = block_header.height; + console.log("Current block height: ", blockheight); } async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { From ad150547869694965dad53f212a85d378c8856aa Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 13:23:09 +0530 Subject: [PATCH 131/153] fix: getElectrumClient error --- clients/apps/nodejs/test_basic_workflow2.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index d6363924..698f1ba4 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -28,6 +28,21 @@ async function createWallet(clientConfig, walletName) { // TODO: add more assertions } +const getElectrumClient = async (clientConfig) => { + + const urlElectrum = clientConfig.electrumServer; + const urlElectrumObject = new URL(urlElectrum); + + const electrumPort = parseInt(urlElectrumObject.port, 10); + const electrumHostname = urlElectrumObject.hostname; + const electrumProtocol = urlElectrumObject.protocol.slice(0, -1); + + const electrumClient = new ElectrumCli(electrumPort, electrumHostname, electrumProtocol); + await electrumClient.connect(); + + return electrumClient; +} + async function generateBlock(numBlocks) { const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); From ae3442339206cd0c26de50654ccc7d3dc947bd8e Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 13:40:06 +0530 Subject: [PATCH 132/153] fix: ElectrumCli import --- clients/apps/nodejs/test_basic_workflow2.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 698f1ba4..ce16b567 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -4,6 +4,7 @@ const assert = require('node:assert/strict'); const mercurynodejslib = require('mercurynodejslib'); const { CoinStatus } = require('mercurynodejslib/coin_enum'); const client_config = require('./client_config'); +const ElectrumCli = require('@mempool/electrum-client'); async function removeDatabase() { try { From 09d5340f1c6054148d5d0f0c7d997d8f400adb67 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 14 Jun 2024 16:34:15 +0530 Subject: [PATCH 133/153] debug: running all tests --- clients/apps/nodejs/test_basic_workflow2.js | 40 ++++++++++----------- clients/libs/nodejs/utils.js | 14 ++++---- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index ce16b567..8781bc02 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -501,8 +501,8 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - // console.log("Disconnect mercurylayer_mercury_1 from network"); - // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); @@ -512,8 +512,8 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na assert(error.message.includes("Server public nonce is not available."), `Unexpected error message: ${error.message}`); } - // console.log("Connect mercurylayer_mercury_1 from network"); - // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { @@ -553,8 +553,8 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW const serverPartialSigRequest = partialSigRequest.partial_signature_request_payload; - // console.log("Disconnect mercurylayer_mercury_1 from network"); - // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); let serverPartialSig; @@ -567,8 +567,8 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW `Unexpected error message: ${error.message}`); } - // console.log("Connect mercurylayer_mercury_1 from network"); - // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_name) { @@ -718,8 +718,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - // console.log("Disconnect mercurylayer_mercury_1 from network"); - // await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); @@ -729,8 +729,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - // console.log("Connect mercurylayer_mercury_1 from network"); - // await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -778,8 +778,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - // console.log("Disconnect mercurylayer_mercury_1 from network"); - // await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -789,8 +789,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - // console.log("Connect mercurylayer_mercury_1 from network"); - // await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); } async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -838,8 +838,8 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - // console.log("Disconnect mercurylayer_mercury_1 from network"); - // await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); + console.log("Disconnect mercurylayer_mercury_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_mercury_1"); try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -849,8 +849,8 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon assert(error.message.includes("Failed to get message address from mercury server"), `Unexpected error message: ${error.message}`); } - // console.log("Connect mercurylayer_mercury_1 from network"); - // await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); + console.log("Connect mercurylayer_mercury_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_mercury_1"); } (async () => { diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index c30daead..31b3908d 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -7,14 +7,12 @@ const infoConfig = async (clientConfig, ecl) => { const statechain_entity_url = clientConfig.statechainEntity; const path = "info/config"; - let fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) - - // let fee_rate_btc_per_kb; - // try { - // fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) - // } catch (error) { - // throw new Error("Error getting fee rate from electrum server"); - // } + let fee_rate_btc_per_kb; + try { + fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) + } catch (error) { + throw new Error("Error getting fee rate from electrum server"); + } console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); From 6fb3601ae628f76e02885d467ca4e37d98769e1d Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 17 Jun 2024 13:44:26 +0530 Subject: [PATCH 134/153] fix: remove generateBlock if coin waiting to be confirmed --- clients/apps/nodejs/test_basic_workflow2.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 8781bc02..2d00205e 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -104,7 +104,6 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -169,7 +168,6 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -247,7 +245,6 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -307,7 +304,6 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -366,7 +362,6 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -428,7 +423,6 @@ async function depositAndTransfer(clientConfig, wallet_name) { if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -488,7 +482,6 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -705,7 +698,6 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -763,7 +755,6 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } @@ -823,7 +814,6 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon if (coinsWithStatechainId.length === 0) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); - await generateBlock(3); await sleep(5000); continue; } From f92e0a889559f582ca67b49a92b1315d58f22c04 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 17 Jun 2024 20:17:42 +0530 Subject: [PATCH 135/153] fix: replace electrumx with electrs --- clients/apps/nodejs/config/default.json | 6 +++--- docker-compose-test.yml | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index b8497809..a050167a 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -2,10 +2,10 @@ "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://127.0.0.1:50001", - "electrumServer": "ssl://127.0.0.1:50002", + "__electrumServer": "tcp://0.0.0.0:50001", + "electrumServer": "tcp://0.0.0.0:50001", "electrumType": "electrs", - "_electrumServer": "tcp://127.0.0.1:50001", + "_electrumServer": "tcp://0.0.0.0:50001", "network": "regtest", "feeRateTolerance": 5, "databaseFile": "wallet.db", diff --git a/docker-compose-test.yml b/docker-compose-test.yml index ee4ac5af..a6dee854 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -25,15 +25,19 @@ services: volumes: - bitcoin_data:/root/.bitcoin - electrumx: - image: lukechilds/electrumx:latest + electrs: + image: getumbrel/electrs:latest environment: - DAEMON_URL: http://user:pass@bitcoin:18443/ - COIN: BitcoinSegwit - NET: regtest + ELECTRS_LOG_FILTERS: "INFO" + ELECTRS_NETWORK: "regtest" + ELECTRS_DAEMON_RPC_ADDR: "http://user:pass@bitcoin:18443" + ELECTRS_ELECTRUM_RPC_ADDR: "0.0.0.0:50001" + ELECTRS_SERVER_BANNER: "Umbrel Electrs" + ELECTRS_COOKIE_FILE: "/bitcoin/regtest/.cookie" ports: - - "50002:50002" - "50001:50001" + volumes: + - electrs-data:/data enclave-sgx: build: @@ -64,4 +68,5 @@ services: - postgres volumes: - bitcoin_data: \ No newline at end of file + bitcoin_data: + electrs-data: \ No newline at end of file From ba9b579748fbad38a0f960b8a397d6882c896b92 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 17 Jun 2024 21:07:59 +0530 Subject: [PATCH 136/153] fix: electrs volume mount --- .github/workflows/tests.yml | 2 +- docker-compose-test.yml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4f572608..8e3f52ef 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,7 +51,7 @@ jobs: docker exec $container_id bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress bcrt1pcngfxjdkf4r2h26k52dh5nunxg8m68uf4lkfhmfjvjj6agfkm5jqmftw4e 0.0001 - name: Verify ElectrumX Service with Curl run: | - container_id=$(docker ps -qf "name=mercurylayer_electrumx_1") + container_id=$(docker ps -qf "name=mercurylayer_electrs_1") echo "Container ID: $container_id" docker logs $container_id - name: Verify Enclave Service with Curl diff --git a/docker-compose-test.yml b/docker-compose-test.yml index a6dee854..4596690b 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -30,14 +30,15 @@ services: environment: ELECTRS_LOG_FILTERS: "INFO" ELECTRS_NETWORK: "regtest" - ELECTRS_DAEMON_RPC_ADDR: "http://user:pass@bitcoin:18443" + ELECTRS_DAEMON_RPC_ADDR: "bitcoin:18443" ELECTRS_ELECTRUM_RPC_ADDR: "0.0.0.0:50001" ELECTRS_SERVER_BANNER: "Umbrel Electrs" - ELECTRS_COOKIE_FILE: "/bitcoin/regtest/.cookie" + ELECTRS_COOKIE_FILE: "/root/.cookie" ports: - "50001:50001" volumes: - electrs-data:/data + - bitcoin_data:/root/.cookie enclave-sgx: build: From 8e59924f302ebcb0afed15ead682599f14820695 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 17 Jun 2024 21:45:42 +0530 Subject: [PATCH 137/153] fix: get_new_x1 --- clients/apps/nodejs/config/default.json | 6 +++--- clients/libs/nodejs/transfer_send.js | 6 ++++-- docker-compose-test.yml | 20 +++++++------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index a050167a..b8497809 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -2,10 +2,10 @@ "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://0.0.0.0:50001", - "electrumServer": "tcp://0.0.0.0:50001", + "__electrumServer": "tcp://127.0.0.1:50001", + "electrumServer": "ssl://127.0.0.1:50002", "electrumType": "electrs", - "_electrumServer": "tcp://0.0.0.0:50001", + "_electrumServer": "tcp://127.0.0.1:50001", "network": "regtest", "feeRateTolerance": 5, "databaseFile": "wallet.db", diff --git a/clients/libs/nodejs/transfer_send.js b/clients/libs/nodejs/transfer_send.js index b916b9ca..695dff6d 100644 --- a/clients/libs/nodejs/transfer_send.js +++ b/clients/libs/nodejs/transfer_send.js @@ -146,7 +146,9 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new let response; try { response = await axios.post(url, transferSenderRequestPayload, socksAgent); - console.log('Response:', response.data); + if (response.data) { + console.log('Response:', response.data); + } } catch (error) { if (error.code === 'ECONNREFUSED') { console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); @@ -154,7 +156,7 @@ const get_new_x1 = async (clientConfig, statechain_id, signed_statechain_id, new console.error('An error occurred:', error.message); } } - return response.data.x1; + return response?.data?.x1; } module.exports = { execute }; \ No newline at end of file diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 4596690b..ee4ac5af 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -25,20 +25,15 @@ services: volumes: - bitcoin_data:/root/.bitcoin - electrs: - image: getumbrel/electrs:latest + electrumx: + image: lukechilds/electrumx:latest environment: - ELECTRS_LOG_FILTERS: "INFO" - ELECTRS_NETWORK: "regtest" - ELECTRS_DAEMON_RPC_ADDR: "bitcoin:18443" - ELECTRS_ELECTRUM_RPC_ADDR: "0.0.0.0:50001" - ELECTRS_SERVER_BANNER: "Umbrel Electrs" - ELECTRS_COOKIE_FILE: "/root/.cookie" + DAEMON_URL: http://user:pass@bitcoin:18443/ + COIN: BitcoinSegwit + NET: regtest ports: + - "50002:50002" - "50001:50001" - volumes: - - electrs-data:/data - - bitcoin_data:/root/.cookie enclave-sgx: build: @@ -69,5 +64,4 @@ services: - postgres volumes: - bitcoin_data: - electrs-data: \ No newline at end of file + bitcoin_data: \ No newline at end of file From e63d77e319adde377760327f9d3fb38e1f842108 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 18 Jun 2024 11:53:21 +0530 Subject: [PATCH 138/153] fix: add entrypoint file for electrs --- clients/apps/nodejs/config/default.json | 6 +++--- docker-compose-test.yml | 19 +++++++++++++------ entrypoint.sh | 8 ++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 entrypoint.sh diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index b8497809..916fefe4 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -2,10 +2,10 @@ "statechainEntity": "http://0.0.0.0:8000", "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", - "__electrumServer": "tcp://127.0.0.1:50001", - "electrumServer": "ssl://127.0.0.1:50002", + "__electrumServer": "tcp://0.0.0.0:50001", + "electrumServer": "ssl://0.0.0.0:50001", "electrumType": "electrs", - "_electrumServer": "tcp://127.0.0.1:50001", + "_electrumServer": "tcp://0.0.0.0:50001", "network": "regtest", "feeRateTolerance": 5, "databaseFile": "wallet.db", diff --git a/docker-compose-test.yml b/docker-compose-test.yml index ee4ac5af..f8cca7c3 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -25,15 +25,22 @@ services: volumes: - bitcoin_data:/root/.bitcoin - electrumx: - image: lukechilds/electrumx:latest + electrs: + image: getumbrel/electrs:latest environment: - DAEMON_URL: http://user:pass@bitcoin:18443/ - COIN: BitcoinSegwit - NET: regtest + ELECTRS_LOG_FILTERS: "INFO" + ELECTRS_NETWORK: "regtest" + ELECTRS_DAEMON_RPC_ADDR: "bitcoin:18443" + ELECTRS_ELECTRUM_RPC_ADDR: "0.0.0.0:50001" + ELECTRS_SERVER_BANNER: "Umbrel Electrs" + ELECTRS_COOKIE_FILE: "/root/.bitcoin/.cookie" + ELECTRS_DB_PATH: "/data/db" ports: - - "50002:50002" - "50001:50001" + volumes: + - electrs-data:/data + - bitcoin_data:/root/.bitcoin + entrypoint: ["/entrypoint.sh"] enclave-sgx: build: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..319a2d0b --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +# Create the required directory +mkdir -p /data/db/regtest + +# Execute the main process +exec "$@" From 2999005c5d2ff80f6037156e3b3e6ffccb4480c3 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Tue, 18 Jun 2024 11:56:27 +0530 Subject: [PATCH 139/153] fix: add volume to docker compose --- docker-compose-test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index f8cca7c3..02746ccb 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -71,4 +71,5 @@ services: - postgres volumes: - bitcoin_data: \ No newline at end of file + bitcoin_data: + electrs-data: \ No newline at end of file From 09f87360f7e569f4b1fd63962a009b7339b9b75b Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 20 Jun 2024 13:09:01 +0530 Subject: [PATCH 140/153] fix: electrs configs --- .github/workflows/tests.yml | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 4 +-- docker-compose-test.yml | 27 ++++++++------------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8e3f52ef..8da2ba72 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -40,7 +40,7 @@ jobs: sleep 80 # Adjust time as necessary for services to initialize - name: Verify Bitcoin core Service with Curl run: | - container_id=$(docker ps -qf "name=mercurylayer_bitcoin_1") + container_id=$(docker ps -qf "name=mercurylayer_bitcoind_1") echo "Container ID: $container_id" docker logs $container_id wallet_name="new_wallet" diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 2d00205e..86ef61b7 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -45,7 +45,7 @@ const getElectrumClient = async (clientConfig) => { } async function generateBlock(numBlocks) { - const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; + const generateBlockCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoind_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass generatetoaddress ${numBlocks} "bcrt1qgh48u8aj4jvjkalc28lqujyx2wveck4jsm59x9"`; exec(generateBlockCommand); console.log(`Generated ${numBlocks} blocks`); @@ -65,7 +65,7 @@ async function depositCoin(clientConfig, wallet_name, amount, deposit_info) { // Sending Bitcoin using bitcoin-cli try { - const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoin_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; + const sendBitcoinCommand = `docker exec $(docker ps -qf "name=mercurylayer_bitcoind_1") bitcoin-cli -regtest -rpcuser=user -rpcpassword=pass sendtoaddress ${deposit_info.deposit_address} ${amountInBtc}`; exec(sendBitcoinCommand); console.log(`Sent ${amountInBtc} BTC to ${deposit_info.deposit_address}`); await generateBlock(3); diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 02746ccb..90b05d42 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -8,39 +8,32 @@ services: ports: - "5432:5432" - bitcoin: - image: ruimarinho/bitcoin-core:latest - command: - -printtoconsole - -regtest=1 - -rpcallowip=0.0.0.0/0 - -rpcbind=0.0.0.0 - -rpcuser=user - -rpcpassword=pass - -txindex=1 - -server=1 - -fallbackfee=0.0001 + bitcoind: + image: lncm/bitcoind:v22.0@sha256:37a1adb29b3abc9f972f0d981f45e41e5fca2e22816a023faa9fdc0084aa4507 + user: root + command: -regtest -rpcbind=0.0.0.0 -rpcallowip=0.0.0.0/0 -rpcauth=user:63cf03615adebaa9356591f95b07ec7b$$920588e53f94798bda636acac1b6a77e10e3ee7fe57e414d62f3ee9e580cd27a -fallbackfee=0.0001 ports: - "18443:18443" volumes: - bitcoin_data:/root/.bitcoin electrs: - image: getumbrel/electrs:latest + image: getumbrel/electrs:v0.9.4@sha256:b1590ac6cfb0e5b481c6a7af7f0626d76cbb91c63702b0f5c47e2829e9c37997 + user: root environment: ELECTRS_LOG_FILTERS: "INFO" ELECTRS_NETWORK: "regtest" - ELECTRS_DAEMON_RPC_ADDR: "bitcoin:18443" + ELECTRS_DAEMON_RPC_ADDR: "bitcoind:18443" + ELECTRS_DAEMON_P2P_ADDR: "bitcoind:18444" ELECTRS_ELECTRUM_RPC_ADDR: "0.0.0.0:50001" ELECTRS_SERVER_BANNER: "Umbrel Electrs" - ELECTRS_COOKIE_FILE: "/root/.bitcoin/.cookie" - ELECTRS_DB_PATH: "/data/db" ports: - "50001:50001" volumes: - electrs-data:/data - bitcoin_data:/root/.bitcoin - entrypoint: ["/entrypoint.sh"] + depends_on: + - bitcoind enclave-sgx: build: From eebdf4cb2a256fca6a1da2d91f9d327bb0ae4ca9 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 20 Jun 2024 16:21:10 +0530 Subject: [PATCH 141/153] fix: add generateBlock if coin stuck in mempool --- clients/apps/nodejs/config/default.json | 2 +- clients/apps/nodejs/test_basic_workflow2.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/clients/apps/nodejs/config/default.json b/clients/apps/nodejs/config/default.json index 916fefe4..a050167a 100644 --- a/clients/apps/nodejs/config/default.json +++ b/clients/apps/nodejs/config/default.json @@ -3,7 +3,7 @@ "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", "_statechainEntity": "http://45.76.136.11:8500/", "__electrumServer": "tcp://0.0.0.0:50001", - "electrumServer": "ssl://0.0.0.0:50001", + "electrumServer": "tcp://0.0.0.0:50001", "electrumType": "electrs", "_electrumServer": "tcp://0.0.0.0:50001", "network": "regtest", diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 86ef61b7..15e55814 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -105,6 +105,7 @@ async function walletTransfersToItselfAndWithdraw(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -169,6 +170,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -246,6 +248,7 @@ async function walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, walle console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -305,6 +308,7 @@ async function depositAndRepeatSend(clientConfig, wallet_1_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -363,6 +367,7 @@ async function transferSenderAfterTransferReceiver(clientConfig, wallet_1_name, console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -424,6 +429,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -483,6 +489,7 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -594,6 +601,7 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -699,6 +707,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -756,6 +765,7 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } @@ -815,6 +825,7 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon console.log("Waiting for coin to be confirmed..."); console.log(`Check the address ${deposit_info.deposit_address} ...\n`); await sleep(5000); + generateBlock(1); continue; } From 01b033772ed914bd024ae77f8e7d211cc740f30f Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 20 Jun 2024 18:45:28 +0530 Subject: [PATCH 142/153] fix: signSecond test for interruption --- clients/apps/nodejs/test_basic_workflow2.js | 48 +++++++++++++-------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 15e55814..d6bf54e8 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -5,6 +5,11 @@ const mercurynodejslib = require('mercurynodejslib'); const { CoinStatus } = require('mercurynodejslib/coin_enum'); const client_config = require('./client_config'); const ElectrumCli = require('@mempool/electrum-client'); +const sqlite3 = require('sqlite3').verbose(); +const sqlite_manager = require('../../libs/nodejs/sqlite_manager'); +const mercury_wasm = require('mercury-wasm'); +const transaction = require('../../libs/nodejs/transaction'); +const utils = require('../../libs/nodejs/utils'); async function removeDatabase() { try { @@ -17,6 +22,13 @@ async function removeDatabase() { } } +const getDatabase = async (clientConfig) => { + const databaseFile = clientConfig.databaseFile; + const db = new sqlite3.Database(databaseFile); + await sqlite_manager.createTables(db); + return db; +} + const sleep = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -519,7 +531,7 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isWithdrawal, qtBackupTx, block_height, network) => { let coin_nonce = mercury_wasm.createAndCommitNonces(coin); - let server_pubnonce = await signFirst(clientConfig, coin_nonce.sign_first_request_payload); + let server_pubnonce = await transaction.signFirst(clientConfig, coin_nonce.sign_first_request_payload); coin.secret_nonce = coin_nonce.secret_nonce; coin.public_nonce = coin_nonce.public_nonce; @@ -559,7 +571,7 @@ const new_transaction = async(clientConfig, electrumClient, coin, toAddress, isW let serverPartialSig; try { - serverPartialSig = await signSecond(clientConfig, serverPartialSigRequest); + serverPartialSig = await transaction.signSecond(clientConfig, serverPartialSigRequest); assert.fail("Expected error when signing second transaction, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); @@ -617,24 +629,26 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n const electrumClient = await getElectrumClient(clientConfig); + let options = transfer_address.transfer_receive; + let batchId = (options && options.batchId) || null; - let wallet = await sqlite_manager.getWallet(db, walletName); + let wallet = await sqlite_manager.getWallet(db, wallet_1_name); - const backupTxs = await sqlite_manager.getBackupTxs(db, statechainId); + const backupTxs = await sqlite_manager.getBackupTxs(db, coinDeposited.statechain_id); if (backupTxs.length === 0) { - throw new Error(`There is no backup transaction for the statechain id ${statechainId}`); + throw new Error(`There is no backup transaction for the statechain id ${coinDeposited.statechain_id}`); } const new_tx_n = backupTxs.length + 1; let coinsWithStatechainId = wallet.coins.filter(c => { - return c.statechain_id === statechainId + return c.statechain_id === coinDeposited.statechain_id }); if (!coinsWithStatechainId || coinsWithStatechainId.length === 0) { - throw new Error(`There is no coin for the statechain id ${statechainId}`); + throw new Error(`There is no coin for the statechain id ${coinDeposited.statechain_id}`); } // If the user sends to himself, he will have two coins with same statechain_id @@ -669,10 +683,10 @@ async function interruptBeforeSignSecond(clientConfig, wallet_1_name, wallet_2_n const block_height = mercury_wasm.getBlockheight(bkp_tx1); - const decodedTransferAddress = mercury_wasm.decodeTransferAddress(toAddress); + const decodedTransferAddress = mercury_wasm.decodeTransferAddress(transfer_address.transfer_receive); const new_auth_pubkey = decodedTransferAddress.auth_pubkey; - const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); + // const new_x1 = await get_new_x1(clientConfig, statechain_id, signed_statechain_id, new_auth_pubkey, batchId); coin = await new_transaction(clientConfig, electrumClient, coin, transfer_address.transfer_receive, isWithdrawal, qtBackupTx, block_height, wallet.network); } @@ -719,8 +733,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na let transfer_address = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Disconnect mercurylayer_electrs_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrs_1"); try { coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); @@ -730,8 +744,8 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Connect mercurylayer_electrs_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrs_1"); } async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_1_name, wallet_2_name) { @@ -779,8 +793,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, coin = await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transfer_address.transfer_receive); - console.log("Disconnect mercurylayer_mercury_1 from network"); - await exec("docker network disconnect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Disconnect mercurylayer_electrs_1 from network"); + await exec("docker network disconnect mercurylayer_default mercurylayer_electrs_1"); try { let received_statechain_ids = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); @@ -790,8 +804,8 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert(error.message.includes("Error getting fee rate from electrum server"), `Unexpected error message: ${error.message}`); } - console.log("Connect mercurylayer_mercury_1 from network"); - await exec("docker network connect mercurylayer_default mercurylayer_electrumx_1"); + console.log("Connect mercurylayer_electrs_1 from network"); + await exec("docker network connect mercurylayer_default mercurylayer_electrs_1"); } async function interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_1_name, wallet_2_name) { From 8e26312d7a54fe05c680267b5f66c1edb8f3010c Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Thu, 20 Jun 2024 21:40:03 +0530 Subject: [PATCH 143/153] fix: error message for electrs unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 4 ++-- clients/libs/nodejs/coin_status.js | 8 ++++++-- clients/libs/nodejs/utils.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index d6bf54e8..be1c90c4 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -741,7 +741,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na assert.fail("Expected error when transferring from wallet one, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting fee rate from electrum server"), + assert(error.message.includes("Error getting fee rate from electrs server"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_electrs_1 from network"); @@ -801,7 +801,7 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert.fail("Expected error when receiving into wallet two, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting fee rate from electrum server"), + assert(error.message.includes("Error getting unspent list from electrs server"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_electrs_1 from network"); diff --git a/clients/libs/nodejs/coin_status.js b/clients/libs/nodejs/coin_status.js index 68a0ae4e..dcf4a262 100644 --- a/clients/libs/nodejs/coin_status.js +++ b/clients/libs/nodejs/coin_status.js @@ -27,8 +27,12 @@ const checkDeposit = async (clientConfig, electrumClient, coin, wallet_network) reversedHash = reversedHash.toString('hex'); let utxo = null; - - let utxo_list = await electrumClient.request('blockchain.scripthash.listunspent', [reversedHash]); + let utxo_list = null; + try { + utxo_list = await electrumClient.request('blockchain.scripthash.listunspent', [reversedHash]); + } catch (error) { + throw new Error("Error getting unspent list from electrs server"); + } for (let unspent of utxo_list) { if (unspent.value === coin.amount) { diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index 31b3908d..c3eae8c6 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -11,7 +11,7 @@ const infoConfig = async (clientConfig, ecl) => { try { fee_rate_btc_per_kb = await ecl.request('blockchain.estimatefee', [3]); // request(promise) } catch (error) { - throw new Error("Error getting fee rate from electrum server"); + throw new Error("Error getting fee rate from electrs server"); } console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); From d95f5dff6563b392dcc2bf78c15c30bfa32c34d2 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 21 Jun 2024 12:49:02 +0530 Subject: [PATCH 144/153] fix: assertions for server unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index be1c90c4..35e37b3d 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -741,7 +741,7 @@ async function interruptSignWithElectrumUnavailability(clientConfig, wallet_1_na assert.fail("Expected error when transferring from wallet one, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting fee rate from electrs server"), + assert(error.message.includes("connect ECONNREFUSED 0.0.0.0:50001"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_electrs_1 from network"); @@ -801,7 +801,7 @@ async function interruptTransferReceiveWithElectrumUnavailability(clientConfig, assert.fail("Expected error when receiving into wallet two, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Error getting unspent list from electrs server"), + assert(error.message.includes("connect ECONNREFUSED 0.0.0.0:50001"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_electrs_1 from network"); @@ -861,7 +861,7 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon assert.fail("Expected error when receiving into wallet two, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Failed to get message address from mercury server"), + assert(error.message.includes("connect ECONNREFUSED 0.0.0.0:8000"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_mercury_1 from network"); @@ -947,4 +947,5 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon let wallet_18_name = "w18"; await createWallet(clientConfig, wallet_18_name); await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); + console.log("Completed test for Deposit, iterative self transfer"); })(); From 8a5b8270fdd0ba187a1c47d0f232c02d836053ab Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 21 Jun 2024 13:18:47 +0530 Subject: [PATCH 145/153] fix: exit test on completion --- clients/apps/nodejs/test_basic_workflow2.js | 163 ++++++++++---------- 1 file changed, 85 insertions(+), 78 deletions(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 35e37b3d..03468949 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -870,82 +870,89 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon (async () => { - const clientConfig = client_config.load(); - - let wallet_1_name = "w1"; - let wallet_2_name = "w2"; - await createWallet(clientConfig, wallet_1_name); - await createWallet(clientConfig, wallet_2_name); - await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); - - - // Deposit, repeat send - let wallet_3_name = "w3"; - let wallet_4_name = "w4"; - await createWallet(clientConfig, wallet_3_name); - await createWallet(clientConfig, wallet_4_name); - await depositAndRepeatSend(clientConfig, wallet_3_name); - await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); - console.log("Completed test for Deposit, repeat send"); - - // Transfer-sender after transfer-receiver - let wallet_5_name = "w5"; - let wallet_6_name = "w6"; - await createWallet(clientConfig, wallet_5_name); - await createWallet(clientConfig, wallet_6_name); - await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); - console.log("Completed test for Transfer-sender after transfer-receiver"); - - // Deposit of 1000 coins in same wallet, and transfer each one 1000 times - let wallet_7_name = "w7"; - await createWallet(clientConfig, wallet_7_name); - await depositAndTransfer(clientConfig, wallet_7_name); - console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); - - // Test for interruption of transferSend before sign first - let wallet_8_name = "w8"; - let wallet_9_name = "w9"; - await createWallet(clientConfig, wallet_8_name); - await createWallet(clientConfig, wallet_9_name); - await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); - console.log("Completed test for interruption of transferSend before sign first"); - - // Test for interruption of transferSend before sign second - let wallet_10_name = "w10"; - let wallet_11_name = "w11"; - await createWallet(clientConfig, wallet_10_name); - await createWallet(clientConfig, wallet_11_name); - await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); - console.log("Completed test for interruption of transferSend before sign second"); - - // Test for interruption of sign with Electrum unavailability - let wallet_12_name = "w12"; - let wallet_13_name = "w13"; - await createWallet(clientConfig, wallet_12_name); - await createWallet(clientConfig, wallet_13_name); - await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); - console.log("Completed test for interruption of sign with Electrum unavailability"); - - // Test for interruption of transfer receive with Electrum unavailability - let wallet_14_name = "w14"; - let wallet_15_name = "w15"; - await createWallet(clientConfig, wallet_14_name); - await createWallet(clientConfig, wallet_15_name); - await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); - console.log("Completed test for interruption of transfer receive with Electrum unavailability"); - - // Test for interruption of transfer receive with mercury server unavailability - let wallet_16_name = "w16"; - let wallet_17_name = "w17"; - await createWallet(clientConfig, wallet_16_name); - await createWallet(clientConfig, wallet_17_name); - await interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_16_name, wallet_17_name); - console.log("Completed test for interruption of transfer receive with mercury server unavailability"); - - // Deposit, iterative self transfer - let wallet_18_name = "w18"; - await createWallet(clientConfig, wallet_18_name); - await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); - console.log("Completed test for Deposit, iterative self transfer"); + try { + const clientConfig = client_config.load(); + + let wallet_1_name = "w1"; + let wallet_2_name = "w2"; + await createWallet(clientConfig, wallet_1_name); + await createWallet(clientConfig, wallet_2_name); + await walletTransfersToItselfAndWithdraw(clientConfig, wallet_1_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_1_name, wallet_2_name); + + + // Deposit, repeat send + let wallet_3_name = "w3"; + let wallet_4_name = "w4"; + await createWallet(clientConfig, wallet_3_name); + await createWallet(clientConfig, wallet_4_name); + await depositAndRepeatSend(clientConfig, wallet_3_name); + await walletTransfersToAnotherAndBroadcastsBackupTx(clientConfig, wallet_3_name, wallet_4_name); + console.log("Completed test for Deposit, repeat send"); + + // Transfer-sender after transfer-receiver + let wallet_5_name = "w5"; + let wallet_6_name = "w6"; + await createWallet(clientConfig, wallet_5_name); + await createWallet(clientConfig, wallet_6_name); + await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); + console.log("Completed test for Transfer-sender after transfer-receiver"); + + // Deposit of 1000 coins in same wallet, and transfer each one 1000 times + let wallet_7_name = "w7"; + await createWallet(clientConfig, wallet_7_name); + await depositAndTransfer(clientConfig, wallet_7_name); + console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); + + // Test for interruption of transferSend before sign first + let wallet_8_name = "w8"; + let wallet_9_name = "w9"; + await createWallet(clientConfig, wallet_8_name); + await createWallet(clientConfig, wallet_9_name); + await interruptBeforeSignFirst(clientConfig, wallet_8_name, wallet_9_name); + console.log("Completed test for interruption of transferSend before sign first"); + + // Test for interruption of transferSend before sign second + let wallet_10_name = "w10"; + let wallet_11_name = "w11"; + await createWallet(clientConfig, wallet_10_name); + await createWallet(clientConfig, wallet_11_name); + await interruptBeforeSignSecond(clientConfig, wallet_10_name, wallet_11_name); + console.log("Completed test for interruption of transferSend before sign second"); + + // Test for interruption of sign with Electrum unavailability + let wallet_12_name = "w12"; + let wallet_13_name = "w13"; + await createWallet(clientConfig, wallet_12_name); + await createWallet(clientConfig, wallet_13_name); + await interruptSignWithElectrumUnavailability(clientConfig, wallet_12_name, wallet_13_name); + console.log("Completed test for interruption of sign with Electrum unavailability"); + + // Test for interruption of transfer receive with Electrum unavailability + let wallet_14_name = "w14"; + let wallet_15_name = "w15"; + await createWallet(clientConfig, wallet_14_name); + await createWallet(clientConfig, wallet_15_name); + await interruptTransferReceiveWithElectrumUnavailability(clientConfig, wallet_14_name, wallet_15_name); + console.log("Completed test for interruption of transfer receive with Electrum unavailability"); + + // Test for interruption of transfer receive with mercury server unavailability + let wallet_16_name = "w16"; + let wallet_17_name = "w17"; + await createWallet(clientConfig, wallet_16_name); + await createWallet(clientConfig, wallet_17_name); + await interruptTransferReceiveWithMercuryServerUnavailability(clientConfig, wallet_16_name, wallet_17_name); + console.log("Completed test for interruption of transfer receive with mercury server unavailability"); + + // Deposit, iterative self transfer + let wallet_18_name = "w18"; + await createWallet(clientConfig, wallet_18_name); + await walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw(clientConfig, wallet_18_name); + console.log("Completed test for Deposit, iterative self transfer"); + + process.exit(0); // Exit successfully + } catch (error) { + console.error("Test encountered an error:", error); + process.exit(1); // Exit with failure + } })(); From e056439e2246af7bc25e2c14a070e7f029a2fe05 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 21 Jun 2024 13:40:47 +0530 Subject: [PATCH 146/153] fix: tear down of docker compose --- .github/workflows/tests.yml | 10 ++-------- clients/apps/nodejs/test_basic_workflow2.js | 6 +++--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8da2ba72..b3f78196 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -38,7 +38,7 @@ jobs: - name: Wait for services to be ready run: | sleep 80 # Adjust time as necessary for services to initialize - - name: Verify Bitcoin core Service with Curl + - name: Verify Bitcoin daemon Service with Curl run: | container_id=$(docker ps -qf "name=mercurylayer_bitcoind_1") echo "Container ID: $container_id" @@ -123,12 +123,6 @@ jobs: run: | cd clients/apps/nodejs node test_basic_workflow2.js - - name: Print environment variables - run: printenv - - name: Print working directory - run: pwd - - name: List files in working directory - run: ls -al - name: Tear Down run: | - docker-compose down + docker-compose -f docker-compose-test.yml down diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 03468949..78921e16 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -425,7 +425,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { assert(usedToken.spent); - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 100; i++) { await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -898,11 +898,11 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); console.log("Completed test for Transfer-sender after transfer-receiver"); - // Deposit of 1000 coins in same wallet, and transfer each one 1000 times + // Deposit of 100 coins in same wallet, and transfer each one 100 times let wallet_7_name = "w7"; await createWallet(clientConfig, wallet_7_name); await depositAndTransfer(clientConfig, wallet_7_name); - console.log("Completed test for Deposit of 1000 coins in same wallet, and transfer each one 1000 times"); + console.log("Completed test for Deposit of 100 coins in same wallet, and transfer each one 100 times"); // Test for interruption of transferSend before sign first let wallet_8_name = "w8"; From 410dff21ddcda9fd6c028acbe1702b3e848c6a85 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Fri, 21 Jun 2024 14:15:00 +0530 Subject: [PATCH 147/153] fix: remove npm link --- .github/workflows/tests.yml | 9 --------- clients/apps/nodejs/test_basic_workflow2.js | 4 ++-- clients/libs/rust/src/coin_status.rs | 5 ----- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b3f78196..39200da7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -110,15 +110,6 @@ jobs: run: | cd clients/libs/nodejs npm install - - name: Run npm link - run: | - cd clients/libs/nodejs - npm link --save - - - name: Link mercurynodejslib - run: | - cd clients/apps/nodejs - npm link mercurynodejslib - name: Run Client-Side Tests run: | cd clients/apps/nodejs diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 78921e16..111bddbf 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -425,7 +425,7 @@ async function depositAndTransfer(clientConfig, wallet_name) { assert(usedToken.spent); - for (let i = 0; i < 100; i++) { + for (let i = 0; i < 10; i++) { await depositCoin(clientConfig, wallet_name, amount, deposit_info); let coin = undefined; @@ -898,7 +898,7 @@ async function interruptTransferReceiveWithMercuryServerUnavailability(clientCon await transferSenderAfterTransferReceiver(clientConfig, wallet_5_name, wallet_6_name); console.log("Completed test for Transfer-sender after transfer-receiver"); - // Deposit of 100 coins in same wallet, and transfer each one 100 times + // Deposit of 10 coins in same wallet, and transfer each one 10 times let wallet_7_name = "w7"; await createWallet(clientConfig, wallet_7_name); await depositAndTransfer(clientConfig, wallet_7_name); diff --git a/clients/libs/rust/src/coin_status.rs b/clients/libs/rust/src/coin_status.rs index 09d3c502..71284d6a 100644 --- a/clients/libs/rust/src/coin_status.rs +++ b/clients/libs/rust/src/coin_status.rs @@ -78,11 +78,6 @@ async fn check_deposit(client_config: &ClientConfig, coin: &mut Coin, wallet_net coin.status = CoinStatus::UNCONFIRMED; - console.log("Blockheight ", blockheight); - console.log("UTXO height ", utxo.height); - console.log("Confirmations ", confirmations); - console.log("Confirmation target ", client_config.confirmation_target); - if confirmations as u32 >= client_config.confirmation_target { coin.status = CoinStatus::CONFIRMED; } From bb2de4a4e0901cc9d841ec8c7ee5d55d448f8bc5 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 24 Jun 2024 11:26:14 +0530 Subject: [PATCH 148/153] fix: replace lockbox_url to enclaves in env variables --- docker-compose-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 90b05d42..0bdefbc3 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -53,11 +53,11 @@ services: context: . dockerfile: ./server/Dockerfile environment: - LOCKBOX_URL: http://mercurylayer_enclave-sgx_1:18080 NETWORK: regtest LOCKHEIGHT_INIT: 1100 LH_DECREMENT: 1 CONNECTION_STRING: postgres://postgres:pgpassword@postgres:5432/postgres + ENCLAVES: '[{"url": "http://mercurylayer_enclave-sgx_1:18080", "allow_deposit": true}]' ports: - "8000:8000" depends_on: From ea2fe407a942005ad569f0f670274b9ab6ab2724 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 24 Jun 2024 18:26:10 +0530 Subject: [PATCH 149/153] fix: assert statement for mercury server unavailability --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index 111bddbf..d2207c80 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -521,7 +521,7 @@ async function interruptBeforeSignFirst(clientConfig, wallet_1_name, wallet_2_na assert.fail("Expected error when transferring from wallet one, but no error was thrown"); } catch (error) { console.log("Expected error received: ", error.message); - assert(error.message.includes("Server public nonce is not available."), + assert(error.message.includes("connect ECONNREFUSED 0.0.0.0:8000"), `Unexpected error message: ${error.message}`); } console.log("Connect mercurylayer_mercury_1 from network"); From 1c04b3b645411ec31f6fba1ce4ebfe3d8bd53fb2 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 24 Jun 2024 19:36:16 +0530 Subject: [PATCH 150/153] fix: remove build.yml from actions --- .github/workflows/build.yml | 62 ------------------------------------- server/.env_example | 2 +- 2 files changed, 1 insertion(+), 63 deletions(-) delete mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 77db1907..00000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,62 +0,0 @@ -name: Build/release Electron app - -on: - pull_request: - branches: - - dev - push: - branches: - - main - tags: - - v*.*.* - -jobs: - release: - permissions: write-all - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ubuntu-latest, windows-latest] - - steps: - - name: Check out Git repository - uses: actions/checkout@v3 - - - name: Install Node.js - uses: actions/setup-node@v3 - with: - node-version: 18 - - - name: Install Dependencies - working-directory: "./clients/react-app" - run: npm install - - - name: build-linux - if: matrix.os == 'ubuntu-latest' - working-directory: "./clients/react-app" - run: | - pwd - id - npm run build:linux - - - name: build-win - if: matrix.os == 'windows-latest' - working-directory: "./clients/react-app" - run: npm run build:win - - - name: release - uses: softprops/action-gh-release@v1 - with: - draft: true - files: | - ./clients/react-app/dist/*.exe - ./clients/react-app/dist/*.zip - ./clients/react-app/dist/*.dmg - ./clients/react-app/dist/*.AppImage - ./clients/react-app/dist/*.snap - ./clients/react-app/dist/*.deb - ./clients/react-app/dist/*.rpm - ./clients/react-app/dist/*.tar.gz - ./clients/react-app/dist/*.yml - ./clients/react-app/dist/*.blockmap diff --git a/server/.env_example b/server/.env_example index f584779e..210c9eb1 100644 --- a/server/.env_example +++ b/server/.env_example @@ -1,5 +1,5 @@ -LOCKBOX_URL = NETWORK = LOCKHEIGHT_INIT = LH_DECREMENT = CONNECTION_STRING = +ENCLAVES = From 9ff75f1568be76d91e6a4d46c51198eb96d870d1 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 26 Jun 2024 13:58:23 +0530 Subject: [PATCH 151/153] chore: remove unused imports and logs --- .github/workflows/tests.yml | 2 -- clients/libs/nodejs/coin_status.js | 8 -------- clients/libs/nodejs/deposit.js | 1 - clients/libs/nodejs/index.js | 4 +--- clients/libs/nodejs/transaction.js | 1 - clients/libs/nodejs/utils.js | 4 ---- entrypoint.sh | 8 -------- server/src/endpoints/deposit.rs | 3 +-- 8 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 entrypoint.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 39200da7..54a7577d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,11 +4,9 @@ on: push: branches: - dev - - dev2 pull_request: branches: - dev - - dev2 jobs: test: diff --git a/clients/libs/nodejs/coin_status.js b/clients/libs/nodejs/coin_status.js index dcf4a262..3134a95b 100644 --- a/clients/libs/nodejs/coin_status.js +++ b/clients/libs/nodejs/coin_status.js @@ -77,11 +77,6 @@ const checkDeposit = async (clientConfig, electrumClient, coin, wallet_network) const confirmations = blockheight - utxo.height + 1; const confirmationTarget = clientConfig.confirmationTarget; - console.log("CONFIRMATIONS ", confirmations); - console.log("CONFIRMATION TARGET ", confirmationTarget); - console.log("COIN STATUS ", coin.status); - console.log("BLOCK HEIGHT ", blockheight); - console.log("UTXO HEIGHT ", utxo.height); coin.status = CoinStatus.UNCONFIRMED; @@ -186,11 +181,8 @@ const updateCoins = async (clientConfig, electrumClient, db, wallet_name) => { let coin = wallet.coins[i]; if (coin.status == CoinStatus.INITIALISED || coin.status == CoinStatus.IN_MEMPOOL || coin.status == CoinStatus.UNCONFIRMED) { - console.log("COIN STATUS ", coin.status); let depositResult = await checkDeposit(clientConfig, electrumClient, coin, network); - console.log("DEPOSIT RESULT ", depositResult); - if (depositResult) { wallet.activities.push(depositResult.activity); await sqlite_manager.insertTransaction(db, coin.statechain_id, [depositResult.backup_tx]); diff --git a/clients/libs/nodejs/deposit.js b/clients/libs/nodejs/deposit.js index 64293243..64de8b07 100644 --- a/clients/libs/nodejs/deposit.js +++ b/clients/libs/nodejs/deposit.js @@ -107,7 +107,6 @@ const init = async (clientConfig, db, wallet, token_id) => { if (torProxy) { socksAgent = { httpAgent: new SocksProxyAgent(torProxy) }; } - console.log('depositMsg1 ->', depositMsg1) const response = await axios.post(url, depositMsg1, socksAgent); if (response.status != 200) { diff --git a/clients/libs/nodejs/index.js b/clients/libs/nodejs/index.js index 31c8e654..180cb912 100644 --- a/clients/libs/nodejs/index.js +++ b/clients/libs/nodejs/index.js @@ -15,7 +15,6 @@ const sqlite_manager = require('./sqlite_manager'); const { v4: uuidv4 } = require('uuid'); const wallet_manager = require('./wallet'); -const path = require('path'); const getDatabase = async (clientConfig) => { const databaseFile = clientConfig.databaseFile; @@ -197,6 +196,5 @@ module.exports = { withdrawCoin, newTransferAddress, transferSend, - transferReceive, - getElectrumClient + transferReceive }; \ No newline at end of file diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 69d7dc05..15f41178 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -105,7 +105,6 @@ const signSecond = async (clientConfig, partialSigRequest) => { let response; try { response = await axios.post(url, partialSigRequest, socksAgent); - console.log('Response:', response.data); } catch (error) { if (error.code === 'ECONNREFUSED') { console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); diff --git a/clients/libs/nodejs/utils.js b/clients/libs/nodejs/utils.js index c3eae8c6..e503c49b 100644 --- a/clients/libs/nodejs/utils.js +++ b/clients/libs/nodejs/utils.js @@ -14,16 +14,12 @@ const infoConfig = async (clientConfig, ecl) => { throw new Error("Error getting fee rate from electrs server"); } - console.log("fee_rate_btc_per_kb:", fee_rate_btc_per_kb); - // Why does it happen? if (fee_rate_btc_per_kb <= 0) { fee_rate_btc_per_kb = 0.00001; } const fee_rate_sats_per_byte = (fee_rate_btc_per_kb * 100000.0); - console.log("fee_rate_sats_per_byte: " + fee_rate_sats_per_byte); - const torProxy = clientConfig.torProxy; let socksAgent = undefined; diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index 319a2d0b..00000000 --- a/entrypoint.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e - -# Create the required directory -mkdir -p /data/db/regtest - -# Execute the main process -exec "$@" diff --git a/server/src/endpoints/deposit.rs b/server/src/endpoints/deposit.rs index f64b4fa9..25ee6f74 100644 --- a/server/src/endpoints/deposit.rs +++ b/server/src/endpoints/deposit.rs @@ -183,8 +183,7 @@ pub async fn post_deposit(statechain_entity: &State, deposit_m Err(err) => { let response_body = json!({ "error": "Internal Server Error", - "message": err.to_string(), - "payload": statechain_id.clone(), + "message": err.to_string() }); return status::Custom(Status::InternalServerError, Json(response_body)); From e0bbf7ebaebf03fcb17b6804f9fd10234af8fe16 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 26 Jun 2024 21:03:34 +0530 Subject: [PATCH 152/153] fix: add regtest and signet to config --- clients/apps/nodejs/config/regtest.json | 16 ++++++++++++++++ clients/apps/nodejs/config/signet.json | 16 ++++++++++++++++ clients/libs/nodejs/transaction.js | 1 - 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 clients/apps/nodejs/config/regtest.json create mode 100644 clients/apps/nodejs/config/signet.json diff --git a/clients/apps/nodejs/config/regtest.json b/clients/apps/nodejs/config/regtest.json new file mode 100644 index 00000000..477042c8 --- /dev/null +++ b/clients/apps/nodejs/config/regtest.json @@ -0,0 +1,16 @@ +{ + "statechainEntity": "http://0.0.0.0:8000", + "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", + "_statechainEntity": "http://45.76.136.11:8500/", + "__electrumServer": "tcp://0.0.0.0:50001", + "electrumServer": "tcp://0.0.0.0:50001", + "electrumType": "electrs", + "_electrumServer": "tcp://0.0.0.0:50001", + "network": "regtest", + "feeRateTolerance": 5, + "databaseFile": "wallet.db", + "confirmationTarget": 2, + "_torProxy": "socks5h://localhost:9050", + "torProxy": null, + "maxFeeRate": 1 +} \ No newline at end of file diff --git a/clients/apps/nodejs/config/signet.json b/clients/apps/nodejs/config/signet.json new file mode 100644 index 00000000..aeac5a9b --- /dev/null +++ b/clients/apps/nodejs/config/signet.json @@ -0,0 +1,16 @@ +{ + "statechainEntity": "http://127.0.0.1:8000", + "__statechainEntity": "http://j23wevaeducxuy3zahd6bpn4x76cymwz2j3bdixv7ow4awjrg5p6jaid.onion", + "_statechainEntity": "http://45.76.136.11:8500/", + "__electrumServer": "tcp://signet-electrumx.wakiyamap.dev:50001", + "electrumServer": "ssl://mempool.space:60602", + "electrumType": "electrs", + "_electrumServer": "tcp://localhost:50001", + "network": "signet", + "feeRateTolerance": 5, + "databaseFile": "wallet.db", + "confirmationTarget": 2, + "_torProxy": "socks5h://localhost:9050", + "torProxy": null, + "maxFeeRate": 1 +} \ No newline at end of file diff --git a/clients/libs/nodejs/transaction.js b/clients/libs/nodejs/transaction.js index 15f41178..434f30f7 100644 --- a/clients/libs/nodejs/transaction.js +++ b/clients/libs/nodejs/transaction.js @@ -66,7 +66,6 @@ const signFirst = async (clientConfig, signFirstRequestPayload) => { let response; try { response = await axios.post(url, signFirstRequestPayload, socksAgent); - console.log('Response:', response.data); } catch (error) { if (error.code === 'ECONNREFUSED') { console.error('Error: Connection refused. The server at 0.0.0.0:8000 is not available.'); From 6bfeeef3038691d253c6ad43c72152c810445630 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Wed, 26 Jun 2024 21:22:05 +0530 Subject: [PATCH 153/153] fix: getElectrumClient import --- clients/apps/nodejs/test_basic_workflow2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/apps/nodejs/test_basic_workflow2.js b/clients/apps/nodejs/test_basic_workflow2.js index d2207c80..fd74d1be 100644 --- a/clients/apps/nodejs/test_basic_workflow2.js +++ b/clients/apps/nodejs/test_basic_workflow2.js @@ -193,7 +193,7 @@ async function walletTransfersToItselfTillLocktimeReachesBlockHeightAndWithdraw( console.log("coin: ", coin); - const electrumClient = await mercurynodejslib.getElectrumClient(clientConfig); + const electrumClient = await getElectrumClient(clientConfig); let block_header = await electrumClient.request('blockchain.headers.subscribe'); let currentBlockHeight = block_header.height;