Skip to content

Commit 3c191bb

Browse files
authored
check status of peers in kdf network (#25)
* require kdf connection string in the config file Signed-off-by: onur-ozkan <work@onurozkan.dev> * add {serializer, deserializer} wrappers for `RpcClient` Signed-off-by: onur-ozkan <work@onurozkan.dev> * implement expirable hashmap Signed-off-by: onur-ozkan <work@onurozkan.dev> * implement peer status check logic into the middleware Signed-off-by: onur-ozkan <work@onurozkan.dev> * check if KDF is available on app initialization Signed-off-by: onur-ozkan <work@onurozkan.dev> * update kdf rpc module Signed-off-by: onur-ozkan <work@onurozkan.dev> * allow dead-code for various `expirable_map` functions Signed-off-by: onur-ozkan <work@onurozkan.dev> * move `peer_connection_healthcheck` priority Signed-off-by: onur-ozkan <work@onurozkan.dev> * update execution flow docs Signed-off-by: onur-ozkan <work@onurozkan.dev> * update drawio document file Signed-off-by: onur-ozkan <work@onurozkan.dev> * Update README.md * keep `RpcSocketPayload` private Signed-off-by: onur-ozkan <work@onurozkan.dev> * update README Signed-off-by: onur-ozkan <work@onurozkan.dev> * Update README.md * extend configuration interface with `peer_healthcheck_caching_secs` Signed-off-by: onur-ozkan <work@onurozkan.dev> * update README Signed-off-by: onur-ozkan <work@onurozkan.dev> * sync the upstream changes Signed-off-by: onur-ozkan <work@onurozkan.dev> * update proxy_signature Signed-off-by: onur-ozkan <work@onurozkan.dev> * sync upstream expirable map impl Signed-off-by: onur-ozkan <work@onurozkan.dev> * exclude nightly pipeline from runner Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent e382d29 commit 3c191bb

File tree

13 files changed

+450
-100
lines changed

13 files changed

+450
-100
lines changed

.github/workflows/pipelines.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest]
17-
rust: [nightly, beta, stable]
17+
rust: [beta, stable]
1818
steps:
1919
- uses: actions/checkout@v2
2020

@@ -35,7 +35,7 @@ jobs:
3535
strategy:
3636
matrix:
3737
os: [ubuntu-latest]
38-
rust: [nightly, beta, stable]
38+
rust: [beta, stable]
3939
steps:
4040
- uses: actions/checkout@v2
4141

@@ -58,7 +58,7 @@ jobs:
5858
strategy:
5959
matrix:
6060
os: [ubuntu-latest]
61-
rust: [nightly, beta, stable]
61+
rust: [beta, stable]
6262
steps:
6363
- uses: actions/checkout@v2
6464

@@ -82,7 +82,7 @@ jobs:
8282
strategy:
8383
matrix:
8484
os: [ubuntu-latest, macos-latest]
85-
rust: [nightly, beta, stable]
85+
rust: [beta, stable]
8686
steps:
8787
- uses: actions/checkout@v2
8888

@@ -103,7 +103,7 @@ jobs:
103103
strategy:
104104
matrix:
105105
os: [ubuntu-latest, macos-latest]
106-
rust: [nightly, beta, stable]
106+
rust: [beta, stable]
107107
steps:
108108
- uses: actions/checkout@v2
109109

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ log = "0.4.17"
2020
once_cell = "1.12.0"
2121
url = { version = "2.2.2", features = ["serde"] }
2222
redis = { version = "0.21.5", default-features = false, features = ["tokio-comp"] }
23+
rustc-hash = "1.1.0"
2324
serde = "1.0.137"
2425
serde_json = { version = "1.0.81", features = ["preserve_order", "raw_value"] }
2526
sha3 = "0.9"
@@ -28,7 +29,7 @@ tokio = { version = "1.12.0", default-features = false, features = ["macros", "r
2829
tokio-tungstenite = { version = "0.20.0", features = ["native-tls"] }
2930
# From our sources
3031
libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.4", default-features = false, features = ["identify"] }
31-
proxy_signature = { git = "https://github.com/KomodoPlatform/komodo-defi-framework", rev = "9ebc006" }
32+
proxy_signature = { git = "https://github.com/KomodoPlatform/komodo-defi-framework", branch = "dev" }
3233

3334
[target.x86_64-unknown-linux-gnu.dependencies]
3435
jemallocator = "0.5.0"

README.md

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Komodo Defi Proxy
2+
3+
Decentralized P2P applications have some limitations by their nature and one of them is the use application/API keys. If an API key is used in the application, any user could retrieve it by simply debugging the app. Some of the blockchain services we use in [komodo-defi-framework](https://github.com/KomodoPlatform/komodo-defi-framework) are paid services and we want to prevent abuse, such as users copying the API key for personal use. To address this problem, we created this project, komodo-defi-proxy. It takes the request, handles the API key, forwards the request to the actual service, and returns the result without modifying the original request. This keeps our secret application keys secure and hidden from end users.
4+
15
### Dev Requirements
26

37
Creating rsa key pairs
@@ -16,12 +20,14 @@ Create the configuration file for app runtime.
1620
"pubkey_path": "/path_to_publick_key.pem",
1721
"privkey_path": "/path_to_private_key.pem",
1822
"redis_connection_string": "redis://localhost",
23+
"kdf_rpc_client": "http://127.0.0.1:7783",
24+
"kdf_rpc_password": "testpass",
1925
"token_expiration_time": 300,
2026
"proxy_routes": [
2127
{
2228
"inbound_route": "/dev",
2329
"outbound_route": "http://localhost:8000",
24-
"proxy_type": "quicknode",
30+
"proxy_type": "quicknode", # available types are: "quicknode", "moralis", "block_pi"
2531
"authorized": false,
2632
"allowed_rpc_methods": [
2733
"eth_blockNumber",
@@ -36,20 +42,18 @@ Create the configuration file for app runtime.
3642
"rp_15_min": 200,
3743
"rp_30_min": 350,
3844
"rp_60_min": 575
39-
}
45+
},
46+
"peer_healthcheck_caching_secs": 10
4047
}
4148
```
4249

4350
Expose configuration file's path as an environment variable in `AUTH_APP_CONFIG_PATH`.
4451

45-
***Important Note:*** The environment where the application will be deployed, the timezone MUST be as UTC. Also, make sure redis is version `6.*`
46-
47-
### Architecture (TODO: OUTDATED)
48-
49-
![arch2](https://github.com/KomodoPlatform/komodo-defi-proxy/assets/39852038/be7fe7ae-2f2a-4f68-afa8-ce4938c570a7)
52+
***Important Note:*** Make sure redis is version `7.*`
5053

54+
### Architecture
5155

52-
**Execution flow (TODO: OUTDATED):**
56+
![2024-09-09_14-09](https://github.com/user-attachments/assets/2775d73e-8003-4bfe-89e1-2c64da9e3004)
5357

5458
1) Client sends the request.
5559

@@ -58,29 +62,17 @@ Expose configuration file's path as an environment variable in `AUTH_APP_CONFIG_
5862
3) If the incoming request comes from the same network, step 4 will be by-passed.
5963

6064
4) Request Handling in the Middleware:
65+
- **Status Checker**:
66+
- **Blocked**: Return `403 Forbidden`.
67+
- **Allowed**: Process continues with the rate limiter.
68+
- **Trusted**: Bypass rate limiter and proof of funding.
6169

62-
**For Quicknode:**
63-
- **Status Checker**:
64-
- **Blocked**: Return `403 Forbidden` immediately.
65-
- **Allowed**: Process continues with the rate limiter.
66-
- **Trusted**: Bypass rate limiter and proof of funding.
70+
- **Peer Status Checker**:
71+
- The requesting peer must be active in the KDF network. Validate this by executing the `peer_connection_healthcheck` KDF RPC. If the peer is not connected to the network, return `401 Unauthorized`.
6772

68-
- **Rate Limiter**:
69-
- First, verify the signed message. If not valid, return `401 Unauthorized` immediately.
70-
- If valid, calculate the request count with the time interval specified in the application configuration. If the wallet address has sent too many requests than the expected amount, process continues with the proof of funding. If not, bypass the proof of funding.
71-
72-
- **Proof of Funding**:
73-
- Return `406 Not Acceptable` if the wallet has a 0 balance. Otherwise, assume the request is valid and process it as usual.
74-
75-
**For Moralis:**
76-
- **Status Checker**:
77-
- **Blocked**: Return `403 Forbidden` immediately.
78-
- **Allowed**: Process continues with the rate limiter.
79-
- **Trusted**: Bypass the rate limiter.
80-
81-
- **Rate Limiter**:
82-
- First, verify the signed message. If not valid, return `401 Unauthorized` immediately.
83-
- If valid, calculate the request count with the time interval specified in the application configuration. If the wallet address has sent too many requests, return an error `406 Not Acceptable` indicating that the wallet address must wait for some time before making more requests.
73+
- **Rate Limiter**:
74+
- First, verify the signed message. If not valid, return `401 Unauthorized`.
75+
- If valid, calculate the request count with the time interval specified in the application configuration. If the wallet address has sent too many requests than the expected amount, process continues with the proof of funding. If not, bypass the proof of funding.
8476

8577
5) Find target route by requested endpoint.
8678

@@ -102,7 +94,7 @@ curl -v --url "'$mm2_address'" -s --data '{
10294
"params": {
10395
"ticker": "ETH",
10496
"nodes": [
105-
{"url": "'$atomicdex_gui_auth_address'", "gui_auth": true }
97+
{"url": "'$atomicdex_gui_auth_address'", "komodo_proxy": true }
10698
],
10799
"swap_contract_address": "0x24ABE4c71FC658C91313b6552cd40cD808b3Ea80",
108100
"erc20_tokens_requests": [
@@ -140,4 +132,4 @@ If you want to test features locally, you can run Docker containers using Docker
140132
4. **Stop the Containers**:
141133
```sh
142134
docker compose down
143-
```
135+
```

assets/.config_test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"port": 6150,
33
"redis_connection_string": "redis://redis:6379",
4+
"kdf_rpc_client": "http://127.0.0.1:7783",
5+
"kdf_rpc_password": "testpass",
46
"pubkey_path": "/usr/src/komodo-defi-proxy/assets/.pubkey_test",
57
"privkey_path": "/usr/src/komodo-defi-proxy/assets/.privkey_test",
68
"token_expiration_time": 300,

0 commit comments

Comments
 (0)