Compare commits

..

11 Commits

Author SHA1 Message Date
cc027d2cc1
pass message from daemon to CLI 2025-03-06 17:22:32 +02:00
64b65d7ecd
don't delete VMs if node is offline for < 1day 2025-02-28 00:37:34 +02:00
02be48fd96
add support for operators 2025-02-17 17:51:34 +02:00
5c213f2eb4
small refactoring on var names and impls 2025-02-12 02:12:56 +02:00
df805ea291
add admin key 2025-02-11 21:03:05 +05:30
c98db7f8c3
rename structs so that they say "VM" 2025-02-09 00:14:32 +02:00
64f892c174
added admin functionality 2025-02-06 15:16:00 +02:00
5359ba039b
added auth 2025-02-04 03:09:53 +02:00
7dfdf4844e
switch language from tokens to LP 2025-01-28 18:31:30 +02:00
928c68f550
reduce the price of memory 2025-01-28 15:32:33 +02:00
9fa62a1978
inform daemon about VMs deleted by cron 2025-01-27 17:45:28 +02:00
7 changed files with 745 additions and 866 deletions

263
Cargo.lock generated

@ -82,12 +82,11 @@ dependencies = [
[[package]] [[package]]
name = "anstyle-wincon" name = "anstyle-wincon"
version = "3.0.7" version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"once_cell",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
@ -121,9 +120,9 @@ dependencies = [
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.86" version = "0.1.83"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "644dd749086bf3771a2fbc5f256fdb982d53f011c7d5d560304eafeecebce79d" checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -218,9 +217,9 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.8.0" version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
@ -238,7 +237,6 @@ dependencies = [
"bs58", "bs58",
"chrono", "chrono",
"dashmap", "dashmap",
"detee-shared",
"ed25519-dalek", "ed25519-dalek",
"env_logger", "env_logger",
"log", "log",
@ -246,7 +244,7 @@ dependencies = [
"prost-types", "prost-types",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_yaml",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
@ -266,9 +264,9 @@ dependencies = [
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.17.0" version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -278,15 +276,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.10.0" version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.13" version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@ -307,6 +305,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"wasm-bindgen", "wasm-bindgen",
"windows-targets", "windows-targets",
] ]
@ -403,6 +402,7 @@ dependencies = [
"lock_api", "lock_api",
"once_cell", "once_cell",
"parking_lot_core", "parking_lot_core",
"serde",
] ]
[[package]] [[package]]
@ -415,20 +415,6 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "detee-shared"
version = "0.1.0"
source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared?branch=stable_01#b8f37dec1845d29ea0b69035712e6ebb214376f4"
dependencies = [
"base64",
"prost",
"serde",
"serde_yaml",
"thiserror",
"tonic",
"tonic-build",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -633,19 +619,7 @@ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"wasi 0.11.0+wasi-snapshot-preview1", "wasi",
]
[[package]]
name = "getrandom"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
dependencies = [
"cfg-if",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets",
] ]
[[package]] [[package]]
@ -666,7 +640,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"http", "http",
"indexmap 2.7.1", "indexmap 2.7.0",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -733,9 +707,9 @@ dependencies = [
[[package]] [[package]]
name = "httparse" name = "httparse"
version = "1.10.0" version = "1.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a" checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946"
[[package]] [[package]]
name = "httpdate" name = "httpdate"
@ -751,9 +725,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "1.6.0" version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc2b571658e38e0c01b1fdca3bbbe93c00d3d71693ff2770043f8c29bc7d6f80" checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
@ -1009,9 +983,9 @@ dependencies = [
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.7.1" version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.15.2", "hashbrown 0.15.2",
@ -1019,9 +993,9 @@ dependencies = [
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.11.0" version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
@ -1046,9 +1020,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.77" version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"wasm-bindgen", "wasm-bindgen",
@ -1056,15 +1030,15 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.169" version = "0.2.168"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.15" version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]] [[package]]
name = "litemap" name = "litemap"
@ -1084,9 +1058,9 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.25" version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]] [[package]]
name = "matchit" name = "matchit"
@ -1108,9 +1082,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.8.3" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924" checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
dependencies = [ dependencies = [
"adler2", "adler2",
] ]
@ -1122,7 +1096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [ dependencies = [
"libc", "libc",
"wasi 0.11.0+wasi-snapshot-preview1", "wasi",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@ -1134,9 +1108,9 @@ checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03"
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.13" version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dab59f8e050d5df8e4dd87d9206fb6f65a483e20ac9fda365ade4fab353196c" checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
@ -1160,24 +1134,24 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.7" version = "0.36.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.3" version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.10.70" version = "0.10.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61cfb4e166a8bb8c9b55c500bc2308550148ece889be90f609377e58140f42c6" checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cfg-if", "cfg-if",
@ -1201,15 +1175,15 @@ dependencies = [
[[package]] [[package]]
name = "openssl-probe" name = "openssl-probe"
version = "0.1.6" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]] [[package]]
name = "openssl-sys" name = "openssl-sys"
version = "0.9.105" version = "0.9.104"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b22d5b84be05a8d6947c7cb71f7c849aa0f112acd4bf51c2a7c1c988ac0a9dc" checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741"
dependencies = [ dependencies = [
"cc", "cc",
"libc", "libc",
@ -1243,23 +1217,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [ dependencies = [
"fixedbitset", "fixedbitset",
"indexmap 2.7.1", "indexmap 2.7.0",
] ]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.1.9" version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfe2e71e1471fe07709406bf725f710b02927c9c54b2b5b2ec0e8087d97c327d" checksum = "be57f64e946e500c8ee36ef6331845d40a93055567ec57e8fae13efd33759b95"
dependencies = [ dependencies = [
"pin-project-internal", "pin-project-internal",
] ]
[[package]] [[package]]
name = "pin-project-internal" name = "pin-project-internal"
version = "1.1.9" version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6e859e6e5bd50440ab63c47e3ebabc90f26251f7c73c3d3e837b74a1cc3fa67" checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1268,9 +1242,9 @@ dependencies = [
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.16" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff"
[[package]] [[package]]
name = "pin-utils" name = "pin-utils"
@ -1305,9 +1279,9 @@ dependencies = [
[[package]] [[package]]
name = "prettyplease" name = "prettyplease"
version = "0.2.29" version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"syn", "syn",
@ -1315,9 +1289,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.93" version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -1376,9 +1350,9 @@ dependencies = [
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.38" version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -1410,7 +1384,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [ dependencies = [
"getrandom 0.2.15", "getrandom",
] ]
[[package]] [[package]]
@ -1453,9 +1427,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.12.12" version = "0.12.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43e734407157c3c2034e0258f5e4473ddb361b1e85f95a66690d67264d7cd1da" checksum = "3d3536321cfc54baa8cf3e273d5e1f63f889067829c4b410fcdbac8ca7b80994"
dependencies = [ dependencies = [
"base64", "base64",
"bytes", "bytes",
@ -1503,7 +1477,7 @@ checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [ dependencies = [
"cc", "cc",
"cfg-if", "cfg-if",
"getrandom 0.2.15", "getrandom",
"libc", "libc",
"spin", "spin",
"untrusted", "untrusted",
@ -1527,9 +1501,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.44" version = "0.38.42"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@ -1540,9 +1514,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.23.22" version = "0.23.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb9263ab4eb695e42321db096e3b8fbd715a59b154d5c88d82db2175b681ba7" checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"rustls-pki-types", "rustls-pki-types",
@ -1562,9 +1536,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls-pki-types" name = "rustls-pki-types"
version = "1.11.0" version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c" checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37"
[[package]] [[package]]
name = "rustls-webpki" name = "rustls-webpki"
@ -1579,15 +1553,15 @@ dependencies = [
[[package]] [[package]]
name = "rustversion" name = "rustversion"
version = "1.0.19" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.19" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]] [[package]]
name = "schannel" name = "schannel"
@ -1619,9 +1593,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework-sys" name = "security-framework-sys"
version = "2.14.0" version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32" checksum = "1863fd3768cd83c56a7f60faa4dc0d403f1b6df0a38c3c25f44b7894e45370d5"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -1655,9 +1629,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.138" version = "1.0.134"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
@ -1683,7 +1657,7 @@ version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [ dependencies = [
"indexmap 2.7.1", "indexmap 2.7.0",
"itoa", "itoa",
"ryu", "ryu",
"serde", "serde",
@ -1771,9 +1745,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.98" version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1823,13 +1797,12 @@ dependencies = [
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.16.0" version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
"getrandom 0.3.1",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.59.0", "windows-sys 0.59.0",
@ -1882,9 +1855,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.43.0" version = "1.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -1898,9 +1871,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "2.5.0" version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2087,9 +2060,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.16" version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]] [[package]]
name = "unsafe-libyaml" name = "unsafe-libyaml"
@ -2134,11 +2107,11 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.13.1" version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0" checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
dependencies = [ dependencies = [
"getrandom 0.3.1", "getrandom",
] ]
[[package]] [[package]]
@ -2168,32 +2141,22 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.100" version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"once_cell", "once_cell",
"rustversion",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.100" version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"log", "log",
@ -2205,9 +2168,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-futures" name = "wasm-bindgen-futures"
version = "0.4.50" version = "0.4.49"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys", "js-sys",
@ -2218,9 +2181,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.100" version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@ -2228,9 +2191,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.100" version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2241,18 +2204,15 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.100" version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
dependencies = [
"unicode-ident",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.77" version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
@ -2379,15 +2339,6 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "write16" name = "write16"
version = "1.0.0" version = "1.0.0"

@ -5,24 +5,21 @@ edition = "2021"
[dependencies] [dependencies]
bs58 = "0.5.1" bs58 = "0.5.1"
chrono = "0.4.39" chrono = { version = "0.4.39", features = ["serde"] }
dashmap = "6.1.0" dashmap = { version = "6.1.0", features = ["serde"] }
ed25519-dalek = "2.1.1" ed25519-dalek = "2.1.1"
env_logger = "0.11.6" env_logger = "0.11.6"
log = "0.4.22" log = "0.4.22"
prost = "0.13.4" prost = "0.13.4"
prost-types = "0.13.4" prost-types = "0.13.4"
reqwest = "0.12.10" reqwest = "0.12.10"
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134" serde_yaml = "0.9.34"
thiserror = "2.0.11" thiserror = "2.0.11"
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.17" tokio-stream = "0.1.17"
tonic = "0.12" tonic = "0.12"
uuid = { version = "1.11.0", features = ["v4"] } uuid = { version = "1.11.0", features = ["v4"] }
detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared", branch = "stable_01" }
# detee-shared = { path = "../detee-shared" }
[build-dependencies] [build-dependencies]
tonic-build = "0.12" tonic-build = "0.12"

@ -1,3 +0,0 @@
# Brain mock
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519

@ -1,39 +1,57 @@
use crate::grpc::snp_proto::{self as grpc}; use crate::grpc::snp_proto::{self as grpc};
use chrono::Utc; use chrono::Utc;
use dashmap::DashMap; use dashmap::DashMap;
use detee_shared::sgx::pb::brain::DelAppReq;
use log::{debug, info, warn}; use log::{debug, info, warn};
use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use std::sync::RwLock; use std::sync::RwLock;
use std::{
collections::{HashMap, HashSet},
fs::File,
io::Write,
};
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::Sender as OneshotSender; use tokio::sync::oneshot::Sender as OneshotSender;
use detee_shared::sgx::pb::brain::AppContract as AppContractPB; const DATA_PATH: &str = "/etc/detee/brain-mock/saved_data.yaml";
use detee_shared::sgx::pb::brain::AppNodeResources;
use detee_shared::sgx::pb::brain::AppResource as AppResourcePB;
use detee_shared::sgx::pb::brain::BrainMessageApp;
use detee_shared::sgx::pb::brain::MappedPort;
use detee_shared::sgx::pb::brain::NewAppReq;
use detee_shared::sgx::pb::brain::NewAppRes;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("We do not allow locking of more than 100000 LP.")] #[error("We do not allow locking of more than 100000 LP.")]
TxTooBig, TxTooBig,
#[error("Escrow must be at least 5000 LP.")]
MinimalEscrow,
#[error("Account has insufficient funds for this operation")] #[error("Account has insufficient funds for this operation")]
InsufficientFunds, InsufficientFunds,
#[error("Could not find contract {0}")] #[error("Could not find contract {0}")]
VmContractNotFound(String), VmContractNotFound(String),
#[error("This error should never happen.")]
ImpossibleError,
#[error("You don't have the required permissions for this operation.")]
AccessDenied,
} }
#[derive(Clone)] #[derive(Clone, Default, Serialize, Deserialize)]
pub struct AccountNanoLP { pub struct AccountData {
pub balance: u64, pub balance: u64,
pub tmp_locked: u64, pub tmp_locked: u64,
// holds reasons why VMs of this account got kicked
pub kicked_for: Vec<String>,
pub last_kick: chrono::DateTime<Utc>,
// holds accounts that banned this account
pub banned_by: HashSet<String>,
} }
impl From<AccountNanoLP> for grpc::AccountBalance { #[derive(Clone, Default, Serialize, Deserialize)]
fn from(value: AccountNanoLP) -> Self { pub struct OperatorData {
pub escrow: u64,
pub email: String,
pub banned_users: HashSet<String>,
pub vm_nodes: HashSet<String>,
}
impl From<AccountData> for grpc::AccountBalance {
fn from(value: AccountData) -> Self {
grpc::AccountBalance { grpc::AccountBalance {
balance: value.balance, balance: value.balance,
tmp_locked: value.tmp_locked, tmp_locked: value.tmp_locked,
@ -41,10 +59,10 @@ impl From<AccountNanoLP> for grpc::AccountBalance {
} }
} }
#[derive(Eq, Hash, PartialEq, Clone, Debug, Default)] #[derive(Eq, PartialEq, Clone, Debug, Default, Serialize, Deserialize)]
pub struct VmNode { pub struct VmNode {
pub public_key: String, pub public_key: String,
pub owner_key: String, pub operator_wallet: String,
pub country: String, pub country: String,
pub region: String, pub region: String,
pub city: String, pub city: String,
@ -58,24 +76,27 @@ pub struct VmNode {
pub max_ports_per_vm: u32, pub max_ports_per_vm: u32,
// nanoLP per unit per minute // nanoLP per unit per minute
pub price: u64, pub price: u64,
// 1st String is user wallet and 2nd String is report message
pub reports: HashMap<String, String>,
pub offline_minutes: u64,
} }
impl Into<grpc::VmNodeListResp> for VmNode { impl Into<grpc::VmNodeListResp> for VmNode {
fn into(self) -> grpc::VmNodeListResp { fn into(self) -> grpc::VmNodeListResp {
grpc::VmNodeListResp { grpc::VmNodeListResp {
operator: self.operator_wallet,
node_pubkey: self.public_key, node_pubkey: self.public_key,
country: self.country, country: self.country,
region: self.region, region: self.region,
city: self.city, city: self.city,
ip: self.ip, ip: self.ip,
server_rating: 0,
provider_rating: 0,
price: self.price, price: self.price,
reports: self.reports.into_values().collect(),
} }
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VmContract { pub struct VmContract {
pub uuid: String, pub uuid: String,
pub hostname: String, pub hostname: String,
@ -91,14 +112,15 @@ pub struct VmContract {
pub dtrfs_sha: String, pub dtrfs_sha: String,
pub created_at: chrono::DateTime<Utc>, pub created_at: chrono::DateTime<Utc>,
pub updated_at: chrono::DateTime<Utc>, pub updated_at: chrono::DateTime<Utc>,
// price per unit per minute
// recommended value is 20000 // recommended value is 20000
/// price per unit per minute
pub price_per_unit: u64, pub price_per_unit: u64,
pub locked_nano: u64, pub locked_nano: u64,
pub collected_at: chrono::DateTime<Utc>, pub collected_at: chrono::DateTime<Utc>,
} }
impl VmContract { impl VmContract {
/// total hardware units of this VM
fn total_units(&self) -> u64 { fn total_units(&self) -> u64 {
// TODO: Optimize this based on price of hardware. // TODO: Optimize this based on price of hardware.
// I tried, but this can be done better. // I tried, but this can be done better.
@ -109,7 +131,7 @@ impl VmContract {
+ (!self.public_ipv4.is_empty() as u64 * 10) + (!self.public_ipv4.is_empty() as u64 * 10)
} }
// Returns price per minute in nanoLP /// Returns price per minute in nanoLP
fn price_per_minute(&self) -> u64 { fn price_per_minute(&self) -> u64 {
self.total_units() * self.price_per_unit self.total_units() * self.price_per_unit
} }
@ -140,137 +162,143 @@ impl Into<grpc::VmContract> for VmContract {
} }
} }
#[derive(Clone, Debug, Default)] #[derive(Default, Serialize, Deserialize)]
pub struct AppContract {
pub uuid: String,
pub package_url: String,
pub admin_pubkey: String,
pub node_pubkey: String,
pub mapped_ports: Vec<(u16, u16)>,
pub host_ipv4: String,
pub disk_size_mb: u32,
pub vcpus: u32,
pub memory_mb: u32,
pub created_at: chrono::DateTime<Utc>,
pub updated_at: chrono::DateTime<Utc>,
// price per unit per minute
// recommended value is 20000
pub price_per_unit: u64,
pub locked_nano: u64,
pub collected_at: chrono::DateTime<Utc>,
pub hratls_pubkey: String,
pub public_package_mr_enclave: Option<Vec<u8>>,
}
impl From<AppContract> for AppContractPB {
fn from(value: AppContract) -> Self {
let mapped_ports = value
.mapped_ports
.clone()
.into_iter()
.map(MappedPort::from)
.collect();
let resource = Some(AppResourcePB {
memory_mb: value.memory_mb,
disk_mb: value.disk_size_mb,
vcpu: value.vcpus,
ports: value.mapped_ports.iter().map(|p| p.1 as u32).collect(),
});
Self {
uuid: value.uuid,
package_url: value.package_url,
admin_pubkey: value.admin_pubkey,
node_pubkey: value.node_pubkey,
mapped_ports,
public_ipv4: value.host_ipv4,
resource,
created_at: value.created_at.to_rfc3339(),
updated_at: value.updated_at.to_rfc3339(),
// TODO: check while implementing pricing
nano_per_minute: value.price_per_unit,
locked_nano: value.locked_nano,
collected_at: value.collected_at.to_rfc3339(),
hratls_pubkey: value.hratls_pubkey,
public_package_mr_enclave: value.public_package_mr_enclave,
}
}
}
#[derive(Eq, Hash, PartialEq, Clone, Debug, Default)]
pub struct AppNode {
pub node_pubkey: String,
pub operator_pubkey: String,
pub country: String,
pub region: String,
pub city: String,
pub ip: String,
pub avail_mem_mb: u32,
pub avail_vcpus: u32,
pub avail_storage_mb: u32,
pub avail_no_of_port: u32,
pub max_ports_per_app: u32,
// nanotokens per unit per minute
pub price: u64,
}
#[derive(Default)]
pub struct BrainData { pub struct BrainData {
// amount of nanoLP in each account // amount of nanoLP in each account
accounts: DashMap<String, AccountNanoLP>, accounts: DashMap<String, AccountData>,
operators: DashMap<String, OperatorData>,
vm_nodes: RwLock<Vec<VmNode>>, vm_nodes: RwLock<Vec<VmNode>>,
vm_contracts: RwLock<Vec<VmContract>>, vm_contracts: RwLock<Vec<VmContract>>,
#[serde(skip_serializing, skip_deserializing)]
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>, tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
#[serde(skip_serializing, skip_deserializing)]
tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>, tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>,
#[serde(skip_serializing, skip_deserializing)]
daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>, daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>,
app_nodes: RwLock<Vec<AppNode>>,
app_daemon_tx: DashMap<String, Sender<BrainMessageApp>>,
tmp_new_container_reqs: DashMap<String, (NewAppReq, OneshotSender<NewAppRes>)>,
app_contracts: RwLock<Vec<AppContract>>,
} }
impl BrainData { impl BrainData {
pub fn save_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::create(DATA_PATH)?;
file.write_all(serde_yaml::to_string(self)?.as_bytes())?;
Ok(())
}
fn load_from_disk() -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(DATA_PATH)?;
let data: Self = serde_yaml::from_str(&content)?;
Ok(data)
}
pub fn new() -> Self { pub fn new() -> Self {
match Self::load_from_disk() {
Ok(data) => data,
Err(e) => {
warn!("Could not data {DATA_PATH} due to error: {e:?}");
info!("Creating new instance of brain.");
Self { Self {
accounts: DashMap::new(), accounts: DashMap::new(),
operators: DashMap::new(),
vm_nodes: RwLock::new(Vec::new()), vm_nodes: RwLock::new(Vec::new()),
vm_contracts: RwLock::new(Vec::new()), vm_contracts: RwLock::new(Vec::new()),
tmp_newvm_reqs: DashMap::new(), tmp_newvm_reqs: DashMap::new(),
tmp_updatevm_reqs: DashMap::new(), tmp_updatevm_reqs: DashMap::new(),
daemon_tx: DashMap::new(), daemon_tx: DashMap::new(),
}
app_daemon_tx: DashMap::new(), }
tmp_new_container_reqs: DashMap::new(),
app_contracts: RwLock::new(Vec::new()),
app_nodes: RwLock::new(Vec::new()),
} }
} }
pub fn get_balance(&self, account: &str) -> AccountNanoLP { pub fn get_balance(&self, account: &str) -> AccountData {
if let Some(account) = self.accounts.get(account) { if let Some(account) = self.accounts.get(account) {
return account.value().clone(); return account.value().clone();
} else { } else {
let balance = AccountNanoLP { let balance = AccountData {
balance: 0, balance: 0,
tmp_locked: 0, tmp_locked: 0,
kicked_for: Vec::new(),
banned_by: HashSet::new(),
last_kick: chrono::Utc::now(),
}; };
return balance; return balance;
} }
} }
pub fn give_airdrop(&self, account: &str, tokens: u64) { pub fn give_airdrop(&self, account: &str, tokens: u64) {
warn!("Airdropping {tokens} to {account}.");
self.add_nano_to_wallet(account, tokens.saturating_mul(1_000_000_000)); self.add_nano_to_wallet(account, tokens.saturating_mul(1_000_000_000));
} }
pub fn slash_account(&self, account: &str, tokens: u64) {
warn!("Slashing {tokens} from {account}.");
self.rm_nano_from_wallet(account, tokens.saturating_mul(1_000_000_000));
}
fn add_nano_to_wallet(&self, account: &str, nano_lp: u64) { fn add_nano_to_wallet(&self, account: &str, nano_lp: u64) {
log::debug!("Adding {nano_lp} nanoLP to {account}"); log::debug!("Adding {nano_lp} nanoLP to {account}");
self.accounts self.accounts
.entry(account.to_string()) .entry(account.to_string())
.and_modify(|d| d.balance += nano_lp) .and_modify(|d| d.balance += nano_lp)
.or_insert(AccountNanoLP { .or_insert(AccountData {
balance: nano_lp, balance: nano_lp,
tmp_locked: 0, ..Default::default()
});
}
fn rm_nano_from_wallet(&self, account: &str, nano_lp: u64) {
log::debug!("Slashing {nano_lp} nanoLP to {account}");
self.accounts.entry(account.to_string()).and_modify(|d| {
d.balance = d.balance.saturating_sub(nano_lp);
});
}
/// This is written to run every minute
pub async fn vm_nodes_cron(&self) {
log::debug!("Running vm nodes cron...");
let mut nodes = self.vm_nodes.write().unwrap();
let mut vm_contracts = self.vm_contracts.write().unwrap();
for node in nodes.iter_mut() {
if self.daemon_tx.contains_key(&node.public_key) {
node.offline_minutes = 0;
continue;
}
let mut operator = match self
.operators
.iter_mut()
.find(|o| o.vm_nodes.contains(&node.public_key))
{
Some(op) => op,
None => continue,
};
node.offline_minutes += 1;
// compensate contract admin if the node is offline more then 5 minutes
if node.offline_minutes > 5 {
for c in vm_contracts
.iter()
.filter(|c| c.node_pubkey == node.public_key)
{
let compensation = c.price_per_minute() * 10;
if compensation < operator.escrow {
operator.escrow -= compensation;
self.add_nano_to_wallet(&c.admin_pubkey, compensation);
}
}
}
}
// delete nodes that are offline more than 3 hours, and clean contracts
nodes.retain(|n| {
if n.offline_minutes > 1600 {
vm_contracts.retain_mut(|c| {
if c.node_pubkey == n.public_key {
self.add_nano_to_wallet(&c.admin_pubkey, c.locked_nano);
}
c.node_pubkey != n.public_key
});
for mut op in self.operators.iter_mut() {
op.vm_nodes.remove(&n.public_key);
}
}
n.offline_minutes <= 180
}); });
} }
@ -280,23 +308,33 @@ impl BrainData {
{ {
let mut contracts = self.vm_contracts.write().unwrap(); let mut contracts = self.vm_contracts.write().unwrap();
contracts.retain_mut(|c| { contracts.retain_mut(|c| {
let owner_key = self let node = self.find_node_by_pubkey(&c.node_pubkey).unwrap();
.find_nodes_by_pubkey(&c.node_pubkey) if node.offline_minutes == 0 {
.unwrap() let operator_wallet = node.operator_wallet.clone();
.owner_key
.clone();
let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64; let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64;
c.collected_at = Utc::now(); c.collected_at = Utc::now();
let mut nanolp_to_collect = c.price_per_minute().saturating_mul(minutes_to_collect); let mut nanolp_to_collect =
c.price_per_minute().saturating_mul(minutes_to_collect);
if nanolp_to_collect > c.locked_nano { if nanolp_to_collect > c.locked_nano {
nanolp_to_collect = c.locked_nano; nanolp_to_collect = c.locked_nano;
} }
log::debug!("Removing {nanolp_to_collect} nanoLP from {}", c.uuid); log::debug!("Removing {nanolp_to_collect} nanoLP from {}", c.uuid);
c.locked_nano -= nanolp_to_collect; c.locked_nano -= nanolp_to_collect;
self.add_nano_to_wallet(&owner_key, nanolp_to_collect); let escrow_multiplier = match self.operators.get(&operator_wallet) {
Some(op) if op.escrow > 5000 => match self.operators.get(&c.admin_pubkey) {
Some(user_is_op) if user_is_op.escrow > 5000 => 1,
_ => 5,
},
_ => 1,
};
self.add_nano_to_wallet(
&operator_wallet,
nanolp_to_collect * escrow_multiplier,
);
if c.locked_nano == 0 { if c.locked_nano == 0 {
deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone())); deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone()));
} }
}
c.locked_nano > 0 c.locked_nano > 0
}); });
} }
@ -315,8 +353,9 @@ impl BrainData {
} }
} }
pub fn insert_node(&self, node: VmNode) { pub fn register_node(&self, node: VmNode) {
info!("Registering node {node:?}"); info!("Registering node {node:?}");
self.add_vmnode_to_operator(&node.operator_wallet, &node.public_key);
let mut nodes = self.vm_nodes.write().unwrap(); let mut nodes = self.vm_nodes.write().unwrap();
for n in nodes.iter_mut() { for n in nodes.iter_mut() {
if n.public_key == node.public_key { if n.public_key == node.public_key {
@ -329,6 +368,101 @@ impl BrainData {
nodes.push(node); nodes.push(node);
} }
// todo: this should also support Apps
/// Receives: operator, contract uuid, reason of kick
pub async fn kick_contract(
&self,
operator: &str,
uuid: &str,
reason: &str,
) -> Result<u64, Error> {
log::debug!("Operator {operator} requested a kick of {uuid} for reason: {reason}");
let contract = self.find_contract_by_uuid(uuid)?;
let mut operator_data = self
.operators
.get_mut(operator)
.ok_or(Error::AccessDenied)?;
if !operator_data.vm_nodes.contains(&contract.node_pubkey) {
return Err(Error::AccessDenied);
}
let mut minutes_to_refund = chrono::Utc::now()
.signed_duration_since(contract.updated_at)
.num_minutes()
.abs() as u64;
// cap refund at 1 week
if minutes_to_refund > 10080 {
minutes_to_refund = 10080;
}
let mut refund_amount = minutes_to_refund * contract.price_per_minute();
let mut admin_account = self
.accounts
.get_mut(&contract.admin_pubkey)
.ok_or(Error::ImpossibleError)?;
// check if he got kicked within the last day
if !chrono::Utc::now()
.signed_duration_since(admin_account.last_kick)
.gt(&chrono::Duration::days(1))
{
refund_amount = 0;
}
if operator_data.escrow < refund_amount {
refund_amount = operator_data.escrow;
}
log::debug!(
"Removing {refund_amount} escrow from {} and giving it to {}",
operator_data.key(),
admin_account.key()
);
admin_account.balance += refund_amount;
admin_account.kicked_for.push(reason.to_string());
operator_data.escrow -= refund_amount;
let admin_pubkey = contract.admin_pubkey.clone();
drop(admin_account);
drop(contract);
self.delete_vm(grpc::DeleteVmReq {
uuid: uuid.to_string(),
admin_pubkey,
})
.await?;
Ok(refund_amount)
}
pub fn ban_user(&self, operator: &str, user: &str) {
self.accounts
.entry(user.to_string())
.and_modify(|a| {
a.banned_by.insert(operator.to_string());
})
.or_insert(AccountData {
banned_by: HashSet::from([operator.to_string()]),
..Default::default()
});
self.operators
.entry(operator.to_string())
.and_modify(|o| {
o.banned_users.insert(user.to_string());
})
.or_insert(OperatorData {
banned_users: HashSet::from([user.to_string()]),
..Default::default()
});
}
pub fn report_node(&self, admin_pubkey: String, node: &str, report: String) {
let mut nodes = self.vm_nodes.write().unwrap();
if let Some(node) = nodes.iter_mut().find(|n| n.public_key == node) {
node.reports.insert(admin_pubkey, report);
}
}
pub fn lock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> { pub fn lock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
if nano_lp > 100_000_000_000_000 { if nano_lp > 100_000_000_000_000 {
return Err(Error::TxTooBig); return Err(Error::TxTooBig);
@ -414,17 +548,11 @@ impl BrainData {
} }
pub async fn delete_vm(&self, delete_vm: grpc::DeleteVmReq) -> Result<(), Error> { pub async fn delete_vm(&self, delete_vm: grpc::DeleteVmReq) -> Result<(), Error> {
let contract = match self.find_contract_by_uuid(&delete_vm.uuid) { log::debug!("Starting deletion of VM {}", delete_vm.uuid);
Some(contract) => { let contract = self.find_contract_by_uuid(&delete_vm.uuid)?;
if contract.admin_pubkey != delete_vm.admin_pubkey { if contract.admin_pubkey != delete_vm.admin_pubkey {
return Err(Error::VmContractNotFound(delete_vm.uuid)); return Err(Error::AccessDenied);
} }
contract
}
None => {
return Err(Error::VmContractNotFound(delete_vm.uuid));
}
};
info!("Found vm {}. Deleting...", delete_vm.uuid); info!("Found vm {}. Deleting...", delete_vm.uuid);
if let Some(daemon_tx) = self.daemon_tx.get(&contract.node_pubkey) { if let Some(daemon_tx) = self.daemon_tx.get(&contract.node_pubkey) {
debug!( debug!(
@ -533,6 +661,11 @@ impl BrainData {
return; return;
} }
}; };
if let Err(e) = update_vm_req.1.send(update_vm_resp.clone()) {
log::warn!(
"CLI RX dropped before receiving UpdateVMResp {update_vm_resp:?}. Error: {e:?}"
);
}
if update_vm_resp.error != "" { if update_vm_resp.error != "" {
return; return;
} }
@ -569,11 +702,6 @@ impl BrainData {
update_vm_resp.error = "VM Contract not found.".to_string(); update_vm_resp.error = "VM Contract not found.".to_string();
} }
} }
if let Err(e) = update_vm_req.1.send(update_vm_resp.clone()) {
log::warn!(
"CLI RX dropped before receiving UpdateVMResp {update_vm_resp:?}. Error: {e:?}"
);
}
} }
pub async fn submit_newvm_req( pub async fn submit_newvm_req(
@ -633,7 +761,7 @@ impl BrainData {
let uuid = req.uuid.clone(); let uuid = req.uuid.clone();
info!("Inserting new vm update request in memory: {req:?}"); info!("Inserting new vm update request in memory: {req:?}");
let node_pubkey = match self.find_contract_by_uuid(&req.uuid) { let node_pubkey = match self.find_contract_by_uuid(&req.uuid) {
Some(contract) => { Ok(contract) => {
if contract.admin_pubkey != req.admin_pubkey { if contract.admin_pubkey != req.admin_pubkey {
let _ = tx.send(grpc::UpdateVmResp { let _ = tx.send(grpc::UpdateVmResp {
uuid, uuid,
@ -644,7 +772,7 @@ impl BrainData {
} }
contract.node_pubkey contract.node_pubkey
} }
None => { Err(_) => {
log::warn!( log::warn!(
"Received UpdateVMReq for a contract that does not exist: {}", "Received UpdateVMReq for a contract that does not exist: {}",
req.uuid req.uuid
@ -695,12 +823,81 @@ impl BrainData {
} }
} }
pub fn find_nodes_by_pubkey(&self, public_key: &str) -> Option<VmNode> { pub fn find_node_by_pubkey(&self, public_key: &str) -> Option<VmNode> {
let nodes = self.vm_nodes.read().unwrap(); let nodes = self.vm_nodes.read().unwrap();
nodes.iter().cloned().find(|n| n.public_key == public_key) nodes.iter().cloned().find(|n| n.public_key == public_key)
} }
pub fn find_nodes_by_filters( pub fn is_user_banned_by_node(&self, user_wallet: &str, node_pubkey: &str) -> bool {
if let Some(node) = self.find_node_by_pubkey(&node_pubkey) {
if let Some(account) = self.accounts.get(user_wallet) {
if account.banned_by.contains(&node.operator_wallet) {
return true;
}
}
}
false
}
pub fn add_vmnode_to_operator(&self, operator_wallet: &str, node_pubkey: &str) {
self.operators
.entry(operator_wallet.to_string())
.and_modify(|op| {
op.vm_nodes.insert(node_pubkey.to_string());
})
.or_insert(OperatorData {
escrow: 0,
email: String::new(),
banned_users: HashSet::new(),
vm_nodes: HashSet::from([node_pubkey.to_string()]),
});
}
pub fn register_operator(&self, req: grpc::RegOperatorReq) -> Result<(), Error> {
let mut operator = match self.operators.get(&req.pubkey) {
Some(o) => (*(o.value())).clone(),
None => OperatorData {
..Default::default()
},
};
if req.escrow < 5000 {
return Err(Error::MinimalEscrow);
}
let escrow = req.escrow * 1_000_000_000;
if let Some(mut account) = self.accounts.get_mut(&req.pubkey) {
if (account.balance + operator.escrow) < escrow {
return Err(Error::InsufficientFunds);
}
account.balance = account.balance + operator.escrow - escrow;
operator.escrow = escrow;
} else {
return Err(Error::InsufficientFunds);
}
operator.email = req.email;
self.operators.insert(req.pubkey, operator);
Ok(())
}
pub fn find_vm_nodes_by_operator(&self, operator_wallet: &str) -> Vec<VmNode> {
let nodes = self.vm_nodes.read().unwrap();
nodes
.iter()
.filter(|node| node.operator_wallet == operator_wallet)
.cloned()
.collect()
}
pub fn total_operator_reports(&self, operator_wallet: &str) -> usize {
let nodes = self.vm_nodes.read().unwrap();
nodes
.iter()
.cloned()
.filter(|n| n.operator_wallet == operator_wallet)
.map(|node| node.reports.len())
.sum()
}
pub fn find_vm_nodes_by_filters(
&self, &self,
filters: &crate::grpc::snp_proto::VmNodeFilters, filters: &crate::grpc::snp_proto::VmNodeFilters,
) -> Vec<VmNode> { ) -> Vec<VmNode> {
@ -747,9 +944,13 @@ impl BrainData {
.cloned() .cloned()
} }
pub fn find_contract_by_uuid(&self, uuid: &str) -> Option<VmContract> { pub fn find_contract_by_uuid(&self, uuid: &str) -> Result<VmContract, Error> {
let contracts = self.vm_contracts.read().unwrap(); let contracts = self.vm_contracts.read().unwrap();
contracts.iter().cloned().find(|c| c.uuid == uuid) contracts
.iter()
.cloned()
.find(|c| c.uuid == uuid)
.ok_or(Error::VmContractNotFound(uuid.to_string()))
} }
pub fn list_all_contracts(&self) -> Vec<VmContract> { pub fn list_all_contracts(&self) -> Vec<VmContract> {
@ -768,21 +969,72 @@ impl BrainData {
.collect() .collect()
} }
pub fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<VmContract> { pub fn list_operators(&self) -> Vec<grpc::ListOperatorsResp> {
debug!("Searching contracts for admin pubkey {admin_pubkey}"); self.operators
.iter()
.map(|op| grpc::ListOperatorsResp {
pubkey: op.key().to_string(),
escrow: op.escrow / 1_000_000_000,
email: op.email.clone(),
app_nodes: 0,
vm_nodes: op.vm_nodes.len() as u64,
reports: self.total_operator_reports(op.key()) as u64,
})
.collect()
}
pub fn inspect_operator(&self, wallet: &str) -> Option<grpc::InspectOperatorResp> {
self.operators.get(wallet).map(|op| {
let nodes = self
.find_vm_nodes_by_operator(wallet)
.into_iter()
.map(|n| n.into())
.collect();
grpc::InspectOperatorResp {
operator: Some(grpc::ListOperatorsResp {
pubkey: op.key().to_string(),
escrow: op.escrow,
email: op.email.clone(),
app_nodes: 0,
vm_nodes: op.vm_nodes.len() as u64,
reports: self.total_operator_reports(op.key()) as u64,
}),
nodes,
}
})
}
pub fn find_vm_contracts_by_operator(&self, wallet: &str) -> Vec<VmContract> {
debug!("Searching contracts for operator {wallet}");
let nodes = match self.operators.get(wallet) {
Some(op) => op.vm_nodes.clone(),
None => return Vec::new(),
};
let contracts: Vec<VmContract> = self let contracts: Vec<VmContract> = self
.vm_contracts .vm_contracts
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.filter(|c| c.admin_pubkey == admin_pubkey) .filter(|c| nodes.contains(&c.node_pubkey))
.cloned() .cloned()
.collect(); .collect();
debug!("Found {} contracts or {admin_pubkey}.", contracts.len());
contracts contracts
} }
pub fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<VmContract> { pub fn find_vm_contracts_by_admin(&self, admin_wallet: &str) -> Vec<VmContract> {
debug!("Searching contracts for admin pubkey {admin_wallet}");
let contracts: Vec<VmContract> = self
.vm_contracts
.read()
.unwrap()
.iter()
.filter(|c| c.admin_pubkey == admin_wallet)
.cloned()
.collect();
contracts
}
pub fn find_vm_contracts_by_node(&self, node_pubkey: &str) -> Vec<VmContract> {
let contracts = self.vm_contracts.read().unwrap(); let contracts = self.vm_contracts.read().unwrap();
contracts contracts
.iter() .iter()
@ -791,204 +1043,3 @@ impl BrainData {
.collect() .collect()
} }
} }
impl BrainData {
pub fn add_app_daemon_tx(&self, node_pubkey: &str, tx: Sender<BrainMessageApp>) {
self.app_daemon_tx.insert(node_pubkey.to_string(), tx);
}
pub fn del_app_daemon_tx(&self, node_pubkey: &str) {
self.app_daemon_tx.remove(node_pubkey);
}
pub fn insert_app_node(&self, node: AppNode) {
let mut nodes = self.app_nodes.write().unwrap();
for n in nodes.iter_mut() {
if n.node_pubkey == node.node_pubkey {
// TODO: figure what to do in this case.
warn!("Node {} already exists. Updating data.", n.node_pubkey);
*n = node;
return;
}
}
nodes.push(node);
}
pub fn find_app_contract_by_uuid(&self, uuid: &str) -> Option<AppContract> {
let contracts = self.app_contracts.read().unwrap();
contracts.iter().find(|c| c.uuid == uuid).cloned()
}
pub fn find_app_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<AppContract> {
debug!("Searching contracts for admin pubkey {admin_pubkey}");
let contracts: Vec<AppContract> = self
.app_contracts
.read()
.unwrap()
.iter()
.filter(|c| c.admin_pubkey == admin_pubkey)
.cloned()
.collect();
debug!("Found {} contracts or {admin_pubkey}.", contracts.len());
contracts
}
pub fn find_app_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<AppContract> {
let app_contracts = self.app_contracts.read().unwrap();
app_contracts
.iter()
.filter(|c| c.node_pubkey == node_pubkey)
.cloned()
.collect()
}
pub fn submit_app_node_resources(&self, node_resource: AppNodeResources) {
debug!("{:#?}", &node_resource);
let mut nodes = self.app_nodes.write().unwrap();
for n in nodes.iter_mut() {
if n.node_pubkey == node_resource.node_pubkey {
debug!(
"Found node {}. Updating resources to {:?}",
n.node_pubkey, node_resource
);
n.avail_vcpus = node_resource.avail_vcpus;
n.avail_mem_mb = node_resource.avail_memory_mb;
n.avail_storage_mb = node_resource.avail_storage_mb;
n.max_ports_per_app = node_resource.max_ports_per_app;
n.avail_no_of_port = node_resource.avail_no_of_port;
return;
}
}
debug!(
"VM Node {} not found when trying to update resources.",
node_resource.node_pubkey
);
debug!("VM Node list:\n{:?}", nodes);
}
pub async fn send_new_container_req(&self, mut req: NewAppReq, tx: OneshotSender<NewAppRes>) {
req.uuid = uuid::Uuid::new_v4().to_string();
info!("Inserting new container request in memory: {req:?}");
self.tmp_new_container_reqs
.insert(req.uuid.clone(), (req.clone(), tx));
if let Some(app_daemon_tx) = self.app_daemon_tx.get(&req.node_pubkey) {
debug!(
"Found daemon TX for {}. Sending newVMReq {}",
req.node_pubkey, req.uuid
);
let msg = BrainMessageApp {
msg: Some(
detee_shared::sgx::pb::brain::brain_message_app::Msg::NewAppReq(req.clone()),
),
};
if let Err(e) = app_daemon_tx.send(msg).await {
warn!(
"Failed to send new container request to {} due to error: {e:?}",
req.node_pubkey
);
info!("Deleting daemon TX for {}", req.node_pubkey);
self.del_app_daemon_tx(&req.node_pubkey);
self.send_new_container_resp(NewAppRes {
uuid: req.uuid,
status: "failed".to_string(),
error: "Daemon is offline.".to_string(),
..Default::default()
})
.await;
}
}
// TODO: implement daemon offline handling
}
pub async fn send_del_container_req(
&self,
req: DelAppReq,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(app_contract) = self.find_app_contract_by_uuid(&req.uuid) {
info!("Found app contract {}. Deleting...", &req.uuid);
if let Some(app_daemon_tx) = self.app_daemon_tx.get(&app_contract.node_pubkey) {
debug!(
"TX for daemon {} found. Informing daemon about deletion of {}.",
app_contract.node_pubkey, &req.uuid
);
let msg = BrainMessageApp {
msg: Some(
detee_shared::sgx::pb::brain::brain_message_app::Msg::DeleteAppReq(
req.clone(),
),
),
};
if let Err(e) = app_daemon_tx.send(msg).await {
warn!(
"Failed to send deletion request to {} due to error: {e:?}",
app_contract.node_pubkey
);
info!("Deleting daemon TX for {}", app_contract.node_pubkey);
self.del_app_daemon_tx(&app_contract.node_pubkey);
}
}
let mut app_contracts = self.app_contracts.write().unwrap();
app_contracts.retain(|c| c.uuid != req.uuid);
Ok(())
} else {
Err("Contract not found".into())
}
}
pub async fn send_new_container_resp(&self, new_container_resp: NewAppRes) {
let new_container_req = match self.tmp_new_container_reqs.remove(&new_container_resp.uuid) {
Some((_, r)) => r,
None => {
log::error!(
"Received confirmation for ghost new container req {}",
new_container_resp.uuid
);
return;
}
};
if let Err(err) = new_container_req.1.send(new_container_resp.clone()) {
log::error!(
"CLI RX for {} dropped before receiving confirmation {:?}.\n{:?}",
&new_container_req.0.admin_pubkey,
new_container_resp,
err
);
}
if new_container_resp.error != "" {
return;
}
let requested_resource = new_container_req.0.resource.clone().unwrap_or_default();
let app_contracts = AppContract {
uuid: new_container_req.0.uuid,
package_url: new_container_req.0.package_url,
admin_pubkey: new_container_req.0.admin_pubkey,
node_pubkey: new_container_req.0.node_pubkey.clone(),
mapped_ports: new_container_resp
.mapped_ports
.iter()
.map(|p| (p.host_port as u16, p.app_port as u16))
.collect::<Vec<(u16, u16)>>(),
host_ipv4: new_container_resp.ip_address,
disk_size_mb: requested_resource.disk_mb,
vcpus: requested_resource.vcpu,
memory_mb: requested_resource.memory_mb,
created_at: Utc::now(),
updated_at: Utc::now(),
price_per_unit: new_container_req.0.price_per_unit,
locked_nano: new_container_req.0.locked_nano,
collected_at: Utc::now(),
hratls_pubkey: new_container_req.0.hratls_pubkey,
public_package_mr_enclave: new_container_req.0.public_package_mr_enclave,
};
log::info!("Created new app contract: {app_contracts:?}");
self.app_contracts.write().unwrap().push(app_contracts);
}
}

@ -1,5 +1,3 @@
#![allow(dead_code)]
pub mod snp_proto { pub mod snp_proto {
tonic::include_proto!("vm_proto"); tonic::include_proto!("vm_proto");
} }
@ -16,15 +14,10 @@ use tokio::sync::mpsc;
use tokio_stream::{wrappers::ReceiverStream, Stream, StreamExt}; use tokio_stream::{wrappers::ReceiverStream, Stream, StreamExt};
use tonic::{Request, Response, Status, Streaming}; use tonic::{Request, Response, Status, Streaming};
use detee_shared::sgx::pb::brain::brain_app_cli_server::BrainAppCli;
use detee_shared::sgx::pb::brain::brain_app_daemon_server::BrainAppDaemon;
use detee_shared::sgx::pb::brain::{
AppContract, BrainMessageApp, DaemonMessageApp, DelAppReq, ListAppContractsReq, NewAppReq,
NewAppRes, RegisterAppNodeReq,
};
const ADMIN_ACCOUNTS: &[&str] = &[ const ADMIN_ACCOUNTS: &[&str] = &[
"x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK", "x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK",
"FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL", "FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL",
"H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc",
]; ];
pub struct BrainDaemonMock { pub struct BrainDaemonMock {
@ -47,26 +40,6 @@ impl BrainCliMock {
} }
} }
pub struct BrainAppCliMock {
data: Arc<BrainData>,
}
impl BrainAppCliMock {
pub fn new(data: Arc<BrainData>) -> Self {
Self { data }
}
}
pub struct BrainAppDaemonMock {
data: Arc<BrainData>,
}
impl BrainAppDaemonMock {
pub fn new(data: Arc<BrainData>) -> Self {
Self { data }
}
}
#[tonic::async_trait] #[tonic::async_trait]
impl BrainVmDaemon for BrainDaemonMock { impl BrainVmDaemon for BrainDaemonMock {
type RegisterVmNodeStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>; type RegisterVmNodeStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
@ -78,7 +51,7 @@ impl BrainVmDaemon for BrainDaemonMock {
info!("Starting registration process for {:?}", req); info!("Starting registration process for {:?}", req);
let node = crate::data::VmNode { let node = crate::data::VmNode {
public_key: req.node_pubkey.clone(), public_key: req.node_pubkey.clone(),
owner_key: req.owner_pubkey, operator_wallet: req.operator_wallet,
country: req.country, country: req.country,
region: req.region, region: req.region,
city: req.city, city: req.city,
@ -86,10 +59,10 @@ impl BrainVmDaemon for BrainDaemonMock {
price: req.price, price: req.price,
..Default::default() ..Default::default()
}; };
self.data.insert_node(node); self.data.register_node(node);
info!("Sending existing contracts to {}", req.node_pubkey); info!("Sending existing contracts to {}", req.node_pubkey);
let contracts = self.data.find_contracts_by_node_pubkey(&req.node_pubkey); let contracts = self.data.find_vm_contracts_by_node(&req.node_pubkey);
let (tx, rx) = mpsc::channel(6); let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move { tokio::spawn(async move {
for contract in contracts { for contract in contracts {
@ -187,6 +160,14 @@ impl BrainCli for BrainCliMock {
async fn new_vm(&self, req: Request<NewVmReq>) -> Result<Response<NewVmResp>, Status> { async fn new_vm(&self, req: Request<NewVmReq>) -> Result<Response<NewVmResp>, Status> {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("New VM requested via CLI: {req:?}"); info!("New VM requested via CLI: {req:?}");
if self
.data
.is_user_banned_by_node(&req.admin_pubkey, &req.node_pubkey)
{
return Err(Status::permission_denied(
"This operator banned you. What did you do?",
));
}
let admin_pubkey = req.admin_pubkey.clone(); let admin_pubkey = req.admin_pubkey.clone();
let (oneshot_tx, oneshot_rx) = tokio::sync::oneshot::channel(); let (oneshot_tx, oneshot_rx) = tokio::sync::oneshot::channel();
self.data.submit_newvm_req(req, oneshot_tx).await; self.data.submit_newvm_req(req, oneshot_tx).await;
@ -214,9 +195,9 @@ impl BrainCli for BrainCliMock {
info!("Sending UpdateVMResp: {response:?}"); info!("Sending UpdateVMResp: {response:?}");
Ok(Response::new(response)) Ok(Response::new(response))
} }
Err(_) => Err(Status::unknown( Err(e) => Err(Status::unknown(format!(
"Update VM request failed due to error: {e}", "Update VM request failed due to error: {e}"
)), ))),
} }
} }
@ -231,20 +212,55 @@ impl BrainCli for BrainCliMock {
} }
} }
async fn delete_vm(&self, req: Request<DeleteVmReq>) -> Result<Response<Empty>, Status> {
let req = check_sig_from_req(req)?;
match self.data.delete_vm(req).await {
Ok(()) => Ok(Response::new(Empty {})),
Err(e) => Err(Status::not_found(e.to_string())),
}
}
async fn report_node(&self, req: Request<ReportNodeReq>) -> Result<Response<Empty>, Status> {
let req = check_sig_from_req(req)?;
match self.data.find_contract_by_uuid(&req.contract) {
Ok(contract)
if contract.admin_pubkey == req.admin_pubkey
&& contract.node_pubkey == req.node_pubkey =>
{
()
}
_ => return Err(Status::unauthenticated("No contract found by this ID.")),
};
self.data
.report_node(req.admin_pubkey, &req.node_pubkey, req.reason);
Ok(Response::new(Empty {}))
}
type ListVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>; type ListVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
async fn list_vm_contracts( async fn list_vm_contracts(
&self, &self,
req: Request<ListVmContractsReq>, req: Request<ListVmContractsReq>,
) -> Result<Response<Self::ListVmContractsStream>, Status> { ) -> Result<Response<Self::ListVmContractsStream>, Status> {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("CLI {} requested ListVMVmContractsStream", req.admin_pubkey); info!(
let contracts = match req.uuid.is_empty() { "CLI {} requested ListVMVmContractsStream. As operator: {}",
false => match self.data.find_contract_by_uuid(&req.uuid) { req.wallet, req.as_operator
Some(contract) => vec![contract], );
None => Vec::new(), let mut contracts = Vec::new();
}, if !req.uuid.is_empty() {
true => self.data.find_contracts_by_admin_pubkey(&req.admin_pubkey), if let Ok(specific_contract) = self.data.find_contract_by_uuid(&req.uuid) {
}; if specific_contract.admin_pubkey == req.wallet {
contracts.push(specific_contract);
}
// TODO: allow operator to inspect contracts
}
} else {
if req.as_operator {
contracts.append(&mut self.data.find_vm_contracts_by_operator(&req.wallet));
} else {
contracts.append(&mut self.data.find_vm_contracts_by_admin(&req.wallet));
}
}
let (tx, rx) = mpsc::channel(6); let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move { tokio::spawn(async move {
for contract in contracts { for contract in contracts {
@ -264,7 +280,7 @@ impl BrainCli for BrainCliMock {
) -> Result<Response<Self::ListVmNodesStream>, tonic::Status> { ) -> Result<Response<Self::ListVmNodesStream>, tonic::Status> {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("CLI requested ListVmNodesStream: {req:?}"); info!("CLI requested ListVmNodesStream: {req:?}");
let nodes = self.data.find_nodes_by_filters(&req); let nodes = self.data.find_vm_nodes_by_filters(&req);
let (tx, rx) = mpsc::channel(6); let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move { tokio::spawn(async move {
for node in nodes { for node in nodes {
@ -291,12 +307,65 @@ impl BrainCli for BrainCliMock {
} }
} }
async fn delete_vm(&self, req: Request<DeleteVmReq>) -> Result<Response<Empty>, Status> { async fn register_operator(
&self,
req: Request<RegOperatorReq>,
) -> Result<Response<Empty>, Status> {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("Unknown CLI requested to delete vm {}", req.uuid); info!("Regitering new operator: {req:?}");
match self.data.delete_vm(req).await { match self.data.register_operator(req) {
Ok(()) => Ok(Response::new(Empty {})), Ok(()) => Ok(Response::new(Empty {})),
Err(e) => Err(Status::not_found(e.to_string())), Err(e) => Err(Status::failed_precondition(e.to_string())),
}
}
async fn kick_contract(&self, req: Request<KickReq>) -> Result<Response<KickResp>, Status> {
let req = check_sig_from_req(req)?;
match self
.data
.kick_contract(&req.operator_wallet, &req.contract_uuid, &req.reason)
.await
{
Ok(nano_lp) => Ok(Response::new(KickResp { nano_lp })),
Err(e) => Err(Status::permission_denied(e.to_string())),
}
}
async fn ban_user(&self, req: Request<BanUserReq>) -> Result<Response<Empty>, Status> {
let req = check_sig_from_req(req)?;
self.data.ban_user(&req.operator_wallet, &req.user_wallet);
Ok(Response::new(Empty {}))
}
type ListOperatorsStream =
Pin<Box<dyn Stream<Item = Result<ListOperatorsResp, Status>> + Send>>;
async fn list_operators(
&self,
req: Request<Empty>,
) -> Result<Response<Self::ListOperatorsStream>, Status> {
let _ = check_sig_from_req(req)?;
let operators = self.data.list_operators();
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for op in operators {
let _ = tx.send(Ok(op.into())).await;
}
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::ListOperatorsStream
))
}
async fn inspect_operator(
&self,
req: Request<Pubkey>,
) -> Result<Response<InspectOperatorResp>, Status> {
match self.data.inspect_operator(&req.into_inner().pubkey) {
Some(op) => Ok(Response::new(op.into())),
None => Err(Status::not_found(
"The wallet you specified is not an operator",
)),
} }
} }
@ -307,6 +376,13 @@ impl BrainCli for BrainCliMock {
Ok(Response::new(Empty {})) Ok(Response::new(Empty {}))
} }
async fn slash(&self, req: Request<SlashReq>) -> Result<Response<Empty>, Status> {
check_admin_key(&req)?;
let req = check_sig_from_req(req)?;
self.data.slash_account(&req.pubkey, req.tokens);
Ok(Response::new(Empty {}))
}
type ListAllVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>; type ListAllVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
async fn list_all_vm_contracts( async fn list_all_vm_contracts(
&self, &self,
@ -352,280 +428,39 @@ trait PubkeyGetter {
fn get_pubkey(&self) -> Option<String>; fn get_pubkey(&self) -> Option<String>;
} }
impl PubkeyGetter for Pubkey { macro_rules! impl_pubkey_getter {
($t:ty, $field:ident) => {
impl PubkeyGetter for $t {
fn get_pubkey(&self) -> Option<String> { fn get_pubkey(&self) -> Option<String> {
Some(self.pubkey.clone()) Some(self.$field.clone())
} }
} }
#[tonic::async_trait]
impl BrainAppCli for BrainAppCliMock {
type ListAppContractsStream = Pin<Box<dyn Stream<Item = Result<AppContract, Status>> + Send>>;
async fn create_app(
&self,
req: tonic::Request<NewAppReq>,
) -> Result<tonic::Response<NewAppRes>, Status> {
let req_data = check_sig_from_req(req)?;
log::info!("Creating new container: {req_data:?}");
let admin_pubkey = req_data.admin_pubkey.clone();
let (oneshot_tx, oneshot_rx) = tokio::sync::oneshot::channel();
self.data.send_new_container_req(req_data, oneshot_tx).await;
match oneshot_rx.await {
Ok(response) => {
info!("responding container confirmation to {admin_pubkey}: {response:?}");
Ok(Response::new(response))
}
Err(e) => {
log::error!("Something went wrong. Reached error {e:?}");
Err(Status::unknown(
"Request failed due to unknown error. Please try again or contact the DeTEE devs team.",
))
}
}
}
async fn delete_app(
&self,
req: tonic::Request<DelAppReq>,
) -> Result<tonic::Response<detee_shared::sgx::pb::brain::Empty>, Status> {
let req_data = check_sig_from_req(req)?;
log::info!("deleting container: {}", req_data.uuid.clone());
if let Err(er) = self.data.send_del_container_req(req_data).await {
info!("Could not delete container: {er}");
return Err(Status::not_found("Could not find container"));
}; };
($t:ty) => {
Ok(Response::new(detee_shared::sgx::pb::brain::Empty {})) impl PubkeyGetter for $t {
} fn get_pubkey(&self) -> Option<String> {
None
async fn list_app_contracts(
&self,
req: tonic::Request<ListAppContractsReq>,
) -> Result<tonic::Response<Self::ListAppContractsStream>, Status> {
let req_data = check_sig_from_req(req)?;
let app_contracts = self
.data
.find_app_contracts_by_admin_pubkey(&req_data.admin_pubkey);
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for contract in app_contracts {
let _ = tx.send(contract.into()).await;
}
});
let output_stream = ReceiverStream::new(rx).map(Ok);
Ok(Response::new(
Box::pin(output_stream) as Self::ListAppContractsStream
))
} }
} }
#[tonic::async_trait]
impl BrainAppDaemon for BrainAppDaemonMock {
type RegisterAppNodeStream = Pin<Box<dyn Stream<Item = Result<AppContract, Status>> + Send>>;
type BrainMessagesStream = Pin<Box<dyn Stream<Item = Result<BrainMessageApp, Status>> + Send>>;
async fn register_app_node(
&self,
req: tonic::Request<RegisterAppNodeReq>,
) -> Result<tonic::Response<Self::RegisterAppNodeStream>, Status> {
let req_data = check_sig_from_req(req)?;
log::info!(
"registering app node_key : {}, operator_key: {}",
&req_data.node_pubkey,
&req_data.operator_pubkey
);
let app_node = crate::data::AppNode {
node_pubkey: req_data.node_pubkey.clone(),
operator_pubkey: req_data.operator_pubkey,
ip: req_data.main_ip,
city: req_data.city,
region: req_data.region,
country: req_data.country,
..Default::default()
}; };
self.data.insert_app_node(app_node);
log::info!("Sending existing contracts to {}", &req_data.node_pubkey);
let app_contracts = self
.data
.find_app_contracts_by_node_pubkey(&req_data.node_pubkey);
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for contract in app_contracts {
let _ = tx.send(contract.into()).await;
}
});
let output_stream = ReceiverStream::new(rx).map(Ok);
Ok(Response::new(Box::pin(output_stream)))
} }
async fn brain_messages( impl_pubkey_getter!(Pubkey, pubkey);
&self, impl_pubkey_getter!(NewVmReq, admin_pubkey);
req: tonic::Request<detee_shared::sgx::pb::brain::DaemonAuth>, impl_pubkey_getter!(DeleteVmReq, admin_pubkey);
) -> Result<tonic::Response<Self::BrainMessagesStream>, Status> { impl_pubkey_getter!(UpdateVmReq, admin_pubkey);
let req_data = req.into_inner(); impl_pubkey_getter!(ExtendVmReq, admin_pubkey);
let pubkey = req_data.pubkey.clone(); impl_pubkey_getter!(ReportNodeReq, admin_pubkey);
check_sig_from_parts( impl_pubkey_getter!(ListVmContractsReq, wallet);
&pubkey, impl_pubkey_getter!(RegisterVmNodeReq, node_pubkey);
&req_data.timestamp, impl_pubkey_getter!(RegOperatorReq, pubkey);
&format!("{:?}", req_data.contracts), impl_pubkey_getter!(KickReq, operator_wallet);
&req_data.signature, impl_pubkey_getter!(BanUserReq, operator_wallet);
)?;
info!( impl_pubkey_getter!(VmNodeFilters);
"Daemon {} connected to receive brain messages", impl_pubkey_getter!(Empty);
req_data.pubkey impl_pubkey_getter!(AirdropReq);
); impl_pubkey_getter!(SlashReq);
let (tx, rx) = mpsc::channel(6);
self.data.add_app_daemon_tx(&req_data.pubkey, tx);
let output_stream = ReceiverStream::new(rx).map(Ok);
Ok(Response::new(
Box::pin(output_stream) as Self::BrainMessagesStream
))
}
async fn daemon_messages(
&self,
req: tonic::Request<Streaming<DaemonMessageApp>>,
) -> Result<tonic::Response<detee_shared::sgx::pb::brain::Empty>, Status> {
let mut req_stream = req.into_inner();
let mut pubkey;
if let Some(Ok(msg)) = req_stream.next().await {
log::debug!(
"demon_messages received the following auth message: {:?}",
msg.msg
);
if let Some(detee_shared::sgx::pb::brain::daemon_message_app::Msg::Auth(auth)) = msg.msg
{
pubkey = auth.pubkey.clone();
check_sig_from_parts(
&pubkey,
&auth.timestamp,
&format!("{:?}", auth.contracts),
&auth.signature,
)?;
} else {
return Err(Status::unauthenticated(
"Could not authenticate the daemon: could not extract auth signature",
));
}
} else {
return Err(Status::unauthenticated("Could not authenticate the daemon"));
}
while let Some(daemon_message) = req_stream.next().await {
match daemon_message {
Ok(msg) => match msg.msg {
Some(detee_shared::sgx::pb::brain::daemon_message_app::Msg::Auth(
daemon_auth,
)) => pubkey = daemon_auth.pubkey,
Some(detee_shared::sgx::pb::brain::daemon_message_app::Msg::NewAppRes(
new_app_res,
)) => self.data.send_new_container_resp(new_app_res).await,
Some(
detee_shared::sgx::pb::brain::daemon_message_app::Msg::AppNodeResources(
node_resource,
),
) => self.data.submit_app_node_resources(node_resource),
_ => {
dbg!("None");
}
},
Err(e) => {
log::warn!("Daemon disconnected: {e:?}");
self.data.del_app_daemon_tx(&pubkey);
}
}
//
}
Ok(Response::new(detee_shared::sgx::pb::brain::Empty {}))
}
}
impl PubkeyGetter for NewVmReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for DeleteVmReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for UpdateVmReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for ExtendVmReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for ListVmContractsReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for VmNodeFilters {
fn get_pubkey(&self) -> Option<String> {
None
}
}
impl PubkeyGetter for RegisterVmNodeReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.node_pubkey.clone())
}
}
impl PubkeyGetter for Empty {
fn get_pubkey(&self) -> Option<String> {
None
}
}
impl PubkeyGetter for AirdropReq {
fn get_pubkey(&self) -> Option<String> {
None
}
}
impl PubkeyGetter for RegisterAppNodeReq {
fn get_pubkey(&self) -> Option<String> {
None
}
}
impl PubkeyGetter for NewAppReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for DelAppReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
impl PubkeyGetter for ListAppContractsReq {
fn get_pubkey(&self) -> Option<String> {
Some(self.admin_pubkey.clone())
}
}
fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Result<T, Status> { fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Result<T, Status> {
let time = match req.metadata().get("timestamp") { let time = match req.metadata().get("timestamp") {
@ -640,9 +475,9 @@ fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Res
let parsed_time = chrono::DateTime::parse_from_rfc3339(time) let parsed_time = chrono::DateTime::parse_from_rfc3339(time)
.map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?; .map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?;
let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds(); let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds();
if seconds_elapsed > 1 || seconds_elapsed < -1 { if seconds_elapsed > 4 || seconds_elapsed < -4 {
return Err(Status::unauthenticated(format!( return Err(Status::unauthenticated(format!(
"Date is not within 1 sec of the time of the server: CLI {} vs Server {}", "Date is not within 4 sec of the time of the server: CLI {} vs Server {}",
parsed_time, now parsed_time, now
))); )));
} }
@ -695,9 +530,9 @@ fn check_sig_from_parts(pubkey: &str, time: &str, msg: &str, sig: &str) -> Resul
let parsed_time = chrono::DateTime::parse_from_rfc3339(time) let parsed_time = chrono::DateTime::parse_from_rfc3339(time)
.map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?; .map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?;
let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds(); let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds();
if seconds_elapsed > 1 || seconds_elapsed < -1 { if seconds_elapsed > 4 || seconds_elapsed < -4 {
return Err(Status::unauthenticated(format!( return Err(Status::unauthenticated(format!(
"Date is not within 1 sec of the time of the server: CLI {} vs Server {}", "Date is not within 4 sec of the time of the server: CLI {} vs Server {}",
parsed_time, now parsed_time, now
))); )));
} }

@ -2,12 +2,8 @@ mod data;
mod grpc; mod grpc;
use data::BrainData; use data::BrainData;
use detee_shared::sgx::pb::brain::brain_app_cli_server::BrainAppCliServer;
use detee_shared::sgx::pb::brain::brain_app_daemon_server::BrainAppDaemonServer;
use grpc::snp_proto::brain_cli_server::BrainCliServer; use grpc::snp_proto::brain_cli_server::BrainCliServer;
use grpc::snp_proto::brain_vm_daemon_server::BrainVmDaemonServer; use grpc::snp_proto::brain_vm_daemon_server::BrainVmDaemonServer;
use grpc::BrainAppCliMock;
use grpc::BrainAppDaemonMock;
use grpc::BrainCliMock; use grpc::BrainCliMock;
use grpc::BrainDaemonMock; use grpc::BrainDaemonMock;
use std::sync::Arc; use std::sync::Arc;
@ -23,7 +19,11 @@ async fn main() {
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
data_clone.vm_nodes_cron().await;
data_clone.vm_contracts_cron().await; data_clone.vm_contracts_cron().await;
if let Err(e) = data_clone.save_to_disk() {
log::error!("Could not save data to disk due to error: {e}")
}
} }
}); });
let addr = "0.0.0.0:31337".parse().unwrap(); let addr = "0.0.0.0:31337".parse().unwrap();
@ -31,14 +31,9 @@ async fn main() {
let daemon_server = BrainVmDaemonServer::new(BrainDaemonMock::new(data.clone())); let daemon_server = BrainVmDaemonServer::new(BrainDaemonMock::new(data.clone()));
let cli_server = BrainCliServer::new(BrainCliMock::new(data.clone())); let cli_server = BrainCliServer::new(BrainCliMock::new(data.clone()));
let sgx_cli_server = BrainAppCliServer::new(BrainAppCliMock::new(data.clone()));
let sgx_daemon_server = BrainAppDaemonServer::new(BrainAppDaemonMock::new(data.clone()));
Server::builder() Server::builder()
.add_service(daemon_server) .add_service(daemon_server)
.add_service(cli_server) .add_service(cli_server)
.add_service(sgx_cli_server)
.add_service(sgx_daemon_server)
.serve(addr) .serve(addr)
.await .await
.unwrap(); .unwrap();

@ -55,7 +55,7 @@ message MeasurementIP {
// This should also include a block hash or similar, for auth // This should also include a block hash or similar, for auth
message RegisterVmNodeReq { message RegisterVmNodeReq {
string node_pubkey = 1; string node_pubkey = 1;
string owner_pubkey = 2; string operator_wallet = 2;
string main_ip = 3; string main_ip = 3;
string country = 4; string country = 4;
string region = 5; string region = 5;
@ -154,8 +154,8 @@ service BrainVmDaemon {
} }
message ListVmContractsReq { message ListVmContractsReq {
string admin_pubkey = 1; string wallet = 1;
string node_pubkey = 2; bool as_operator = 2;
string uuid = 3; string uuid = 3;
} }
@ -174,15 +174,14 @@ message VmNodeFilters {
} }
message VmNodeListResp { message VmNodeListResp {
string node_pubkey = 1; string operator = 1;
string country = 2; string node_pubkey = 2;
string region = 3; string country = 3;
string city = 4; string region = 4;
string ip = 5; // required for latency test string city = 5;
uint32 server_rating = 6; string ip = 6; // required for latency test
uint32 provider_rating = 7; repeated string reports = 7; // TODO: this will become an enum
// nanoLP per unit per minute uint64 price = 8; // nanoLP per unit per minute
uint64 price = 8;
} }
message ExtendVmReq { message ExtendVmReq {
@ -196,12 +195,59 @@ message AirdropReq {
uint64 tokens = 2; uint64 tokens = 2;
} }
message SlashReq {
string pubkey = 1;
uint64 tokens = 2;
}
message Account { message Account {
string pubkey = 1; string pubkey = 1;
uint64 balance = 2; uint64 balance = 2;
uint64 tmp_locked = 3; uint64 tmp_locked = 3;
} }
message RegOperatorReq {
string pubkey = 1;
uint64 escrow = 2;
string email = 3;
}
message ListOperatorsResp {
string pubkey = 1;
uint64 escrow = 2;
string email = 3;
uint64 app_nodes = 4;
uint64 vm_nodes = 5;
uint64 reports = 6;
}
message InspectOperatorResp {
ListOperatorsResp operator = 1;
repeated VmNodeListResp nodes = 2;
}
message ReportNodeReq {
string admin_pubkey = 1;
string node_pubkey = 2;
string contract = 3;
string reason = 4;
}
message KickReq {
string operator_wallet = 1;
string contract_uuid = 2;
string reason = 3;
}
message BanUserReq {
string operator_wallet = 1;
string user_wallet = 2;
}
message KickResp {
uint64 nano_lp = 1;
}
service BrainCli { service BrainCli {
rpc GetBalance (Pubkey) returns (AccountBalance); rpc GetBalance (Pubkey) returns (AccountBalance);
rpc NewVm (NewVmReq) returns (NewVmResp); rpc NewVm (NewVmReq) returns (NewVmResp);
@ -211,8 +257,15 @@ service BrainCli {
rpc DeleteVm (DeleteVmReq) returns (Empty); rpc DeleteVm (DeleteVmReq) returns (Empty);
rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp); rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp);
rpc ExtendVm (ExtendVmReq) returns (Empty); rpc ExtendVm (ExtendVmReq) returns (Empty);
rpc ReportNode (ReportNodeReq) returns (Empty);
rpc ListOperators (Empty) returns (stream ListOperatorsResp);
rpc InspectOperator (Pubkey) returns (InspectOperatorResp);
rpc RegisterOperator (RegOperatorReq) returns (Empty);
rpc KickContract (KickReq) returns (KickResp);
rpc BanUser (BanUserReq) returns (Empty);
// admin commands // admin commands
rpc Airdrop (AirdropReq) returns (Empty); rpc Airdrop (AirdropReq) returns (Empty);
rpc Slash (SlashReq) returns (Empty);
rpc ListAllVmContracts (Empty) returns (stream VmContract); rpc ListAllVmContracts (Empty) returns (stream VmContract);
rpc ListAccounts (Empty) returns (stream Account); rpc ListAccounts (Empty) returns (stream Account);
} }