From f1dfac5d58e317205bfe2b68486a3cc29454b375 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 13 Jun 2024 09:09:27 +0200 Subject: [PATCH 01/47] startet processing chain --- notification-server/Cargo.lock | 623 +++++++++++++++++++++++++++++++- notification-server/Cargo.toml | 10 + notification-server/src/main.rs | 66 +++- 3 files changed, 690 insertions(+), 9 deletions(-) diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index a8f325da..568c568b 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -96,6 +96,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -273,7 +322,7 @@ checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", "axum-core", - "bitflags", + "bitflags 1.3.2", "bytes", "futures-util", "http", @@ -312,9 +361,9 @@ dependencies = [ [[package]] name = "backtrace" -version = "0.3.72" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ "addr2line", "cc", @@ -349,6 +398,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + [[package]] name = "bitvec" version = "1.0.1" @@ -505,6 +560,52 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "4.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "clap_lex" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "concordium-contracts-common" version = "9.1.0" @@ -666,6 +767,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[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.6" @@ -900,6 +1011,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "ff" version = "0.13.0" @@ -1119,6 +1236,18 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "hex" version = "0.4.3" @@ -1158,9 +1287,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.8.0" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" [[package]] name = "httpdate" @@ -1265,6 +1394,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" @@ -1307,6 +1442,12 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "leb128" version = "0.2.5" @@ -1319,6 +1460,16 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.21" @@ -1331,6 +1482,16 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.7.2" @@ -1383,7 +1544,26 @@ dependencies = [ name = "notification-server" version = "0.1.0" dependencies = [ + "anyhow", + "clap", "concordium-rust-sdk", + "futures", + "log", + "tokio", + "tokio-postgres", + "tonic", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", ] [[package]] @@ -1465,6 +1645,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "num_enum" version = "0.6.1" @@ -1488,9 +1678,9 @@ dependencies = [ [[package]] name = "object" -version = "0.35.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" +checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" dependencies = [ "memchr", ] @@ -1507,6 +1697,41 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.1", + "smallvec", + "windows-targets 0.52.5", +] + [[package]] name = "password-hash" version = "0.4.2" @@ -1542,6 +1767,24 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -1602,6 +1845,35 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "postgres-protocol" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b6c5ef183cd3ab4ba005f1ca64c21e8bd97ce4699cfea9e8d9a2c4958ca520" +dependencies = [ + "base64 0.21.7", + "byteorder", + "bytes", + "fallible-iterator", + "hmac", + "md-5", + "memchr", + "rand", + "sha2", + "stringprep", +] + +[[package]] +name = "postgres-types" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d2234cdee9408b523530a9b6d2d6b373d1db34f6a8e51dc03ded1828d7fb67c" +dependencies = [ + "bytes", + "fallible-iterator", + "postgres-protocol", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -1773,6 +2045,24 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" +dependencies = [ + "bitflags 2.5.0", +] + [[package]] name = "rend" version = "0.4.2" @@ -1782,6 +2072,21 @@ dependencies = [ "bytecheck", ] +[[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 = "rkyv" version = "0.7.44" @@ -1842,6 +2147,49 @@ dependencies = [ "semver", ] +[[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-native-certs" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9aace74cb666635c918e9c12bc0d348266037aa8eb599b5cba565709a8dff00" +dependencies = [ + "openssl-probe", + "rustls-pemfile", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + +[[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 = "rustversion" version = "1.0.17" @@ -1854,6 +2202,31 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "schannel" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "seahash" version = "4.1.0" @@ -1878,6 +2251,29 @@ dependencies = [ "cc", ] +[[package]] +name = "security-framework" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" +dependencies = [ + "bitflags 2.5.0", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.23" @@ -1966,6 +2362,24 @@ dependencies = [ "keccak", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[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 = "signature" version = "2.2.0" @@ -1981,6 +2395,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "slab" version = "0.4.9" @@ -1990,6 +2410,12 @@ 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" @@ -2000,6 +2426,12 @@ dependencies = [ "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 = "spki" version = "0.7.3" @@ -2010,6 +2442,17 @@ dependencies = [ "der", ] +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + [[package]] name = "strsim" version = "0.11.1" @@ -2088,6 +2531,16 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.36" @@ -2144,7 +2597,9 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.48.0", @@ -2171,6 +2626,42 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "tokio-postgres" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d340244b32d920260ae7448cb72b6e238bddc3d4f7603394e7dd46ed8e48f5b8" +dependencies = [ + "async-trait", + "byteorder", + "bytes", + "fallible-iterator", + "futures-channel", + "futures-util", + "log", + "parking_lot", + "percent-encoding", + "phf", + "pin-project-lite", + "postgres-protocol", + "postgres-types", + "rand", + "socket2", + "tokio", + "tokio-util", + "whoami", +] + +[[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-stream" version = "0.1.15" @@ -2242,7 +2733,11 @@ dependencies = [ "percent-encoding", "pin-project", "prost", + "rustls", + "rustls-native-certs", + "rustls-pemfile", "tokio", + "tokio-rustls", "tokio-stream", "tower", "tower-layer", @@ -2311,6 +2806,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2325,12 +2846,33 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[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.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4259d9d4425d9f0661581b804cb85fe66a4c631cadd8f490d1c13a35d5d9291" + [[package]] name = "universal-hash" version = "0.5.1" @@ -2341,12 +2883,30 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" @@ -2368,6 +2928,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -2422,6 +2988,49 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "whoami" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44ab49fad634e88f55bf8f9bb3abd2f27d7204172a112c7c9987e01c1c94ea9" +dependencies = [ + "redox_syscall 0.4.1", + "wasite", + "web-sys", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 4728acf0..fe099429 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -8,4 +8,14 @@ version = "0.1.0" [dependencies] +anyhow = "1.0.86" +clap = { version = "4.5.7", features = ["derive"] } concordium-rust-sdk = { version = "*", path = "../deps/concordium-rust-sdk" } +futures = "0.3.30" +log = "0.4.21" +tokio = { version = "1.23", features = ["rt-multi-thread", "signal"] } +tokio-postgres = "0.7" +tonic = {version = "0.10.0", features = ["tls", "tls-roots"]} # Use system trust roots. +tracing-subscriber = { version = "0.3" } +tracing = { version = "0.1" } + diff --git a/notification-server/src/main.rs b/notification-server/src/main.rs index e7a11a96..46aef33c 100644 --- a/notification-server/src/main.rs +++ b/notification-server/src/main.rs @@ -1,3 +1,65 @@ -fn main() { - println!("Hello, world!"); +use clap::Parser; +use concordium_rust_sdk::v2::{Client, Endpoint}; +use futures::StreamExt; +use tonic::codegen::http; +use tonic::transport::ClientTlsConfig; + +#[derive(Debug, Parser)] +struct Args { + /// The node used for querying + #[arg( + long = "node", + help = "The endpoints are expected to point to concordium node grpc v2 API's.", + default_value = "https://grpc.testnet.concordium.com:20000", + )] + endpoint: Endpoint, + /// Database connection string. + #[arg( + long = "db-connection", + default_value = "host=localhost dbname=kpi-tracker user=postgres password=password \ + port=5432", + help = "A connection string detailing the connection to the database used by the \ + application.", + )] + db_connection: tokio_postgres::config::Config, +} + + + +#[tokio::main(flavor = "multi_thread")] +async fn main() -> anyhow::Result<()> { + let args = Args::parse(); + let endpoint = if args + .endpoint + .uri() + .scheme() + .map_or(false, |x| x == &http::uri::Scheme::HTTPS) + { + args.endpoint + .tls_config(ClientTlsConfig::new())? + } else { + args.endpoint + } + .connect_timeout(std::time::Duration::from_secs(10)) + .timeout(std::time::Duration::from_secs(300)) + .http2_keep_alive_interval(std::time::Duration::from_secs(300)) + .keep_alive_timeout(std::time::Duration::from_secs(10)) + .keep_alive_while_idle(true); + + let mut client = Client::new(endpoint) + .await?; + let mut receiver = client.get_finalized_blocks().await?; + while let Some(v) = receiver.next().await { + println!("Blockhash: {:?}", v.clone()?.block_hash); + let transactions = client.get_block_transaction_events(v?.block_hash).await?.response; + let addresses: Vec<_> = transactions + .map(|t| { + // TODO remove unwrap + t.unwrap().affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>() + }) + .collect() + .await; + println!("Addresses: {:#?}", addresses); + } + Ok(()) } From 5b9e9302b2c9831caaf203c19cf2e7d264e13ad1 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 11:09:49 +0200 Subject: [PATCH 02/47] implemented server --- kpi-tracker/src/main.rs | 2 +- notification-server/.env.template | 2 + notification-server/.gitignore | 1 + notification-server/Cargo.lock | 595 +++++++++++++++++- notification-server/Cargo.toml | 16 +- notification-server/Makefile | 36 ++ notification-server/docker-compose.yml | 12 + notification-server/resources/schema.sql | 6 + notification-server/src/bin/server.rs | 51 ++ .../src/{main.rs => bin/service.rs} | 4 +- 10 files changed, 698 insertions(+), 27 deletions(-) create mode 100644 notification-server/.env.template create mode 100644 notification-server/.gitignore create mode 100644 notification-server/Makefile create mode 100644 notification-server/docker-compose.yml create mode 100644 notification-server/resources/schema.sql create mode 100644 notification-server/src/bin/server.rs rename notification-server/src/{main.rs => bin/service.rs} (98%) diff --git a/kpi-tracker/src/main.rs b/kpi-tracker/src/main.rs index d860d87e..f1346a85 100644 --- a/kpi-tracker/src/main.rs +++ b/kpi-tracker/src/main.rs @@ -49,7 +49,7 @@ struct Args { port=5432", help = "A connection string detailing the connection to the database used by the \ application.", - env = "KPI_TRACKER_DB_CONNECTION" + env = "DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, /// Logging level of the application diff --git a/notification-server/.env.template b/notification-server/.env.template new file mode 100644 index 00000000..207d5e7c --- /dev/null +++ b/notification-server/.env.template @@ -0,0 +1,2 @@ +PGPASSWORD=$DBPASSWORD +DBCONNECTION="localhost dbname=kpi-tracker user=postgres password=$DBPASSWORD port=5432" diff --git a/notification-server/.gitignore b/notification-server/.gitignore new file mode 100644 index 00000000..2eea525d --- /dev/null +++ b/notification-server/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index 568c568b..c89ca725 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -75,6 +75,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.18" @@ -325,7 +334,7 @@ dependencies = [ "bitflags 1.3.2", "bytes", "futures-util", - "http", + "http 0.2.12", "http-body", "hyper", "itoa", @@ -351,7 +360,7 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http", + "http 0.2.12", "http-body", "mime", "rustversion", @@ -648,7 +657,7 @@ dependencies = [ "ed25519-dalek", "futures", "hex", - "http", + "http 0.2.12", "num", "num-bigint", "num-traits", @@ -902,6 +911,12 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + [[package]] name = "der" version = "0.7.9" @@ -935,15 +950,15 @@ dependencies = [ [[package]] name = "derive_more" -version = "0.99.17" +version = "0.99.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "convert_case", "proc-macro2", "quote", "rustc_version", - "syn 1.0.109", + "syn 2.0.66", ] [[package]] @@ -957,6 +972,17 @@ dependencies = [ "subtle", ] +[[package]] +name = "displaydoc" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "ed25519" version = "2.2.3" @@ -1005,6 +1031,28 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[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 = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1040,6 +1088,15 @@ 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 = "funty" version = "2.0.0" @@ -1194,7 +1251,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.12", "indexmap 2.2.6", "slab", "tokio", @@ -1236,6 +1293,30 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "headers" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" +dependencies = [ + "base64 0.21.7", + "bytes", + "headers-core", + "http 0.2.12", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http 0.2.12", +] + [[package]] name = "heck" version = "0.5.0" @@ -1274,6 +1355,17 @@ dependencies = [ "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" @@ -1281,15 +1373,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.12", "pin-project-lite", ] [[package]] name = "httparse" -version = "1.9.3" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" [[package]] name = "httpdate" @@ -1297,6 +1389,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.29" @@ -1308,7 +1406,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.12", "http-body", "httparse", "httpdate", @@ -1356,12 +1454,142 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" + +[[package]] +name = "icu_properties" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f8ac670d7422d7f76b32e17a5db556510825b29ec9154f235977c9caba61036" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "ident_case" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "idna" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4716a3a0933a1d01c2f72450e89596eb51dd34ef3c211ccd875acdf1f8fe47ed" +dependencies = [ + "icu_normalizer", + "icu_properties", + "smallvec", + "utf8_iter", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -1394,6 +1622,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "is-terminal" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -1460,6 +1699,12 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "litemap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" + [[package]] name = "lock_api" version = "0.4.12" @@ -1494,9 +1739,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "mime" @@ -1504,6 +1749,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1512,9 +1767,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] @@ -1530,6 +1785,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "multer" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" +dependencies = [ + "bytes", + "encoding_rs", + "futures-util", + "http 0.2.12", + "httparse", + "log", + "memchr", + "mime", + "spin", + "version_check", +] + [[package]] name = "nom" version = "7.1.3" @@ -1547,13 +1820,16 @@ dependencies = [ "anyhow", "clap", "concordium-rust-sdk", + "env_logger", "futures", "log", + "serde", "tokio", "tokio-postgres", "tonic", "tracing", "tracing-subscriber", + "warp", ] [[package]] @@ -1727,7 +2003,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.1", + "redox_syscall 0.5.2", "smallvec", "windows-targets 0.52.5", ] @@ -2056,13 +2332,42 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" +checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ "bitflags 2.5.0", ] +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "rend" version = "0.4.2" @@ -2211,6 +2516,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scopeguard" version = "1.2.0" @@ -2311,6 +2622,18 @@ dependencies = [ "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 = "serde_with" version = "3.8.1" @@ -2341,6 +2664,17 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sha2" version = "0.10.8" @@ -2442,6 +2776,12 @@ dependencies = [ "der", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "stringprep" version = "0.1.5" @@ -2505,12 +2845,32 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "tap" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.61" @@ -2572,6 +2932,16 @@ dependencies = [ "time-core", ] +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2673,6 +3043,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-tungstenite" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + [[package]] name = "tokio-util" version = "0.7.11" @@ -2726,7 +3108,7 @@ dependencies = [ "base64 0.21.7", "bytes", "h2", - "http", + "http 0.2.12", "http-body", "hyper", "hyper-timeout", @@ -2783,6 +3165,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2840,12 +3223,40 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tungstenite" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http 1.1.0", + "httparse", + "log", + "rand", + "sha1", + "thiserror", + "url", + "utf-8", +] + [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" @@ -2889,6 +3300,35 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "url" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c25da092f0a868cdf09e8674cd3b7ef3a7d92a24253e663a2fb85e2496de56" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" @@ -2922,6 +3362,35 @@ dependencies = [ "try-lock", ] +[[package]] +name = "warp" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4378d202ff965b011c64817db11d5829506d3404edeadb61f190d111da3f231c" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "headers", + "http 0.2.12", + "hyper", + "log", + "mime", + "mime_guess", + "multer", + "percent-encoding", + "pin-project", + "scoped-tls", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-tungstenite", + "tokio-util", + "tower-service", + "tracing", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3025,6 +3494,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -3188,6 +3666,18 @@ dependencies = [ "memchr", ] +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + [[package]] name = "wyz" version = "0.5.1" @@ -3197,6 +3687,30 @@ dependencies = [ "tap", ] +[[package]] +name = "yoke" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "synstructure", +] + [[package]] name = "zerocopy" version = "0.7.34" @@ -3217,6 +3731,27 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "zerofrom" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "synstructure", +] + [[package]] name = "zeroize" version = "1.8.1" @@ -3236,3 +3771,25 @@ dependencies = [ "quote", "syn 2.0.66", ] + +[[package]] +name = "zerovec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2cc8827d6c0994478a15c53f374f46fbd41bea663d809b14744bc42e6b109c" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97cf56601ee5052b4417d90c8755c6683473c926039908196cf35d99f893ebe7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index fe099429..9146d048 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -1,21 +1,29 @@ [package] -authors = ["Concordium AG developers@concordium.com"] -edition = "2021" name = "notification-server" version = "0.1.0" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] - anyhow = "1.0.86" clap = { version = "4.5.7", features = ["derive"] } concordium-rust-sdk = { version = "*", path = "../deps/concordium-rust-sdk" } +env_logger = "0.10" futures = "0.3.30" log = "0.4.21" tokio = { version = "1.23", features = ["rt-multi-thread", "signal"] } tokio-postgres = "0.7" -tonic = {version = "0.10.0", features = ["tls", "tls-roots"]} # Use system trust roots. +tonic = {version = "0.10", features = ["tls", "tls-roots"]} # Use system trust roots. tracing-subscriber = { version = "0.3" } tracing = { version = "0.1" } +warp = "0.3" +serde = { version = "1.0.203", features = ["derive"] } + +[[bin]] +path = "src/bin/server.rs" +name = "notification-server" +[[bin]] +path = "src/bin/service.rs" +name = "notification-service" diff --git a/notification-server/Makefile b/notification-server/Makefile new file mode 100644 index 00000000..d3d4ed71 --- /dev/null +++ b/notification-server/Makefile @@ -0,0 +1,36 @@ +.DEFAULT_GOAL := all + +all: setup-env docker-up wait-for-db create-tables + +setup-env: + @echo "Setting default password" + export DB_PASSWORD=$(openssl rand -base64 12) + cat .env.template | envsubst > .env + +docker-up: + @echo "Starting Docker containers..." + docker-compose up -d + +# Target to wait for the PostgreSQL database to be ready. +wait-for-db: + @echo "Waiting for the database to be ready..." + @max_attempts=10; \ + current_attempt=1; \ + until docker-compose exec -T db pg_isready -U postgres; do \ + sleep 5; \ + current_attempt=$$((current_attempt+1)); \ + if [ $$current_attempt -gt $$max_attempts ]; then \ + echo "Database did not become ready in time."; \ + exit 1; \ + fi; \ + echo "Retrying ($$current_attempt/$$max_attempts)..."; \ + done + +create-tables: + @echo "Creating tables from SQL files..." + @for sql_file in resources/*.sql; do \ + echo "Processing $$sql_file..."; \ + docker-compose exec -T db psql --username=postgres < $$sql_file; \ + done + +.PHONY: setup-env all docker-up wait-for-db create-tables diff --git a/notification-server/docker-compose.yml b/notification-server/docker-compose.yml new file mode 100644 index 00000000..6229b4be --- /dev/null +++ b/notification-server/docker-compose.yml @@ -0,0 +1,12 @@ +# Use postgres/example user/password credentials +version: '3.9' + +services: + + db: + image: postgres + restart: always + ports: + - "5432:5432" + environment: + POSTGRES_PASSWORD: $PGPASSWORD \ No newline at end of file diff --git a/notification-server/resources/schema.sql b/notification-server/resources/schema.sql new file mode 100644 index 00000000..51ae3047 --- /dev/null +++ b/notification-server/resources/schema.sql @@ -0,0 +1,6 @@ +-- accounts created. +CREATE TABLE IF NOT EXISTS account_device_mapping ( + id SERIAL8 PRIMARY KEY, + address BYTEA NOT NULL UNIQUE, + device_id VARCHAR NOT NULL UNIQUE +); diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs new file mode 100644 index 00000000..30d2f0f0 --- /dev/null +++ b/notification-server/src/bin/server.rs @@ -0,0 +1,51 @@ +use clap::Parser; +use concordium_rust_sdk::v2::{Client, Endpoint}; +use futures::StreamExt; +use tonic::codegen::http; +use tonic::transport::ClientTlsConfig; +use warp::{Filter, Reply}; +use serde::Deserialize; +use warp::http::StatusCode; + +#[derive(Debug, Parser)] +struct Args { + #[arg( + long = "db-connection", + default_value = "host=localhost dbname=kpi-tracker user=postgres password=password \ + port=5432", + help = "A connection string detailing the connection to the database used by the \ + application.", + )] + db_connection: tokio_postgres::config::Config, + /// Logging level of the application + #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] + log_level: log::LevelFilter, +} + + +#[derive(Deserialize)] +struct DeviceMapping { + device_id: String, +} +pub async fn upsert_account_device( + account: String, + device_mapping: DeviceMapping, +) -> Result { + println!("Upserting account {} with device id {}", account, device_mapping.device_id); + Ok(StatusCode::OK) +} + +#[tokio::main(flavor = "multi_thread")] +async fn main() -> anyhow::Result<()> { + let args = Args::parse(); + env_logger::Builder::new() + .filter_module(module_path!(), args.log_level) // Only log the current module (main). + .init(); + let account_device_route = warp::path!("api" / "v1" / "account" / String / "device_map") + .and(warp::put()) + .and(warp::body::json()) + .and_then(upsert_account_device); + let routes = account_device_route; + warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; + Ok(()) +} diff --git a/notification-server/src/main.rs b/notification-server/src/bin/service.rs similarity index 98% rename from notification-server/src/main.rs rename to notification-server/src/bin/service.rs index 46aef33c..0ab733bf 100644 --- a/notification-server/src/main.rs +++ b/notification-server/src/bin/service.rs @@ -16,7 +16,7 @@ struct Args { /// Database connection string. #[arg( long = "db-connection", - default_value = "host=localhost dbname=kpi-tracker user=postgres password=password \ + default_value = "host=localhost dbname=kpi-tracker user=postgres password=$DBPASSWORD \ port=5432", help = "A connection string detailing the connection to the database used by the \ application.", @@ -24,8 +24,6 @@ struct Args { db_connection: tokio_postgres::config::Config, } - - #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()> { let args = Args::parse(); From f0dd162cb696560b0f0c105f961b5c0600dfdc8c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 11:44:37 +0200 Subject: [PATCH 03/47] make notification server be more correct --- notification-server/README.md | 22 ++++++++++++++++++++++ notification-server/src/bin/service.rs | 21 +++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 notification-server/README.md diff --git a/notification-server/README.md b/notification-server/README.md new file mode 100644 index 00000000..5e7d6322 --- /dev/null +++ b/notification-server/README.md @@ -0,0 +1,22 @@ +# Notification server + +Server to obtain information about particular account to device mappings and store them in a persistence layer. + +# Notification service + +Service running to browse the CCD chain and given incoming transactions, emit a notification to the device +associated with the account that received the transaction. + +## Setting up local dev + +```shell +make +``` + +and run the application with + +```shell +cargo run --bin +``` + +where `` is the name of the binary you want to run. \ No newline at end of file diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 0ab733bf..e9a73bc5 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,7 +1,7 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; -use futures::StreamExt; use tonic::codegen::http; +use tonic::codegen::tokio_stream::StreamExt; use tonic::transport::ClientTlsConfig; #[derive(Debug, Parser)] @@ -50,13 +50,18 @@ async fn main() -> anyhow::Result<()> { while let Some(v) = receiver.next().await { println!("Blockhash: {:?}", v.clone()?.block_hash); let transactions = client.get_block_transaction_events(v?.block_hash).await?.response; - let addresses: Vec<_> = transactions - .map(|t| { - // TODO remove unwrap - t.unwrap().affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>() - }) - .collect() - .await; + let addresses: Vec = transactions + .filter_map(|t| { + match t { + Ok(t) => Some(t.affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>()), + Err(_) => None + } + }) + .collect::>>() + .await + .into_iter() + .flatten() + .collect(); println!("Addresses: {:#?}", addresses); } Ok(()) From 4ecb9fb2ec40a6bb521e3999d243206bfadcf025 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 11:49:29 +0200 Subject: [PATCH 04/47] log in case there is an issue with the block hash --- notification-server/src/bin/service.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index e9a73bc5..10ec5c7d 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,5 +1,6 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; +use log::info; use tonic::codegen::http; use tonic::codegen::tokio_stream::StreamExt; use tonic::transport::ClientTlsConfig; @@ -48,13 +49,17 @@ async fn main() -> anyhow::Result<()> { .await?; let mut receiver = client.get_finalized_blocks().await?; while let Some(v) = receiver.next().await { - println!("Blockhash: {:?}", v.clone()?.block_hash); - let transactions = client.get_block_transaction_events(v?.block_hash).await?.response; + let block_hash = v?.block_hash; + println!("Blockhash: {:?}", block_hash); + let transactions = client.get_block_transaction_events(block_hash).await?.response; let addresses: Vec = transactions .filter_map(|t| { match t { - Ok(t) => Some(t.affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>()), - Err(_) => None + Ok(t) => Some(t.affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>()), + Err(_) => { + info!("Not found block {}", block_hash); + None + } } }) .collect::>>() From 3c9fba218ecff478d4fa3d28e7f8769deaab81bb Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 12:41:47 +0200 Subject: [PATCH 05/47] run cargo fmt --- notification-server/.gitignore | 2 +- notification-server/Cargo.toml | 1 + notification-server/docker-compose.yml | 2 +- notification-server/src/bin/server.rs | 18 ++++++----- notification-server/src/bin/service.rs | 42 ++++++++++++++------------ 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/notification-server/.gitignore b/notification-server/.gitignore index 2eea525d..4c49bd78 100644 --- a/notification-server/.gitignore +++ b/notification-server/.gitignore @@ -1 +1 @@ -.env \ No newline at end of file +.env diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 9146d048..426a5ea5 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -1,4 +1,5 @@ [package] +authors = ["Concordium AG developers@concordium.com"] name = "notification-server" version = "0.1.0" edition = "2021" diff --git a/notification-server/docker-compose.yml b/notification-server/docker-compose.yml index 6229b4be..a68796a6 100644 --- a/notification-server/docker-compose.yml +++ b/notification-server/docker-compose.yml @@ -9,4 +9,4 @@ services: ports: - "5432:5432" environment: - POSTGRES_PASSWORD: $PGPASSWORD \ No newline at end of file + POSTGRES_PASSWORD: ${PGPASSWORD} diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 30d2f0f0..41c9c42f 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -1,11 +1,11 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; use futures::StreamExt; +use serde::Deserialize; use tonic::codegen::http; use tonic::transport::ClientTlsConfig; -use warp::{Filter, Reply}; -use serde::Deserialize; use warp::http::StatusCode; +use warp::{Filter, Reply}; #[derive(Debug, Parser)] struct Args { @@ -14,15 +14,14 @@ struct Args { default_value = "host=localhost dbname=kpi-tracker user=postgres password=password \ port=5432", help = "A connection string detailing the connection to the database used by the \ - application.", + application." )] - db_connection: tokio_postgres::config::Config, - /// Logging level of the application + db_connection: tokio_postgres::config::Config, + /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] - log_level: log::LevelFilter, + log_level: log::LevelFilter, } - #[derive(Deserialize)] struct DeviceMapping { device_id: String, @@ -31,7 +30,10 @@ pub async fn upsert_account_device( account: String, device_mapping: DeviceMapping, ) -> Result { - println!("Upserting account {} with device id {}", account, device_mapping.device_id); + println!( + "Upserting account {} with device id {}", + account, device_mapping.device_id + ); Ok(StatusCode::OK) } diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 10ec5c7d..8e14eda4 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -11,7 +11,7 @@ struct Args { #[arg( long = "node", help = "The endpoints are expected to point to concordium node grpc v2 API's.", - default_value = "https://grpc.testnet.concordium.com:20000", + default_value = "https://grpc.testnet.concordium.com:20000" )] endpoint: Endpoint, /// Database connection string. @@ -20,9 +20,9 @@ struct Args { default_value = "host=localhost dbname=kpi-tracker user=postgres password=$DBPASSWORD \ port=5432", help = "A connection string detailing the connection to the database used by the \ - application.", + application." )] - db_connection: tokio_postgres::config::Config, + db_connection: tokio_postgres::config::Config, } #[tokio::main(flavor = "multi_thread")] @@ -34,32 +34,36 @@ async fn main() -> anyhow::Result<()> { .scheme() .map_or(false, |x| x == &http::uri::Scheme::HTTPS) { - args.endpoint - .tls_config(ClientTlsConfig::new())? + args.endpoint.tls_config(ClientTlsConfig::new())? } else { args.endpoint } .connect_timeout(std::time::Duration::from_secs(10)) - .timeout(std::time::Duration::from_secs(300)) - .http2_keep_alive_interval(std::time::Duration::from_secs(300)) - .keep_alive_timeout(std::time::Duration::from_secs(10)) - .keep_alive_while_idle(true); + .timeout(std::time::Duration::from_secs(300)) + .http2_keep_alive_interval(std::time::Duration::from_secs(300)) + .keep_alive_timeout(std::time::Duration::from_secs(10)) + .keep_alive_while_idle(true); - let mut client = Client::new(endpoint) - .await?; + let mut client = Client::new(endpoint).await?; let mut receiver = client.get_finalized_blocks().await?; while let Some(v) = receiver.next().await { let block_hash = v?.block_hash; println!("Blockhash: {:?}", block_hash); - let transactions = client.get_block_transaction_events(block_hash).await?.response; + let transactions = client + .get_block_transaction_events(block_hash) + .await? + .response; let addresses: Vec = transactions - .filter_map(|t| { - match t { - Ok(t) => Some(t.affected_addresses().into_iter().map(|addr| addr.to_string()).collect::>()), - Err(_) => { - info!("Not found block {}", block_hash); - None - } + .filter_map(|t| match t { + Ok(t) => Some( + t.affected_addresses() + .into_iter() + .map(|addr| addr.to_string()) + .collect::>(), + ), + Err(_) => { + info!("Not found block {}", block_hash); + None } }) .collect::>>() From 1adc8dcb6ffadb4e458ee230553a24f6ee4baced Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 12:48:30 +0200 Subject: [PATCH 06/47] change to be using same fmt --- notification-server/src/bin/server.rs | 8 +++----- notification-server/src/bin/service.rs | 9 +++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 41c9c42f..619400cf 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -2,10 +2,8 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; use futures::StreamExt; use serde::Deserialize; -use tonic::codegen::http; -use tonic::transport::ClientTlsConfig; -use warp::http::StatusCode; -use warp::{Filter, Reply}; +use tonic::{codegen::http, transport::ClientTlsConfig}; +use warp::{http::StatusCode, Filter, Reply}; #[derive(Debug, Parser)] struct Args { @@ -19,7 +17,7 @@ struct Args { db_connection: tokio_postgres::config::Config, /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] - log_level: log::LevelFilter, + log_level: log::LevelFilter, } #[derive(Deserialize)] diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 8e14eda4..30199277 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,9 +1,10 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; use log::info; -use tonic::codegen::http; -use tonic::codegen::tokio_stream::StreamExt; -use tonic::transport::ClientTlsConfig; +use tonic::{ + codegen::{http, tokio_stream::StreamExt}, + transport::ClientTlsConfig, +}; #[derive(Debug, Parser)] struct Args { @@ -13,7 +14,7 @@ struct Args { help = "The endpoints are expected to point to concordium node grpc v2 API's.", default_value = "https://grpc.testnet.concordium.com:20000" )] - endpoint: Endpoint, + endpoint: Endpoint, /// Database connection string. #[arg( long = "db-connection", From 63d808ba69d6de47e50329a7716c12e137352485 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 18 Jun 2024 12:54:53 +0200 Subject: [PATCH 07/47] clippy --- notification-server/src/bin/server.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 619400cf..e8d7f129 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -1,8 +1,5 @@ use clap::Parser; -use concordium_rust_sdk::v2::{Client, Endpoint}; -use futures::StreamExt; use serde::Deserialize; -use tonic::{codegen::http, transport::ClientTlsConfig}; use warp::{http::StatusCode, Filter, Reply}; #[derive(Debug, Parser)] @@ -24,7 +21,7 @@ struct Args { struct DeviceMapping { device_id: String, } -pub async fn upsert_account_device( +async fn upsert_account_device( account: String, device_mapping: DeviceMapping, ) -> Result { From 1ceddbb24824eba56a816f2f1583a6822cf710d0 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 10:13:08 +0200 Subject: [PATCH 08/47] fix issue --- kpi-tracker/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kpi-tracker/src/main.rs b/kpi-tracker/src/main.rs index f1346a85..d860d87e 100644 --- a/kpi-tracker/src/main.rs +++ b/kpi-tracker/src/main.rs @@ -49,7 +49,7 @@ struct Args { port=5432", help = "A connection string detailing the connection to the database used by the \ application.", - env = "DB_CONNECTION" + env = "KPI_TRACKER_DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, /// Logging level of the application From 0b1a5502688737da3e55416576a71de3e00e55a5 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 10:28:59 +0200 Subject: [PATCH 09/47] fix make issues --- notification-server/.env.template | 4 ++-- notification-server/Makefile | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index 207d5e7c..e7058c2d 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,2 @@ -PGPASSWORD=$DBPASSWORD -DBCONNECTION="localhost dbname=kpi-tracker user=postgres password=$DBPASSWORD port=5432" +PGPASSWORD=$DB_PASSWORD +DBCONNECTION="localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" diff --git a/notification-server/Makefile b/notification-server/Makefile index d3d4ed71..1451ff5f 100644 --- a/notification-server/Makefile +++ b/notification-server/Makefile @@ -4,7 +4,8 @@ all: setup-env docker-up wait-for-db create-tables setup-env: @echo "Setting default password" - export DB_PASSWORD=$(openssl rand -base64 12) + @DB_PASSWORD=$$(openssl rand -base64 12) && \ + export DB_PASSWORD=$$DB_PASSWORD && \ cat .env.template | envsubst > .env docker-up: From dc03e83c452f85bccc757f241941c883dc10bb3f Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 10:30:28 +0200 Subject: [PATCH 10/47] fix connection name --- notification-server/.env.template | 2 +- notification-server/src/bin/server.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index e7058c2d..5080cf0e 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,2 @@ PGPASSWORD=$DB_PASSWORD -DBCONNECTION="localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" +DB_CONNECTION="localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index e8d7f129..f0a6e40b 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -10,6 +10,7 @@ struct Args { port=5432", help = "A connection string detailing the connection to the database used by the \ application." + env = "DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, /// Logging level of the application From 6f42c506b8376b4c5bc6155530d39790b4c55642 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 10:31:24 +0200 Subject: [PATCH 11/47] alphabetically orders --- notification-server/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 426a5ea5..06ef2a3d 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -1,8 +1,8 @@ [package] authors = ["Concordium AG developers@concordium.com"] +edition = "2021" name = "notification-server" version = "0.1.0" -edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From d79c87ab051f4e2d05d501a7370d21d3f7fa283d Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 11:07:18 +0200 Subject: [PATCH 12/47] removed default value --- notification-server/src/bin/server.rs | 2 -- notification-server/src/bin/service.rs | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index f0a6e40b..80f05ded 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -6,8 +6,6 @@ use warp::{http::StatusCode, Filter, Reply}; struct Args { #[arg( long = "db-connection", - default_value = "host=localhost dbname=kpi-tracker user=postgres password=password \ - port=5432", help = "A connection string detailing the connection to the database used by the \ application." env = "DB_CONNECTION" diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 30199277..32afc9d2 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -18,10 +18,9 @@ struct Args { /// Database connection string. #[arg( long = "db-connection", - default_value = "host=localhost dbname=kpi-tracker user=postgres password=$DBPASSWORD \ - port=5432", help = "A connection string detailing the connection to the database used by the \ application." + env = "DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, } From a98a4cecdd81b78d82dd554450ca6fee32b802f0 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 11:23:37 +0200 Subject: [PATCH 13/47] automatically load .env file --- notification-server/.env.template | 2 +- notification-server/Cargo.lock | 7 +++++++ notification-server/Cargo.toml | 3 ++- notification-server/Makefile | 2 +- notification-server/src/bin/server.rs | 4 +++- notification-server/src/bin/service.rs | 4 +++- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index 5080cf0e..d6226eae 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,2 @@ PGPASSWORD=$DB_PASSWORD -DB_CONNECTION="localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" +DB_CONNECTION="host=localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index c89ca725..d7403feb 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -983,6 +983,12 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "ed25519" version = "2.2.3" @@ -1820,6 +1826,7 @@ dependencies = [ "anyhow", "clap", "concordium-rust-sdk", + "dotenv", "env_logger", "futures", "log", diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 06ef2a3d..6f8310f3 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -8,8 +8,9 @@ version = "0.1.0" [dependencies] anyhow = "1.0.86" -clap = { version = "4.5.7", features = ["derive"] } +clap = { version = "4.5.7", features = ["derive", "env"] } concordium-rust-sdk = { version = "*", path = "../deps/concordium-rust-sdk" } +dotenv = "0.15" env_logger = "0.10" futures = "0.3.30" log = "0.4.21" diff --git a/notification-server/Makefile b/notification-server/Makefile index 1451ff5f..bc529a5f 100644 --- a/notification-server/Makefile +++ b/notification-server/Makefile @@ -1,6 +1,6 @@ .DEFAULT_GOAL := all -all: setup-env docker-up wait-for-db create-tables +all: docker-up wait-for-db create-tables setup-env: @echo "Setting default password" diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 80f05ded..bb6bbb3a 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -1,4 +1,5 @@ use clap::Parser; +use dotenv::dotenv; use serde::Deserialize; use warp::{http::StatusCode, Filter, Reply}; @@ -7,7 +8,7 @@ struct Args { #[arg( long = "db-connection", help = "A connection string detailing the connection to the database used by the \ - application." + application.", env = "DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, @@ -33,6 +34,7 @@ async fn upsert_account_device( #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()> { + dotenv().ok(); let args = Args::parse(); env_logger::Builder::new() .filter_module(module_path!(), args.log_level) // Only log the current module (main). diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 32afc9d2..bb8bb25a 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,5 +1,6 @@ use clap::Parser; use concordium_rust_sdk::v2::{Client, Endpoint}; +use dotenv::dotenv; use log::info; use tonic::{ codegen::{http, tokio_stream::StreamExt}, @@ -19,7 +20,7 @@ struct Args { #[arg( long = "db-connection", help = "A connection string detailing the connection to the database used by the \ - application." + application.", env = "DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, @@ -27,6 +28,7 @@ struct Args { #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()> { + dotenv().ok(); let args = Args::parse(); let endpoint = if args .endpoint From 10794d8fc2a96012cd228bc56752cb551c1f228c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 11:57:22 +0200 Subject: [PATCH 14/47] fixed readme --- notification-server/Makefile | 2 +- notification-server/README.md | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/notification-server/Makefile b/notification-server/Makefile index bc529a5f..74b63725 100644 --- a/notification-server/Makefile +++ b/notification-server/Makefile @@ -2,7 +2,7 @@ all: docker-up wait-for-db create-tables -setup-env: +setup: @echo "Setting default password" @DB_PASSWORD=$$(openssl rand -base64 12) && \ export DB_PASSWORD=$$DB_PASSWORD && \ diff --git a/notification-server/README.md b/notification-server/README.md index 5e7d6322..8d6bfaf9 100644 --- a/notification-server/README.md +++ b/notification-server/README.md @@ -10,9 +10,11 @@ associated with the account that received the transaction. ## Setting up local dev ```shell -make +make setup && make ``` +where `make setup` will be a onetime setup and `make` will be continuously used to ensure containers are valid. + and run the application with ```shell From 6a4251d9fbe3906aac581a24f678de5634ad54d2 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 14:22:37 +0200 Subject: [PATCH 15/47] process only certain events --- notification-server/src/bin/service.rs | 32 +++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index bb8bb25a..73020917 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,7 +1,8 @@ use clap::Parser; +use concordium_rust_sdk::types::AccountTransactionEffects; +use concordium_rust_sdk::types::BlockItemSummaryDetails::AccountTransaction; use concordium_rust_sdk::v2::{Client, Endpoint}; use dotenv::dotenv; -use log::info; use tonic::{ codegen::{http, tokio_stream::StreamExt}, transport::ClientTlsConfig, @@ -26,6 +27,16 @@ struct Args { db_connection: tokio_postgres::config::Config, } +fn is_notification_emitting_transaction_effect(effect: &AccountTransactionEffects) -> bool { + match effect { + AccountTransactionEffects::AccountTransfer { .. } + | AccountTransactionEffects::AccountTransferWithMemo { .. } + | AccountTransactionEffects::TransferredWithSchedule { .. } + | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } => true, + _ => false, + } +} + #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()> { dotenv().ok(); @@ -56,18 +67,17 @@ async fn main() -> anyhow::Result<()> { .await? .response; let addresses: Vec = transactions - .filter_map(|t| match t { - Ok(t) => Some( - t.affected_addresses() - .into_iter() - .map(|addr| addr.to_string()) - .collect::>(), - ), - Err(_) => { - info!("Not found block {}", block_hash); - None + .filter_map(Result::ok) + .filter(|t| { + match t.details { + AccountTransaction(ref account_transaction) => is_notification_emitting_transaction_effect(&account_transaction.effects), + _ => false, } }) + .map(|t| t.affected_addresses() + .into_iter() + .map(|addr| addr.to_string()) + .collect::>()) .collect::>>() .await .into_iter() From cd44ce2839b8eb068b3dea62c2916353874354c4 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 19 Jun 2024 14:50:27 +0200 Subject: [PATCH 16/47] cleaned up processing --- notification-server/src/bin/service.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 73020917..b9bf4fe0 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -68,16 +68,23 @@ async fn main() -> anyhow::Result<()> { .response; let addresses: Vec = transactions .filter_map(Result::ok) - .filter(|t| { + .filter_map(|t| { match t.details { - AccountTransaction(ref account_transaction) => is_notification_emitting_transaction_effect(&account_transaction.effects), - _ => false, + AccountTransaction(ref account_transaction) => { + if is_notification_emitting_transaction_effect(&account_transaction.effects) { + Some( + t.affected_addresses() + .into_iter() + .map(|addr| addr.to_string()) + .collect::>(), + ) + } else { + None + } + } + _ => None, } }) - .map(|t| t.affected_addresses() - .into_iter() - .map(|addr| addr.to_string()) - .collect::>()) .collect::>>() .await .into_iter() From cfbce9f8dcfb4c4e2ae12eac864399a26769cae6 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:04:52 +0200 Subject: [PATCH 17/47] notification server --- notification-server/src/bin/service.rs | 36 +++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index b9bf4fe0..5f5d3ed5 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,5 +1,7 @@ use clap::Parser; -use concordium_rust_sdk::types::AccountTransactionEffects; +use concordium_rust_sdk::cis2; +use concordium_rust_sdk::cis2::Event; +use concordium_rust_sdk::types::{AccountTransactionEffects, ContractTraceElement}; use concordium_rust_sdk::types::BlockItemSummaryDetails::AccountTransaction; use concordium_rust_sdk::v2::{Client, Endpoint}; use dotenv::dotenv; @@ -27,8 +29,34 @@ struct Args { db_connection: tokio_postgres::config::Config, } -fn is_notification_emitting_transaction_effect(effect: &AccountTransactionEffects) -> bool { - match effect { +fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { + match &effects { + AccountTransactionEffects::ContractUpdateIssued { effects } => { + Some(effects.iter().flat_map(|effect| match effect { + ContractTraceElement::Updated { data } => { + data.events.iter().map(|event| { + match cis2::Event::try_from(event) { + Ok(Event::Transfer { to, .. }) => { + Some(to.to_string()) + }, + Ok(Event::Mint { amount, .. }) => { + Some(amount.to_string()) + }, + _ => None + } + }).filter(|t| Option::is_some(t)).collect() + }, + _ => None, + }).collect()) + } + _ => None, + } +} + + + +fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffects) -> bool { + match effects { AccountTransactionEffects::AccountTransfer { .. } | AccountTransactionEffects::AccountTransferWithMemo { .. } | AccountTransactionEffects::TransferredWithSchedule { .. } @@ -79,7 +107,7 @@ async fn main() -> anyhow::Result<()> { .collect::>(), ) } else { - None + get_cis2_events_addresses(&account_transaction.effects) } } _ => None, From 0f97068e6229ebb55cfd5acf6333bda89d5288fd Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:28:46 +0200 Subject: [PATCH 18/47] migrate to b eusing axum instead of warp --- notification-server/Cargo.lock | 151 ++++++++++++++++++++++++-- notification-server/Cargo.toml | 1 + notification-server/src/bin/server.rs | 64 +++++++---- 3 files changed, 185 insertions(+), 31 deletions(-) diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index d7403feb..c2e2aa9a 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -330,13 +330,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", - "axum-core", + "axum-core 0.3.4", "bitflags 1.3.2", "bytes", "futures-util", "http 0.2.12", - "http-body", - "hyper", + "http-body 0.4.6", + "hyper 0.14.29", "itoa", "matchit", "memchr", @@ -345,12 +345,46 @@ dependencies = [ "pin-project-lite", "rustversion", "serde", - "sync_wrapper", + "sync_wrapper 0.1.2", "tower", "tower-layer", "tower-service", ] +[[package]] +name = "axum" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" +dependencies = [ + "async-trait", + "axum-core 0.4.3", + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.3.1", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "axum-core" version = "0.3.4" @@ -361,13 +395,34 @@ dependencies = [ "bytes", "futures-util", "http 0.2.12", - "http-body", + "http-body 0.4.6", "mime", "rustversion", "tower-layer", "tower-service", ] +[[package]] +name = "axum-core" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper 0.1.2", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "backtrace" version = "0.3.73" @@ -1383,6 +1438,29 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-body" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +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.0", + "pin-project-lite", +] + [[package]] name = "httparse" version = "1.9.4" @@ -1413,7 +1491,7 @@ dependencies = [ "futures-util", "h2", "http 0.2.12", - "http-body", + "http-body 0.4.6", "httparse", "httpdate", "itoa", @@ -1425,18 +1503,52 @@ dependencies = [ "want", ] +[[package]] +name = "hyper" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + [[package]] name = "hyper-timeout" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" dependencies = [ - "hyper", + "hyper 0.14.29", "pin-project-lite", "tokio", "tokio-io-timeout", ] +[[package]] +name = "hyper-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "hyper 1.3.1", + "pin-project-lite", + "tokio", +] + [[package]] name = "iana-time-zone" version = "0.1.60" @@ -1824,6 +1936,7 @@ name = "notification-server" version = "0.1.0" dependencies = [ "anyhow", + "axum 0.7.5", "clap", "concordium-rust-sdk", "dotenv", @@ -2629,6 +2742,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebd154a240de39fdebcf5775d2675c204d7c13cf39a4c697be6493c8e734337c" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2852,6 +2975,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" + [[package]] name = "synstructure" version = "0.13.1" @@ -3111,13 +3240,13 @@ checksum = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e" dependencies = [ "async-stream", "async-trait", - "axum", + "axum 0.6.20", "base64 0.21.7", "bytes", "h2", "http 0.2.12", - "http-body", - "hyper", + "http-body 0.4.6", + "hyper 0.14.29", "hyper-timeout", "percent-encoding", "pin-project", @@ -3380,7 +3509,7 @@ dependencies = [ "futures-util", "headers", "http 0.2.12", - "hyper", + "hyper 0.14.29", "log", "mime", "mime_guess", diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 6f8310f3..cfe1dc9f 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" [dependencies] anyhow = "1.0.86" +axum = "0.7.5" clap = { version = "4.5.7", features = ["derive", "env"] } concordium-rust-sdk = { version = "*", path = "../deps/concordium-rust-sdk" } dotenv = "0.15" diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index bb6bbb3a..10069f05 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -1,7 +1,16 @@ +use axum::{ + extract::{Json, Path, State}, + http::StatusCode, + routing::put, + Router, +}; use clap::Parser; use dotenv::dotenv; use serde::Deserialize; -use warp::{http::StatusCode, Filter, Reply}; +use std::sync::Arc; +use tokio_postgres::Config; +use tracing::info; +use tracing_subscriber; #[derive(Debug, Parser)] struct Args { @@ -11,39 +20,54 @@ struct Args { application.", env = "DB_CONNECTION" )] - db_connection: tokio_postgres::config::Config, + db_connection: String, // Changed to String for axum, will parse later /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] - log_level: log::LevelFilter, + log_level: log::LevelFilter, } #[derive(Deserialize)] struct DeviceMapping { device_id: String, } + +#[derive(Clone)] +struct AppState { + db_connection: Config, +} + async fn upsert_account_device( - account: String, - device_mapping: DeviceMapping, -) -> Result { - println!( - "Upserting account {} with device id {}", - account, device_mapping.device_id - ); + Path(account): Path, + State(state): State>, + Json(device_mapping): Json, +) -> Result { + info!("Upserting account {} with device id {}", account, device_mapping.device_id); + + // Example of how you might use the state + let _ = &state.db_connection; + Ok(StatusCode::OK) } -#[tokio::main(flavor = "multi_thread")] +#[tokio::main] async fn main() -> anyhow::Result<()> { dotenv().ok(); let args = Args::parse(); - env_logger::Builder::new() - .filter_module(module_path!(), args.log_level) // Only log the current module (main). - .init(); - let account_device_route = warp::path!("api" / "v1" / "account" / String / "device_map") - .and(warp::put()) - .and(warp::body::json()) - .and_then(upsert_account_device); - let routes = account_device_route; - warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; + + tracing_subscriber::fmt::init(); + + // Parse the database connection string into a tokio_postgres::Config + let db_connection: Config = args.db_connection.parse()?; + let app_state = Arc::new(AppState { db_connection }); + + // Define the router + let app = Router::new() + .route( + "/api/v1/account/:account/device_map", + put(upsert_account_device), + ) + .with_state(app_state); + let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?; + axum::serve(listener, app).await?; Ok(()) } From 3126520ac1e5cc173d5ac67be78c0ede7a20ad42 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:30:03 +0200 Subject: [PATCH 19/47] remove warp --- notification-server/Cargo.lock | 436 --------------------------------- notification-server/Cargo.toml | 1 - 2 files changed, 437 deletions(-) diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index c2e2aa9a..499f4038 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -966,12 +966,6 @@ dependencies = [ "syn 2.0.66", ] -[[package]] -name = "data-encoding" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" - [[package]] name = "der" version = "0.7.9" @@ -1027,17 +1021,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "displaydoc" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] - [[package]] name = "dotenv" version = "0.15.0" @@ -1092,15 +1075,6 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" -[[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 = "env_logger" version = "0.10.2" @@ -1354,30 +1328,6 @@ dependencies = [ "allocator-api2", ] -[[package]] -name = "headers" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" -dependencies = [ - "base64 0.21.7", - "bytes", - "headers-core", - "http 0.2.12", - "httpdate", - "mime", - "sha1", -] - -[[package]] -name = "headers-core" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http 0.2.12", -] - [[package]] name = "heck" version = "0.5.0" @@ -1572,142 +1522,12 @@ dependencies = [ "cc", ] -[[package]] -name = "icu_collections" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" -dependencies = [ - "displaydoc", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_locid" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" -dependencies = [ - "displaydoc", - "litemap", - "tinystr", - "writeable", - "zerovec", -] - -[[package]] -name = "icu_locid_transform" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_locid_transform_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_locid_transform_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" - -[[package]] -name = "icu_normalizer" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_normalizer_data", - "icu_properties", - "icu_provider", - "smallvec", - "utf16_iter", - "utf8_iter", - "write16", - "zerovec", -] - -[[package]] -name = "icu_normalizer_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" - -[[package]] -name = "icu_properties" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f8ac670d7422d7f76b32e17a5db556510825b29ec9154f235977c9caba61036" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_locid_transform", - "icu_properties_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_properties_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" - -[[package]] -name = "icu_provider" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_provider_macros", - "stable_deref_trait", - "tinystr", - "writeable", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_provider_macros" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] - [[package]] name = "ident_case" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" -[[package]] -name = "idna" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4716a3a0933a1d01c2f72450e89596eb51dd34ef3c211ccd875acdf1f8fe47ed" -dependencies = [ - "icu_normalizer", - "icu_properties", - "smallvec", - "utf8_iter", -] - [[package]] name = "indexmap" version = "1.9.3" @@ -1817,12 +1637,6 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" -[[package]] -name = "litemap" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" - [[package]] name = "lock_api" version = "0.4.12" @@ -1867,16 +1681,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "mime_guess" -version = "2.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1903,24 +1707,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "multer" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" -dependencies = [ - "bytes", - "encoding_rs", - "futures-util", - "http 0.2.12", - "httparse", - "log", - "memchr", - "mime", - "spin", - "version_check", -] - [[package]] name = "nom" version = "7.1.3" @@ -1949,7 +1735,6 @@ dependencies = [ "tonic", "tracing", "tracing-subscriber", - "warp", ] [[package]] @@ -2636,12 +2421,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "scopeguard" version = "1.2.0" @@ -2794,17 +2573,6 @@ dependencies = [ "syn 2.0.66", ] -[[package]] -name = "sha1" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "sha2" version = "0.10.8" @@ -2906,12 +2674,6 @@ dependencies = [ "der", ] -[[package]] -name = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - [[package]] name = "stringprep" version = "0.1.5" @@ -2981,17 +2743,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" -[[package]] -name = "synstructure" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] - [[package]] name = "tap" version = "1.0.1" @@ -3068,16 +2819,6 @@ dependencies = [ "time-core", ] -[[package]] -name = "tinystr" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" -dependencies = [ - "displaydoc", - "zerovec", -] - [[package]] name = "tinyvec" version = "1.6.0" @@ -3179,18 +2920,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-tungstenite" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" -dependencies = [ - "futures-util", - "log", - "tokio", - "tungstenite", -] - [[package]] name = "tokio-util" version = "0.7.11" @@ -3359,40 +3088,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "tungstenite" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" -dependencies = [ - "byteorder", - "bytes", - "data-encoding", - "http 1.1.0", - "httparse", - "log", - "rand", - "sha1", - "thiserror", - "url", - "utf-8", -] - [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" -[[package]] -name = "unicase" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-bidi" version = "0.3.15" @@ -3436,35 +3137,6 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" -[[package]] -name = "url" -version = "2.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c25da092f0a868cdf09e8674cd3b7ef3a7d92a24253e663a2fb85e2496de56" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - -[[package]] -name = "utf-8" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" - -[[package]] -name = "utf16_iter" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" - -[[package]] -name = "utf8_iter" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" - [[package]] name = "utf8parse" version = "0.2.2" @@ -3498,35 +3170,6 @@ dependencies = [ "try-lock", ] -[[package]] -name = "warp" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4378d202ff965b011c64817db11d5829506d3404edeadb61f190d111da3f231c" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "headers", - "http 0.2.12", - "hyper 0.14.29", - "log", - "mime", - "mime_guess", - "multer", - "percent-encoding", - "pin-project", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-tungstenite", - "tokio-util", - "tower-service", - "tracing", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3802,18 +3445,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "write16" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" - -[[package]] -name = "writeable" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" - [[package]] name = "wyz" version = "0.5.1" @@ -3823,30 +3454,6 @@ dependencies = [ "tap", ] -[[package]] -name = "yoke" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" -dependencies = [ - "serde", - "stable_deref_trait", - "yoke-derive", - "zerofrom", -] - -[[package]] -name = "yoke-derive" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", - "synstructure", -] - [[package]] name = "zerocopy" version = "0.7.34" @@ -3867,27 +3474,6 @@ dependencies = [ "syn 2.0.66", ] -[[package]] -name = "zerofrom" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" -dependencies = [ - "zerofrom-derive", -] - -[[package]] -name = "zerofrom-derive" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", - "synstructure", -] - [[package]] name = "zeroize" version = "1.8.1" @@ -3907,25 +3493,3 @@ dependencies = [ "quote", "syn 2.0.66", ] - -[[package]] -name = "zerovec" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2cc8827d6c0994478a15c53f374f46fbd41bea663d809b14744bc42e6b109c" -dependencies = [ - "yoke", - "zerofrom", - "zerovec-derive", -] - -[[package]] -name = "zerovec-derive" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97cf56601ee5052b4417d90c8755c6683473c926039908196cf35d99f893ebe7" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index cfe1dc9f..81f06b6e 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -20,7 +20,6 @@ tokio-postgres = "0.7" tonic = {version = "0.10", features = ["tls", "tls-roots"]} # Use system trust roots. tracing-subscriber = { version = "0.3" } tracing = { version = "0.1" } -warp = "0.3" serde = { version = "1.0.203", features = ["derive"] } [[bin]] From 08fb4b99d1d9e62ec5948c13c08e1b154827d0d5 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:31:16 +0200 Subject: [PATCH 20/47] fmt --- notification-server/src/bin/server.rs | 7 ++- notification-server/src/bin/service.rs | 84 +++++++++++++------------- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 10069f05..75732040 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -23,7 +23,7 @@ struct Args { db_connection: String, // Changed to String for axum, will parse later /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] - log_level: log::LevelFilter, + log_level: log::LevelFilter, } #[derive(Deserialize)] @@ -41,7 +41,10 @@ async fn upsert_account_device( State(state): State>, Json(device_mapping): Json, ) -> Result { - info!("Upserting account {} with device id {}", account, device_mapping.device_id); + info!( + "Upserting account {} with device id {}", + account, device_mapping.device_id + ); // Example of how you might use the state let _ = &state.db_connection; diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 5f5d3ed5..591b06d9 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,9 +1,13 @@ use clap::Parser; -use concordium_rust_sdk::cis2; -use concordium_rust_sdk::cis2::Event; -use concordium_rust_sdk::types::{AccountTransactionEffects, ContractTraceElement}; -use concordium_rust_sdk::types::BlockItemSummaryDetails::AccountTransaction; -use concordium_rust_sdk::v2::{Client, Endpoint}; +use concordium_rust_sdk::{ + cis2, + cis2::Event, + types::{ + AccountTransactionEffects, BlockItemSummaryDetails::AccountTransaction, + ContractTraceElement, + }, + v2::{Client, Endpoint}, +}; use dotenv::dotenv; use tonic::{ codegen::{http, tokio_stream::StreamExt}, @@ -30,31 +34,29 @@ struct Args { } fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { - match &effects { - AccountTransactionEffects::ContractUpdateIssued { effects } => { - Some(effects.iter().flat_map(|effect| match effect { - ContractTraceElement::Updated { data } => { - data.events.iter().map(|event| { - match cis2::Event::try_from(event) { - Ok(Event::Transfer { to, .. }) => { - Some(to.to_string()) - }, - Ok(Event::Mint { amount, .. }) => { - Some(amount.to_string()) - }, - _ => None - } - }).filter(|t| Option::is_some(t)).collect() - }, - _ => None, - }).collect()) - } - _ => None, - } + match &effects { + AccountTransactionEffects::ContractUpdateIssued { effects } => Some( + effects + .iter() + .flat_map(|effect| match effect { + ContractTraceElement::Updated { data } => data + .events + .iter() + .map(|event| match cis2::Event::try_from(event) { + Ok(Event::Transfer { to, .. }) => Some(to.to_string()), + Ok(Event::Mint { amount, .. }) => Some(amount.to_string()), + _ => None, + }) + .filter(|t| Option::is_some(t)) + .collect(), + _ => None, + }) + .collect(), + ), + _ => None, + } } - - fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffects) -> bool { match effects { AccountTransactionEffects::AccountTransfer { .. } @@ -96,22 +98,20 @@ async fn main() -> anyhow::Result<()> { .response; let addresses: Vec = transactions .filter_map(Result::ok) - .filter_map(|t| { - match t.details { - AccountTransaction(ref account_transaction) => { - if is_notification_emitting_transaction_effect(&account_transaction.effects) { - Some( - t.affected_addresses() - .into_iter() - .map(|addr| addr.to_string()) - .collect::>(), - ) - } else { - get_cis2_events_addresses(&account_transaction.effects) - } + .filter_map(|t| match t.details { + AccountTransaction(ref account_transaction) => { + if is_notification_emitting_transaction_effect(&account_transaction.effects) { + Some( + t.affected_addresses() + .into_iter() + .map(|addr| addr.to_string()) + .collect::>(), + ) + } else { + get_cis2_events_addresses(&account_transaction.effects) } - _ => None, } + _ => None, }) .collect::>>() .await From 5dda1ec12a3b39cae67af6c5f9076f716155a51b Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:38:17 +0200 Subject: [PATCH 21/47] clippy --- notification-server/src/bin/server.rs | 2 +- notification-server/src/bin/service.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 75732040..9574dd9e 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -10,7 +10,7 @@ use serde::Deserialize; use std::sync::Arc; use tokio_postgres::Config; use tracing::info; -use tracing_subscriber; + #[derive(Debug, Parser)] struct Args { diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 591b06d9..872dba57 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -47,7 +47,7 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option Some(amount.to_string()), _ => None, }) - .filter(|t| Option::is_some(t)) + .filter(Option::is_some) .collect(), _ => None, }) From eeb4ce63c4908fd760130c0d530ad3bdb493ef4b Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:40:19 +0200 Subject: [PATCH 22/47] removed whitespace --- notification-server/src/bin/server.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 9574dd9e..02dad511 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -11,7 +11,6 @@ use std::sync::Arc; use tokio_postgres::Config; use tracing::info; - #[derive(Debug, Parser)] struct Args { #[arg( From 488ff6bf58e26351ff86fe26306ed222812e4280 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:46:14 +0200 Subject: [PATCH 23/47] clippy --- notification-server/src/bin/service.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 872dba57..474fec12 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -58,13 +58,10 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option bool { - match effects { - AccountTransactionEffects::AccountTransfer { .. } + matches!(effects, AccountTransactionEffects::AccountTransfer { .. } | AccountTransactionEffects::AccountTransferWithMemo { .. } | AccountTransactionEffects::TransferredWithSchedule { .. } - | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } => true, - _ => false, - } + | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. }) } #[tokio::main(flavor = "multi_thread")] From 8fe7bf8bdac69c1b43e36b4f3f8a165f5babf569 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:47:26 +0200 Subject: [PATCH 24/47] fmt --- notification-server/src/bin/service.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 474fec12..b42ec265 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -58,10 +58,13 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option bool { - matches!(effects, AccountTransactionEffects::AccountTransfer { .. } - | AccountTransactionEffects::AccountTransferWithMemo { .. } - | AccountTransactionEffects::TransferredWithSchedule { .. } - | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. }) + matches!( + effects, + AccountTransactionEffects::AccountTransfer { .. } + | AccountTransactionEffects::AccountTransferWithMemo { .. } + | AccountTransactionEffects::TransferredWithSchedule { .. } + | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } + ) } #[tokio::main(flavor = "multi_thread")] From 02ac2e1063d65d7d9286f4115dffebdfbc1a14c2 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 10:50:44 +0200 Subject: [PATCH 25/47] updated phony --- notification-server/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/Makefile b/notification-server/Makefile index 74b63725..ae578741 100644 --- a/notification-server/Makefile +++ b/notification-server/Makefile @@ -34,4 +34,4 @@ create-tables: docker-compose exec -T db psql --username=postgres < $$sql_file; \ done -.PHONY: setup-env all docker-up wait-for-db create-tables +.PHONY: setup all docker-up wait-for-db create-tables From 8bcc4568ab0965d293ae4312e2a51f1cbacc8caf Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 13:27:59 +0200 Subject: [PATCH 26/47] moved into separate function --- notification-server/resources/schema.sql | 5 +- notification-server/src/bin/service.rs | 64 +----------------------- notification-server/src/lib.rs | 1 + notification-server/src/processor.rs | 61 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 64 deletions(-) create mode 100644 notification-server/src/lib.rs create mode 100644 notification-server/src/processor.rs diff --git a/notification-server/resources/schema.sql b/notification-server/resources/schema.sql index 51ae3047..4c7e7390 100644 --- a/notification-server/resources/schema.sql +++ b/notification-server/resources/schema.sql @@ -1,6 +1,7 @@ -- accounts created. CREATE TABLE IF NOT EXISTS account_device_mapping ( id SERIAL8 PRIMARY KEY, - address BYTEA NOT NULL UNIQUE, - device_id VARCHAR NOT NULL UNIQUE + address BYTEA NOT NULL, + device_id VARCHAR NOT NULL, + UNIQUE (address, device_id) ); diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index b42ec265..d4899ec2 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,11 +1,5 @@ use clap::Parser; use concordium_rust_sdk::{ - cis2, - cis2::Event, - types::{ - AccountTransactionEffects, BlockItemSummaryDetails::AccountTransaction, - ContractTraceElement, - }, v2::{Client, Endpoint}, }; use dotenv::dotenv; @@ -13,6 +7,7 @@ use tonic::{ codegen::{http, tokio_stream::StreamExt}, transport::ClientTlsConfig, }; +use notification_server::processor::process; #[derive(Debug, Parser)] struct Args { @@ -33,40 +28,6 @@ struct Args { db_connection: tokio_postgres::config::Config, } -fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { - match &effects { - AccountTransactionEffects::ContractUpdateIssued { effects } => Some( - effects - .iter() - .flat_map(|effect| match effect { - ContractTraceElement::Updated { data } => data - .events - .iter() - .map(|event| match cis2::Event::try_from(event) { - Ok(Event::Transfer { to, .. }) => Some(to.to_string()), - Ok(Event::Mint { amount, .. }) => Some(amount.to_string()), - _ => None, - }) - .filter(Option::is_some) - .collect(), - _ => None, - }) - .collect(), - ), - _ => None, - } -} - -fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffects) -> bool { - matches!( - effects, - AccountTransactionEffects::AccountTransfer { .. } - | AccountTransactionEffects::AccountTransferWithMemo { .. } - | AccountTransactionEffects::TransferredWithSchedule { .. } - | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } - ) -} - #[tokio::main(flavor = "multi_thread")] async fn main() -> anyhow::Result<()> { dotenv().ok(); @@ -96,28 +57,7 @@ async fn main() -> anyhow::Result<()> { .get_block_transaction_events(block_hash) .await? .response; - let addresses: Vec = transactions - .filter_map(Result::ok) - .filter_map(|t| match t.details { - AccountTransaction(ref account_transaction) => { - if is_notification_emitting_transaction_effect(&account_transaction.effects) { - Some( - t.affected_addresses() - .into_iter() - .map(|addr| addr.to_string()) - .collect::>(), - ) - } else { - get_cis2_events_addresses(&account_transaction.effects) - } - } - _ => None, - }) - .collect::>>() - .await - .into_iter() - .flatten() - .collect(); + let addresses = process(transactions).await; println!("Addresses: {:#?}", addresses); } Ok(()) diff --git a/notification-server/src/lib.rs b/notification-server/src/lib.rs new file mode 100644 index 00000000..946b39c3 --- /dev/null +++ b/notification-server/src/lib.rs @@ -0,0 +1 @@ +pub mod processor; \ No newline at end of file diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs new file mode 100644 index 00000000..5831dc3f --- /dev/null +++ b/notification-server/src/processor.rs @@ -0,0 +1,61 @@ +use concordium_rust_sdk::{cis2, types}; +use concordium_rust_sdk::cis2::Event; +use concordium_rust_sdk::types::{AccountTransactionEffects, ContractTraceElement}; +use concordium_rust_sdk::types::BlockItemSummaryDetails::AccountTransaction; +use futures::{Stream, StreamExt}; + +fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { + match &effects { + AccountTransactionEffects::ContractUpdateIssued { effects } => Some( + effects + .iter() + .flat_map(|effect| match effect { + ContractTraceElement::Updated { data } => data + .events + .iter() + .map(|event| match cis2::Event::try_from(event) { + Ok(Event::Transfer { to, .. }) => Some(to.to_string()), + Ok(Event::Mint { amount, .. }) => Some(amount.to_string()), + _ => None, + }) + .filter(Option::is_some) + .collect(), + _ => None, + }) + .collect(), + ), + _ => None, + } +} + +fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffects) -> bool { + matches!( + effects, + AccountTransactionEffects::AccountTransfer { .. } + | AccountTransactionEffects::AccountTransferWithMemo { .. } + | AccountTransactionEffects::TransferredWithSchedule { .. } + | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } + ) +} + +pub async fn process(transactions: impl Stream>) -> Vec { + transactions + .filter_map(|result| async move { result.ok() }) + .filter_map(|t| async move { + match t.details { + AccountTransaction(ref account_transaction) => { + if is_notification_emitting_transaction_effect(&account_transaction.effects) { + Some( + t.affected_addresses() + .into_iter() + .map(|addr| addr.to_string()) + .collect::>(), + ) + } else { + get_cis2_events_addresses(&account_transaction.effects) + } + } + _ => None, + }}) + .collect::>>().await.into_iter().flatten().collect() +} From 9ba49c04f8fc401e34f9d368c88b6384251ccbd2 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 15:32:14 +0200 Subject: [PATCH 27/47] fix fmt --- notification-server/src/bin/service.rs | 6 ++---- notification-server/src/lib.rs | 2 +- notification-server/src/processor.rs | 26 +++++++++++++++++++------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index d4899ec2..a6f60ae3 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -1,13 +1,11 @@ use clap::Parser; -use concordium_rust_sdk::{ - v2::{Client, Endpoint}, -}; +use concordium_rust_sdk::v2::{Client, Endpoint}; use dotenv::dotenv; +use notification_server::processor::process; use tonic::{ codegen::{http, tokio_stream::StreamExt}, transport::ClientTlsConfig, }; -use notification_server::processor::process; #[derive(Debug, Parser)] struct Args { diff --git a/notification-server/src/lib.rs b/notification-server/src/lib.rs index 946b39c3..3cde65f0 100644 --- a/notification-server/src/lib.rs +++ b/notification-server/src/lib.rs @@ -1 +1 @@ -pub mod processor; \ No newline at end of file +pub mod processor; diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 5831dc3f..1eba3bf8 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -1,7 +1,12 @@ -use concordium_rust_sdk::{cis2, types}; -use concordium_rust_sdk::cis2::Event; -use concordium_rust_sdk::types::{AccountTransactionEffects, ContractTraceElement}; -use concordium_rust_sdk::types::BlockItemSummaryDetails::AccountTransaction; +use concordium_rust_sdk::{ + cis2, + cis2::Event, + types, + types::{ + AccountTransactionEffects, BlockItemSummaryDetails::AccountTransaction, + ContractTraceElement, + }, +}; use futures::{Stream, StreamExt}; fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { @@ -38,7 +43,9 @@ fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffec ) } -pub async fn process(transactions: impl Stream>) -> Vec { +pub async fn process( + transactions: impl Stream>, +) -> Vec { transactions .filter_map(|result| async move { result.ok() }) .filter_map(|t| async move { @@ -56,6 +63,11 @@ pub async fn process(transactions: impl Stream None, - }}) - .collect::>>().await.into_iter().flatten().collect() + } + }) + .collect::>>() + .await + .into_iter() + .flatten() + .collect() } From 2745abff48e59b9cd606d43b2f27fbc538d6790b Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 15:42:19 +0200 Subject: [PATCH 28/47] fixed db connection --- notification-server/.env.template | 2 +- notification-server/src/bin/server.rs | 2 +- notification-server/src/bin/service.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index d6226eae..cb2630bf 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,2 @@ PGPASSWORD=$DB_PASSWORD -DB_CONNECTION="host=localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" +NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index 02dad511..ecd01d24 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -17,7 +17,7 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "DB_CONNECTION" + env = "NOTIFICATION_SERVER_DB_CONNECTION" )] db_connection: String, // Changed to String for axum, will parse later /// Logging level of the application diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index a6f60ae3..039240ed 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -21,7 +21,7 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "DB_CONNECTION" + env = "NOTIFICATION_SERVER_DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, } From 73a5df883f4b036da15c0b212b33393c24ea989e Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 15:56:57 +0200 Subject: [PATCH 29/47] changing endpoint name --- notification-server/src/bin/server.rs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index ecd01d24..b7baef62 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -6,7 +6,6 @@ use axum::{ }; use clap::Parser; use dotenv::dotenv; -use serde::Deserialize; use std::sync::Arc; use tokio_postgres::Config; use tracing::info; @@ -19,33 +18,27 @@ struct Args { application.", env = "NOTIFICATION_SERVER_DB_CONNECTION" )] - db_connection: String, // Changed to String for axum, will parse later + db_connection: String, /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] log_level: log::LevelFilter, } -#[derive(Deserialize)] -struct DeviceMapping { - device_id: String, -} - #[derive(Clone)] struct AppState { db_connection: Config, } async fn upsert_account_device( - Path(account): Path, + Path(device): Path, State(state): State>, - Json(device_mapping): Json, + Json(account): Json>, ) -> Result { info!( - "Upserting account {} with device id {}", - account, device_mapping.device_id + "Subscribing accounts {:?} to device {}", + account, device ); - // Example of how you might use the state let _ = &state.db_connection; Ok(StatusCode::OK) @@ -58,14 +51,12 @@ async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); - // Parse the database connection string into a tokio_postgres::Config let db_connection: Config = args.db_connection.parse()?; let app_state = Arc::new(AppState { db_connection }); - // Define the router let app = Router::new() .route( - "/api/v1/account/:account/device_map", + "/api/v1/device/:device/subscriptions", put(upsert_account_device), ) .with_state(app_state); From 1016aec97806e8adbf4ee34f95f62d73f616be01 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Thu, 20 Jun 2024 18:53:50 +0200 Subject: [PATCH 30/47] cargo clippy --- notification-server/src/bin/server.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index b7baef62..fcc3eb66 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -34,11 +34,7 @@ async fn upsert_account_device( State(state): State>, Json(account): Json>, ) -> Result { - info!( - "Subscribing accounts {:?} to device {}", - account, device - ); - + info!("Subscribing accounts {:?} to device {}", account, device); let _ = &state.db_connection; Ok(StatusCode::OK) From 5e8fa0955bb18bc619c1dfa2a9bf6161f1fd619f Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 10:08:18 +0200 Subject: [PATCH 31/47] only get Account addr --- notification-server/src/processor.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 1eba3bf8..ad29fabc 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -7,9 +7,11 @@ use concordium_rust_sdk::{ ContractTraceElement, }, }; +use concordium_rust_sdk::base::contracts_common::AccountAddress; +use concordium_rust_sdk::types::Address::Account; use futures::{Stream, StreamExt}; -fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { +fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { match &effects { AccountTransactionEffects::ContractUpdateIssued { effects } => Some( effects @@ -19,13 +21,16 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option Some(to.to_string()), - Ok(Event::Mint { amount, .. }) => Some(amount.to_string()), + Ok(Event::Transfer { to, .. }) => Some(to), + Ok(Event::Mint { owner, .. }) => Some(owner), + _ => None, + }) + .filter_map(|addr| match addr { + Some(Account(addr)) => Some(addr), _ => None, }) - .filter(Option::is_some) .collect(), - _ => None, + _ => vec![], }) .collect(), ), @@ -45,7 +50,7 @@ fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffec pub async fn process( transactions: impl Stream>, -) -> Vec { +) -> Vec { transactions .filter_map(|result| async move { result.ok() }) .filter_map(|t| async move { @@ -55,7 +60,7 @@ pub async fn process( Some( t.affected_addresses() .into_iter() - .map(|addr| addr.to_string()) + .map(|addr| addr) .collect::>(), ) } else { @@ -65,7 +70,7 @@ pub async fn process( _ => None, } }) - .collect::>>() + .collect::>>() .await .into_iter() .flatten() From e54b55198d15236e8eeb703868e11878100bbaad Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 10:36:26 +0200 Subject: [PATCH 32/47] simplified collect in the end --- notification-server/src/processor.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index ad29fabc..dcb0bbd2 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -11,9 +11,9 @@ use concordium_rust_sdk::base::contracts_common::AccountAddress; use concordium_rust_sdk::types::Address::Account; use futures::{Stream, StreamExt}; -fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option> { +fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Vec { match &effects { - AccountTransactionEffects::ContractUpdateIssued { effects } => Some( + AccountTransactionEffects::ContractUpdateIssued { effects } => effects .iter() .flat_map(|effect| match effect { @@ -33,8 +33,7 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Option vec![], }) .collect(), - ), - _ => None, + _ => vec![] } } @@ -53,26 +52,18 @@ pub async fn process( ) -> Vec { transactions .filter_map(|result| async move { result.ok() }) - .filter_map(|t| async move { - match t.details { + .map(|t| futures::stream::iter(match t.details { AccountTransaction(ref account_transaction) => { if is_notification_emitting_transaction_effect(&account_transaction.effects) { - Some( - t.affected_addresses() - .into_iter() - .map(|addr| addr) - .collect::>(), - ) + t.affected_addresses() } else { get_cis2_events_addresses(&account_transaction.effects) } } - _ => None, + _ => vec![], } - }) - .collect::>>() - .await - .into_iter() + )) .flatten() - .collect() + .collect::>() + .await } From e4b7a45585c3d7ac183896fdcb51105637ec49cd Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 11:18:12 +0200 Subject: [PATCH 33/47] cleanup --- notification-server/src/processor.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index dcb0bbd2..34f6d2ba 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -52,7 +52,7 @@ pub async fn process( ) -> Vec { transactions .filter_map(|result| async move { result.ok() }) - .map(|t| futures::stream::iter(match t.details { + .flat_map(|t| futures::stream::iter(match t.details { AccountTransaction(ref account_transaction) => { if is_notification_emitting_transaction_effect(&account_transaction.effects) { t.affected_addresses() @@ -63,7 +63,6 @@ pub async fn process( _ => vec![], } )) - .flatten() .collect::>() .await } From 6390908be2f1c4f5c841e6d8abdf513f4fe45843 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 11:34:03 +0200 Subject: [PATCH 34/47] update dbname --- notification-server/.env.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index cb2630bf..626eb378 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,2 @@ PGPASSWORD=$DB_PASSWORD -NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=kpi-tracker user=postgres password=$DB_PASSWORD port=5432" +NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" From f26421347e3f0a37aecce35de98e2b21eb7504da Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 11:36:38 +0200 Subject: [PATCH 35/47] readme --- notification-server/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/README.md b/notification-server/README.md index 8d6bfaf9..8d2177b2 100644 --- a/notification-server/README.md +++ b/notification-server/README.md @@ -4,7 +4,7 @@ Server to obtain information about particular account to device mappings and sto # Notification service -Service running to browse the CCD chain and given incoming transactions, emit a notification to the device +Service indexing the CCD chain and given incoming transactions, emit a notification to the device associated with the account that received the transaction. ## Setting up local dev From aa2b9aa51fc37d7fe759d8bbc83478db7279619f Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Fri, 21 Jun 2024 12:59:42 +0200 Subject: [PATCH 36/47] expanded ccd contract updated event --- notification-server/src/bin/server.rs | 1 + notification-server/src/processor.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index fcc3eb66..cbce984b 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -22,6 +22,7 @@ struct Args { /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] log_level: log::LevelFilter, + } #[derive(Clone)] diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 34f6d2ba..2031bbb4 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -17,6 +17,7 @@ fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Vec vec![*to], ContractTraceElement::Updated { data } => data .events .iter() From 9a91944ed8cc2e8b356e7fa88cad27d9791e8cc3 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Sun, 23 Jun 2024 20:29:52 +0200 Subject: [PATCH 37/47] add amount --- notification-server/Cargo.lock | 1 + notification-server/Cargo.toml | 1 + notification-server/src/bin/service.rs | 2 +- notification-server/src/processor.rs | 66 ++++++++++++++++---------- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index 499f4038..cefcb781 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -1729,6 +1729,7 @@ dependencies = [ "env_logger", "futures", "log", + "num-bigint", "serde", "tokio", "tokio-postgres", diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index 81f06b6e..b14658df 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -21,6 +21,7 @@ tonic = {version = "0.10", features = ["tls", "tls-roots"]} # Use system trust r tracing-subscriber = { version = "0.3" } tracing = { version = "0.1" } serde = { version = "1.0.203", features = ["derive"] } +num-bigint = "0.4.5" [[bin]] path = "src/bin/server.rs" diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 039240ed..22d0b376 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -12,7 +12,7 @@ struct Args { /// The node used for querying #[arg( long = "node", - help = "The endpoints are expected to point to concordium node grpc v2 API's.", + help = "The endpoint is expected to point to concordium node grpc v2 API's.", default_value = "https://grpc.testnet.concordium.com:20000" )] endpoint: Endpoint, diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 2031bbb4..54211f85 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -1,3 +1,4 @@ +use std::fmt::{Debug}; use concordium_rust_sdk::{ cis2, cis2::Event, @@ -8,62 +9,75 @@ use concordium_rust_sdk::{ }, }; use concordium_rust_sdk::base::contracts_common::AccountAddress; +use concordium_rust_sdk::types::Address; use concordium_rust_sdk::types::Address::Account; use futures::{Stream, StreamExt}; +use num_bigint::BigInt; -fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Vec { +#[derive(Debug)] +pub struct NotificationInformation { + effects: AccountAddress, + amount: BigInt, +} + +impl NotificationInformation { + pub fn new(effects: AccountAddress, amount: BigInt) -> Self { + Self { effects, amount } + } +} + +fn convert>(address: Address, amount: T, is_positive: bool) -> Option { + let mut amount: BigInt = amount.into(); + if !is_positive { + amount = -amount; + } + + match address { + Account(address) => Some(NotificationInformation::new(address, amount)), + _ => None + } +} + +fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Vec { match &effects { + AccountTransactionEffects::AccountTransfer { to, amount } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], + AccountTransactionEffects::AccountTransferWithMemo { to, amount, .. } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], AccountTransactionEffects::ContractUpdateIssued { effects } => effects .iter() .flat_map(|effect| match effect { - ContractTraceElement::Transferred { to, .. } => vec![*to], + ContractTraceElement::Transferred { to, amount,.. } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], ContractTraceElement::Updated { data } => data .events .iter() - .map(|event| match cis2::Event::try_from(event) { - Ok(Event::Transfer { to, .. }) => Some(to), - Ok(Event::Mint { owner, .. }) => Some(owner), - _ => None, - }) - .filter_map(|addr| match addr { - Some(Account(addr)) => Some(addr), + .filter_map(|event| match cis2::Event::try_from(event) { + Ok(Event::Transfer { to, amount, .. }) => convert(to, amount.0, true), + Ok(Event::Mint { owner, amount, .. }) => convert(owner, amount.0, true), + Ok(Event::Burn { owner, amount, .. }) => convert(owner, amount.0, false), _ => None, }) .collect(), _ => vec![], }) .collect(), + AccountTransactionEffects::TransferredWithSchedule { to, amount } => vec![NotificationInformation::new(*to, amount.iter().fold(BigInt::from(0), |acc, &(_, item)| acc + BigInt::from(item.micro_ccd)))], + AccountTransactionEffects::TransferredWithScheduleAndMemo { to, amount, .. } => vec![NotificationInformation::new(*to, amount.iter().fold(BigInt::from(0), |acc, &(_, item)| acc + BigInt::from(item.micro_ccd)))], _ => vec![] } } -fn is_notification_emitting_transaction_effect(effects: &AccountTransactionEffects) -> bool { - matches!( - effects, - AccountTransactionEffects::AccountTransfer { .. } - | AccountTransactionEffects::AccountTransferWithMemo { .. } - | AccountTransactionEffects::TransferredWithSchedule { .. } - | AccountTransactionEffects::TransferredWithScheduleAndMemo { .. } - ) -} - pub async fn process( transactions: impl Stream>, -) -> Vec { +) -> Vec { transactions .filter_map(|result| async move { result.ok() }) .flat_map(|t| futures::stream::iter(match t.details { AccountTransaction(ref account_transaction) => { - if is_notification_emitting_transaction_effect(&account_transaction.effects) { - t.affected_addresses() - } else { - get_cis2_events_addresses(&account_transaction.effects) - } + get_cis2_events_addresses(&account_transaction.effects) } _ => vec![], } )) - .collect::>() + .collect::>() .await } From 28f423197737040fd743a47797fc17594ce7f273 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Sun, 23 Jun 2024 20:51:50 +0200 Subject: [PATCH 38/47] fmt --- notification-server/src/bin/server.rs | 1 - notification-server/src/bin/service.rs | 2 +- notification-server/src/processor.rs | 96 ++++++++++++++++---------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/server.rs index cbce984b..fcc3eb66 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/server.rs @@ -22,7 +22,6 @@ struct Args { /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] log_level: log::LevelFilter, - } #[derive(Clone)] diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 22d0b376..b7192e2f 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -12,7 +12,7 @@ struct Args { /// The node used for querying #[arg( long = "node", - help = "The endpoint is expected to point to concordium node grpc v2 API's.", + help = "The endpoint is expected to point to a concordium node grpc v2 API's.", default_value = "https://grpc.testnet.concordium.com:20000" )] endpoint: Endpoint, diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 54211f85..1e0b3fe2 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -1,32 +1,32 @@ -use std::fmt::{Debug}; use concordium_rust_sdk::{ + base::contracts_common::AccountAddress, cis2, cis2::Event, types, types::{ - AccountTransactionEffects, BlockItemSummaryDetails::AccountTransaction, - ContractTraceElement, + AccountTransactionEffects, Address, Address::Account, + BlockItemSummaryDetails::AccountTransaction, ContractTraceElement, }, }; -use concordium_rust_sdk::base::contracts_common::AccountAddress; -use concordium_rust_sdk::types::Address; -use concordium_rust_sdk::types::Address::Account; use futures::{Stream, StreamExt}; use num_bigint::BigInt; +use std::fmt::Debug; #[derive(Debug)] pub struct NotificationInformation { effects: AccountAddress, - amount: BigInt, + amount: BigInt, } impl NotificationInformation { - pub fn new(effects: AccountAddress, amount: BigInt) -> Self { - Self { effects, amount } - } + pub fn new(effects: AccountAddress, amount: BigInt) -> Self { Self { effects, amount } } } -fn convert>(address: Address, amount: T, is_positive: bool) -> Option { +fn convert>( + address: Address, + amount: T, + is_positive: bool, +) -> Option { let mut amount: BigInt = amount.into(); if !is_positive { amount = -amount; @@ -34,35 +34,54 @@ fn convert>(address: Address, amount: T, is_positive: bool) -> O match address { Account(address) => Some(NotificationInformation::new(address, amount)), - _ => None + _ => None, } } fn get_cis2_events_addresses(effects: &AccountTransactionEffects) -> Vec { match &effects { - AccountTransactionEffects::AccountTransfer { to, amount } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], - AccountTransactionEffects::AccountTransferWithMemo { to, amount, .. } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], - AccountTransactionEffects::ContractUpdateIssued { effects } => - effects - .iter() - .flat_map(|effect| match effect { - ContractTraceElement::Transferred { to, amount,.. } => vec![NotificationInformation::new(*to, amount.micro_ccd.into())], - ContractTraceElement::Updated { data } => data - .events - .iter() - .filter_map(|event| match cis2::Event::try_from(event) { - Ok(Event::Transfer { to, amount, .. }) => convert(to, amount.0, true), - Ok(Event::Mint { owner, amount, .. }) => convert(owner, amount.0, true), - Ok(Event::Burn { owner, amount, .. }) => convert(owner, amount.0, false), - _ => None, - }) - .collect(), - _ => vec![], - }) - .collect(), - AccountTransactionEffects::TransferredWithSchedule { to, amount } => vec![NotificationInformation::new(*to, amount.iter().fold(BigInt::from(0), |acc, &(_, item)| acc + BigInt::from(item.micro_ccd)))], - AccountTransactionEffects::TransferredWithScheduleAndMemo { to, amount, .. } => vec![NotificationInformation::new(*to, amount.iter().fold(BigInt::from(0), |acc, &(_, item)| acc + BigInt::from(item.micro_ccd)))], - _ => vec![] + AccountTransactionEffects::AccountTransfer { to, amount } => { + vec![NotificationInformation::new(*to, amount.micro_ccd.into())] + } + AccountTransactionEffects::AccountTransferWithMemo { to, amount, .. } => { + vec![NotificationInformation::new(*to, amount.micro_ccd.into())] + } + AccountTransactionEffects::ContractUpdateIssued { effects } => effects + .iter() + .flat_map(|effect| match effect { + ContractTraceElement::Transferred { to, amount, .. } => { + vec![NotificationInformation::new(*to, amount.micro_ccd.into())] + } + ContractTraceElement::Updated { data } => data + .events + .iter() + .filter_map(|event| match cis2::Event::try_from(event) { + Ok(Event::Transfer { to, amount, .. }) => convert(to, amount.0, true), + Ok(Event::Mint { owner, amount, .. }) => convert(owner, amount.0, true), + Ok(Event::Burn { owner, amount, .. }) => convert(owner, amount.0, false), + _ => None, + }) + .collect(), + _ => vec![], + }) + .collect(), + AccountTransactionEffects::TransferredWithSchedule { to, amount } => { + vec![NotificationInformation::new( + *to, + amount.iter().fold(BigInt::from(0), |acc, &(_, item)| { + acc + BigInt::from(item.micro_ccd) + }), + )] + } + AccountTransactionEffects::TransferredWithScheduleAndMemo { to, amount, .. } => { + vec![NotificationInformation::new( + *to, + amount.iter().fold(BigInt::from(0), |acc, &(_, item)| { + acc + BigInt::from(item.micro_ccd) + }), + )] + } + _ => vec![], } } @@ -71,13 +90,14 @@ pub async fn process( ) -> Vec { transactions .filter_map(|result| async move { result.ok() }) - .flat_map(|t| futures::stream::iter(match t.details { + .flat_map(|t| { + futures::stream::iter(match t.details { AccountTransaction(ref account_transaction) => { get_cis2_events_addresses(&account_transaction.effects) } _ => vec![], - } - )) + }) + }) .collect::>() .await } From 3e8ba7168660439b349452d65d8ec7d012f14329 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Sun, 23 Jun 2024 21:01:24 +0200 Subject: [PATCH 39/47] address --- notification-server/src/bin/service.rs | 6 ++++-- notification-server/src/processor.rs | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index b7192e2f..8c42a7de 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -55,8 +55,10 @@ async fn main() -> anyhow::Result<()> { .get_block_transaction_events(block_hash) .await? .response; - let addresses = process(transactions).await; - println!("Addresses: {:#?}", addresses); + let results = process(transactions).await; + results.iter().for_each(|result| { + println!("address: {}, amount: {}", result.address, result.amount); + }); } Ok(()) } diff --git a/notification-server/src/processor.rs b/notification-server/src/processor.rs index 1e0b3fe2..d64fa84b 100644 --- a/notification-server/src/processor.rs +++ b/notification-server/src/processor.rs @@ -14,12 +14,12 @@ use std::fmt::Debug; #[derive(Debug)] pub struct NotificationInformation { - effects: AccountAddress, - amount: BigInt, + pub address: AccountAddress, + pub amount: BigInt, } impl NotificationInformation { - pub fn new(effects: AccountAddress, amount: BigInt) -> Self { Self { effects, amount } } + pub fn new(address: AccountAddress, amount: BigInt) -> Self { Self { address, amount } } } fn convert>( From c8f5d244edb26d371e388ad26e27eef9c78db697 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 09:52:29 +0200 Subject: [PATCH 40/47] added args on listen addr --- notification-server/.env.template | 3 ++- notification-server/Cargo.toml | 4 ++-- notification-server/src/bin/{server.rs => api.rs} | 13 ++++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) rename notification-server/src/bin/{server.rs => api.rs} (78%) diff --git a/notification-server/.env.template b/notification-server/.env.template index 626eb378..0d232b74 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,2 +1,3 @@ PGPASSWORD=$DB_PASSWORD -NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" +NOTIFICATION_API_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" +NOTIFICATION_API_LISTEN_ADDRESS="0.0.0.0:3030" \ No newline at end of file diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index b14658df..b999680e 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -24,8 +24,8 @@ serde = { version = "1.0.203", features = ["derive"] } num-bigint = "0.4.5" [[bin]] -path = "src/bin/server.rs" -name = "notification-server" +path = "src/bin/api.rs" +name = "notification-api" [[bin]] path = "src/bin/service.rs" diff --git a/notification-server/src/bin/server.rs b/notification-server/src/bin/api.rs similarity index 78% rename from notification-server/src/bin/server.rs rename to notification-server/src/bin/api.rs index fcc3eb66..966ea61c 100644 --- a/notification-server/src/bin/server.rs +++ b/notification-server/src/bin/api.rs @@ -16,9 +16,16 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "NOTIFICATION_SERVER_DB_CONNECTION" + env = "NOTIFICATION_API_DB_CONNECTION" )] db_connection: String, + #[arg( + long = "listen-address", + help = "Listen address for the server.", + env = "NOTIFICATION_API_LISTEN_ADDRESS", + default_value = "0.0.0.0:3030" + )] + listen_address: std::net::SocketAddr, /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] log_level: log::LevelFilter, @@ -52,11 +59,11 @@ async fn main() -> anyhow::Result<()> { let app = Router::new() .route( - "/api/v1/device/:device/subscriptions", + "/api/v1/device/:device/subscription", put(upsert_account_device), ) .with_state(app_state); - let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?; + let listener = tokio::net::TcpListener::bind(args.listen_address).await?; axum::serve(listener, app).await?; Ok(()) } From 30954bac9c8229dcd9d36a86d4d59266c7095df0 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 09:54:12 +0200 Subject: [PATCH 41/47] added todo comments --- notification-server/src/bin/api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index 966ea61c..d7350b1a 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -43,7 +43,7 @@ async fn upsert_account_device( ) -> Result { info!("Subscribing accounts {:?} to device {}", account, device); let _ = &state.db_connection; - + // TODO write to the database Ok(StatusCode::OK) } @@ -56,7 +56,7 @@ async fn main() -> anyhow::Result<()> { let db_connection: Config = args.db_connection.parse()?; let app_state = Arc::new(AppState { db_connection }); - + // TODO add authentication middleware let app = Router::new() .route( "/api/v1/device/:device/subscription", From 036a08bfb82248d6a905de363b13b6d5a7f5177c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 09:56:51 +0200 Subject: [PATCH 42/47] fmt --- notification-server/src/bin/api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index d7350b1a..7b04909c 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -18,7 +18,7 @@ struct Args { application.", env = "NOTIFICATION_API_DB_CONNECTION" )] - db_connection: String, + db_connection: String, #[arg( long = "listen-address", help = "Listen address for the server.", @@ -28,7 +28,7 @@ struct Args { listen_address: std::net::SocketAddr, /// Logging level of the application #[arg(long = "log-level", default_value_t = log::LevelFilter::Info)] - log_level: log::LevelFilter, + log_level: log::LevelFilter, } #[derive(Clone)] From e6525278f50ac623cec2015f5966c0b94f943489 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 11:03:38 +0200 Subject: [PATCH 43/47] bumped env_logger --- notification-server/.env.template | 4 +-- notification-server/Cargo.lock | 49 ++++++++------------------ notification-server/Cargo.toml | 2 +- notification-server/src/bin/api.rs | 4 +-- notification-server/src/bin/service.rs | 2 +- 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index 0d232b74..27ccb648 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,3 +1,3 @@ PGPASSWORD=$DB_PASSWORD -NOTIFICATION_API_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" -NOTIFICATION_API_LISTEN_ADDRESS="0.0.0.0:3030" \ No newline at end of file +NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" +NOTIFICATION_SERVER_LISTEN_ADDRESS="0.0.0.0:3030" \ No newline at end of file diff --git a/notification-server/Cargo.lock b/notification-server/Cargo.lock index cefcb781..d6204bf8 100644 --- a/notification-server/Cargo.lock +++ b/notification-server/Cargo.lock @@ -1075,17 +1075,27 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + [[package]] name = "env_logger" -version = "0.10.2" +version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" dependencies = [ + "anstream", + "anstyle", + "env_filter", "humantime", - "is-terminal", "log", - "regex", - "termcolor", ] [[package]] @@ -1560,17 +1570,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "is-terminal" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -2750,15 +2749,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.61" @@ -3274,15 +3264,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/notification-server/Cargo.toml b/notification-server/Cargo.toml index b999680e..39a8a3cd 100644 --- a/notification-server/Cargo.toml +++ b/notification-server/Cargo.toml @@ -12,7 +12,7 @@ axum = "0.7.5" clap = { version = "4.5.7", features = ["derive", "env"] } concordium-rust-sdk = { version = "*", path = "../deps/concordium-rust-sdk" } dotenv = "0.15" -env_logger = "0.10" +env_logger = "0.11.3" futures = "0.3.30" log = "0.4.21" tokio = { version = "1.23", features = ["rt-multi-thread", "signal"] } diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index 7b04909c..b97279de 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -16,13 +16,13 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "NOTIFICATION_API_DB_CONNECTION" + env = "NOTIFICATION_SERVER_DB_CONNECTION" )] db_connection: String, #[arg( long = "listen-address", help = "Listen address for the server.", - env = "NOTIFICATION_API_LISTEN_ADDRESS", + env = "NOTIFICATION_SERVER_LISTEN_ADDRESS", default_value = "0.0.0.0:3030" )] listen_address: std::net::SocketAddr, diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index 8c42a7de..a914f08a 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -21,7 +21,7 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "NOTIFICATION_SERVER_DB_CONNECTION" + env = "NOTIFICATION_SERVER_DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, } From b2bc18005f40b04438a55feb8e39c50e16a22f60 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 11:04:53 +0200 Subject: [PATCH 44/47] fmt --- notification-server/src/bin/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/src/bin/service.rs b/notification-server/src/bin/service.rs index a914f08a..8c42a7de 100644 --- a/notification-server/src/bin/service.rs +++ b/notification-server/src/bin/service.rs @@ -21,7 +21,7 @@ struct Args { long = "db-connection", help = "A connection string detailing the connection to the database used by the \ application.", - env = "NOTIFICATION_SERVER_DB_CONNECTION" + env = "NOTIFICATION_SERVER_DB_CONNECTION" )] db_connection: tokio_postgres::config::Config, } From 1515cc8450e704faf7f951c2e8e94565b4017820 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 11:05:50 +0200 Subject: [PATCH 45/47] fix newline --- notification-server/.env.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/.env.template b/notification-server/.env.template index 27ccb648..21ec9828 100644 --- a/notification-server/.env.template +++ b/notification-server/.env.template @@ -1,3 +1,3 @@ PGPASSWORD=$DB_PASSWORD NOTIFICATION_SERVER_DB_CONNECTION="host=localhost dbname=notification-server user=postgres password=$DB_PASSWORD port=5432" -NOTIFICATION_SERVER_LISTEN_ADDRESS="0.0.0.0:3030" \ No newline at end of file +NOTIFICATION_SERVER_LISTEN_ADDRESS="0.0.0.0:3030" From f4de0642394a9124b3be39774e992f3e8ecf7f7a Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 11:09:02 +0200 Subject: [PATCH 46/47] change resource name --- notification-server/src/bin/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index b97279de..3b07d532 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -59,7 +59,7 @@ async fn main() -> anyhow::Result<()> { // TODO add authentication middleware let app = Router::new() .route( - "/api/v1/device/:device/subscription", + "/api/v1/device/:device/subscribe", put(upsert_account_device), ) .with_state(app_state); From 480d8f2289dcc574388770cfc2f2299f2e4102c1 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 24 Jun 2024 11:09:26 +0200 Subject: [PATCH 47/47] change again --- notification-server/src/bin/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notification-server/src/bin/api.rs b/notification-server/src/bin/api.rs index 3b07d532..b97279de 100644 --- a/notification-server/src/bin/api.rs +++ b/notification-server/src/bin/api.rs @@ -59,7 +59,7 @@ async fn main() -> anyhow::Result<()> { // TODO add authentication middleware let app = Router::new() .route( - "/api/v1/device/:device/subscribe", + "/api/v1/device/:device/subscription", put(upsert_account_device), ) .with_state(app_state);