decred / dcrlnd

Decred Lightning Network Daemon ⚡️
MIT License
36 stars 24 forks source link

Docker examples #66

Closed fguisso closed 4 years ago

fguisso commented 4 years ago

Hello guys, I've been trying to script for a few months to automate my simnet environments and also update the example with LN docker-compose.

For now I have succeeded in building a Minimum Voting Wallet (MVW), see #12 , and interacting with dcrd anddcrwallet in a nice way. Just add a docker-compose up --build to get everything up and running.

I'm still going to improve some things, but I've opened PR so they can give me a light and tell if it's all by default since I'm not an expert docker.

What we have so far: You can choose wallet_seed and wallet_pass from docker-compose.yml or using --build-args. Thus, when docker creates the application, dcrwallet dockerfile uses two steps, one for creating the wallet, generating an address and saving that address to a volume like dcrd.conf, and the other for leaving the container clean only with what is needed. for the dcrwallet to run. With this, dcrd runs with --miningaddr flag set for your wallet and you can generate blocks and init dcrwallet with ticketbuyer.

Next steps:

I'm using this repos to refer in how MVW works:

fguisso commented 4 years ago

Draft for doc General workflow is the following:

dcrd, dcrwallet and MVW

We need to start dcrd service and generate 100 blocks for the first coinbase to mature, because without coins, the MVW(Minimum Voting Wallet) can't buy tickets.

# Run dcrd service for the first time
docker-compose start dcrd

# Use dcrctl to generate 100 blocks
docker-compose run dcrctl generate 100

Now we can start the dcrwallet and the MVW will work great. docker-compose start dcrwallet

dcrlnd, the history about Alice and Bob

Create Alices container and send funds from dcrwallet:

#Create "Alice"s container
docker-compose run -d --name alice dcrlnd

#Log into the "Alice" container
docker exec -it alice bash

# Generate a new backward compatible nested p2sh address for Alice:
alice$ dcrlncli -n simnet newaddress p2pkh

We can keep logged in Alice container, just need to use another terminal tab to execute commands in docker-compose.

Generate a block to update the wallet balance

docker-compose run dcrctl generate 1

Check Alice's wallet balance

alice$ dcrlncli -n simnet walletbalance


Create `Bob`s container and connect to `Alice`:
```bash
#Create "Bob"s container
`docker-compose run -d --name bob dcrlnd`

#Log into the "Bob" container
`docker exec -it bob bash`

Connect Bob node to Alice node

#Get identity pubkey from Bob's node
bob$ dcrlncli -n simnet getinfo
{
    "version": "0.2.0-pre+dev",
    ------>"identity_pubkey": "02fe9daea36c1b1bd9e8dc1f5dc9edfc887d223199f2bac99e1cf92eb8cdb654e7",
    "alias": "02fe9daea36c1b1bd9e8",
    "color": "#3399ff",
    "num_pending_channels": 0,
    "num_active_channels": 0,
    "num_inactive_channels": 0,
    "num_peers": 0,
    "block_height": 101,
    "block_hash": "0000066c30cee9bae93a05b9b0edacef1fea1443f4b43a96f5d0761a70965c18",
    "best_header_timestamp": 1576527447,
    "synced_to_chain": true,
    "synced_to_graph": false,
    "testnet": false,
    "chains": [
        {
            "chain": "decred",
            "network": "simnet"
        }
    ],
    "uris": null
}

#Connect Alice to Bob's node
alice$ dcrlncli -n simnet connect <bob_pubkey>@bob

# Check list of peers on "Alice" side:
alice$ dcrlncli -n simnet listpeers
{
    "peers": [
        {
            "pub_key": "02fe9daea36c1b1bd9e8dc1f5dc9edfc887d223199f2bac99e1cf92eb8cdb654e7",
            "address": "172.25.0.5:9735",
            "bytes_sent": "139",
            "bytes_recv": "139",
            "atoms_sent": "0",
            "atoms_recv": "0",
            "inbound": false,
            "ping_time": "0",
            "sync_type": "ACTIVE_SYNC"
        }
    ]
}

Create the Alice<->Bob channel.

# Open the channel with "Bob":
alice$ dcrlncli -n simnet openchannel --node_key=<bob_pubkey> --local_amt=1000000

# Include funding transaction in block thereby opening the channel:
# We need six confirmations to channel active
$ docker-compose run dcrctl generate 6

# Check that channel with "Bob" was opened:
alice$ dcrlncli --n simnet listchannels
{
    "channels": [
        {
            "active": true,
            "remote_pubkey": "02fe9daea36c1b1bd9e8dc1f5dc9edfc887d223199f2bac99e1cf92eb8cdb654e7",
            "channel_point": "ed19031307d1ae49b06f38587e9336dac1015be11ad59432f84af9de4d4fdef7:0",
            "chan_id": "112150186164224",
            "capacity": "1000000",
            "local_balance": "996360",
            "remote_balance": "0",
            "commit_fee": "3640",
            "commit_size": "328",
            "fee_per_kb": "10000",
            "unsettled_balance": "0",
            "total_atoms_sent": "0",
            "total_atoms_received": "0",
            "num_updates": "0",
            "pending_htlcs": [
            ],
            "csv_delay": 288,
            "private": false,
            "initiator": true,
            "chan_status_flags": "ChanStatusDefault",
            "local_chan_reserve_atoms": "10000",
            "remote_chan_reserve_atoms": "10000",
            "static_remote_key": true
        }
    ]
}

Send the payment from Alice to Bob.

# Add invoice on "Bob" side:
bob$ dcrlncli -n simnet addinvoice --amt=10000
{
        "r_hash": "<your_random_rhash_here>", 
        "pay_req": "<encoded_invoice>", 
        "add_index": 1
}

# Send payment from "Alice" to "Bob":
alice$ dcrlncli -n simnet payinvoice <encoded_invoice>

# Check "Alice"'s channel balance
alice$ dcrlncli -n simnet channelbalance

# Check "Bob"'s channel balance
bob$ lncli -n simnet channelbalance

Now we have open channel in which we sent only one payment, let's imagine that we sent lots of them and we'd now like to close the channel. Let's do it!

# List the "Alice" channel and retrieve "channel_point" which represents
# the opened channel:
alice$ dcrlncli -n simnet listchannels
{
    "channels": [
        {
            "active": true,
            "remote_pubkey": "02fe9daea36c1b1bd9e8dc1f5dc9edfc887d223199f2bac99e1cf92eb8cdb654e7",
            ------>"channel_point": "ed19031307d1ae49b06f38587e9336dac1015be11ad59432f84af9de4d4fdef7:0",
            "chan_id": "112150186164224",
            "capacity": "1000000",
            "local_balance": "986360",
            "remote_balance": "10000",
            "commit_fee": "3640",
            "commit_size": "364",
            "fee_per_kb": "10000",
            "unsettled_balance": "0",
            "total_atoms_sent": "10000",
            "total_atoms_received": "0",
            "num_updates": "2",
            "pending_htlcs": [
            ],
            "csv_delay": 288,
            "private": false,
            "initiator": true,
            "chan_status_flags": "ChanStatusDefault",
            "local_chan_reserve_atoms": "10000",
            "remote_chan_reserve_atoms": "10000",
            "static_remote_key": true
        }
    ]
}

# Channel point consists of two numbers separated by a colon. The first one 
# is "funding_txid" and the second one is "output_index":
alice$ dcrlncli -n simnet closechannel <funding_txid> <output_index>

# Include close transaction in a block thereby closing the channel:
$ docker-compose run dcrctl generate 6

# Check "Alice" on-chain balance was credited by her settled amount in the channel:
alice$ dcrlncli -n simnet walletbalance

# Check "Bob" on-chain balance was credited with the funds he received in the
# channel:
bob$ dcrlncli -n simnet walletbalance
{
    "total_balance": "10000",
    "confirmed_balance": "10000",
    "unconfirmed_balance": "0"
}
fguisso commented 4 years ago

@matheusd and @jholdstock can you review please? I stopped by simnet since we need MVW, but the upstream LND tutorial has a part about using testnet, wanted an idea as to whether it is better to create another compose just for testnet or continue the example with more dcrlnd nodes? https://github.com/lightningnetwork/lnd/tree/master/docker#connect-to-faucet-lightning-node

jholdstock commented 4 years ago

I've run into some issues whilst testing this, going to debug with @fguisso on Matrix