diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..90f0431 --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1 @@ +target/** diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..2c6b9ee --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,1488 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + +[[package]] +name = "api" +version = "0.1.0" +dependencies = [ + "async-trait", + "bytes", + "prost", + "prost-build", + "rand", + "reqwest", +] + +[[package]] +name = "async-trait" +version = "0.1.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" + +[[package]] +name = "cc" +version = "1.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9540e661f81799159abee814118cc139a2004b3a3aa3ea37724a1b66530b90e0" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-io", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" + +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.1.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "0.14.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-rustls" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" +dependencies = [ + "futures-util", + "http 0.2.12", + "hyper 0.14.30", + "rustls", + "tokio", + "tokio-rustls", +] + +[[package]] +name = "hyper-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "hyper 1.4.1", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "impls" +version = "0.1.0" +dependencies = [ + "api", + "async-trait", +] + +[[package]] +name = "indexmap" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipnet" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "multimap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" + +[[package]] +name = "object" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "petgraph" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" +dependencies = [ + "proc-macro2", + "syn 1.0.109", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prost" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270" +dependencies = [ + "bytes", + "heck", + "itertools", + "lazy_static", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "regex", + "syn 1.0.109", + "tempfile", + "which", +] + +[[package]] +name = "prost-derive" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "prost-types" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213622a1460818959ac1181aaeb2dc9c7f63df720db7d788b3e24eacd1983e13" +dependencies = [ + "prost", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.30", + "hyper-rustls", + "ipnet", + "js-sys", + "log", + "mime", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-rustls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", + "winreg", +] + +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustix" +version = "0.38.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustls" +version = "0.21.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" +dependencies = [ + "log", + "ring", + "rustls-webpki", + "sct", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64", +] + +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "server" +version = "0.1.0" +dependencies = [ + "api", + "bytes", + "http-body-util", + "hyper 1.4.1", + "hyper-util", + "impls", + "prost", + "tokio", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "tempfile" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +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.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + +[[package]] +name = "tokio-rustls" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.79", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + +[[package]] +name = "web-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" + +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..90843ca --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,12 @@ +[workspace] +resolver = "2" +members = ["server", "api", "impls"] +default-members = ["server"] + +[profile.release] +panic = "abort" +opt-level = 3 +lto = true + +[profile.dev] +panic = "abort" diff --git a/rust/README.md b/rust/README.md new file mode 100644 index 0000000..03d99ca --- /dev/null +++ b/rust/README.md @@ -0,0 +1,2 @@ +## WIP +Not ready for use !! \ No newline at end of file diff --git a/rust/api/.gitignore b/rust/api/.gitignore new file mode 100644 index 0000000..a3cf438 --- /dev/null +++ b/rust/api/.gitignore @@ -0,0 +1,3 @@ +Cargo.lock +target/** +src/proto/** diff --git a/rust/api/Cargo.toml b/rust/api/Cargo.toml new file mode 100644 index 0000000..f45614e --- /dev/null +++ b/rust/api/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "api" +version = "0.1.0" +edition = "2021" + +[dependencies] +async-trait = "0.1.77" +prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] } +bytes = "1.4.0" +rand = { version = "0.8.5", optional = true} + +[target.'cfg(genproto)'.build-dependencies] +prost-build = { version = "0.11.3" } +reqwest = { version = "0.11.13", default-features = false, features = ["rustls-tls", "blocking"] } + +[dev-dependencies] + +[features] +_test_utils =["rand"] diff --git a/rust/api/build.rs b/rust/api/build.rs new file mode 100644 index 0000000..c916832 --- /dev/null +++ b/rust/api/build.rs @@ -0,0 +1,35 @@ +#[cfg(genproto)] +extern crate prost_build; +#[cfg(genproto)] +use std::{env, fs, fs::File, path::Path}; + +/// To generate updated proto objects, run `RUSTFLAGS="--cfg genproto" cargo build` +fn main() { + #[cfg(genproto)] + generate_protos(); +} + +#[cfg(genproto)] +fn generate_protos() { + download_file( + "https://raw.githubusercontent.com/lightningdevkit/vss-server/7f492fcac0c561b212f49ca40f7d16075822440f/app/src/main/proto/vss.proto", + "src/proto/vss.proto", + ).unwrap(); + + prost_build::Config::new() + .bytes(&["."]) + .compile_protos(&["src/proto/vss.proto"], &["src/"]) + .expect("protobuf compilation failed"); + println!("OUT_DIR: {}", &env::var("OUT_DIR").unwrap()); + let from_path = Path::new(&env::var("OUT_DIR").unwrap()).join("vss.rs"); + fs::copy(from_path, "src/types.rs").unwrap(); +} + +#[cfg(genproto)] +fn download_file(url: &str, save_to: &str) -> Result<(), Box> { + let mut response = reqwest::blocking::get(url)?; + fs::create_dir_all(Path::new(save_to).parent().unwrap())?; + let mut out_file = File::create(save_to)?; + response.copy_to(&mut out_file)?; + Ok(()) +} diff --git a/rust/api/src/auth.rs b/rust/api/src/auth.rs new file mode 100644 index 0000000..5b742ba --- /dev/null +++ b/rust/api/src/auth.rs @@ -0,0 +1,35 @@ +use crate::error::VssError; +use async_trait::async_trait; +use std::collections::HashMap; +use std::string::ToString; + +/// Response returned for [`Authorizer`] request if user is authenticated and authorized. +#[derive(Debug, Clone)] +pub struct AuthResponse { + /// A `user_token` identifying the authenticated and authorized user. + pub user_token: String, +} + +/// Interface for authorizer that is run before executing every request. +#[async_trait] +pub trait Authorizer: Send + Sync { + /// Verifies authentication and authorization based on request headers. + /// Returns [`AuthResponse`] for an authenticated and authorized user or [`VssError::AuthError`] + /// for an unauthorized request. + async fn verify(&self, headers_map: &HashMap) + -> Result; +} + +/// A no-operation authorizer, which lets any user-request go through. +pub struct NoopAuthorizer {} + +const UNAUTHENTICATED_USER: &str = "unauth-user"; + +#[async_trait] +impl Authorizer for NoopAuthorizer { + async fn verify( + &self, _headers_map: &HashMap, + ) -> Result { + Ok(AuthResponse { user_token: UNAUTHENTICATED_USER.to_string() }) + } +} diff --git a/rust/api/src/error.rs b/rust/api/src/error.rs new file mode 100644 index 0000000..018827a --- /dev/null +++ b/rust/api/src/error.rs @@ -0,0 +1,65 @@ +use std::error::Error; +use std::fmt::{Display, Formatter}; +use std::io; +/// When there is an error while writing to VSS storage, the response contains a relevant error code. +/// A mapping for VSS server error codes. Refer to [`ErrorResponse`] docs for more +/// information regarding each error code and corresponding use-cases. +/// +/// [`ErrorResponse`]: crate::types::ErrorResponse +#[derive(Debug)] +pub enum VssError { + /// Please refer to [`ErrorCode::NoSuchKeyException`]. + /// + /// [`ErrorCode::NoSuchKeyException`]: crate::types::ErrorCode::NoSuchKeyException + NoSuchKeyError(String), + + /// Please refer to [`ErrorCode::InvalidRequestException`]. + /// + /// [`ErrorCode::InvalidRequestException`]: crate::types::ErrorCode::InvalidRequestException + InvalidRequestError(String), + + /// Please refer to [`ErrorCode::ConflictException`]. + /// + /// [`ErrorCode::ConflictException`]: crate::types::ErrorCode::ConflictException + ConflictError(String), + + /// Please refer to [`ErrorCode::AuthException`]. + /// + /// [`ErrorCode::AuthException`]: crate::types::ErrorCode::AuthException + AuthError(String), + + /// Please refer to [`ErrorCode::InternalServerException`]. + /// + /// [`ErrorCode::InternalServerException`]: crate::types::ErrorCode::InternalServerException + InternalServerError(String), +} + +impl Display for VssError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + VssError::NoSuchKeyError(message) => { + write!(f, "Requested key does not exist: {}", message) + }, + VssError::InvalidRequestError(message) => { + write!(f, "Request sent to VSS was invalid: {}", message) + }, + VssError::ConflictError(message) => { + write!(f, "Version conflict in write operation: {}", message) + }, + VssError::AuthError(message) => { + write!(f, "Authentication or Authorization failure: {}", message) + }, + VssError::InternalServerError(message) => { + write!(f, "InternalServerError: {}", message) + }, + } + } +} + +impl Error for VssError {} + +impl From for VssError { + fn from(err: io::Error) -> Self { + VssError::InternalServerError(err.to_string()) + } +} diff --git a/rust/api/src/kv_store.rs b/rust/api/src/kv_store.rs new file mode 100644 index 0000000..de016ec --- /dev/null +++ b/rust/api/src/kv_store.rs @@ -0,0 +1,36 @@ +use crate::error::VssError; +use crate::types::{ + DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse, + ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse, +}; +use async_trait::async_trait; + +/// The key used to store and retrieve the global version of the store. +pub const GLOBAL_VERSION_KEY: &str = "global_version"; + +/// The initial version number assigned to newly created records. +pub const INITIAL_RECORD_VERSION: i32 = 1; + +/// An interface that must be implemented by every backend implementation of VSS. +#[async_trait] +pub trait KvStore: Send + Sync { + /// Retrieves an object based on the provided request and user token. + async fn get( + &self, user_token: String, request: GetObjectRequest, + ) -> Result; + + /// Stores an object with the provided request and user token. + async fn put( + &self, user_token: String, request: PutObjectRequest, + ) -> Result; + + /// Deletes an object based on the provided request and user token. + async fn delete( + &self, user_token: String, request: DeleteObjectRequest, + ) -> Result; + + /// Lists the versions of keys based on the provided request and user token. + async fn list_key_versions( + &self, user_token: String, request: ListKeyVersionsRequest, + ) -> Result; +} diff --git a/rust/api/src/kv_store_tests.rs b/rust/api/src/kv_store_tests.rs new file mode 100644 index 0000000..b1f998d --- /dev/null +++ b/rust/api/src/kv_store_tests.rs @@ -0,0 +1,615 @@ +use crate::error::VssError; +use crate::kv_store::{KvStore, GLOBAL_VERSION_KEY}; +use crate::types::{ + DeleteObjectRequest, GetObjectRequest, KeyValue, ListKeyVersionsRequest, + ListKeyVersionsResponse, PutObjectRequest, +}; +use async_trait::async_trait; +use bytes::Bytes; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; + +/// Defines KvStoreTestSuite which is required for an implementation to be VSS protocol compliant. +#[macro_export] +macro_rules! define_kv_store_tests { + ($test_suite_name:ident, $store_type:path, $create_store_expr: expr) => { + use crate::api::error::VssError; + use crate::api::kv_store_tests::KvStoreTestSuite; + use async_trait::async_trait; + struct $test_suite_name; + + #[async_trait] + impl KvStoreTestSuite for $test_suite_name { + type Store = $store_type; + + async fn create_store() -> Self::Store { + $create_store_expr + } + } + + macro_rules! create_test { + ($test_fn:ident) => { + #[tokio::test] + async fn $test_fn() -> Result<(), VssError> { + $test_suite_name::$test_fn().await?; + Ok(()) + } + }; + } + + create_test!(put_should_succeed_when_single_object_put_operation); + create_test!(put_should_succeed_when_multi_object_put_operation); + create_test!(put_should_fail_when_key_version_mismatched); + create_test!(put_multi_object_should_fail_when_single_key_version_mismatched); + create_test!(put_should_fail_when_global_version_mismatched); + create_test!(put_should_succeed_when_no_global_version_is_given); + create_test!(put_and_delete_should_succeed_as_atomic_transaction); + create_test!(delete_should_succeed_when_item_exists); + create_test!(delete_should_succeed_when_item_does_not_exist); + create_test!(delete_should_be_idempotent); + create_test!(get_should_throw_no_such_key_exception_when_key_does_not_exist); + create_test!(get_should_return_correct_value_when_key_exists); + create_test!(list_should_return_paginated_response); + create_test!(list_should_honour_page_size_and_key_prefix_if_provided); + create_test!(list_should_return_zero_global_version_when_global_versioning_not_enabled); + create_test!(list_should_limit_max_page_size); + }; +} + +/// Contains tests for a [`KvStore`] implementation to ensure it complies with the VSS protocol. +#[allow(missing_docs)] +#[async_trait] +pub trait KvStoreTestSuite { + /// The type of store being tested. This must implement the [`KvStore`] trait. + type Store: KvStore + 'static; + + /// Creates and returns a new instance of the store to be tested. + async fn create_store() -> Self::Store; + + async fn put_should_succeed_when_single_object_put_operation() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + // Conditional Put + ctx.put_objects(Some(0), vec![kv("k1", "k1v1", 0)]).await?; + + ctx.put_objects(Some(1), vec![kv("k1", "k1v2", 1)]).await?; + + // Non-conditional Put + ctx.put_objects(Some(2), vec![kv("k2", "k2v1", -1)]).await?; + ctx.put_objects(Some(3), vec![kv("k2", "k2v2", -1)]).await?; + ctx.put_objects(Some(4), vec![kv("k2", "k2v3", -1)]).await?; + + // Get object k1 + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k1v2")); + + // Get object k2 + let response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k2v3")); + + // Get GLOBAL_VERSION_KEY + let response = ctx.get_object(GLOBAL_VERSION_KEY).await?; + assert_eq!(response.version, 5); + Ok(()) + } + + async fn put_should_succeed_when_multi_object_put_operation() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let key_values = vec![kv("k1", "k1v1", 0), kv("k2", "k2v1", 0)]; + + ctx.put_objects(Some(0), key_values).await?; + + let second_request = vec![kv("k1", "k1v2", 1), kv("k2", "k2v2", 1)]; + ctx.put_objects(Some(1), second_request).await?; + + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k1v2")); + + let response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k2v2")); + + let response = ctx.get_object(GLOBAL_VERSION_KEY).await?; + assert_eq!(response.version, 2); + + Ok(()) + } + + async fn put_should_fail_when_key_version_mismatched() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + // Initial put + ctx.put_objects(Some(0), vec![kv("k1", "k1v1", 0)]).await?; + + // Attempt to put with mismatched key version + let result = ctx.put_objects(Some(1), vec![kv("k1", "k1v2", 0)]).await; + + assert!(matches!(result, Err(VssError::ConflictError(..)))); + + // Verify values didn't change + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k1v1")); + + let response = ctx.get_object(GLOBAL_VERSION_KEY).await?; + assert_eq!(response.version, 1); + Ok(()) + } + + async fn put_multi_object_should_fail_when_single_key_version_mismatched( + ) -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let key_values = vec![kv("k1", "k1v1", 0), kv("k2", "k2v1", 0)]; + + ctx.put_objects(None, key_values).await?; + + let second_request = vec![kv("k1", "k1v2", 0), kv("k2", "k2v2", 1)]; + + // Should throw ConflictError due to key-version mismatch on "k1" + let result = ctx.put_objects(None, second_request).await; + assert!(matches!(result, Err(VssError::ConflictError(..)))); + + // Verify values didn't change + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k1v1")); + + let response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k2v1")); + + Ok(()) + } + + async fn put_should_fail_when_global_version_mismatched() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(Some(0), vec![kv("k1", "k1v1", 0)]).await?; + + // Should throw ConflictError due to global_version mismatch + let result = ctx.put_objects(Some(0), vec![kv("k1", "k1v2", 1)]).await; + assert!(matches!(result, Err(VssError::ConflictError(_)))); + + // Verify values didn't change + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k1v1")); + + Ok(()) + } + + async fn put_should_succeed_when_no_global_version_is_given() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(None, vec![kv("k1", "k1v1", 0)]).await?; + ctx.put_objects(None, vec![kv("k1", "k1v2", 1)]).await?; + + let response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k1v2")); + + let response = ctx.get_object(GLOBAL_VERSION_KEY).await?; + assert_eq!(response.version, 0); + + Ok(()) + } + + async fn put_and_delete_should_succeed_as_atomic_transaction() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(None, vec![kv("k1", "k1v1", 0)]).await?; + + // Put and Delete succeeds + ctx.put_and_delete_objects(None, vec![kv("k2", "k2v1", 0)], vec![kv("k1", "", 1)]).await?; + + let response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k2v1")); + + // "k1" should be deleted + let result = ctx.get_object("k1").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + + // Delete fails (and hence put as well) due to mismatched version for the deleted item. + let result = ctx + .put_and_delete_objects(None, vec![kv("k3", "k3v1", 0)], vec![kv("k2", "", 3)]) + .await; + assert!(matches!(result, Err(VssError::ConflictError(_)))); + + // Verify "k3" was not inserted and "k2" still exists + let result = ctx.get_object("k3").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + ctx.get_object("k2").await?; + + // Put fails (and hence delete as well) due to mismatched version for the put item. + let result = ctx + .put_and_delete_objects(None, vec![kv("k3", "k3v1", 1)], vec![kv("k2", "", 1)]) + .await; + assert!(matches!(result, Err(VssError::ConflictError(_)))); + + // Put and delete both fail due to mismatched global version. + let result = ctx + .put_and_delete_objects(Some(2), vec![kv("k3", "k3v1", 0)], vec![kv("k2", "", 1)]) + .await; + assert!(matches!(result, Err(VssError::ConflictError(_)))); + + // Verify "k3" was not inserted and "k2" still exists + let result = ctx.get_object("k3").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + ctx.get_object("k2").await?; + + let response = ctx.get_object(GLOBAL_VERSION_KEY).await?; + assert_eq!(response.version, 0); + + Ok(()) + } + + async fn delete_should_succeed_when_item_exists() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(None, vec![kv("k1", "k1v1", 0)]).await?; + + // Conditional Delete + ctx.delete_object(kv("k1", "", 1)).await?; + + let result = ctx.get_object("k1").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + + ctx.put_objects(None, vec![kv("k1", "k1v1", 0)]).await?; + ctx.put_objects(None, vec![kv("k1", "k1v2", 1)]).await?; + + // Non-conditional Delete + ctx.delete_object(kv("k1", "", -1)).await?; + + let result = ctx.get_object("k1").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + + Ok(()) + } + + async fn delete_should_succeed_when_item_does_not_exist() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.delete_object(kv("non_existent_key", "", 0)).await?; + + Ok(()) + } + + async fn delete_should_be_idempotent() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(None, vec![kv("k1", "k1v1", 0)]).await?; + ctx.delete_object(kv("k1", "", 1)).await?; + ctx.delete_object(kv("k1", "", 1)).await?; + + let result = ctx.get_object("k1").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + + Ok(()) + } + + async fn get_should_throw_no_such_key_exception_when_key_does_not_exist() -> Result<(), VssError> + { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let result = ctx.get_object("non_existent_key").await; + assert!(matches!(result, Err(VssError::NoSuchKeyError(_)))); + + Ok(()) + } + async fn get_should_return_correct_value_when_key_exists() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + ctx.put_objects(Some(0), vec![kv("k1", "k1v1", 0)]).await?; + + let mut response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k1v1")); + + let key_values = vec![kv("k1", "k1v2", 1), kv("k2", "k2v1", 0)]; + ctx.put_objects(Some(1), key_values).await?; + + response = ctx.get_object("k1").await?; + assert_eq!(response.key, "k1"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k1v2")); + + response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k2v1")); + + let key_values = vec![kv("k2", "k2v2", 1), kv("k3", "k3v1", 0)]; + ctx.put_objects(Some(2), key_values).await?; + + response = ctx.get_object("k2").await?; + assert_eq!(response.key, "k2"); + assert_eq!(response.version, 2); + assert_eq!(response.value, Bytes::from("k2v2")); + + response = ctx.get_object("k3").await?; + assert_eq!(response.key, "k3"); + assert_eq!(response.version, 1); + assert_eq!(response.value, Bytes::from("k3v1")); + + Ok(()) + } + + async fn list_should_return_paginated_response() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let total_kv_objects = 1000; + for i in 0..total_kv_objects { + ctx.put_objects(Some(i as i64), vec![kv(&format!("k{}", i), "k1v1", 0)]).await?; + } + + ctx.put_objects(Some(1000), vec![kv("k1", "k1v2", 1)]).await?; + ctx.put_objects(Some(1001), vec![kv("k2", "k2v2", 1)]).await?; + ctx.put_objects(Some(1002), vec![kv("k2", "k2v3", 2)]).await?; + + let mut next_page_token: Option = None; + let mut all_key_versions: Vec = Vec::new(); + + loop { + let current_page = match next_page_token.take() { + None => { + let page = ctx.list(None, None, None).await?; + assert_eq!(page.global_version, Some(1003)); + page + }, + Some(next_page_token) => { + let page = ctx.list(Some(next_page_token), None, None).await?; + assert!(page.global_version.is_none()); + page + }, + }; + + if current_page.key_versions.is_empty() { + break; + } + + all_key_versions.extend(current_page.key_versions); + next_page_token = current_page.next_page_token; + } + + if let Some(k1_response) = all_key_versions.iter().find(|kv| kv.key == "k1") { + assert_eq!(k1_response.key, "k1"); + assert_eq!(k1_response.version, 2); + assert_eq!(k1_response.value, Bytes::new()); + } + + if let Some(k2_response) = all_key_versions.iter().find(|kv| kv.key == "k2") { + assert_eq!(k2_response.key, "k2"); + assert_eq!(k2_response.version, 3); + assert_eq!(k2_response.value, Bytes::new()); + } + + let unique_keys: std::collections::HashSet = + all_key_versions.into_iter().map(|kv| kv.key).collect(); + assert_eq!(unique_keys.len(), total_kv_objects as usize); + assert!(!unique_keys.contains(GLOBAL_VERSION_KEY)); + + Ok(()) + } + + async fn list_should_honour_page_size_and_key_prefix_if_provided() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let total_kv_objects = 20; + let page_size = 5; + for i in 0..total_kv_objects { + ctx.put_objects(Some(i as i64), vec![kv(&format!("{}k", i), "k1v1", 0)]).await?; + } + + let mut next_page_token: Option = None; + let mut all_key_versions: Vec = Vec::new(); + let key_prefix = "1"; + + loop { + let current_page = match next_page_token.take() { + None => ctx.list(None, Some(page_size), Some(key_prefix.to_string())).await?, + Some(next_page_token) => { + ctx.list(Some(next_page_token), Some(page_size), Some(key_prefix.to_string())) + .await? + }, + }; + + if current_page.key_versions.is_empty() { + break; + } + + assert!(current_page.key_versions.len() <= page_size as usize); + all_key_versions.extend(current_page.key_versions); + next_page_token = current_page.next_page_token; + } + + let unique_keys: std::collections::HashSet = + all_key_versions.into_iter().map(|kv| kv.key).collect(); + + assert_eq!(unique_keys.len(), 11); + let expected_keys: std::collections::HashSet = + ["1k", "10k", "11k", "12k", "13k", "14k", "15k", "16k", "17k", "18k", "19k"] + .into_iter() + .map(|s| s.to_string()) + .collect(); + assert_eq!(unique_keys, expected_keys); + + Ok(()) + } + + async fn list_should_return_zero_global_version_when_global_versioning_not_enabled( + ) -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let total_kv_objects = 1000; + for i in 0..total_kv_objects { + ctx.put_objects(None, vec![kv(&format!("k{}", i), "k1v1", 0)]).await?; + } + + let mut next_page_token: Option = None; + let mut all_key_versions: Vec = Vec::new(); + + loop { + let current_page = match next_page_token.take() { + None => { + let page = ctx.list(None, None, None).await?; + assert_eq!(page.global_version.unwrap_or(0), 0); + page + }, + Some(next_page_token) => ctx.list(Some(next_page_token), None, None).await?, + }; + + if current_page.key_versions.is_empty() { + break; + } + + all_key_versions.extend(current_page.key_versions); + next_page_token = current_page.next_page_token; + } + + let unique_keys: std::collections::HashSet = + all_key_versions.into_iter().map(|kv| kv.key).collect(); + assert_eq!(unique_keys.len(), total_kv_objects as usize); + assert!(!unique_keys.contains(GLOBAL_VERSION_KEY)); + + Ok(()) + } + + async fn list_should_limit_max_page_size() -> Result<(), VssError> { + let kv_store = Self::create_store().await; + let ctx = TestContext::new(&kv_store); + + let total_kv_objects = 10_000; + let vss_arbitrary_page_size_max = 3000; + for i in 0..total_kv_objects { + ctx.put_objects(Some(i as i64), vec![kv(&format!("k{}", i), "k1v1", 0)]).await?; + } + + let mut next_page_token: Option = None; + let mut all_key_versions: Vec = Vec::new(); + + loop { + let current_page = match next_page_token.take() { + None => ctx.list(None, None, None).await?, + Some(next_page_token) => ctx.list(Some(next_page_token), None, None).await?, + }; + if current_page.key_versions.is_empty() { + break; + } + + assert!( + current_page.key_versions.len() < vss_arbitrary_page_size_max as usize, + "Page size exceeds the maximum allowed size" + ); + all_key_versions.extend(current_page.key_versions); + next_page_token = current_page.next_page_token; + } + + assert_eq!(all_key_versions.len(), total_kv_objects as usize); + + Ok(()) + } +} + +/// Represents the context used for testing [`KvStore`] operations. +pub struct TestContext<'a> { + kv_store: &'a dyn KvStore, + user_token: String, + store_id: String, +} + +impl<'a> TestContext<'a> { + /// Creates a new [`TestContext`] with the given [`KvStore`] implementation. + pub fn new(kv_store: &'a dyn KvStore) -> Self { + let store_id: String = (0..7).map(|_| thread_rng().sample(Alphanumeric) as char).collect(); + TestContext { kv_store, user_token: "userToken".to_string(), store_id } + } + + async fn get_object(&self, key: &str) -> Result { + let request = GetObjectRequest { store_id: self.store_id.clone(), key: key.to_string() }; + let response = self.kv_store.get(self.user_token.clone(), request).await?; + Ok(response.value.unwrap()) + } + + async fn put_objects( + &self, global_version: Option, key_values: Vec, + ) -> Result<(), VssError> { + let request = PutObjectRequest { + store_id: self.store_id.clone(), + transaction_items: key_values, + delete_items: vec![], + global_version, + }; + self.kv_store.put(self.user_token.clone(), request).await?; + Ok(()) + } + + async fn put_and_delete_objects( + &self, global_version: Option, put_key_values: Vec, + delete_key_values: Vec, + ) -> Result<(), VssError> { + let request = PutObjectRequest { + store_id: self.store_id.clone(), + transaction_items: put_key_values, + delete_items: delete_key_values, + global_version, + }; + self.kv_store.put(self.user_token.clone(), request).await?; + Ok(()) + } + + async fn delete_object(&self, key_value: KeyValue) -> Result<(), VssError> { + let request = + DeleteObjectRequest { store_id: self.store_id.clone(), key_value: Some(key_value) }; + self.kv_store.delete(self.user_token.clone(), request).await?; + Ok(()) + } + + async fn list( + &self, next_page_token: Option, page_size: Option, key_prefix: Option, + ) -> Result { + let request = ListKeyVersionsRequest { + store_id: self.store_id.clone(), + page_token: next_page_token, + page_size, + key_prefix, + }; + let response = self.kv_store.list_key_versions(self.user_token.clone(), request).await?; + Ok(response) + } +} + +fn kv(key: &str, value: &str, version: i64) -> KeyValue { + KeyValue { key: key.to_string(), version, value: Bytes::from(value.to_string()) } +} diff --git a/rust/api/src/lib.rs b/rust/api/src/lib.rs new file mode 100644 index 0000000..8da2680 --- /dev/null +++ b/rust/api/src/lib.rs @@ -0,0 +1,27 @@ +//! Hosts API contract for VSS. +//! +//! VSS is an open-source project designed to offer a server-side cloud storage solution specifically +//! tailored for noncustodial Lightning supporting mobile wallets. Its primary objective is to +//! simplify the development process for Lightning wallets by providing a secure means to store +//! and manage the essential state required for Lightning Network (LN) operations. + +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![deny(missing_docs)] + +/// Contains interface for authorizer that is run before every request, and its corresponding implementations. +pub mod auth; +/// Implements the error type ([`error::VssError`]) which is eventually converted to [`ErrorResponse`] and returned to the client. +/// +/// [`ErrorResponse`]: types::ErrorResponse +pub mod error; + +/// Contains [`kv_store::KvStore`] interface which needs to be implemented by every backend implementation of VSS. +pub mod kv_store; + +/// Contains request/response types generated from the API definition of VSS. +pub mod types; + +#[cfg(feature = "_test_utils")] +/// Defines [`kv_store_tests::KvStoreTestSuite`] which is required for an implementation to be VSS protocol compliant. +pub mod kv_store_tests; diff --git a/rust/api/src/types.rs b/rust/api/src/types.rs new file mode 100644 index 0000000..c4b79c3 --- /dev/null +++ b/rust/api/src/types.rs @@ -0,0 +1,381 @@ +/// Request payload to be used for `GetObject` API call to server. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct GetObjectRequest { + /// `store_id` is a keyspace identifier. + /// Ref: ) + /// All APIs operate within a single `store_id`. + /// It is up to clients to use single or multiple stores for their use-case. + /// This can be used for client-isolation/ rate-limiting / throttling on the server-side. + /// Authorization and billing can also be performed at the `store_id` level. + #[prost(string, tag = "1")] + pub store_id: ::prost::alloc::string::String, + /// The key of the value to be fetched. + /// + /// If the specified `key` does not exist, returns `ErrorCode.NO_SUCH_KEY_EXCEPTION` in the + /// the `ErrorResponse`. + /// + /// Consistency Guarantee: + /// Get(read) operations against a `key` are consistent reads and will reflect all previous writes, + /// since Put/Write provides read-after-write and read-after-update consistency guarantees. + /// + /// Read Isolation: + /// Get/Read operations against a `key` are ensured to have read-committed isolation. + /// Ref: )#Read_committed + #[prost(string, tag = "2")] + pub key: ::prost::alloc::string::String, +} +/// Server response for `GetObject` API. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct GetObjectResponse { + /// Fetched `value` and `version` along with the corresponding `key` in the request. + #[prost(message, optional, tag = "2")] + pub value: ::core::option::Option, +} +/// Request payload to be used for `PutObject` API call to server. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct PutObjectRequest { + /// `store_id` is a keyspace identifier. + /// Ref: ) + /// All APIs operate within a single `store_id`. + /// It is up to clients to use single or multiple stores for their use-case. + /// This can be used for client-isolation/ rate-limiting / throttling on the server-side. + /// Authorization and billing can also be performed at the `store_id` level. + #[prost(string, tag = "1")] + pub store_id: ::prost::alloc::string::String, + /// `global_version` is a sequence-number/version of the whole store. This can be used for versioning + /// and ensures that multiple updates in case of multiple devices can only be done linearly, even + /// if those updates did not directly conflict with each other based on keys/`transaction_items`. + /// + /// If present, the write will only succeed if the current server-side `global_version` against + /// the `store_id` is same as in the request. + /// Clients are expected to store (client-side) the global version against `store_id`. + /// The request must contain their client-side value of `global_version` if global versioning and + /// conflict detection is desired. + /// + /// For the first write of the store, global version should be '0'. If the write succeeds, clients + /// must increment their global version (client-side) by 1. + /// The server increments `global_version` (server-side) for every successful write, hence this + /// client-side increment is required to ensure matching versions. This updated global version + /// should be used in subsequent `PutObjectRequest`s for the store. + /// + /// Requests with a conflicting version will fail with `CONFLICT_EXCEPTION` as ErrorCode. + #[prost(int64, optional, tag = "2")] + pub global_version: ::core::option::Option, + /// Items to be written as a result of this `PutObjectRequest`. + /// + /// In an item, each `key` is supplied with its corresponding `value` and `version`. + /// Clients can choose to encrypt the keys client-side in order to obfuscate their usage patterns. + /// If the write is successful, the previous `value` corresponding to the `key` will be overwritten. + /// + /// Multiple items in `transaction_items` and `delete_items` of a single `PutObjectRequest` are written in + /// a database-transaction in an all-or-nothing fashion. + /// All Items in a single `PutObjectRequest` must have distinct keys. + /// + /// Key-level versioning (Conditional Write): + /// Clients are expected to store a `version` against every `key`. + /// The write will succeed if the current DB version against the `key` is the same as in the request. + /// When initiating a `PutObjectRequest`, the request should contain their client-side `version` + /// for that key-value. + /// + /// For the first write of any `key`, the `version` should be '0'. If the write succeeds, the client + /// must increment their corresponding key versions (client-side) by 1. + /// The server increments key versions (server-side) for every successful write, hence this + /// client-side increment is required to ensure matching versions. These updated key versions should + /// be used in subsequent `PutObjectRequest`s for the keys. + /// + /// Requests with a conflicting/mismatched version will fail with `CONFLICT_EXCEPTION` as ErrorCode + /// for conditional writes. + /// + /// Skipping key-level versioning (Non-conditional Write): + /// If you wish to skip key-level version checks, set the `version` against the `key` to '-1'. + /// This will perform a non-conditional write query, after which the `version` against the `key` + /// is reset to '1'. Hence, the next `PutObjectRequest` for the `key` can be either + /// a non-conditional write or a conditional write with `version` set to `1`. + /// + /// Considerations for transactions: + /// Transaction writes of multiple items have a performance overhead, hence it is recommended to use + /// them only if required by the client application to ensure logic/code correctness. + /// That is, `transaction_items` are not a substitute for batch-write of multiple unrelated items. + /// When a write of multiple unrelated items is desired, it is recommended to use separate + /// `PutObjectRequest`s. + /// + /// Consistency guarantee: + /// All `PutObjectRequest`s are strongly consistent i.e. they provide read-after-write and + /// read-after-update consistency guarantees. + #[prost(message, repeated, tag = "3")] + pub transaction_items: ::prost::alloc::vec::Vec, + /// Items to be deleted as a result of this `PutObjectRequest`. + /// + /// Each item in the `delete_items` field consists of a `key` and its corresponding `version`. + /// + /// Key-Level Versioning (Conditional Delete): + /// The `version` is used to perform a version check before deleting the item. + /// The delete will only succeed if the current database version against the `key` is the same as + /// the `version` specified in the request. + /// + /// Skipping key-level versioning (Non-conditional Delete): + /// If you wish to skip key-level version checks, set the `version` against the `key` to '-1'. + /// This will perform a non-conditional delete query. + /// + /// Fails with `CONFLICT_EXCEPTION` as the ErrorCode if: + /// * The requested item does not exist. + /// * The requested item does exist but there is a version-number mismatch (in conditional delete) + /// with the one in the database. + /// + /// Multiple items in the `delete_items` field, along with the `transaction_items`, are written in a + /// database transaction in an all-or-nothing fashion. + /// + /// All items within a single `PutObjectRequest` must have distinct keys. + #[prost(message, repeated, tag = "4")] + pub delete_items: ::prost::alloc::vec::Vec, +} +/// Server response for `PutObject` API. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct PutObjectResponse {} +/// Request payload to be used for `DeleteObject` API call to server. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct DeleteObjectRequest { + /// `store_id` is a keyspace identifier. + /// Ref: ) + /// All APIs operate within a single `store_id`. + /// It is up to clients to use single or multiple stores for their use-case. + /// This can be used for client-isolation/ rate-limiting / throttling on the server-side. + /// Authorization and billing can also be performed at the `store_id` level. + #[prost(string, tag = "1")] + pub store_id: ::prost::alloc::string::String, + /// Item to be deleted as a result of this `DeleteObjectRequest`. + /// + /// An item consists of a `key` and its corresponding `version`. + /// + /// Key-level Versioning (Conditional Delete): + /// The item is only deleted if the current database version against the `key` is the same as + /// the `version` specified in the request. + /// + /// Skipping key-level versioning (Non-conditional Delete): + /// If you wish to skip key-level version checks, set the `version` against the `key` to '-1'. + /// This will perform a non-conditional delete query. + /// + /// This operation is idempotent, that is, multiple delete calls for the same item will not fail. + /// + /// If the requested item does not exist, this operation will not fail. + /// If you wish to perform stricter checks while deleting an item, consider using `PutObject` API. + #[prost(message, optional, tag = "2")] + pub key_value: ::core::option::Option, +} +/// Server response for `DeleteObject` API. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct DeleteObjectResponse {} +/// Request payload to be used for `ListKeyVersions` API call to server. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct ListKeyVersionsRequest { + /// `store_id` is a keyspace identifier. + /// Ref: ) + /// All APIs operate within a single `store_id`. + /// It is up to clients to use single or multiple stores for their use-case. + /// This can be used for client-isolation/ rate-limiting / throttling on the server-side. + /// Authorization and billing can also be performed at the `store_id` level. + #[prost(string, tag = "1")] + pub store_id: ::prost::alloc::string::String, + /// A `key_prefix` is a string of characters at the beginning of the key. Prefixes can be used as + /// a way to organize key-values in a similar way to directories. + /// + /// If `key_prefix` is specified, the response results will be limited to those keys that begin with + /// the specified prefix. + /// + /// If no `key_prefix` is specified or it is empty (""), all the keys are eligible to be returned in + /// the response. + #[prost(string, optional, tag = "2")] + pub key_prefix: ::core::option::Option<::prost::alloc::string::String>, + /// `page_size` is used by clients to specify the maximum number of results that can be returned by + /// the server. + /// The server may further constrain the maximum number of results returned in a single page. + /// If the `page_size` is 0 or not set, the server will decide the number of results to be returned. + #[prost(int32, optional, tag = "3")] + pub page_size: ::core::option::Option, + /// `page_token` is a pagination token. + /// + /// To query for the first page of `ListKeyVersions`, `page_token` must not be specified. + /// + /// For subsequent pages, use the value that was returned as `next_page_token` in the previous + /// page's `ListKeyVersionsResponse`. + #[prost(string, optional, tag = "4")] + pub page_token: ::core::option::Option<::prost::alloc::string::String>, +} +/// Server response for `ListKeyVersions` API. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct ListKeyVersionsResponse { + /// Fetched keys and versions. + /// Even though this API reuses the `KeyValue` struct, the `value` sub-field will not be set by the server. + #[prost(message, repeated, tag = "1")] + pub key_versions: ::prost::alloc::vec::Vec, + /// `next_page_token` is a pagination token, used to retrieve the next page of results. + /// Use this value to query for next-page of paginated `ListKeyVersions` operation, by specifying + /// this value as the `page_token` in the next request. + /// + /// If `next_page_token` is empty (""), then the "last page" of results has been processed and + /// there is no more data to be retrieved. + /// + /// If `next_page_token` is not empty, it does not necessarily mean that there is more data in the + /// result set. The only way to know when you have reached the end of the result set is when + /// `next_page_token` is empty. + /// + /// Caution: Clients must not assume a specific number of key_versions to be present in a page for + /// paginated response. + #[prost(string, optional, tag = "2")] + pub next_page_token: ::core::option::Option<::prost::alloc::string::String>, + /// `global_version` is a sequence-number/version of the whole store. + /// + /// `global_version` is only returned in response for the first page of the `ListKeyVersionsResponse` + /// and is guaranteed to be read before reading any key-versions. + /// + /// In case of refreshing the complete key-version view on the client-side, correct usage for + /// the returned `global_version` is as following: + /// 1. Read `global_version` from the first page of paginated response and save it as local variable. + /// 2. Update all the `key_versions` on client-side from all the pages of paginated response. + /// 3. Update `global_version` on client_side from the local variable saved in step-1. + /// This ensures that on client-side, all current `key_versions` were stored at `global_version` or later. + /// This guarantee is helpful for ensuring the versioning correctness if using the `global_version` + /// in `PutObject` API and can help avoid the race conditions related to it. + #[prost(int64, optional, tag = "3")] + pub global_version: ::core::option::Option, +} +/// When HttpStatusCode is not ok (200), the response `content` contains a serialized `ErrorResponse` +/// with the relevant `ErrorCode` and `message` +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct ErrorResponse { + /// The error code uniquely identifying an error condition. + /// It is meant to be read and understood programmatically by code that detects/handles errors by + /// type. + #[prost(enumeration = "ErrorCode", tag = "1")] + pub error_code: i32, + /// The error message containing a generic description of the error condition in English. + /// It is intended for a human audience only and should not be parsed to extract any information + /// programmatically. Client-side code may use it for logging only. + #[prost(string, tag = "2")] + pub message: ::prost::alloc::string::String, +} +/// Represents a key-value pair to be stored or retrieved. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct KeyValue { + /// Key against which the value is stored. + #[prost(string, tag = "1")] + pub key: ::prost::alloc::string::String, + /// Version field is used for key-level versioning. + /// For first write of key, `version` should be '0'. If the write succeeds, clients must increment + /// their corresponding key version (client-side) by 1. + /// The server increments key version (server-side) for every successful write, hence this + /// client-side increment is required to ensure matching versions. These updated key versions should + /// be used in subsequent `PutObjectRequest`s for the keys. + #[prost(int64, tag = "2")] + pub version: i64, + /// Object value in bytes which is stored (in put) and fetched (in get). + /// Clients must encrypt the secret contents of this blob client-side before sending it over the + /// wire to the server in order to preserve privacy and security. + /// Clients may use a `Storable` object, serialize it and set it here. + #[prost(bytes = "bytes", tag = "3")] + pub value: ::prost::bytes::Bytes, +} +/// Represents a storable object that can be serialized and stored as `value` in `PutObjectRequest`. +/// Only provided as a helper object for ease of use by clients. +/// Clients MUST encrypt the `PlaintextBlob` before using it as `data` in `Storable`. +/// The server does not use or read anything from `Storable`, Clients may use its fields as +/// required. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Storable { + /// Represents an encrypted and serialized `PlaintextBlob`. MUST encrypt the whole `PlaintextBlob` + /// using client-side encryption before setting here. + #[prost(bytes = "bytes", tag = "1")] + pub data: ::prost::bytes::Bytes, + /// Represents encryption related metadata + #[prost(message, optional, tag = "2")] + pub encryption_metadata: ::core::option::Option, +} +/// Represents encryption related metadata +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct EncryptionMetadata { + /// The encryption algorithm used for encrypting the `PlaintextBlob`. + #[prost(string, tag = "1")] + pub cipher_format: ::prost::alloc::string::String, + /// The nonce used for encryption. Nonce is a random or unique value used to ensure that the same + /// plaintext results in different ciphertexts every time it is encrypted. + #[prost(bytes = "bytes", tag = "2")] + pub nonce: ::prost::bytes::Bytes, + /// The authentication tag used for encryption. It provides integrity and authenticity assurance + /// for the encrypted data. + #[prost(bytes = "bytes", tag = "3")] + pub tag: ::prost::bytes::Bytes, +} +/// Represents a data blob, which is encrypted, serialized and later used in `Storable.data`. +/// Since the whole `Storable.data` is client-side encrypted, the server cannot understand this. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct PlaintextBlob { + /// The unencrypted value. + #[prost(bytes = "bytes", tag = "1")] + pub value: ::prost::bytes::Bytes, + /// The version of the value. Can be used by client to verify version integrity. + #[prost(int64, tag = "2")] + pub version: i64, +} +/// ErrorCodes to be used in `ErrorResponse` +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] +#[repr(i32)] +pub enum ErrorCode { + /// Default protobuf Enum value. Will not be used as `ErrorCode` by server. + Unknown = 0, + /// Used when the request contains mismatched version (either key or global) + /// in `PutObjectRequest`. For more info refer `PutObjectRequest`. + ConflictException = 1, + /// Used in the following cases: + /// - The request was missing a required argument. + /// - The specified argument was invalid, incomplete or in the wrong format. + /// - The request body of api cannot be deserialized into corresponding protobuf object. + InvalidRequestException = 2, + /// Used when an internal server error occurred, client is probably at no fault and can safely retry + /// this error with exponential backoff. + InternalServerException = 3, + /// Used when the specified `key` in a `GetObjectRequest` does not exist. + NoSuchKeyException = 4, + /// Used when authentication fails or in case of an unauthorized request. + AuthException = 5, +} +impl ErrorCode { + /// String value of the enum field names used in the ProtoBuf definition. + /// + /// The values are not transformed in any way and thus are considered stable + /// (if the ProtoBuf definition does not change) and safe for programmatic use. + pub fn as_str_name(&self) -> &'static str { + match self { + ErrorCode::Unknown => "UNKNOWN", + ErrorCode::ConflictException => "CONFLICT_EXCEPTION", + ErrorCode::InvalidRequestException => "INVALID_REQUEST_EXCEPTION", + ErrorCode::InternalServerException => "INTERNAL_SERVER_EXCEPTION", + ErrorCode::NoSuchKeyException => "NO_SUCH_KEY_EXCEPTION", + ErrorCode::AuthException => "AUTH_EXCEPTION", + } + } + /// Creates an enum from field names used in the ProtoBuf definition. + pub fn from_str_name(value: &str) -> ::core::option::Option { + match value { + "UNKNOWN" => Some(Self::Unknown), + "CONFLICT_EXCEPTION" => Some(Self::ConflictException), + "INVALID_REQUEST_EXCEPTION" => Some(Self::InvalidRequestException), + "INTERNAL_SERVER_EXCEPTION" => Some(Self::InternalServerException), + "NO_SUCH_KEY_EXCEPTION" => Some(Self::NoSuchKeyException), + "AUTH_EXCEPTION" => Some(Self::AuthException), + _ => None, + } + } +} diff --git a/rust/impls/Cargo.toml b/rust/impls/Cargo.toml new file mode 100644 index 0000000..a122fa1 --- /dev/null +++ b/rust/impls/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "impls" +version = "0.1.0" +edition = "2021" + +[dependencies] +async-trait = "0.1.77" +api = { path = "../api" } diff --git a/rust/impls/src/lib.rs b/rust/impls/src/lib.rs new file mode 100644 index 0000000..b417d95 --- /dev/null +++ b/rust/impls/src/lib.rs @@ -0,0 +1,15 @@ +//! Hosts VSS protocol compliant [`KvStore`] implementations for various backends. +//! +//! VSS is an open-source project designed to offer a server-side cloud storage solution specifically +//! tailored for noncustodial Lightning supporting mobile wallets. Its primary objective is to +//! simplify the development process for Lightning wallets by providing a secure means to store +//! and manage the essential state required for Lightning Network (LN) operations. +//! +//! [`KvStore`]: api::kv_store::KvStore + +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![deny(missing_docs)] + +/// Contains [PostgreSQL](https://www.postgresql.org/) based backend implementation for VSS. +pub mod postgres_store; diff --git a/rust/impls/src/postgres_store.rs b/rust/impls/src/postgres_store.rs new file mode 100644 index 0000000..8c05f2b --- /dev/null +++ b/rust/impls/src/postgres_store.rs @@ -0,0 +1,45 @@ +use api::error::VssError; +use api::kv_store::KvStore; +use api::types::{ + DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse, + ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse, +}; +use async_trait::async_trait; +use std::io::Error; + +/// A [PostgreSQL](https://www.postgresql.org/) based backend implementation for VSS. +pub struct PostgresBackendImpl {} + +impl PostgresBackendImpl { + /// Constructs a [`PostgresBackendImpl`] using `dsn` for PostgreSQL connection information. + pub async fn new(dsn: &str) -> Result { + todo!("pending implementation."); + } +} + +#[async_trait] +impl KvStore for PostgresBackendImpl { + async fn get( + &self, user_token: String, request: GetObjectRequest, + ) -> Result { + todo!("pending implementation."); + } + + async fn put( + &self, user_token: String, request: PutObjectRequest, + ) -> Result { + todo!("pending implementation."); + } + + async fn delete( + &self, user_token: String, request: DeleteObjectRequest, + ) -> Result { + todo!("pending implementation."); + } + + async fn list_key_versions( + &self, user_token: String, request: ListKeyVersionsRequest, + ) -> Result { + todo!("pending implementation."); + } +} diff --git a/rust/rustfmt.toml b/rust/rustfmt.toml new file mode 100644 index 0000000..4f88472 --- /dev/null +++ b/rust/rustfmt.toml @@ -0,0 +1,12 @@ +use_small_heuristics = "Max" +fn_params_layout = "Compressed" +hard_tabs = true +use_field_init_shorthand = true +max_width = 100 +match_block_trailing_comma = true +# UNSTABLE: format_code_in_doc_comments = true +# UNSTABLE: overflow_delimited_expr = true +# UNSTABLE: comment_width = 100 +# UNSTABLE: format_macro_matchers = true +# UNSTABLE: format_strings = true +# UNSTABLE: group_imports = "StdExternalCrate" diff --git a/rust/server/Cargo.toml b/rust/server/Cargo.toml new file mode 100644 index 0000000..8674aa8 --- /dev/null +++ b/rust/server/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "server" +version = "0.1.0" +edition = "2021" + +[dependencies] +api = { path = "../api" } +impls = { path = "../impls" } + +hyper = { version = "1", default-features = false, features = ["server", "http1"] } +http-body-util = { version = "0.1", default-features = false } +hyper-util = { version = "0.1", default-features = false, features = ["server-graceful"] } +tokio = { version = "1.38.0", default-features = false, features = ["time", "signal", "rt-multi-thread", "macros"] } +prost = { version = "0.11.6", default-features = false, features = ["std"] } +bytes = "1.4.0" diff --git a/rust/server/src/main.rs b/rust/server/src/main.rs new file mode 100644 index 0000000..2b13a19 --- /dev/null +++ b/rust/server/src/main.rs @@ -0,0 +1,83 @@ +//! Hosts VSS http-server implementation. +//! +//! VSS is an open-source project designed to offer a server-side cloud storage solution specifically +//! tailored for noncustodial Lightning supporting mobile wallets. Its primary objective is to +//! simplify the development process for Lightning wallets by providing a secure means to store +//! and manage the essential state required for Lightning Network (LN) operations. + +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![deny(missing_docs)] + +use std::net::SocketAddr; + +use tokio::net::TcpListener; +use tokio::signal::unix::SignalKind; + +use hyper::server::conn::http1; +use hyper_util::rt::TokioIo; + +use crate::vss_service::VssService; +use api::auth::{Authorizer, NoopAuthorizer}; +use api::kv_store::KvStore; +use impls::postgres_store::PostgresBackendImpl; +use std::sync::Arc; + +pub(crate) mod vss_service; + +fn main() { + // Define the address to bind the server to + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + + let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() { + Ok(runtime) => Arc::new(runtime), + Err(e) => { + eprintln!("Failed to setup tokio runtime: {}", e); + std::process::exit(-1); + }, + }; + + runtime.block_on(async { + let mut sigterm_stream = match tokio::signal::unix::signal(SignalKind::terminate()) { + Ok(stream) => stream, + Err(e) => { + println!("Failed to register for SIGTERM stream: {}", e); + std::process::exit(-1); + }, + }; + let authorizer = Arc::new(NoopAuthorizer {}); + let store = Arc::new( + PostgresBackendImpl::new("postgresql://postgres:postgres@localhost:5432/postgres") + .await + .unwrap(), + ); + let rest_svc_listener = + TcpListener::bind(&addr).await.expect("Failed to bind listening port"); + loop { + tokio::select! { + res = rest_svc_listener.accept() => { + match res { + Ok((stream, _)) => { + let io_stream = TokioIo::new(stream); + let vss_service = VssService::new(Arc::clone(&store) as Arc, Arc::clone(&authorizer) as Arc); + runtime.spawn(async move { + if let Err(err) = http1::Builder::new().serve_connection(io_stream, vss_service).await { + eprintln!("Failed to serve connection: {}", err); + } + }); + }, + Err(e) => eprintln!("Failed to accept connection: {}", e), + } + } + _ = tokio::signal::ctrl_c() => { + println!("Received CTRL-C, shutting down.."); + break; + } + _ = sigterm_stream.recv() => { + println!("Received SIGTERM, shutting down.."); + break; + } + } + } + }); +} diff --git a/rust/server/src/vss_service.rs b/rust/server/src/vss_service.rs new file mode 100644 index 0000000..60ecc12 --- /dev/null +++ b/rust/server/src/vss_service.rs @@ -0,0 +1,153 @@ +use http_body_util::{BodyExt, Full}; +use hyper::body::{Bytes, Incoming}; +use hyper::service::Service; +use hyper::{Request, Response, StatusCode}; +use std::collections::HashMap; + +use prost::Message; + +use api::auth::Authorizer; +use api::error::VssError; +use api::kv_store::KvStore; +use api::types::{ + DeleteObjectRequest, DeleteObjectResponse, ErrorCode, ErrorResponse, GetObjectRequest, + GetObjectResponse, ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, + PutObjectResponse, +}; +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; + +#[derive(Clone)] +pub struct VssService { + store: Arc, + authorizer: Arc, +} + +impl VssService { + pub(crate) fn new(store: Arc, authorizer: Arc) -> Self { + Self { store, authorizer } + } +} + +impl Service> for VssService { + type Response = Response>; + type Error = hyper::Error; + type Future = Pin> + Send>>; + + fn call(&self, req: Request) -> Self::Future { + let store = Arc::clone(&self.store); + let authorizer = Arc::clone(&self.authorizer); + let path = req.uri().path().to_owned(); + Box::pin(async move { + match path.as_str() { + "/getObject" => { + handle_request(store, authorizer, req, handle_get_object_request).await + }, + "/putObjects" => { + handle_request(store, authorizer, req, handle_put_object_request).await + }, + "/deleteObject" => { + handle_request(store, authorizer, req, handle_delete_object_request).await + }, + "/listKeyVersions" => { + handle_request(store, authorizer, req, handle_list_object_request).await + }, + _ => { + let error = format!("Unknown request: {}", path).into_bytes(); + Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Full::new(Bytes::from(error))) + .unwrap()) + }, + } + }) + } +} + +async fn handle_get_object_request( + store: Arc, user_token: String, request: GetObjectRequest, +) -> Result { + store.get(user_token, request).await +} +async fn handle_put_object_request( + store: Arc, user_token: String, request: PutObjectRequest, +) -> Result { + store.put(user_token, request).await +} +async fn handle_delete_object_request( + store: Arc, user_token: String, request: DeleteObjectRequest, +) -> Result { + store.delete(user_token, request).await +} +async fn handle_list_object_request( + store: Arc, user_token: String, request: ListKeyVersionsRequest, +) -> Result { + store.list_key_versions(user_token, request).await +} +async fn handle_request< + T: Message + Default, + R: Message, + F: FnOnce(Arc, String, T) -> Fut + Send + 'static, + Fut: Future> + Send, +>( + store: Arc, authorizer: Arc, request: Request, + handler: F, +) -> Result<>>::Response, hyper::Error> { + let (parts, body) = request.into_parts(); + let headers_map = parts + .headers + .iter() + .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string())) + .collect::>(); + + let user_token = match authorizer.verify(&headers_map).await { + Ok(auth_response) => auth_response.user_token, + Err(e) => return Ok(build_error_response(e)), + }; + // TODO: we should bound the amount of data we read to avoid allocating too much memory. + let bytes = body.collect().await?.to_bytes(); + match T::decode(bytes) { + Ok(request) => match handler(store.clone(), user_token, request).await { + Ok(response) => Ok(Response::builder() + .body(Full::new(Bytes::from(response.encode_to_vec()))) + // unwrap safety: body only errors when previous chained calls failed. + .unwrap()), + Err(e) => Ok(build_error_response(e)), + }, + Err(_) => Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Full::new(Bytes::from(b"Error parsing request".to_vec()))) + // unwrap safety: body only errors when previous chained calls failed. + .unwrap()), + } +} + +fn build_error_response(e: VssError) -> Response> { + let error_response = match e { + VssError::NoSuchKeyError(msg) => ErrorResponse { + error_code: ErrorCode::NoSuchKeyException.into(), + message: msg.to_string(), + }, + VssError::ConflictError(msg) => ErrorResponse { + error_code: ErrorCode::ConflictException.into(), + message: msg.to_string(), + }, + VssError::InvalidRequestError(msg) => ErrorResponse { + error_code: ErrorCode::InvalidRequestException.into(), + message: msg.to_string(), + }, + VssError::AuthError(msg) => { + ErrorResponse { error_code: ErrorCode::AuthException.into(), message: msg.to_string() } + }, + _ => ErrorResponse { + error_code: ErrorCode::InternalServerException.into(), + message: "Unknown Server Error occurred.".to_string(), + }, + }; + Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Full::new(Bytes::from(error_response.encode_to_vec()))) + // unwrap safety: body only errors when previous chained calls failed. + .unwrap() +}