|
| 1 | +# Local regtest setup |
| 2 | + |
| 3 | +To help with development of apps that use the Loop daemon (`loopd`) for |
| 4 | +liquidity management, it is often useful to test it in controlled environments |
| 5 | +such as on the Bitcoin `regtest` network. |
| 6 | + |
| 7 | +Lightning Labs provides a stripped down version of the Loop server that works |
| 8 | +only for `regtest` and is only published in compiled, binary form (currently as |
| 9 | +a Docker image). |
| 10 | + |
| 11 | +The `docker-compose.yml` shows an example setup and is accompanied by a quick |
| 12 | +start script that should make getting started very easy. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +To get this quick start demo environment operational, you need to have the |
| 17 | +following tools installed on your system: |
| 18 | + - Docker |
| 19 | + - `docker-compose` |
| 20 | + - `jq` |
| 21 | + - `git` |
| 22 | + |
| 23 | +## Starting the environment |
| 24 | + |
| 25 | +Simply download the Loop repository and run the `start` command of the helper |
| 26 | +script. This will boot up the `docker-compose` environment, mine some `regtest` |
| 27 | +coins, connect and create channels between the two `lnd` nodes and hook up the |
| 28 | +Loop client and servers to each other. |
| 29 | + |
| 30 | +```shell |
| 31 | +$ git clone https://github.com/lightninglabs/loop |
| 32 | +$ cd loop/regtest |
| 33 | +$ ./regtest.sh start |
| 34 | +``` |
| 35 | + |
| 36 | +## Looping out |
| 37 | + |
| 38 | +Looping out is the process of moving funds from your channel (off-chain) balance |
| 39 | +into your on-chain wallet through what is called a submarine swap. We use the |
| 40 | +`--fast` option here, otherwise the server would wait 30 minutes before |
| 41 | +proceeding to allow for batched swaps. |
| 42 | + |
| 43 | +```shell |
| 44 | +$ ./regtest.sh loop out --amt 500000 --fast |
| 45 | +Send off-chain: 500000 sat |
| 46 | +Receive on-chain: 492688 sat |
| 47 | +Estimated total fee: 7312 sat |
| 48 | + |
| 49 | +Fast swap requested. |
| 50 | + |
| 51 | +CONTINUE SWAP? (y/n): y |
| 52 | +Swap initiated |
| 53 | +ID: f96b2e40fd75a63eba61a6ee1e0b17788d77804c0454beb5fbbe4a42a8a06245 |
| 54 | +HTLC address: bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 55 | + |
| 56 | +Run `loop monitor` to monitor progress. |
| 57 | +``` |
| 58 | + |
| 59 | +The swap has been initiated now. Let's observe how it works: |
| 60 | + |
| 61 | +```shell |
| 62 | +$ ./regtest.sh loop monitor |
| 63 | +Note: offchain cost may report as 0 after loopd restart during swap |
| 64 | +2021-03-19T17:01:39Z LOOP_OUT INITIATED 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 65 | +``` |
| 66 | + |
| 67 | +Leave the command running, it will update with every new event. Open a second |
| 68 | +terminal window in the same folder/location and mine one block: |
| 69 | + |
| 70 | +```shell |
| 71 | +$ ./regtest.sh mine 1 |
| 72 | +``` |
| 73 | + |
| 74 | +You should see your previous window with the `monitor` command update: |
| 75 | + |
| 76 | +```shell |
| 77 | +2021-03-19T17:01:39Z LOOP_OUT INITIATED 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 78 | +2021-03-19T17:04:37Z LOOP_OUT PREIMAGE_REVEALED 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 79 | +``` |
| 80 | + |
| 81 | +As soon as the `PREIMAGE_REVEALED` message appears, you know the HTLC was |
| 82 | +published to the chain. Finish the process by mining yet another block (in the |
| 83 | +second terminal window): |
| 84 | + |
| 85 | +```shell |
| 86 | +$ ./regtest.sh mine 1 |
| 87 | +``` |
| 88 | + |
| 89 | +Your `monitor` window should now show the completion of the swap: |
| 90 | + |
| 91 | +```shell |
| 92 | +2021-03-19T17:01:39Z LOOP_OUT INITIATED 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 93 | +2021-03-19T17:04:37Z LOOP_OUT PREIMAGE_REVEALED 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn |
| 94 | +2021-03-19T17:06:25Z LOOP_OUT SUCCESS 0.005 BTC - bcrt1qwgzsmag0579twr4d8hgfvarqxn27mdmdgfh7wc2hdlupsfqtxwyq5zltqn (cost: server 50, onchain 6687, offchain 0) |
| 95 | +``` |
| 96 | + |
| 97 | +Congratulations, you just did a loop out! |
| 98 | + |
| 99 | +# Loop in |
| 100 | + |
| 101 | +Looping in is very similar but does the opposite: swap your on-chain balance for |
| 102 | +off-chain (channel) balance: |
| 103 | + |
| 104 | +```shell |
| 105 | +$ ./regtest.sh loop in --amt 500000 |
| 106 | +Send on-chain: 500000 sat |
| 107 | +Receive off-chain: 492279 sat |
| 108 | +Estimated total fee: 7721 sat |
| 109 | + |
| 110 | +CONTINUE SWAP? (y/n): y |
| 111 | +Swap initiated |
| 112 | +ID: d95167e33e79df292e5ffeda29d34614a5a087c9378ffae003b9a25bb0f1c2e4 |
| 113 | +HTLC address (P2WSH): bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f |
| 114 | + |
| 115 | +Run `loop monitor` to monitor progress. |
| 116 | + |
| 117 | + |
| 118 | +$ ./regtest.sh loop monitor |
| 119 | +Note: offchain cost may report as 0 after loopd restart during swap |
| 120 | +2021-03-19T17:17:13Z LOOP_IN HTLC_PUBLISHED 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f |
| 121 | +``` |
| 122 | + |
| 123 | +In your second terminal window, go ahead and mine a block: |
| 124 | + |
| 125 | +```shell |
| 126 | +$ ./regtest.sh mine 1 |
| 127 | +``` |
| 128 | + |
| 129 | +You should see your previous window with the `monitor` command update: |
| 130 | + |
| 131 | +```shell |
| 132 | +2021-03-19T17:17:13Z LOOP_IN HTLC_PUBLISHED 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f |
| 133 | +2021-03-19T17:18:38Z LOOP_IN INVOICE_SETTLED 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f (cost: server -499929, onchain 7650, offchain 0) |
| 134 | +``` |
| 135 | + |
| 136 | +After mining yet another block, the process should complete: |
| 137 | + |
| 138 | +```shell |
| 139 | +$ ./regtest.sh mine 1 |
| 140 | +``` |
| 141 | + |
| 142 | +```shell |
| 143 | +2021-03-19T17:17:13Z LOOP_IN HTLC_PUBLISHED 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f |
| 144 | +2021-03-19T17:18:38Z LOOP_IN INVOICE_SETTLED 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f (cost: server -499929, onchain 7650, offchain 0) |
| 145 | +2021-03-19T17:19:21Z LOOP_IN SUCCESS 0.005 BTC - P2WSH: bcrt1q4p0pead4tfepm683fff8fl8g3kpx8spjztuquu2sctfzed9rufls2z0g2f (cost: server 71, onchain 7650, offchain 0) |
| 146 | +``` |
| 147 | + |
| 148 | +# Using the Loop server in an existing setup |
| 149 | + |
| 150 | +This `docker-compose` is only meant as a demo and quick start help. You can of |
| 151 | +course also integrate the Loop server Docker image into your existing `regtest` |
| 152 | +environment. |
| 153 | + |
| 154 | +Simply pull the image and run it by pointing it to an existing `lnd` node: |
| 155 | + |
| 156 | +```shell |
| 157 | +$ docker pull lightninglabs/loopserver:latest |
| 158 | +$ docker run -d \ |
| 159 | + -p 11009:11009 \ |
| 160 | + -v /some/dir/to/lnd:/root/.lnd \ |
| 161 | + lightninglabs/loopserver:latest \ |
| 162 | + daemon \ |
| 163 | + --maxamt=5000000 \ |
| 164 | + --lnd.host=some-lnd-node:10009 \ |
| 165 | + --lnd.macaroondir=/root/.lnd/data/chain/bitcoin/regtest \ |
| 166 | + --lnd.tlspath=/root/.lnd/tls.cert \ |
| 167 | +``` |
| 168 | + |
| 169 | +An existing Loop client (`loopd`) can then be pointed to this server with: |
| 170 | + |
| 171 | +```shell |
| 172 | +$ loopd \ |
| 173 | + --network=regtest \ |
| 174 | + --debuglevel=debug \ |
| 175 | + --server.host=localhost:11009 \ |
| 176 | + --server.notls \ |
| 177 | + --lnd.host=some-other-lnd-node:10009 \ |
| 178 | + --lnd.macaroonpath=/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon \ |
| 179 | + --lnd.tlspath=/root/.lnd/tls.cert |
| 180 | +``` |
| 181 | + |
| 182 | +The `--server.notls` is important here as the `regtest` version of the Loop |
| 183 | +server only supports insecure, non-TLS connections. |
| 184 | + |
| 185 | +Also make sure you connect the Loop server and Loop client to a different `lnd` |
| 186 | +node each since an `lnd` node cannot pay itself. Obviously there also need to be |
| 187 | +some channels with enough liquidity between the server's and client's `lnd` |
| 188 | +nodes (direct or indirect doesn't matter). |
0 commit comments