Skip to content

Commit 76ad7e3

Browse files
Traditional environment for backend development
1 parent 7b3c62e commit 76ad7e3

File tree

13 files changed

+2324
-15
lines changed

13 files changed

+2324
-15
lines changed

.env-sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ REDIS_HOST="localhost"
4747
REDIS_PORT="6379"
4848
REDIS_DB_NUMBER="1"
4949

50+
LOG_TO_CONSOLE=False
51+
LOGGER_LEVEL="WARNING"
52+
5053
# List of market price public APIs. If the currency is available in more than 1 API, will use median price.
5154
MARKET_PRICE_APIS = https://blockchain.info/ticker, https://api.yadio.io/exrates/BTC, https://bitpay.com/rates/BTC, https://criptoya.com/api/btc
5255

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,6 @@ mobile/html/Web.bundle/assets*
653653

654654
# Protocol Buffers
655655
api/lightning/*.proto
656+
657+
# Traditional environment
658+
regtest/

api/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def compute_avg_premium(queryset):
387387

388388
def validate_pgp_keys(pub_key, enc_priv_key):
389389
"""Validates PGP valid keys. Formats them in a way understandable by the frontend"""
390-
gpg = gnupg.GPG()
390+
gpg = gnupg.GPG(gnupghome=config("GNUPG_DIR", default=None))
391391

392392
# Standardize format with linux linebreaks '\n'. Windows users submitting their own keys have '\r\n' breaking communication.
393393
enc_priv_key = enc_priv_key.replace("\r\n", "\n").replace("\\", "\n")
@@ -441,7 +441,7 @@ def verify_signed_message(pub_key, signed_message):
441441
Verifies a signed cleartext PGP message. Returns whether the signature
442442
is valid (was made by the given pub_key) and the content of the message.
443443
"""
444-
gpg = gnupg.GPG()
444+
gpg = gnupg.GPG(gnupghome=config("GNUPG_DIR", default=None))
445445

446446
# import the public key
447447
import_result = gpg.import_keys(pub_key)

api/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from rest_framework.views import APIView
1717

1818
from api.logics import Logics
19+
from api.tasks import cache_market
1920
from api.models import (
2021
Currency,
2122
LNPayment,
@@ -146,6 +147,9 @@ def post(self, request):
146147
status.HTTP_400_BAD_REQUEST,
147148
)
148149

150+
if len(Currency.objects.all()) == 0:
151+
cache_market()
152+
149153
# Creates a new order
150154
order = Order(
151155
type=type,

requirements_dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
coverage==7.5.0
22
ruff==0.5.1
33
git+https://github.com/Reckless-Satoshi/drf-openapi-tester.git@soften-django-requirements
4-
pre-commit==3.7.0
4+
pre-commit==3.7.0
5+
python-dotenv[cli]==1.0.1

robosats/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
SESSION_COOKIE_HTTPONLY = False
6060

6161
# Logging settings
62-
if os.environ.get("LOG_TO_CONSOLE"):
62+
if config("LOG_TO_CONSOLE", cast=bool, default=False):
6363
LOGGING = {
6464
"version": 1,
6565
"disable_existing_loggers": False,
@@ -70,12 +70,12 @@
7070
},
7171
"root": {
7272
"handlers": ["console"],
73-
"level": "WARNING",
73+
"level": str(config("LOGGER_LEVEL", cast=str, default="WARNING")),
7474
},
7575
"loggers": {
7676
"api.utils": {
7777
"handlers": ["console"],
78-
"level": "WARNING",
78+
"level": str(config("LOGGER_LEVEL", cast=str, default="WARNING")),
7979
},
8080
},
8181
}

scripts/traditional/README.md

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# Robosats traditional environment
2+
3+
Robosats backend development and testing without docker and containers.
4+
5+
Binaries needed:
6+
* postgresql (`postgres`, `initdb`, `psql`)
7+
* redis (`redis-server`)
8+
* bitcoin (`bitcoind`, `bitcoin-cli`)
9+
* cln (`lightningd`, `lightning-cli`, `holdinvoice`)
10+
* lnd (`lnd`, `lncli`)
11+
12+
## Preparation
13+
14+
Postgresql and redis can be found in all linux distros and bsds.
15+
16+
Some distros do not put postgresql binaries in `PATH`, if this is the
17+
case simply link them somewhere in your `PATH`.
18+
19+
Example on debian:
20+
```
21+
ln -sf /usr/lib/postgresql/16/bin/postgres ~/.local/bin/
22+
ln -sf /usr/lib/postgresql/16/bin/initdb ~/.local/bin/
23+
ln -sf /usr/lib/postgresql/16/bin/psql ~/.local/bin/
24+
```
25+
26+
Bitcoin nodes if not already installed need to be manually downloaded.
27+
* bitcoin core binaries can be found here: https://bitcoincore.org/en/download
28+
* cln binaries can be found here: https://github.com/ElementsProject/lightning/releases
29+
* holdinvoice binary can be found here: https://github.com/daywalker90/holdinvoice/releases
30+
* lnd binaries can be found here: https://github.com/lightningnetwork/lnd/releases
31+
32+
Example preparation:
33+
```
34+
$ python3 -m venv venv
35+
$ . venv/bin/activate
36+
$ pip install -r requirements_dev.txt
37+
$ pip install -r requirements.txt
38+
39+
$ mkdir regtest
40+
$ mkdir regtest/programs
41+
$ cd regtest/programs
42+
$ mkdir bitcoin cln lnd
43+
# download bitcoin, cln (and holdinvoice) and lnd binaries
44+
45+
# if you want to use roboauto
46+
# still in regtest/programs
47+
$ git clone https://github.com/jerryfletcher21/roboauto
48+
$ cd roboauto
49+
$ pip install -r requirements.txt
50+
$ pip install .
51+
```
52+
53+
## env file
54+
55+
```
56+
$ cp .env-sample .env
57+
```
58+
Edit `.env`, both robosats and regtest scripts will read from there.
59+
60+
Variables to change:
61+
```
62+
LNVENDOR = "CLN"
63+
# LNVENDOR = "LND"
64+
65+
LND_DIR = "regtest/nodes/lnd-coord/"
66+
MACAROON_PATH = "data/chain/bitcoin/regtest/admin.macaroon"
67+
CLN_DIR = "regtest/nodes/cln-coord/regtest/"
68+
BITCOIND_RPCPORT = "18443"
69+
POSTGRES_DB = "robosats"
70+
POSTGRES_USER = "robosats"
71+
POSTGRES_PASSWORD = "robosats"
72+
USE_TOR = False
73+
LOG_TO_CONSOLE = True
74+
LOGGER_LEVEL = "INFO"
75+
```
76+
77+
Variables to add:
78+
```
79+
DEVELOPMENT = True
80+
TESTING = True
81+
82+
LNVENDOR_ROBOT = "LND"
83+
# LNVENDOR_ROBOT = "CLN"
84+
85+
BITCOIND_BIN = "regtest/programs/bitcoin/bitcoin-27.1/bin/bitcoind"
86+
BITCOIN_CLI_BIN = "regtest/programs/bitcoin/bitcoin-27.1/bin/bitcoin-cli"
87+
LIGHTNINGD_BIN = "regtest/programs/cln/usr/bin/lightningd"
88+
LIGHTNING_CLI_BIN = "regtest/programs/cln/usr/bin/lightning-cli"
89+
HOLDINVOICE_PLUGIN_BIN = "regtest/programs/cln/holdinvoice"
90+
LND_BIN = "regtest/programs/lnd/lnd-linux-amd64-v0.18.1-beta/lnd"
91+
LNCLI_BIN = "regtest/programs/lnd/lnd-linux-amd64-v0.18.1-beta/lncli"
92+
ROBOAUTO_GIT_DIR = "regtest/programs/roboauto"
93+
94+
REGTEST_NODES_DIR = "regtest/nodes"
95+
REGTEST_SERVICES_DIR = "regtest/services"
96+
REGTEST_LOGS_DIR = "regtest/logs"
97+
GNUPG_DIR = "regtest/services/gnupg"
98+
99+
BITCOIND_TEST_RPCUSER = "robodev"
100+
BITCOIND_TEST_RPCPASSWORD = "robodev"
101+
CLN_TEST_COORD_LISTEN_PORT = 9785
102+
LND_TEST_COORD_LISTEN_PORT = 9885
103+
LND_TEST_COORD_MACAROON_PATH = "regtest/nodes/lnd-coord/data/chain/bitcoin/regtest/admin.macaroon"
104+
LND_TEST_ROBOT_MACAROON_PATH = "regtest/nodes/lnd-user/data/chain/bitcoin/regtest/admin.macaroon"
105+
LND_TEST_ROBOT_REST_PORT = 8180
106+
LND_TEST_COORD_REST_PORT = 8181
107+
```
108+
109+
Paths can be relative or absolute. Binaries should be paths, they are
110+
not resolved with `PATH`.
111+
112+
Roboauto can be disabled by not setting `ROBOAUTO_GIT_DIR` or setting it
113+
to `false`.
114+
115+
If some ports are already in use, change their value.
116+
117+
To check which port are already in use, `netstat -tulnp` with root
118+
privileges can be used.
119+
120+
For example if there is alread an instance of postgresql running on the
121+
default port, change `POSTGRES_PORT = "5433"`.
122+
123+
## Usage
124+
125+
For development and testing two scripts are provided:
126+
* `regtest-services` for non bitcoin related services
127+
* `regtest-nodes` for bitcoin and lightning nodes
128+
129+
`regtest-services` sets up the database and manages the services.
130+
131+
Everything is done locally, so no root privileges/service managers are
132+
needed.
133+
134+
`regtest-nodes` is a script that should be sourced, it sets up a regtest
135+
environment, with bitcoin core, cln, lnd and roboauto, connecting them
136+
and creating channels. It then exposes the functions `btc_reg`,
137+
`cln_coord`, `cln_user`, `lnd_coord`, `lnd_user`, `ra_reg` to interact
138+
with the nodes and roboauto.
139+
140+
If the script is sourced in a `bash` shell, it will also source
141+
completions for all the functions.
142+
143+
`regtest-nodes` can also be run without arguments to simply expose the
144+
functions to start and remove the nodes and to create the channels
145+
between them, without setting up a specific environment.
146+
147+
Setup:
148+
```
149+
# after having changed .env file
150+
151+
$ . venv/bin/activate
152+
153+
# generate cln and lnd grpc
154+
$ scripts/generate_grpc.sh
155+
156+
$ scripts/traditional/regtest-services postgres-setup
157+
158+
$ scripts/traditional/regtest-services postgres-database
159+
160+
# postgres-database will create the database specified by
161+
# POSTGRES_DB in .env, it can be run multiple times with
162+
# different values of POSTGRES_DB for different databases
163+
```
164+
165+
Testing:
166+
```
167+
# edit .env setting LNVENDOR to either "CLN" or "LND"
168+
# LNVENDOR_ROBOT while running tests should be set to "LND"
169+
170+
# in the main window
171+
$ . venv/bin/activate
172+
$ . scripts/traditional/regtest-nodes test
173+
174+
# in a secondary window
175+
$ . venv/bin/activate
176+
# can be stopped with Control-C
177+
$ scripts/traditional/regtest-services test
178+
179+
# back in the main window
180+
$ python3 manage.py test
181+
182+
# after having run the tests run
183+
$ robosats_regtest_stop_and_remove_all
184+
# to remove the nodes, they will be recreated when
185+
# running the tests again
186+
187+
# the tests should be run with a clean database so if you have already run
188+
# the server and want to keep the database, either use a different value
189+
# of POSTGRES_DB or use a different directory by moving
190+
# regtest/services/postgres somewhere and the moving it back when you want
191+
# to run the server again
192+
```
193+
194+
Development:
195+
```
196+
# edit .env setting LNVENDOR to either "CLN" or "LND"
197+
# and LNVENDOR_ROBOT to either "CLN" or "LND"
198+
199+
# in the main window
200+
$ . venv/bin/activate
201+
$ . scripts/traditional/regtest-nodes server
202+
203+
# in a secondary window
204+
$ . venv/bin/activate
205+
# can be stopped with Control-C
206+
$ scripts/traditional/regtest-services server
207+
208+
# to see the output of the django runserver command
209+
# in a third window
210+
$ tail -f regtest/logs/runserver
211+
212+
# if roboauto is active, use it to test the backend
213+
# back in the main window
214+
$ ra_reg --help
215+
...
216+
$ ra_reg create-order "$(ra_reg generate-robot --loc)" type=buy currency=btc min_amount=0.01 max_amount=0.02 payment_method="On-Chain BTC" premium=-1.5
217+
...
218+
$ ra_reg take-order $(ra_reg generate-robot --loc) order-id
219+
...
220+
$ ra_reg escrow-pay RobotName
221+
...
222+
```

0 commit comments

Comments
 (0)