|
1 | | -# Backend Architecture |
| 1 | +# CYB local backend(in-browser) |
| 2 | + |
| 3 | +Cyb plays singinficat role in cyber infrastructure. The app reconstruct self-sufficient backend+frontend pattern inside the browser. |
| 4 | +In big view app consist from 3 parts: |
2 | 5 |
|
3 | 6 | ```mermaid |
4 | 7 | graph TD; |
| 8 | + App["frontend\n(main thread)"]-.proxy.->Backend["backend\n(shared worker)"]; |
| 9 | + App-.proxy.->Db["graph db\n(shared worker)"]; |
| 10 | + Backend-.proxy.->Db; |
| 11 | + App<-.message\nchannels.->Backend; |
| 12 | +``` |
5 | 13 |
|
6 | | - subgraph frontend["frontend(main thread)"] |
7 | | - App["Frontend"]-->Hook["useBackend()"]; |
8 | | - Hook-->methods("startSync()\nloadIpfs()\n...\nisReady\nipfsError\n..."); |
9 | | - Hook-.broadcast channel\n(any worker).->reducer["redux(state)"] |
10 | | - Hook-.save history from app.->defferedDbApiFront[/"DefferedDbApi(proxy)"/] |
11 | | - Hook--osenseApi["senseApi"]; |
12 | | - Hook--oipfsApiFront[/"ipfsApi(proxy)"/]; |
13 | | - senseApi--odbApi[/"dbApi(proxy)"/]; |
14 | | - end |
| 14 | +To reduce overload of main thread we have created 2 separate shared workers, where all the stuff is hosted. Bi-interraction between all layers occurs using proxy(comlink abstraction) or directly using broadcast channels. |
15 | 15 |
|
16 | | - dbApi<-.message channel.->dbWorker["dbApi"]; |
17 | | - subgraph dbWorkerGraph["cyb~db(worker)"] |
18 | | - dbWorker<-.bindings(webApi).->cozodb{{"CozoDb(wasm)"}} |
19 | | - end |
| 16 | +## Db layer |
| 17 | + |
| 18 | +Db worker is pretty simple it it's host only local relational-graph-vector database - [[cozo]]. It's represented with DbApi in frontend and backend layers. |
| 19 | +Cozo provide bazing fast access to brain and ipfs data in relational form and also in vector format, processing by [ml]embedder. |
20 | 20 |
|
21 | | - defferedDbApiFront-.->defferedDbApi; |
22 | | - ipfsApiFront<-.->ipfsApi; |
23 | | - subgraph backgroundWorker["cyb~backend(worker)"] |
24 | | - subgraph sync["sync service"] |
25 | | - ipfsNode["ipfs node"]; |
26 | | - links; |
27 | | - transactions; |
| 21 | +```mermaid |
| 22 | +graph TD; |
| 23 | + dbApi["dbApi"]--odb_meta_orm; |
| 24 | + subgraph rune["cozo db"] |
| 25 | + db_meta_orm[["meta orm"]]-.->db; |
28 | 26 | end |
29 | | - sync--oparticleResolver[["Particle resolver"]] |
30 | | - particleResolver--oqueue; |
31 | | - particleResolver--odbProxyWorker; |
32 | | - sync--oipfsApi; |
33 | | - sync--odbProxyWorker[/"dbApi(proxy)"/]; |
34 | | - defferedDbApi[["defferedDbApi"]]-->dbProxyWorker; |
35 | | - queue-->defferedDbApi; |
36 | | - ipfsApi--oqueue[["queue"]]; |
37 | | - ipfsApi--onode["node"]; |
38 | | - queue--balancer-->node; |
39 | | - node--embedded-->helia; |
40 | | - node--rpc-->kubo; |
41 | | - node--embedded-->js-ipfs; |
| 27 | +``` |
| 28 | + |
| 29 | +### Db entities |
| 30 | + |
| 31 | +- brain: |
| 32 | + - particles |
| 33 | + - embeddings |
| 34 | + - links |
| 35 | + - transactions |
| 36 | + - community |
| 37 | +- sense: |
| 38 | + |
| 39 | + - sync items + update status |
| 40 | + |
| 41 | +- system: |
| 42 | + - config |
| 43 | + - queue messages |
| 44 | + |
| 45 | +## Backend layer |
| 46 | + |
| 47 | +Backend worker is more complicated it contains significant elements of cyb architecture: |
| 48 | + |
| 49 | +```mermaid |
| 50 | +graph TD; |
| 51 | + subgraph Backend["backend(shared worker)"] |
| 52 | +
|
42 | 53 | subgraph ipfs["ipfs implementations"] |
43 | 54 | helia; |
44 | 55 | kubo; |
45 | 56 | js-ipfs; |
46 | 57 | end |
47 | 58 |
|
48 | | - dbProxyWorker<-.message channel.->dbWorker |
| 59 | + subgraph queues["message brokers"] |
| 60 | + ipfs_queue["ipfs load balancer"]; |
| 61 | + queue["data processing queue aka bus"]; |
| 62 | + end |
| 63 | +
|
| 64 | + subgraph rune["rune"] |
| 65 | + vm["virtual machine"]--ovm_bingen{{"cyb bindings"}}; |
| 66 | + end |
| 67 | +
|
| 68 | + subgraph sense["sense"] |
| 69 | + link_sync["link sync"]; |
| 70 | + msg_sync["message sync"]; |
| 71 | + swarm_sync["swarm sync"]; |
| 72 | + end |
| 73 | +
|
| 74 | + subgraph ml["ML transformers"] |
| 75 | + feature_extractor["embedder"]; |
| 76 | + end |
| 77 | +
|
| 78 | + end |
| 79 | +``` |
| 80 | + |
| 81 | +### Ipfs module |
| 82 | + |
| 83 | +Represented with IpfsApi at frontend layer, but also have direct access for some edge cases |
| 84 | + |
| 85 | +- Uses module that encapsulate different Ipfs implementations(kubo, helia, js-ipfs(obsolete)) |
| 86 | + - cache content(local storage & cozo) |
| 87 | + - preserve redundancy |
| 88 | +- Ipfs queue, process all requests to ipfs, prioritize, cancel non-actual requests and organize content pipeline |
| 89 | + - responsible for: |
| 90 | + - ipfs load balancing(limit of requests) |
| 91 | + - request prioritizing(actual requests first) |
| 92 | + - fault processing(switch fetch policy) |
| 93 | + - post processing(**inline rune vm** into pipeline) |
| 94 | + |
| 95 | +```mermaid |
| 96 | +graph LR |
| 97 | +user(ipfsApi\nenqueue particle) --> q[["queue\n(balancer)"]] --> node[/"ipfs"/] -- found --> rune[rune vm] -- mutation | content --> cache["cache"] --> app(app\ncontent) |
| 98 | +node -. not found\n(retry | error) .-> q |
| 99 | +``` |
| 100 | + |
| 101 | +## Bus |
| 102 | + |
| 103 | +Represented with some helpers and used for cases when blaancer is needed, some services not initialized yet(deffered actions), or long calculations is requered(ml inference, ipfs requests): |
| 104 | + |
| 105 | +- particle, request ipfs, save; calc embedding |
| 106 | +- link, deffered save |
| 107 | +- message persistence is protected by db store |
| 108 | + |
| 109 | +```mermaid |
| 110 | +graph TD; |
| 111 | + sender{{"enqueue(...)"}} -.message bus.-> bus |
| 112 | + subgraph task["task manager"] |
| 113 | + bus[["queue listener"]]; |
| 114 | +
|
| 115 | + bus-.task.->db("store\ndata")--odb1["dbApi"]; |
| 116 | + bus-.task.->ml("calculate\nembedding")--oml1["mlApi"]; |
| 117 | + bus-.task.->ipfs("request ipfs\nlow-priority")--oi["ipfsApi"] |
| 118 | + end |
| 119 | +``` |
| 120 | + |
| 121 | +## Sense |
| 122 | + |
| 123 | +Represented by SenseApi + subscription to broadcast channel at fronted layer. Provide continious update of cyberlinks related to my brain and my swarm, recieving on chain messages etc.: |
| 124 | + |
| 125 | +- Particles service (pooling) |
| 126 | +- Transactions service (pooling + websocket) |
| 127 | +- My friends service (pooling) |
| 128 | +- Ipfs service(pooling) |
| 129 | + |
| 130 | +All data and update status is stored into db, when some new data is recieved that triggers notification for frontendю |
| 131 | + |
| 132 | +```mermaid |
| 133 | +graph TD; |
| 134 | + db[["dbApi"]]; |
| 135 | + bus[["particle queue"]]; |
| 136 | +
|
| 137 | + subgraph sense["sync service"] |
| 138 | + notification("notification service") |
| 139 | +
|
| 140 | + particles[["particle service"]]--onotification; |
| 141 | + transactions[["transaction service"]]--onotification; |
| 142 | + myfriend[["my friends service"]]--onotification; |
| 143 | +
|
| 144 | + particles -.loop.-> particles; |
| 145 | + transactions -.loop.-> transactions; |
| 146 | + myfriend -.loop.-> myfriend; |
| 147 | + end |
| 148 | +
|
| 149 | +
|
| 150 | + subgraph blockchain["blockchain"] |
| 151 | + lcd[["lcd"]] |
| 152 | + websockets("websockets") |
| 153 | + indexer[["indexer"]] |
| 154 | + end |
| 155 | +
|
| 156 | + subgraph app["frontend"] |
| 157 | + redux["redux"] |
| 158 | + sender{{"senseApi"}}; |
| 159 | + end |
| 160 | +
|
| 161 | + notification -.message.-> redux; |
| 162 | + sender -.proxy.-> db; |
| 163 | + sense -.proxy.-> db; |
| 164 | + sense -.message.-> bus; |
| 165 | + bus -.proxy.-> db; |
| 166 | +
|
| 167 | + sense <-.request\nsubscriptin.->blockchain; |
| 168 | +
|
| 169 | +``` |
| 170 | + |
| 171 | +## Rune |
| 172 | + |
| 173 | +Rune VM execution is pipelined thru special abstraction called entrypoints. VM have bindings to all app parts: DB, transformers, signer, blockchain api, ipfs and also includes context of the entrypoint.(see. [[scripting]] for detailed description). |
| 174 | + |
| 175 | +## ML transformers |
| 176 | + |
| 177 | +Represented my mlApi. Uses inference from local ML models hosted inside browser. |
| 178 | + |
| 179 | +- future extractor. BERT-like model to trnsform text-to-embeddings. |
| 180 | + |
| 181 | +```mermaid |
| 182 | +graph TD; |
| 183 | + subgraph ml["transformers"] |
| 184 | + embedder["embedder"]; |
| 185 | + end |
| 186 | +
|
| 187 | + subgraph dbApi["dbApi"] |
| 188 | + db[["DB"]]; |
49 | 189 | end |
| 190 | + mlApi["mlApi"]; |
50 | 191 |
|
| 192 | + mlApi--odb; |
| 193 | + mlApi--oembedder; |
51 | 194 | ``` |
0 commit comments