Merge branch 'main' into sgx_brain

This commit is contained in:
Noor 2025-02-10 17:09:54 +05:30
parent f4f16228fc
commit b22b08799b
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
7 changed files with 928 additions and 365 deletions

480
Cargo.lock generated

@ -82,11 +82,12 @@ dependencies = [
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"
dependencies = [
"anstyle",
"once_cell",
"windows-sys 0.59.0",
]
@ -120,9 +121,9 @@ dependencies = [
[[package]]
name = "async-trait"
version = "0.1.83"
version = "0.1.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
checksum = "644dd749086bf3771a2fbc5f256fdb982d53f011c7d5d560304eafeecebce79d"
dependencies = [
"proc-macro2",
"quote",
@ -210,18 +211,35 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
version = "2.6.0"
name = "base64ct"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "brain-mock"
version = "0.1.0"
dependencies = [
"bs58",
"chrono",
"dashmap",
"detee-shared",
"ed25519-dalek",
"env_logger",
"log",
"prost",
@ -238,10 +256,19 @@ dependencies = [
]
[[package]]
name = "bumpalo"
version = "3.16.0"
name = "bs58"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4"
dependencies = [
"tinyvec",
]
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "byteorder"
@ -251,15 +278,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.9.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9"
[[package]]
name = "cc"
version = "1.2.5"
version = "1.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda"
dependencies = [
"shlex",
]
@ -290,6 +317,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "const-oid"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -306,12 +339,58 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "curve25519-dalek"
version = "4.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be"
dependencies = [
"cfg-if",
"cpufeatures",
"curve25519-dalek-derive",
"digest",
"fiat-crypto",
"rustc_version",
"subtle",
"zeroize",
]
[[package]]
name = "curve25519-dalek-derive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "dashmap"
version = "6.1.0"
@ -326,6 +405,16 @@ dependencies = [
"parking_lot_core",
]
[[package]]
name = "der"
version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0"
dependencies = [
"const-oid",
"zeroize",
]
[[package]]
name = "detee-shared"
version = "0.1.0"
@ -339,6 +428,16 @@ dependencies = [
"tonic-build",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
@ -350,6 +449,30 @@ dependencies = [
"syn",
]
[[package]]
name = "ed25519"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53"
dependencies = [
"pkcs8",
"signature",
]
[[package]]
name = "ed25519-dalek"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871"
dependencies = [
"curve25519-dalek",
"ed25519",
"serde",
"sha2",
"subtle",
"zeroize",
]
[[package]]
name = "either"
version = "1.13.0"
@ -410,6 +533,12 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "fiat-crypto"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d"
[[package]]
name = "fixedbitset"
version = "0.4.2"
@ -485,6 +614,16 @@ dependencies = [
"pin-utils",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.15"
@ -493,7 +632,19 @@ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[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]]
@ -514,7 +665,7 @@ dependencies = [
"futures-core",
"futures-sink",
"http",
"indexmap 2.7.0",
"indexmap 2.7.1",
"slab",
"tokio",
"tokio-util",
@ -581,9 +732,9 @@ dependencies = [
[[package]]
name = "httparse"
version = "1.9.5"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946"
checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a"
[[package]]
name = "httpdate"
@ -599,9 +750,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "1.5.2"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0"
checksum = "cc2b571658e38e0c01b1fdca3bbbe93c00d3d71693ff2770043f8c29bc7d6f80"
dependencies = [
"bytes",
"futures-channel",
@ -857,9 +1008,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "2.7.0"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652"
dependencies = [
"equivalent",
"hashbrown 0.15.2",
@ -867,9 +1018,9 @@ dependencies = [
[[package]]
name = "ipnet"
version = "2.10.1"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
[[package]]
name = "is_terminal_polyfill"
@ -894,9 +1045,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "js-sys"
version = "0.3.76"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
@ -904,9 +1055,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.168"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "libyml"
@ -920,9 +1071,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
[[package]]
name = "litemap"
@ -942,9 +1093,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.22"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "matchit"
@ -966,9 +1117,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "miniz_oxide"
version = "0.8.0"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"
dependencies = [
"adler2",
]
@ -980,7 +1131,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.52.0",
]
@ -992,9 +1143,9 @@ checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03"
[[package]]
name = "native-tls"
version = "0.2.12"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466"
checksum = "0dab59f8e050d5df8e4dd87d9206fb6f65a483e20ac9fda365ade4fab353196c"
dependencies = [
"libc",
"log",
@ -1018,24 +1169,24 @@ dependencies = [
[[package]]
name = "object"
version = "0.36.5"
version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.20.2"
version = "1.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e"
[[package]]
name = "openssl"
version = "0.10.68"
version = "0.10.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5"
checksum = "61cfb4e166a8bb8c9b55c500bc2308550148ece889be90f609377e58140f42c6"
dependencies = [
"bitflags",
"cfg-if",
@ -1059,15 +1210,15 @@ dependencies = [
[[package]]
name = "openssl-probe"
version = "0.1.5"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
[[package]]
name = "openssl-sys"
version = "0.9.104"
version = "0.9.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741"
checksum = "8b22d5b84be05a8d6947c7cb71f7c849aa0f112acd4bf51c2a7c1c988ac0a9dc"
dependencies = [
"cc",
"libc",
@ -1101,23 +1252,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [
"fixedbitset",
"indexmap 2.7.0",
"indexmap 2.7.1",
]
[[package]]
name = "pin-project"
version = "1.1.7"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be57f64e946e500c8ee36ef6331845d40a93055567ec57e8fae13efd33759b95"
checksum = "dfe2e71e1471fe07709406bf725f710b02927c9c54b2b5b2ec0e8087d97c327d"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.7"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c"
checksum = "f6e859e6e5bd50440ab63c47e3ebabc90f26251f7c73c3d3e837b74a1cc3fa67"
dependencies = [
"proc-macro2",
"quote",
@ -1126,9 +1277,9 @@ dependencies = [
[[package]]
name = "pin-project-lite"
version = "0.2.15"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
@ -1136,6 +1287,16 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkcs8"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
dependencies = [
"der",
"spki",
]
[[package]]
name = "pkg-config"
version = "0.3.31"
@ -1153,9 +1314,9 @@ dependencies = [
[[package]]
name = "prettyplease"
version = "0.2.25"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac"
dependencies = [
"proc-macro2",
"syn",
@ -1163,9 +1324,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.92"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
@ -1224,9 +1385,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.37"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
@ -1258,7 +1419,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]
[[package]]
@ -1301,9 +1462,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "reqwest"
version = "0.12.10"
version = "0.12.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d3536321cfc54baa8cf3e273d5e1f63f889067829c4b410fcdbac8ca7b80994"
checksum = "43e734407157c3c2034e0258f5e4473ddb361b1e85f95a66690d67264d7cd1da"
dependencies = [
"base64",
"bytes",
@ -1351,7 +1512,7 @@ checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
dependencies = [
"cc",
"cfg-if",
"getrandom",
"getrandom 0.2.15",
"libc",
"spin",
"untrusted",
@ -1365,10 +1526,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustix"
version = "0.38.42"
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver",
]
[[package]]
name = "rustix"
version = "0.38.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
dependencies = [
"bitflags",
"errno",
@ -1379,9 +1549,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.20"
version = "0.23.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b"
checksum = "9fb9263ab4eb695e42321db096e3b8fbd715a59b154d5c88d82db2175b681ba7"
dependencies = [
"once_cell",
"rustls-pki-types",
@ -1401,9 +1571,9 @@ dependencies = [
[[package]]
name = "rustls-pki-types"
version = "1.10.1"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37"
checksum = "917ce264624a4b4db1c364dcc35bfca9ded014d0a958cd47ad3e960e988ea51c"
[[package]]
name = "rustls-webpki"
@ -1418,15 +1588,15 @@ dependencies = [
[[package]]
name = "rustversion"
version = "1.0.18"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "ryu"
version = "1.0.18"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd"
[[package]]
name = "schannel"
@ -1458,28 +1628,34 @@ dependencies = [
[[package]]
name = "security-framework-sys"
version = "2.13.0"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1863fd3768cd83c56a7f60faa4dc0d403f1b6df0a38c3c25f44b7894e45370d5"
checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "serde"
version = "1.0.216"
name = "semver"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03"
[[package]]
name = "serde"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.216"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [
"proc-macro2",
"quote",
@ -1488,9 +1664,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.134"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949"
dependencies = [
"itoa",
"memchr",
@ -1516,7 +1692,7 @@ version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd"
dependencies = [
"indexmap 2.7.0",
"indexmap 2.7.1",
"itoa",
"libyml",
"memchr",
@ -1525,12 +1701,32 @@ dependencies = [
"version_check",
]
[[package]]
name = "sha2"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signature"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
dependencies = [
"rand_core",
]
[[package]]
name = "slab"
version = "0.4.9"
@ -1562,6 +1758,16 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "spki"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d"
dependencies = [
"base64ct",
"der",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -1576,9 +1782,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "2.0.90"
version = "2.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
dependencies = [
"proc-macro2",
"quote",
@ -1628,12 +1834,13 @@ dependencies = [
[[package]]
name = "tempfile"
version = "3.14.0"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c"
checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91"
dependencies = [
"cfg-if",
"fastrand",
"getrandom 0.3.1",
"once_cell",
"rustix",
"windows-sys 0.59.0",
@ -1670,10 +1877,25 @@ dependencies = [
]
[[package]]
name = "tokio"
version = "1.42.0"
name = "tinyvec"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551"
checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
dependencies = [
"backtrace",
"bytes",
@ -1687,9 +1909,9 @@ dependencies = [
[[package]]
name = "tokio-macros"
version = "2.4.0"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [
"proc-macro2",
"quote",
@ -1869,10 +2091,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "unicode-ident"
version = "1.0.14"
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"
[[package]]
name = "untrusted"
@ -1911,11 +2139,11 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.11.0"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0"
dependencies = [
"getrandom",
"getrandom 0.3.1",
]
[[package]]
@ -1946,21 +2174,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.99"
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
@ -1972,9 +2210,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.49"
version = "0.4.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"
checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61"
dependencies = [
"cfg-if",
"js-sys",
@ -1985,9 +2223,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -1995,9 +2233,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
@ -2008,15 +2246,18 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "web-sys"
version = "0.3.76"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -2143,6 +2384,15 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "write16"
version = "1.0.0"

@ -4,8 +4,10 @@ version = "0.1.0"
edition = "2021"
[dependencies]
bs58 = "0.5.1"
chrono = "0.4.39"
dashmap = "6.1.0"
ed25519-dalek = "2.1.1"
env_logger = "0.11.6"
log = "0.4.22"
prost = "0.13.4"

@ -1,6 +1,6 @@
fn main() {
tonic_build::configure()
.build_server(true)
.compile_protos(&["snp.proto"], &["proto"])
.compile_protos(&["vm.proto"], &["proto"])
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
}

@ -1,4 +1,3 @@
#![allow(dead_code)]
use crate::grpc::snp_proto::{self as grpc};
use chrono::Utc;
use dashmap::DashMap;
@ -17,24 +16,22 @@ use detee_shared::pb::brain::NewAppRes;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("We do not allow locking of more than 100000 tokens.")]
#[error("We do not allow locking of more than 100000 LP.")]
TxTooBig,
#[error("Account has insufficient funds for this operation")]
InsufficientFunds,
#[error("I have no idea how this happened. Please report this bug.")]
ImpossibleError,
#[error("Could not find contract {0}")]
ContractNotFound(String),
VmContractNotFound(String),
}
#[derive(Clone)]
pub struct AccountNanoTokens {
pub struct AccountNanoLP {
pub balance: u64,
pub tmp_locked: u64,
}
impl From<AccountNanoTokens> for grpc::AccountBalance {
fn from(value: AccountNanoTokens) -> Self {
impl From<AccountNanoLP> for grpc::AccountBalance {
fn from(value: AccountNanoLP) -> Self {
grpc::AccountBalance {
balance: value.balance,
tmp_locked: value.tmp_locked,
@ -43,7 +40,7 @@ impl From<AccountNanoTokens> for grpc::AccountBalance {
}
#[derive(Eq, Hash, PartialEq, Clone, Debug, Default)]
pub struct Node {
pub struct VmNode {
pub public_key: String,
pub owner_key: String,
pub country: String,
@ -57,13 +54,13 @@ pub struct Node {
pub avail_ipv6: u32,
pub avail_ports: u32,
pub max_ports_per_vm: u32,
// nanotokens per unit per minute
// nanoLP per unit per minute
pub price: u64,
}
impl Into<grpc::NodeListResp> for Node {
fn into(self) -> grpc::NodeListResp {
grpc::NodeListResp {
impl Into<grpc::VmNodeListResp> for VmNode {
fn into(self) -> grpc::VmNodeListResp {
grpc::VmNodeListResp {
node_pubkey: self.public_key,
country: self.country,
region: self.region,
@ -77,7 +74,7 @@ impl Into<grpc::NodeListResp> for Node {
}
#[derive(Clone, Debug)]
pub struct Contract {
pub struct VmContract {
pub uuid: String,
pub hostname: String,
pub admin_pubkey: String,
@ -99,27 +96,27 @@ pub struct Contract {
pub collected_at: chrono::DateTime<Utc>,
}
impl Contract {
impl VmContract {
fn total_units(&self) -> u64 {
// TODO: Optimize this based on price of hardware.
// I tried, but this can be done better.
// Storage cost should also be based on tier
(self.vcpus as u64 * 10)
+ ((self.memory_mb + 256) as u64 * 4 / 100)
+ ((self.memory_mb + 256) as u64 / 200)
+ (self.disk_size_gb as u64 / 10)
+ (!self.public_ipv4.is_empty() as u64 * 10)
}
// Returns price per minute in nanotokens
// Returns price per minute in nanoLP
fn price_per_minute(&self) -> u64 {
self.total_units() * self.price_per_unit
}
}
impl Into<grpc::Contract> for Contract {
fn into(self) -> grpc::Contract {
impl Into<grpc::VmContract> for VmContract {
fn into(self) -> grpc::VmContract {
let nano_per_minute = self.price_per_minute();
grpc::Contract {
grpc::VmContract {
uuid: self.uuid,
hostname: self.hostname,
admin_pubkey: self.admin_pubkey,
@ -197,13 +194,13 @@ pub struct AppNode {
#[derive(Default)]
pub struct BrainData {
// amount of nanotokens in each account
accounts: DashMap<String, AccountNanoTokens>,
nodes: RwLock<Vec<Node>>,
contracts: RwLock<Vec<Contract>>,
// amount of nanoLP in each account
accounts: DashMap<String, AccountNanoLP>,
vm_nodes: RwLock<Vec<VmNode>>,
vm_contracts: RwLock<Vec<VmContract>>,
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>,
daemon_tx: DashMap<String, Sender<grpc::BrainMessage>>,
daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>,
app_nodes: RwLock<Vec<AppNode>>,
app_daemon_tx: DashMap<String, Sender<BrainMessageApp>>,
@ -211,19 +208,12 @@ pub struct BrainData {
app_contracts: RwLock<Vec<AppContract>>,
}
#[derive(Debug)]
enum TxType {
CliContract,
DaemonDeleteVm,
DaemonNewVm,
}
impl BrainData {
pub fn new() -> Self {
Self {
accounts: DashMap::new(),
nodes: RwLock::new(Vec::new()),
contracts: RwLock::new(Vec::new()),
vm_nodes: RwLock::new(Vec::new()),
vm_contracts: RwLock::new(Vec::new()),
tmp_newvm_reqs: DashMap::new(),
tmp_updatevm_reqs: DashMap::new(),
daemon_tx: DashMap::new(),
@ -235,11 +225,11 @@ impl BrainData {
}
}
pub fn get_balance(&self, account: &str) -> AccountNanoTokens {
pub fn get_balance(&self, account: &str) -> AccountNanoLP {
if let Some(account) = self.accounts.get(account) {
return account.value().clone();
} else {
let balance = AccountNanoTokens {
let balance = AccountNanoLP {
balance: 0,
tmp_locked: 0,
};
@ -247,24 +237,26 @@ impl BrainData {
}
}
pub fn get_airdrop(&self, account: &str) {
self.add_nano_to_wallet(account, 1000_000000000);
pub fn give_airdrop(&self, account: &str, tokens: u64) {
self.add_nano_to_wallet(account, tokens.saturating_mul(1_000_000_000));
}
fn add_nano_to_wallet(&self, account: &str, nanotokens: u64) {
log::debug!("Adding {nanotokens} nanotokens to {account}");
fn add_nano_to_wallet(&self, account: &str, nano_lp: u64) {
log::debug!("Adding {nano_lp} nanoLP to {account}");
self.accounts
.entry(account.to_string())
.and_modify(|tokens| tokens.balance += nanotokens)
.or_insert(AccountNanoTokens {
balance: nanotokens,
.and_modify(|d| d.balance += nano_lp)
.or_insert(AccountNanoLP {
balance: nano_lp,
tmp_locked: 0,
});
}
pub fn contracts_cron(&self) {
pub async fn vm_contracts_cron(&self) {
let mut deleted_contracts: Vec<(String, String)> = Vec::new();
log::debug!("Running contracts cron...");
let mut contracts = self.contracts.write().unwrap();
{
let mut contracts = self.vm_contracts.write().unwrap();
contracts.retain_mut(|c| {
let owner_key = self
.find_nodes_by_pubkey(&c.node_pubkey)
@ -273,28 +265,41 @@ impl BrainData {
.clone();
let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64;
c.collected_at = Utc::now();
log::debug!("{minutes_to_collect}");
let mut nanotokens_to_collect = c.price_per_minute().saturating_mul(minutes_to_collect);
if nanotokens_to_collect > c.locked_nano {
nanotokens_to_collect = c.locked_nano;
let mut nanolp_to_collect = c.price_per_minute().saturating_mul(minutes_to_collect);
if nanolp_to_collect > c.locked_nano {
nanolp_to_collect = c.locked_nano;
}
log::debug!("Removing {nanolp_to_collect} nanoLP from {}", c.uuid);
c.locked_nano -= nanolp_to_collect;
self.add_nano_to_wallet(&owner_key, nanolp_to_collect);
if c.locked_nano == 0 {
deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone()));
}
log::debug!(
"Removing {nanotokens_to_collect} nanotokens from {}",
c.uuid
);
c.locked_nano -= nanotokens_to_collect;
self.add_nano_to_wallet(&owner_key, nanotokens_to_collect);
c.locked_nano > 0
});
}
// inform daemons of the deletion of the contracts
for (uuid, node_pubkey) in deleted_contracts.iter() {
if let Some(daemon_tx) = self.daemon_tx.get(&node_pubkey.clone()) {
let msg = grpc::BrainVmMessage {
msg: Some(grpc::brain_vm_message::Msg::DeleteVm(grpc::DeleteVmReq {
uuid: uuid.to_string(),
admin_pubkey: String::new(),
})),
};
let daemon_tx = daemon_tx.clone();
let _ = daemon_tx.send(msg).await;
}
}
}
pub fn insert_node(&self, node: Node) {
pub fn insert_node(&self, node: VmNode) {
info!("Registering node {node:?}");
let mut nodes = self.nodes.write().unwrap();
let mut nodes = self.vm_nodes.write().unwrap();
for n in nodes.iter_mut() {
if n.public_key == node.public_key {
// TODO: figure what to do in this case.
warn!("Node {} already exists. Updating data.", n.public_key);
warn!("VM Node {} already exists. Updating data.", n.public_key);
*n = node;
return;
}
@ -302,69 +307,59 @@ impl BrainData {
nodes.push(node);
}
pub fn lock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
if nanotokens > 100_000_000_000_000 {
pub fn lock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
if nano_lp > 100_000_000_000_000 {
return Err(Error::TxTooBig);
}
if let Some(mut account) = self.accounts.get_mut(account) {
if nanotokens > account.balance {
if nano_lp > account.balance {
return Err(Error::InsufficientFunds);
}
account.balance = account.balance.saturating_sub(nanotokens);
account.tmp_locked = account.tmp_locked.saturating_add(nanotokens);
account.balance = account.balance.saturating_sub(nano_lp);
account.tmp_locked = account.tmp_locked.saturating_add(nano_lp);
Ok(())
} else {
Err(Error::InsufficientFunds)
}
}
pub fn unlock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
if let Some(mut account) = self.accounts.get_mut(account) {
if nanotokens > account.tmp_locked {
return Err(Error::ImpossibleError);
}
account.balance = account.balance.saturating_add(nanotokens);
account.tmp_locked = account.tmp_locked.saturating_sub(nanotokens);
Ok(())
} else {
Err(Error::ImpossibleError)
}
}
pub fn extend_contract_time(
pub fn extend_vm_contract_time(
&self,
uuid: &str,
account: &str,
nanotokens: u64,
wallet: &str,
nano_lp: u64,
) -> Result<(), Error> {
if nanotokens > 100_000_000_000_000 {
if nano_lp > 100_000_000_000_000 {
return Err(Error::TxTooBig);
}
let mut account = match self.accounts.get_mut(account) {
let mut account = match self.accounts.get_mut(wallet) {
Some(account) => account,
None => return Err(Error::InsufficientFunds),
};
match self
.contracts
.vm_contracts
.write()
.unwrap()
.iter_mut()
.find(|c| c.uuid == uuid)
{
Some(contract) => {
if account.balance + contract.locked_nano < nanotokens {
if contract.admin_pubkey != wallet {
return Err(Error::VmContractNotFound(uuid.to_string()));
}
if account.balance + contract.locked_nano < nano_lp {
return Err(Error::InsufficientFunds);
}
account.balance = account.balance + contract.locked_nano - nanotokens;
contract.locked_nano = nanotokens;
account.balance = account.balance + contract.locked_nano - nano_lp;
contract.locked_nano = nano_lp;
Ok(())
}
None => Err(Error::ContractNotFound(uuid.to_string())),
None => Err(Error::VmContractNotFound(uuid.to_string())),
}
}
pub fn submit_node_resources(&self, res: grpc::NodeResources) {
let mut nodes = self.nodes.write().unwrap();
pub fn submit_node_resources(&self, res: grpc::VmNodeResources) {
let mut nodes = self.vm_nodes.write().unwrap();
for n in nodes.iter_mut() {
if n.public_key == res.node_pubkey {
debug!(
@ -382,13 +377,13 @@ impl BrainData {
}
}
debug!(
"Node {} not found when trying to update resources.",
"VM Node {} not found when trying to update resources.",
res.node_pubkey
);
debug!("Node list:\n{:?}", nodes);
debug!("VM Node list:\n{:?}", nodes);
}
pub fn add_daemon_tx(&self, node_pubkey: &str, tx: Sender<grpc::BrainMessage>) {
pub fn add_daemon_tx(&self, node_pubkey: &str, tx: Sender<grpc::BrainVmMessage>) {
self.daemon_tx.insert(node_pubkey.to_string(), tx);
}
@ -396,16 +391,26 @@ impl BrainData {
self.daemon_tx.remove(node_pubkey);
}
pub async fn delete_vm(&self, delete_vm: grpc::DeleteVmReq) {
if let Some(contract) = self.find_contract_by_uuid(&delete_vm.uuid) {
pub async fn delete_vm(&self, delete_vm: grpc::DeleteVmReq) -> Result<(), Error> {
let contract = match self.find_contract_by_uuid(&delete_vm.uuid) {
Some(contract) => {
if contract.admin_pubkey != delete_vm.admin_pubkey {
return Err(Error::VmContractNotFound(delete_vm.uuid));
}
contract
}
None => {
return Err(Error::VmContractNotFound(delete_vm.uuid));
}
};
info!("Found vm {}. Deleting...", delete_vm.uuid);
if let Some(daemon_tx) = self.daemon_tx.get(&contract.node_pubkey) {
debug!(
"TX for daemon {} found. Informing daemon about deletion of {}.",
contract.node_pubkey, delete_vm.uuid
);
let msg = grpc::BrainMessage {
msg: Some(grpc::brain_message::Msg::DeleteVm(delete_vm.clone())),
let msg = grpc::BrainVmMessage {
msg: Some(grpc::brain_vm_message::Msg::DeleteVm(delete_vm.clone())),
};
if let Err(e) = daemon_tx.send(msg).await {
warn!(
@ -418,11 +423,12 @@ impl BrainData {
}
self.add_nano_to_wallet(&contract.admin_pubkey, contract.locked_nano);
let mut contracts = self.contracts.write().unwrap();
let mut contracts = self.vm_contracts.write().unwrap();
contracts.retain(|c| c.uuid != delete_vm.uuid);
}
Ok(())
}
// also unlocks nanotokens in case VM creation failed
pub async fn submit_newvm_resp(&self, new_vm_resp: grpc::NewVmResp) {
let new_vm_req = match self.tmp_newvm_reqs.remove(&new_vm_resp.uuid) {
Some((_, r)) => r,
@ -469,7 +475,7 @@ impl BrainData {
admin_wallet.tmp_locked -= new_vm_req.0.locked_nano;
}
let contract = Contract {
let contract = VmContract {
uuid: new_vm_resp.uuid,
exposed_ports: args.exposed_ports.clone(),
public_ipv4,
@ -489,7 +495,7 @@ impl BrainData {
collected_at: Utc::now(),
};
info!("Created new contract: {contract:?}");
self.contracts.write().unwrap().push(contract);
self.vm_contracts.write().unwrap().push(contract);
}
pub async fn submit_updatevm_resp(&self, mut update_vm_resp: grpc::UpdateVmResp) {
@ -508,7 +514,7 @@ impl BrainData {
if update_vm_resp.error != "" {
return;
}
let mut contracts = self.contracts.write().unwrap();
let mut contracts = self.vm_contracts.write().unwrap();
match contracts.iter_mut().find(|c| c.uuid == update_vm_resp.uuid) {
Some(contract) => {
if update_vm_req.0.vcpus != 0 {
@ -537,8 +543,8 @@ impl BrainData {
contract.updated_at = Utc::now();
}
None => {
log::error!("Contract not found for {}.", update_vm_req.0.uuid);
update_vm_resp.error = "Contract not found.".to_string();
log::error!("VM Contract not found for {}.", update_vm_req.0.uuid);
update_vm_resp.error = "VM Contract not found.".to_string();
}
}
if let Err(e) = update_vm_req.1.send(update_vm_resp.clone()) {
@ -570,8 +576,8 @@ impl BrainData {
"Found daemon TX for {}. Sending newVMReq {}",
req.node_pubkey, req.uuid
);
let msg = grpc::BrainMessage {
msg: Some(grpc::brain_message::Msg::NewVmReq(req.clone())),
let msg = grpc::BrainVmMessage {
msg: Some(grpc::brain_vm_message::Msg::NewVmReq(req.clone())),
};
if let Err(e) = daemon_tx.send(msg).await {
warn!(
@ -587,6 +593,13 @@ impl BrainData {
})
.await;
}
} else {
self.submit_newvm_resp(grpc::NewVmResp {
error: "Daemon is offline.".to_string(),
uuid: req.uuid,
args: None,
})
.await;
}
}
@ -598,7 +611,17 @@ impl BrainData {
let uuid = req.uuid.clone();
info!("Inserting new vm update request in memory: {req:?}");
let node_pubkey = match self.find_contract_by_uuid(&req.uuid) {
Some(contract) => contract.node_pubkey,
Some(contract) => {
if contract.admin_pubkey != req.admin_pubkey {
let _ = tx.send(grpc::UpdateVmResp {
uuid,
error: "VM Contract does not exist.".to_string(),
args: None,
});
return;
}
contract.node_pubkey
}
None => {
log::warn!(
"Received UpdateVMReq for a contract that does not exist: {}",
@ -606,7 +629,7 @@ impl BrainData {
);
let _ = tx.send(grpc::UpdateVmResp {
uuid,
error: "Contract does not exist.".to_string(),
error: "VM Contract does not exist.".to_string(),
args: None,
});
return;
@ -619,8 +642,8 @@ impl BrainData {
"Found daemon TX for {}. Sending updateVMReq {}",
node_pubkey, req.uuid
);
let msg = grpc::BrainMessage {
msg: Some(grpc::brain_message::Msg::UpdateVmReq(req.clone())),
let msg = grpc::BrainVmMessage {
msg: Some(grpc::brain_vm_message::Msg::UpdateVmReq(req.clone())),
};
match server_tx.send(msg).await {
Ok(_) => {
@ -650,26 +673,16 @@ impl BrainData {
}
}
pub fn insert_contract(&self, contract: Contract) {
let mut contracts = self.contracts.write().unwrap();
contracts.push(contract);
}
pub fn find_nodes_by_pubkey(&self, public_key: &str) -> Option<Node> {
let nodes = self.nodes.read().unwrap();
pub fn find_nodes_by_pubkey(&self, public_key: &str) -> Option<VmNode> {
let nodes = self.vm_nodes.read().unwrap();
nodes.iter().cloned().find(|n| n.public_key == public_key)
}
pub fn find_ns_by_owner_key(&self, owner_key: &str) -> Option<Node> {
let nodes = self.nodes.read().unwrap();
nodes.iter().cloned().find(|n| n.owner_key == owner_key)
}
pub fn find_nodes_by_filters(
&self,
filters: &crate::grpc::snp_proto::NodeFilters,
) -> Vec<Node> {
let nodes = self.nodes.read().unwrap();
filters: &crate::grpc::snp_proto::VmNodeFilters,
) -> Vec<VmNode> {
let nodes = self.vm_nodes.read().unwrap();
nodes
.iter()
.filter(|n| {
@ -691,9 +704,9 @@ impl BrainData {
// TODO: sort by rating
pub fn get_one_node_by_filters(
&self,
filters: &crate::grpc::snp_proto::NodeFilters,
) -> Option<Node> {
let nodes = self.nodes.read().unwrap();
filters: &crate::grpc::snp_proto::VmNodeFilters,
) -> Option<VmNode> {
let nodes = self.vm_nodes.read().unwrap();
nodes
.iter()
.find(|n| {
@ -712,15 +725,31 @@ impl BrainData {
.cloned()
}
pub fn find_contract_by_uuid(&self, uuid: &str) -> Option<Contract> {
let contracts = self.contracts.read().unwrap();
pub fn find_contract_by_uuid(&self, uuid: &str) -> Option<VmContract> {
let contracts = self.vm_contracts.read().unwrap();
contracts.iter().cloned().find(|c| c.uuid == uuid)
}
pub fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<Contract> {
pub fn list_all_contracts(&self) -> Vec<VmContract> {
let contracts = self.vm_contracts.read().unwrap();
contracts.iter().cloned().collect()
}
pub fn list_accounts(&self) -> Vec<grpc::Account> {
self.accounts
.iter()
.map(|a| grpc::Account {
pubkey: a.key().to_string(),
balance: a.balance,
tmp_locked: a.tmp_locked,
})
.collect()
}
pub fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<VmContract> {
debug!("Searching contracts for admin pubkey {admin_pubkey}");
let contracts: Vec<Contract> = self
.contracts
let contracts: Vec<VmContract> = self
.vm_contracts
.read()
.unwrap()
.iter()
@ -731,8 +760,8 @@ impl BrainData {
contracts
}
pub fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<Contract> {
let contracts = self.contracts.read().unwrap();
pub fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<VmContract> {
let contracts = self.vm_contracts.read().unwrap();
contracts
.iter()
.filter(|c| c.node_pubkey == node_pubkey)

@ -1,13 +1,14 @@
#![allow(dead_code)]
pub mod snp_proto {
tonic::include_proto!("snp_proto");
tonic::include_proto!("vm_proto");
}
use crate::data::BrainData;
use crate::grpc::vm_daemon_message;
use log::info;
use snp_proto::brain_cli_server::BrainCli;
use snp_proto::brain_daemon_server::BrainDaemon;
use snp_proto::brain_vm_daemon_server::BrainVmDaemon;
use snp_proto::*;
use std::pin::Pin;
use std::sync::Arc;
@ -21,6 +22,10 @@ use detee_shared::pb::brain::{
AppContract, BrainMessageApp, DaemonMessageApp, DelAppReq, ListAppContractsReq, NewAppReq,
NewAppRes, RegisterAppNodeReq,
};
const ADMIN_ACCOUNTS: &[&str] = &[
"x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK",
"FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL",
];
pub struct BrainDaemonMock {
data: Arc<BrainData>,
@ -63,15 +68,15 @@ impl BrainAppDaemonMock {
}
#[tonic::async_trait]
impl BrainDaemon for BrainDaemonMock {
type RegisterNodeStream = Pin<Box<dyn Stream<Item = Result<Contract, Status>> + Send>>;
async fn register_node(
impl BrainVmDaemon for BrainDaemonMock {
type RegisterVmNodeStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
async fn register_vm_node(
&self,
req: Request<RegisterNodeReq>,
) -> Result<Response<Self::RegisterNodeStream>, Status> {
let req = req.into_inner();
req: Request<RegisterVmNodeReq>,
) -> Result<Response<Self::RegisterVmNodeStream>, Status> {
let req = check_sig_from_req(req)?;
info!("Starting registration process for {:?}", req);
let node = crate::data::Node {
let node = crate::data::VmNode {
public_key: req.node_pubkey.clone(),
owner_key: req.owner_pubkey,
country: req.country,
@ -93,19 +98,26 @@ impl BrainDaemon for BrainDaemonMock {
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::RegisterNodeStream
Box::pin(output_stream) as Self::RegisterVmNodeStream
))
}
type BrainMessagesStream = Pin<Box<dyn Stream<Item = Result<BrainMessage, Status>> + Send>>;
type BrainMessagesStream = Pin<Box<dyn Stream<Item = Result<BrainVmMessage, Status>> + Send>>;
async fn brain_messages(
&self,
req: Request<Pubkey>,
req: Request<DaemonStreamAuth>,
) -> Result<Response<Self::BrainMessagesStream>, Status> {
let req = req.into_inner();
info!("Daemon {} connected to receive brain messages", req.pubkey);
let auth = req.into_inner();
let pubkey = auth.pubkey.clone();
check_sig_from_parts(
&pubkey,
&auth.timestamp,
&format!("{:?}", auth.contracts),
&auth.signature,
)?;
info!("Daemon {} connected to receive brain messages", pubkey);
let (tx, rx) = mpsc::channel(6);
self.data.add_daemon_tx(&req.pubkey, tx);
self.data.add_daemon_tx(&pubkey, tx);
let output_stream = ReceiverStream::new(rx).map(|msg| Ok(msg));
Ok(Response::new(
Box::pin(output_stream) as Self::BrainMessagesStream
@ -114,27 +126,46 @@ impl BrainDaemon for BrainDaemonMock {
async fn daemon_messages(
&self,
req: Request<Streaming<DaemonMessage>>,
req: Request<Streaming<VmDaemonMessage>>,
) -> Result<Response<Empty>, Status> {
let mut req_stream = req.into_inner();
let mut pubkey = String::new();
let pubkey: String;
if let Some(Ok(msg)) = req_stream.next().await {
log::debug!(
"demon_messages received the following auth message: {:?}",
msg.msg
);
if let Some(vm_daemon_message::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"));
}
// info!("Received a message from daemon {pubkey}: {daemon_message:?}");
while let Some(daemon_message) = req_stream.next().await {
info!("Received a message from daemon {pubkey}: {daemon_message:?}");
match daemon_message {
Ok(msg) => match msg.msg {
Some(daemon_message::Msg::Pubkey(p)) => {
pubkey = p.pubkey;
}
Some(daemon_message::Msg::NewVmResp(new_vm_resp)) => {
Some(vm_daemon_message::Msg::NewVmResp(new_vm_resp)) => {
self.data.submit_newvm_resp(new_vm_resp).await;
}
Some(daemon_message::Msg::UpdateVmResp(update_vm_resp)) => {
Some(vm_daemon_message::Msg::UpdateVmResp(update_vm_resp)) => {
self.data.submit_updatevm_resp(update_vm_resp).await;
}
Some(daemon_message::Msg::NodeResources(node_resources)) => {
Some(vm_daemon_message::Msg::VmNodeResources(node_resources)) => {
self.data.submit_node_resources(node_resources);
}
None => {}
_ => {}
},
Err(e) => {
log::warn!("Daemon disconnected: {e:?}");
@ -149,18 +180,12 @@ impl BrainDaemon for BrainDaemonMock {
#[tonic::async_trait]
impl BrainCli for BrainCliMock {
async fn get_balance(&self, req: Request<Pubkey>) -> Result<Response<AccountBalance>, Status> {
Ok(Response::new(
self.data.get_balance(&req.into_inner().pubkey).into(),
))
}
async fn get_airdrop(&self, req: Request<Pubkey>) -> Result<Response<Empty>, Status> {
self.data.get_airdrop(&req.into_inner().pubkey);
Ok(Response::new(Empty {}))
let req = check_sig_from_req(req)?;
Ok(Response::new(self.data.get_balance(&req.pubkey).into()))
}
async fn new_vm(&self, req: Request<NewVmReq>) -> Result<Response<NewVmResp>, Status> {
let req = req.into_inner();
let req = check_sig_from_req(req)?;
info!("New VM requested via CLI: {req:?}");
let admin_pubkey = req.admin_pubkey.clone();
let (oneshot_tx, oneshot_rx) = tokio::sync::oneshot::channel();
@ -180,7 +205,7 @@ impl BrainCli for BrainCliMock {
}
async fn update_vm(&self, req: Request<UpdateVmReq>) -> Result<Response<UpdateVmResp>, Status> {
let req = req.into_inner();
let req = check_sig_from_req(req)?;
info!("Update VM requested via CLI: {req:?}");
let (oneshot_tx, oneshot_rx) = tokio::sync::oneshot::channel();
self.data.submit_updatevm_req(req, oneshot_tx).await;
@ -196,23 +221,23 @@ impl BrainCli for BrainCliMock {
}
async fn extend_vm(&self, req: Request<ExtendVmReq>) -> Result<Response<Empty>, Status> {
let req = req.into_inner();
let req = check_sig_from_req(req)?;
match self
.data
.extend_contract_time(&req.uuid, &req.admin_pubkey, req.locked_nano)
.extend_vm_contract_time(&req.uuid, &req.admin_pubkey, req.locked_nano)
{
Ok(()) => Ok(Response::new(Empty {})),
Err(e) => Err(Status::unknown(format!("Could not extend contract: {e}"))),
}
}
type ListContractsStream = Pin<Box<dyn Stream<Item = Result<Contract, Status>> + Send>>;
async fn list_contracts(
type ListVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
async fn list_vm_contracts(
&self,
req: Request<ListContractsReq>,
) -> Result<Response<Self::ListContractsStream>, Status> {
let req = req.into_inner();
info!("CLI {} requested ListVMContractsStream", req.admin_pubkey);
req: Request<ListVmContractsReq>,
) -> Result<Response<Self::ListVmContractsStream>, Status> {
let req = check_sig_from_req(req)?;
info!("CLI {} requested ListVMVmContractsStream", req.admin_pubkey);
let contracts = match req.uuid.is_empty() {
false => match self.data.find_contract_by_uuid(&req.uuid) {
Some(contract) => vec![contract],
@ -228,17 +253,17 @@ impl BrainCli for BrainCliMock {
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::ListContractsStream
Box::pin(output_stream) as Self::ListVmContractsStream
))
}
type ListNodesStream = Pin<Box<dyn Stream<Item = Result<NodeListResp, Status>> + Send>>;
async fn list_nodes(
type ListVmNodesStream = Pin<Box<dyn Stream<Item = Result<VmNodeListResp, Status>> + Send>>;
async fn list_vm_nodes(
&self,
req: Request<NodeFilters>,
) -> Result<Response<Self::ListNodesStream>, tonic::Status> {
let req = req.into_inner();
info!("Unknown CLI requested ListNodesStream: {req:?}");
req: Request<VmNodeFilters>,
) -> Result<Response<Self::ListVmNodesStream>, tonic::Status> {
let req = check_sig_from_req(req)?;
info!("CLI requested ListVmNodesStream: {req:?}");
let nodes = self.data.find_nodes_by_filters(&req);
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
@ -248,16 +273,16 @@ impl BrainCli for BrainCliMock {
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::ListNodesStream
Box::pin(output_stream) as Self::ListVmNodesStream
))
}
async fn get_one_node(
async fn get_one_vm_node(
&self,
req: Request<NodeFilters>,
) -> Result<Response<NodeListResp>, Status> {
let req = req.into_inner();
info!("Unknown CLI requested ListNodesStream: {req:?}");
req: Request<VmNodeFilters>,
) -> Result<Response<VmNodeListResp>, Status> {
let req = check_sig_from_req(req)?;
info!("Unknown CLI requested ListVmNodesStream: {req:?}");
match self.data.get_one_node_by_filters(&req) {
Some(node) => Ok(Response::new(node.into())),
None => Err(Status::not_found(
@ -267,11 +292,70 @@ impl BrainCli for BrainCliMock {
}
async fn delete_vm(&self, req: Request<DeleteVmReq>) -> Result<Response<Empty>, Status> {
let req = req.into_inner();
let req = check_sig_from_req(req)?;
info!("Unknown CLI requested to delete vm {}", req.uuid);
self.data.delete_vm(req).await;
match self.data.delete_vm(req).await {
Ok(()) => Ok(Response::new(Empty {})),
Err(e) => Err(Status::not_found(e.to_string())),
}
}
async fn airdrop(&self, req: Request<AirdropReq>) -> Result<Response<Empty>, Status> {
check_admin_key(&req)?;
let req = check_sig_from_req(req)?;
self.data.give_airdrop(&req.pubkey, req.tokens);
Ok(Response::new(Empty {}))
}
type ListAllVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
async fn list_all_vm_contracts(
&self,
req: Request<Empty>,
) -> Result<Response<Self::ListVmContractsStream>, Status> {
check_admin_key(&req)?;
let _ = check_sig_from_req(req)?;
let contracts = self.data.list_all_contracts();
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for contract in contracts {
let _ = tx.send(Ok(contract.into())).await;
}
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::ListVmContractsStream
))
}
type ListAccountsStream = Pin<Box<dyn Stream<Item = Result<Account, Status>> + Send>>;
async fn list_accounts(
&self,
req: Request<Empty>,
) -> Result<Response<Self::ListAccountsStream>, Status> {
check_admin_key(&req)?;
let _ = check_sig_from_req(req)?;
let accounts = self.data.list_accounts();
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for account in accounts {
let _ = tx.send(Ok(account.into())).await;
}
});
let output_stream = ReceiverStream::new(rx);
Ok(Response::new(
Box::pin(output_stream) as Self::ListAccountsStream
))
}
}
trait PubkeyGetter {
fn get_pubkey(&self) -> Option<String>;
}
impl PubkeyGetter for Pubkey {
fn get_pubkey(&self) -> Option<String> {
Some(self.pubkey.clone())
}
}
#[tonic::async_trait]
@ -431,3 +515,177 @@ impl BrainAppDaemon for BrainAppDaemonMock {
Ok(Response::new(detee_shared::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
}
}
fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Result<T, Status> {
let time = match req.metadata().get("timestamp") {
Some(t) => t.clone(),
None => return Err(Status::unauthenticated("Timestamp not found in metadata.")),
};
let time = time
.to_str()
.map_err(|_| Status::unauthenticated("Timestamp in metadata is not a string"))?;
let now = chrono::Utc::now();
let parsed_time = chrono::DateTime::parse_from_rfc3339(time)
.map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?;
let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds();
if seconds_elapsed > 1 || seconds_elapsed < -1 {
return Err(Status::unauthenticated(format!(
"Date is not within 1 sec of the time of the server: CLI {} vs Server {}",
parsed_time, now
)));
}
let signature = match req.metadata().get("request-signature") {
Some(t) => t,
None => return Err(Status::unauthenticated("signature not found in metadata.")),
};
let signature = bs58::decode(signature)
.into_vec()
.map_err(|_| Status::unauthenticated("signature is not a bs58 string"))?;
let signature = ed25519_dalek::Signature::from_bytes(
signature
.as_slice()
.try_into()
.map_err(|_| Status::unauthenticated("could not parse ed25519 signature"))?,
);
let pubkey_value = match req.metadata().get("pubkey") {
Some(p) => p.clone(),
None => return Err(Status::unauthenticated("pubkey not found in metadata.")),
};
let pubkey = ed25519_dalek::VerifyingKey::from_bytes(
&bs58::decode(&pubkey_value)
.into_vec()
.map_err(|_| Status::unauthenticated("pubkey is not a bs58 string"))?
.try_into()
.map_err(|_| Status::unauthenticated("pubkey does not have the correct size."))?,
)
.map_err(|_| Status::unauthenticated("could not parse ed25519 pubkey"))?;
let req = req.into_inner();
let message = format!("{time}{req:?}");
use ed25519_dalek::Verifier;
pubkey
.verify(message.as_bytes(), &signature)
.map_err(|_| Status::unauthenticated("the signature is not valid"))?;
if let Some(req_pubkey) = req.get_pubkey() {
if pubkey_value.to_str().unwrap().to_string() != req_pubkey {
return Err(Status::unauthenticated(
"pubkey of signature does not match pubkey of request",
));
}
}
Ok(req)
}
fn check_sig_from_parts(pubkey: &str, time: &str, msg: &str, sig: &str) -> Result<(), Status> {
let now = chrono::Utc::now();
let parsed_time = chrono::DateTime::parse_from_rfc3339(time)
.map_err(|_| Status::unauthenticated("Coult not parse timestamp"))?;
let seconds_elapsed = now.signed_duration_since(parsed_time).num_seconds();
if seconds_elapsed > 1 || seconds_elapsed < -1 {
return Err(Status::unauthenticated(format!(
"Date is not within 1 sec of the time of the server: CLI {} vs Server {}",
parsed_time, now
)));
}
let signature = bs58::decode(sig)
.into_vec()
.map_err(|_| Status::unauthenticated("signature is not a bs58 string"))?;
let signature = ed25519_dalek::Signature::from_bytes(
signature
.as_slice()
.try_into()
.map_err(|_| Status::unauthenticated("could not parse ed25519 signature"))?,
);
let pubkey = ed25519_dalek::VerifyingKey::from_bytes(
&bs58::decode(&pubkey)
.into_vec()
.map_err(|_| Status::unauthenticated("pubkey is not a bs58 string"))?
.try_into()
.map_err(|_| Status::unauthenticated("pubkey does not have the correct size."))?,
)
.map_err(|_| Status::unauthenticated("could not parse ed25519 pubkey"))?;
let msg = time.to_string() + msg;
use ed25519_dalek::Verifier;
pubkey
.verify(msg.as_bytes(), &signature)
.map_err(|_| Status::unauthenticated("the signature is not valid"))?;
Ok(())
}
fn check_admin_key<T>(req: &Request<T>) -> Result<(), Status> {
let pubkey = match req.metadata().get("pubkey") {
Some(p) => p.clone(),
None => return Err(Status::unauthenticated("pubkey not found in metadata.")),
};
let pubkey = pubkey
.to_str()
.map_err(|_| Status::unauthenticated("could not parse pubkey metadata to str"))?;
if !ADMIN_ACCOUNTS.contains(&pubkey) {
return Err(Status::unauthenticated(
"This operation is reserved to admin accounts",
));
}
Ok(())
}

@ -5,7 +5,7 @@ use data::BrainData;
use detee_shared::pb::brain::brain_app_cli_server::BrainAppCliServer;
use detee_shared::pb::brain::brain_app_daemon_server::BrainAppDaemonServer;
use grpc::snp_proto::brain_cli_server::BrainCliServer;
use grpc::snp_proto::brain_daemon_server::BrainDaemonServer;
use grpc::snp_proto::brain_vm_daemon_server::BrainVmDaemonServer;
use grpc::BrainAppCliMock;
use grpc::BrainAppDaemonMock;
use grpc::BrainCliMock;
@ -23,12 +23,12 @@ async fn main() {
tokio::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
data_clone.contracts_cron();
data_clone.vm_contracts_cron().await;
}
});
let addr = "0.0.0.0:31337".parse().unwrap();
let daemon_server = BrainDaemonServer::new(BrainDaemonMock::new(data.clone()));
let daemon_server = BrainVmDaemonServer::new(BrainDaemonMock::new(data.clone()));
let cli_server = BrainCliServer::new(BrainCliMock::new(data.clone()));
let sgx_cli_server = BrainAppCliServer::new(BrainAppCliMock::new(data.clone()));

@ -1,5 +1,5 @@
syntax = "proto3";
package snp_proto;
package vm_proto;
message Empty {
}
@ -13,7 +13,7 @@ message AccountBalance {
uint64 tmp_locked = 2;
}
message Contract {
message VmContract {
string uuid = 1;
string hostname = 2;
string admin_pubkey = 3;
@ -28,7 +28,7 @@ message Contract {
string dtrfs_sha = 12;
string created_at = 13;
string updated_at = 14;
// total nanotoken cost per minute (for all units)
// total nanoLP cost per minute (for all units)
uint64 nano_per_minute = 15;
uint64 locked_nano = 16;
string collected_at = 17;
@ -52,18 +52,19 @@ message MeasurementIP {
string gateway = 4;
}
message RegisterNodeReq {
// This should also include a block hash or similar, for auth
message RegisterVmNodeReq {
string node_pubkey = 1;
string owner_pubkey = 2;
string main_ip = 3;
string country = 4;
string region = 5;
string city = 6;
// nanotokens per unit per minute
// nanoLP per unit per minute
uint64 price = 7;
}
message NodeResources {
message VmNodeResources {
string node_pubkey = 1;
uint32 avail_ports = 2;
uint32 avail_ipv4 = 3;
@ -101,13 +102,14 @@ message NewVmResp {
message UpdateVmReq {
string uuid = 1;
uint32 disk_size_gb = 2;
uint32 vcpus = 3;
uint32 memory_mb = 4;
string kernel_url = 5;
string kernel_sha = 6;
string dtrfs_url = 7;
string dtrfs_sha = 8;
string admin_pubkey = 2;
uint32 disk_size_gb = 3;
uint32 vcpus = 4;
uint32 memory_mb = 5;
string kernel_url = 6;
string kernel_sha = 7;
string dtrfs_url = 8;
string dtrfs_sha = 9;
}
message UpdateVmResp {
@ -118,9 +120,10 @@ message UpdateVmResp {
message DeleteVmReq {
string uuid = 1;
string admin_pubkey = 2;
}
message BrainMessage {
message BrainVmMessage {
oneof Msg {
NewVmReq new_vm_req = 1;
UpdateVmReq update_vm_req = 2;
@ -128,28 +131,35 @@ message BrainMessage {
}
}
message DaemonMessage {
message DaemonStreamAuth {
string timestamp = 1;
string pubkey = 2;
repeated string contracts = 3;
string signature = 4;
}
message VmDaemonMessage {
oneof Msg {
Pubkey pubkey = 1;
DaemonStreamAuth auth = 1;
NewVmResp new_vm_resp = 2;
UpdateVmResp update_vm_resp = 3;
NodeResources node_resources = 4;
VmNodeResources vm_node_resources = 4;
}
}
service BrainDaemon {
rpc RegisterNode (RegisterNodeReq) returns (stream Contract);
rpc BrainMessages (Pubkey) returns (stream BrainMessage);
rpc DaemonMessages (stream DaemonMessage) returns (Empty);
service BrainVmDaemon {
rpc RegisterVmNode (RegisterVmNodeReq) returns (stream VmContract);
rpc BrainMessages (DaemonStreamAuth) returns (stream BrainVmMessage);
rpc DaemonMessages (stream VmDaemonMessage) returns (Empty);
}
message ListContractsReq {
message ListVmContractsReq {
string admin_pubkey = 1;
string node_pubkey = 2;
string uuid = 3;
}
message NodeFilters {
message VmNodeFilters {
uint32 free_ports = 1;
bool offers_ipv4 = 2;
bool offers_ipv6 = 3;
@ -163,7 +173,7 @@ message NodeFilters {
string node_pubkey = 11;
}
message NodeListResp {
message VmNodeListResp {
string node_pubkey = 1;
string country = 2;
string region = 3;
@ -171,7 +181,7 @@ message NodeListResp {
string ip = 5; // required for latency test
uint32 server_rating = 6;
uint32 provider_rating = 7;
// nanotokens per unit per minute
// nanoLP per unit per minute
uint64 price = 8;
}
@ -181,14 +191,28 @@ message ExtendVmReq {
uint64 locked_nano = 3;
}
message AirdropReq {
string pubkey = 1;
uint64 tokens = 2;
}
message Account {
string pubkey = 1;
uint64 balance = 2;
uint64 tmp_locked = 3;
}
service BrainCli {
rpc GetAirdrop (Pubkey) returns (Empty);
rpc GetBalance (Pubkey) returns (AccountBalance);
rpc NewVm (NewVmReq) returns (NewVmResp);
rpc ListContracts (ListContractsReq) returns (stream Contract);
rpc ListNodes (NodeFilters) returns (stream NodeListResp);
rpc GetOneNode (NodeFilters) returns (NodeListResp);
rpc ListVmContracts (ListVmContractsReq) returns (stream VmContract);
rpc ListVmNodes (VmNodeFilters) returns (stream VmNodeListResp);
rpc GetOneVmNode (VmNodeFilters) returns (VmNodeListResp);
rpc DeleteVm (DeleteVmReq) returns (Empty);
rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp);
rpc ExtendVm (ExtendVmReq) returns (Empty);
// admin commands
rpc Airdrop (AirdropReq) returns (Empty);
rpc ListAllVmContracts (Empty) returns (stream VmContract);
rpc ListAccounts (Empty) returns (stream Account);
}