From b4e435f534afe5444bd0bd668d2500d8f67c9c80 Mon Sep 17 00:00:00 2001 From: Tjemmmic Date: Tue, 29 Oct 2024 18:47:34 -0500 Subject: [PATCH] fix!: test fixes, debugging session key and node execution --- Cargo.lock | 130 ++++++++++++++++++++++++-------------------- src/lib.rs | 107 ++++++++++++++++++++---------------- src/main.rs | 27 ++++++--- src/utils/tangle.rs | 111 +++++++++++++++++++++++++------------ 4 files changed, 227 insertions(+), 148 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9928d4a..b2d3a9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,11 +128,11 @@ checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "alloy-chains" -version = "0.1.40" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4932d790c723181807738cf1ac68198ab581cd699545b155601332541ee47bd" +checksum = "c660915971620592abe2c292c859957eb60e73a60c0eba34a6793eea60512cff" dependencies = [ - "alloy-primitives 0.8.9", + "alloy-primitives 0.8.10", "num_enum", "strum", ] @@ -271,9 +271,9 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.8.9" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c71738eb20c42c5fb149571e76536a0f309d142f3957c28791662b96baf77a3d" +checksum = "8edae627382349b56cd6a7a2106f4fd69b243a9233e560c55c2e03cabb7e1d3c" dependencies = [ "bytes", "cfg-if", @@ -312,7 +312,7 @@ dependencies = [ "futures-utils-wasm", "lru", "pin-project", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde", "serde_json", "tokio", @@ -376,7 +376,7 @@ dependencies = [ "futures", "hyper-util", "pin-project", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde", "serde_json", "tokio", @@ -576,7 +576,7 @@ dependencies = [ "http-body-util", "hyper 1.5.0", "hyper-util", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde_json", "tower", "tracing", @@ -593,7 +593,7 @@ dependencies = [ "alloy-transport", "futures", "http 1.1.0", - "rustls 0.23.15", + "rustls 0.23.16", "serde_json", "tokio", "tokio-tungstenite 0.23.1", @@ -1031,7 +1031,7 @@ dependencies = [ "futures-lite", "parking", "polling", - "rustix 0.38.37", + "rustix 0.38.38", "slab", "tracing", "windows-sys 0.59.0", @@ -1074,7 +1074,7 @@ dependencies = [ "cfg-if", "event-listener 5.3.1", "futures-lite", - "rustix 0.38.37", + "rustix 0.38.38", "tracing", ] @@ -1090,7 +1090,7 @@ dependencies = [ "cfg-if", "futures-core", "futures-io", - "rustix 0.38.37", + "rustix 0.38.38", "signal-hook-registry", "slab", "windows-sys 0.59.0", @@ -1306,9 +1306,9 @@ dependencies = [ [[package]] name = "aws-sdk-kms" -version = "1.47.0" +version = "1.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "564a597a3c71a957d60a2e4c62c93d78ee5a0d636531e15b760acad983a5c18e" +checksum = "2afbd208dabc6785946d4ef2444eb1f54fe0aaf0f62f2a4f9a9e9c303aeff0be" dependencies = [ "aws-credential-types", "aws-runtime", @@ -1328,9 +1328,9 @@ dependencies = [ [[package]] name = "aws-sigv4" -version = "1.2.4" +version = "1.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc8db6904450bafe7473c6ca9123f88cc11089e41a025408f992db4e22d3be68" +checksum = "5619742a0d8f253be760bfbb8e8e8368c69e3587e4637af5754e488a611499b1" dependencies = [ "aws-credential-types", "aws-smithy-http", @@ -1767,7 +1767,7 @@ dependencies = [ "itertools 0.13.0", "libp2p", "parking_lot", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde", "sha2 0.10.8", "sp-core", @@ -1796,6 +1796,7 @@ dependencies = [ "futures", "gadget-io", "gadget-sdk", + "lazy_static", "libp2p", "log", "parking_lot", @@ -3177,7 +3178,7 @@ dependencies = [ "eigen-logging", "eigen-signer", "k256", - "reqwest 0.12.8", + "reqwest 0.12.9", "thiserror", ] @@ -3482,7 +3483,7 @@ dependencies = [ "alloy-sol-types", "alloy-transport", "alloy-transport-http", - "reqwest 0.12.8", + "reqwest 0.12.9", ] [[package]] @@ -3660,9 +3661,9 @@ dependencies = [ [[package]] name = "escargot" -version = "0.5.12" +version = "0.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c000f23e9d459aef148b7267e02b03b94a0aaacf4ec64c65612f67e02f525fb6" +checksum = "05a3ac187a16b5382fef8c69fd1bad123c67b7cf3932240a2d43dcdd32cded88" dependencies = [ "log", "once_cell", @@ -4402,7 +4403,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f2f12607f92c69b12ed746fabf9ca4f5c482cba46679c1a75b874ed7c26adb" dependencies = [ "futures-io", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-pki-types", ] @@ -4581,6 +4582,7 @@ dependencies = [ "subxt", "subxt-core", "subxt-signer", + "symbiotic-rs", "sysinfo", "tangle-subxt", "thiserror", @@ -5402,7 +5404,7 @@ dependencies = [ "hyper 1.5.0", "hyper-util", "log", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-native-certs 0.8.0", "rustls-pki-types", "tokio", @@ -5442,9 +5444,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" +checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4" dependencies = [ "bytes", "futures-channel", @@ -5869,7 +5871,7 @@ dependencies = [ "http 1.1.0", "jsonrpsee-core 0.23.2", "pin-project", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-pki-types", "rustls-platform-verifier", "soketto 0.8.0", @@ -6134,9 +6136,9 @@ dependencies = [ [[package]] name = "libm" -version = "0.2.8" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" [[package]] name = "libp2p" @@ -6498,7 +6500,7 @@ dependencies = [ "quinn", "rand", "ring 0.17.8", - "rustls 0.23.15", + "rustls 0.23.16", "socket2", "thiserror", "tokio", @@ -6617,7 +6619,7 @@ dependencies = [ "libp2p-identity", "rcgen", "ring 0.17.8", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-webpki 0.101.7", "thiserror", "x509-parser", @@ -6914,7 +6916,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64" dependencies = [ - "rustix 0.38.37", + "rustix 0.38.38", ] [[package]] @@ -7364,9 +7366,9 @@ dependencies = [ [[package]] name = "ntex-h2" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e01b86bf30768ed7dca26bf279d0e0798ba5acf0baef4b0ea8e17a91ba71ad4" +checksum = "52c26686e5f3b21552a554e83b5a3312a00038d82e262d6e01f507e2f81bda8a" dependencies = [ "bitflags 2.6.0", "fxhash", @@ -8347,7 +8349,7 @@ dependencies = [ "concurrent-queue", "hermit-abi 0.4.0", "pin-project-lite", - "rustix 0.38.37", + "rustix 0.38.38", "tracing", "windows-sys 0.59.0", ] @@ -8595,7 +8597,7 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash 2.0.0", - "rustls 0.23.15", + "rustls 0.23.16", "socket2", "thiserror", "tokio", @@ -8612,7 +8614,7 @@ dependencies = [ "rand", "ring 0.17.8", "rustc-hash 2.0.0", - "rustls 0.23.15", + "rustls 0.23.16", "slab", "thiserror", "tinyvec", @@ -8621,10 +8623,11 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fe68c2e9e1a1234e218683dbdf9f9dfcb094113c5ac2b938dfcb9bab4c4140b" +checksum = "e346e016eacfff12233c243718197ca12f148c84e1e84268a896699b41c71780" dependencies = [ + "cfg_aliases 0.2.1", "libc", "once_cell", "socket2", @@ -8909,9 +8912,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.12.8" +version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f713147fbe92361e52392c73b8c9e48c04c6625bce969ef54dc901e58e042a7b" +checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ "base64 0.22.1", "bytes", @@ -8936,7 +8939,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "quinn", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-native-certs 0.8.0", "rustls-pemfile 2.2.0", "rustls-pki-types", @@ -9251,9 +9254,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.37" +version = "0.38.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" dependencies = [ "bitflags 2.6.0", "errno", @@ -9290,9 +9293,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.15" +version = "0.23.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fbb44d7acc4e873d613422379f69f237a1b141928c02f6bc6ccfddddc2d7993" +checksum = "eee87ff5d9b36712a58574e12e9f0ea80f915a5b0ac518d322b24a465617925e" dependencies = [ "aws-lc-rs", "log", @@ -9377,7 +9380,7 @@ dependencies = [ "jni", "log", "once_cell", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-native-certs 0.7.3", "rustls-platform-verifier-android", "rustls-webpki 0.102.8", @@ -9827,9 +9830,9 @@ checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" [[package]] name = "serde" -version = "1.0.213" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" +checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" dependencies = [ "serde_derive", ] @@ -9845,9 +9848,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.213" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" +checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" dependencies = [ "proc-macro2", "quote", @@ -11221,6 +11224,17 @@ dependencies = [ "zip", ] +[[package]] +name = "symbiotic-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02f31217207638b7cba9751cedb9ddb612478423d7bf8700091cf1f9970bbc5f" +dependencies = [ + "alloy-contract", + "alloy-sol-types", + "serde", +] + [[package]] name = "syn" version = "1.0.109" @@ -11367,7 +11381,7 @@ dependencies = [ "hex", "lock_api", "parking_lot", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde_json", "structopt", "tokio", @@ -11412,7 +11426,7 @@ checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", "fastrand", - "rustix 0.38.37", + "rustix 0.38.38", "windows-sys 0.52.0", ] @@ -11452,7 +11466,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f599bd7ca042cfdf8f4512b277c02ba102247820f9d9d4a9f521f496751a6ef" dependencies = [ - "rustix 0.38.37", + "rustix 0.38.38", "windows-sys 0.59.0", ] @@ -11474,7 +11488,7 @@ dependencies = [ "memchr", "parse-display", "pin-project-lite", - "reqwest 0.12.8", + "reqwest 0.12.9", "serde", "serde_json", "serde_with", @@ -11673,7 +11687,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.15", + "rustls 0.23.16", "rustls-pki-types", "tokio", ] @@ -11713,7 +11727,7 @@ checksum = "c6989540ced10490aaf14e6bad2e3d33728a2813310a0c71d1574304c49631cd" dependencies = [ "futures-util", "log", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-pki-types", "tokio", "tokio-rustls 0.26.0", @@ -11987,7 +12001,7 @@ dependencies = [ "httparse", "log", "rand", - "rustls 0.23.15", + "rustls 0.23.16", "rustls-pki-types", "sha1", "thiserror", @@ -12569,7 +12583,7 @@ dependencies = [ "either", "home", "once_cell", - "rustix 0.38.37", + "rustix 0.38.38", ] [[package]] diff --git a/src/lib.rs b/src/lib.rs index 908ef3e..b4d5660 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,11 @@ pub use crate::utils::eigenlayer::*; -use crate::utils::tangle::{generate_keys, register_operator_to_tangle}; +use crate::utils::tangle::update_session_key; pub use crate::utils::tangle::{run_tangle_validator, BalanceTransferContext}; use color_eyre::eyre::Result; -use gadget_sdk::config::{ContextConfig, GadgetCLICoreSettings, Protocol}; +use gadget_sdk::config::GadgetConfiguration; use gadget_sdk::event_listener::tangle::{TangleEvent, TangleEventListener}; use gadget_sdk::{info, job}; use std::convert::Infallible; -use std::net::IpAddr; -use std::str::FromStr; pub mod utils; @@ -32,15 +30,13 @@ pub async fn register_to_tangle( { info!("Balance Transfer Event Found: {:?} sent {:?} tTNT to {:?}", balance_transfer.from.to_string(), balance_transfer.amount, balance_transfer.to.to_string()); - // let _ = register_operator_to_tangle().await.unwrap(); - - return if event.stop() { - info!("Successfully stopped job"); - Ok(0) - } else { - info!("Failed to stop job"); - Ok(1) - } + // return if event.stop() { + // info!("Successfully stopped job"); + // Ok(0) + // } else { + // info!("Failed to stop job"); + // Ok(1) + // } } Ok(0) } @@ -62,21 +58,27 @@ pub async fn register_to_tangle( // } pub async fn tangle_avs_registration( - // env: &GadgetConfiguration, - context: BalanceTransferContext, + env: &GadgetConfiguration, + _context: BalanceTransferContext, ) -> Result<(), gadget_sdk::Error> { info!("TANGLE AVS REGISTRATION HOOK"); - let _node_key = generate_keys().await.map_err(|e| gadget_sdk::Error::Job { - reason: e.to_string(), - })?; + // info!("Registering to EigenLayer"); + // register_to_eigenlayer(&env.clone()).await?; // Run Tangle Validator - let _tangle_stream = run_tangle_validator().await.unwrap(); // We need to return necessary values - tokio::time::sleep(std::time::Duration::from_secs(4)).await; + run_tangle_validator().await.unwrap(); - // info!("Registering to EigenLayer"); - // register_to_eigenlayer(&env.clone()).await?; + tokio::time::sleep(std::time::Duration::from_secs(4)).await; // Let Node start up + + // proxy_and_stash(&env.clone()).await.map_err(|e| gadget_sdk::Error::Job { + // reason: "Stash or Proxy failed".to_string(), + // })?; + + // Rotate Keys and Update Session Key + update_session_key(&env.clone()).await.unwrap(); + + // Validate! // info!("Registering to Tangle"); // register_operator_to_tangle(&self.env.clone()).await?; @@ -88,18 +90,19 @@ pub async fn tangle_avs_registration( mod tests { use super::*; use crate::utils::sol_imports::*; - use crate::utils::tangle::update_session_key; use alloy_provider::Provider; use blueprint_test_utils::inject_test_keys; use blueprint_test_utils::test_ext::NAME_IDS; + use gadget_sdk::config::protocol::TangleInstanceSettings; use gadget_sdk::config::{ContextConfig, GadgetCLICoreSettings, Protocol}; use gadget_sdk::ext::sp_core; use gadget_sdk::ext::sp_core::Pair; use gadget_sdk::ext::subxt::tx::Signer; - use gadget_sdk::job_runner::{JobBuilder, MultiJobRunner}; use gadget_sdk::keystore::backend::fs::FilesystemKeystore; use gadget_sdk::keystore::backend::GenericKeyStore; use gadget_sdk::keystore::{Backend, BackendExt}; + use gadget_sdk::runners::tangle::TangleConfig; + use gadget_sdk::runners::BlueprintRunner; use gadget_sdk::{error, info}; use std::net::IpAddr; use std::path::PathBuf; @@ -119,7 +122,6 @@ mod tests { blueprint_test_utils::anvil::start_anvil_container(ANVIL_STATE_PATH, false).await; std::env::set_var("EIGENLAYER_HTTP_ENDPOINT", http_endpoint.clone()); std::env::set_var("EIGENLAYER_WS_ENDPOINT", ws_endpoint.clone()); - // std::env::set_var("REGISTRATION_MODE_ON", "true"); std::env::set_var( "REGISTRY_COORDINATOR_ADDR", @@ -216,12 +218,6 @@ mod tests { .unwrap(); let transfer_destination = operator_keystore.sr25519_key().unwrap().account_id(); - // let alice_keystore_uri = keystore_paths[0].clone(); - // let alice_keystore = - // gadget_sdk::keystore::backend::fs::FilesystemKeystore::open(alice_keystore_uri.clone()) - // .unwrap(); - // let transfer_destination = alice_keystore.sr25519_key().unwrap().account_id(); - let bob_keystore_uri = keystore_paths[1].clone(); let bob_keystore = gadget_sdk::keystore::backend::fs::FilesystemKeystore::open(bob_keystore_uri).unwrap(); @@ -240,14 +236,23 @@ mod tests { verbose: 3, pretty: true, keystore_password: None, - blueprint_id: 0, + blueprint_id: Some(0), service_id: Some(0), + skip_registration: Some(true), protocol: Protocol::Tangle, registry_coordinator: Some(REGISTRY_COORDINATOR_ADDR), operator_state_retriever: Some(OPERATOR_STATE_RETRIEVER_ADDR), delegation_manager: Some(DELEGATION_MANAGER_ADDR), ws_rpc_url: ws_tangle_url, strategy_manager: Some(STRATEGY_MANAGER_ADDR), + avs_directory: Some(AVS_DIRECTORY_ADDR), + operator_registry: None, + network_registry: None, + base_delegator: None, + network_opt_in_service: None, + vault_opt_in_service: None, + slasher: None, + veto_slasher: None, }, }; let env = gadget_sdk::config::load(config).expect("Failed to load environment"); @@ -282,20 +287,29 @@ mod tests { address: Default::default(), }; + tangle_avs_registration(&env.clone(), context.clone()) + .await + .unwrap(); + + let tangle_settings = env.protocol_specific.tangle().unwrap(); + let TangleInstanceSettings { service_id, .. } = tangle_settings; + let tangle_avs = RegisterToTangleEventHandler { - service_id: env.service_id.unwrap(), + service_id: *service_id, context: context.clone(), client, signer, }; info!("~~~ Executing the Tangle AVS ~~~"); - let error = MultiJobRunner::new(env.clone()) - .job(JobBuilder::new(tangle_avs).registration(context, tangle_avs_registration)) + let tangle_config = TangleConfig { + price_targets: Default::default(), + }; + BlueprintRunner::new(tangle_config, env.clone()) + .job(tangle_avs) .run() - .await; - - update_session_key(&env.clone()).await.unwrap(); + .await + .unwrap(); info!("Exiting..."); } @@ -384,7 +398,7 @@ mod tests { let keystore_uri_str = format!("file:{}", keystore_uri_normalized.display()); keystore_paths.push(keystore_uri_str.clone()); - tokio::fs::create_dir_all(keystore_uri_str.clone()) + tokio::fs::create_dir_all(keystore_uri.clone()) .await .unwrap(); let keystore = GenericKeyStore::::Fs( @@ -392,18 +406,15 @@ mod tests { ); let acco_suri = std::env::var("ACCO_SURI").expect("ACCO_SURI not set"); - let suri = format!("//{acco_suri}"); - let acco = - sp_core::sr25519::Pair::from_string(&suri, None).expect("Should be valid SR keypair"); - let acco_seed = &acco.as_ref().secret.to_bytes(); + // let suri = format!("//{acco_suri}"); + let (_acco, acco_seed) = sp_core::sr25519::Pair::from_phrase(&acco_suri, None) + .expect("Should be valid SR keypair"); info!("Found SR_SEED: {:?}", acco_seed); let role_suri = std::env::var("ROLE_SURI").expect("ROLE_SURI not set"); - let suri = format!("//{role_suri}"); - let role = - sp_core::ecdsa::Pair::from_string(&suri, None).expect("Should be valid ECDSA keypair"); - let role_seed = role.seed(); - info!("Found SR_SEED: {:?}", role_seed); + let (_role, role_seed) = sp_core::ecdsa::Pair::from_phrase(&role_suri, None) + .expect("Should be valid ECDSA keypair"); + info!("Found ROLE_SEED: {:?}", role_seed); keystore .sr25519_generate_new(Some(acco_seed.as_ref())) diff --git a/src/main.rs b/src/main.rs index 5ba307e..31cd8f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use color_eyre::eyre::eyre; use color_eyre::Result; +use gadget_sdk::config::protocol::TangleInstanceSettings; use gadget_sdk::info; -use gadget_sdk::job_runner::{JobBuilder, MultiJobRunner}; +use gadget_sdk::runners::tangle::TangleConfig; +use gadget_sdk::runners::BlueprintRunner; use gadget_sdk::subxt_core::tx::signer::Signer; use tangle_avs as blueprint; -use tangle_avs::tangle_avs_registration; +use tangle_avs::{tangle_avs_registration, RegisterToTangleEventHandler}; #[gadget_sdk::main(env)] async fn main() { @@ -18,18 +20,29 @@ async fn main() { address: Default::default(), }; - let tangle_avs = blueprint::RegisterToTangleEventHandler { - service_id: env.service_id.unwrap(), + tangle_avs_registration(&env.clone(), context.clone()) + .await + .unwrap(); + + let tangle_settings = env.protocol_specific.tangle().unwrap(); + let TangleInstanceSettings { service_id, .. } = tangle_settings; + + let tangle_avs = RegisterToTangleEventHandler { + service_id: *service_id, context: context.clone(), client, signer, }; info!("~~~ Executing the Tangle AVS ~~~"); - MultiJobRunner::new(env) - .job(JobBuilder::new(tangle_avs).registration(context, tangle_avs_registration)) + let tangle_config = TangleConfig { + price_targets: Default::default(), + }; + BlueprintRunner::new(tangle_config, env.clone()) + .job(tangle_avs) .run() - .await?; + .await + .unwrap(); info!("Exiting..."); Ok(()) diff --git a/src/utils/tangle.rs b/src/utils/tangle.rs index 6cd1cca..fef3ffc 100644 --- a/src/utils/tangle.rs +++ b/src/utils/tangle.rs @@ -11,7 +11,11 @@ use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::proxy::calls::types:: use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::staking::calls::types; use gadget_sdk::{info, trace, tx}; use std::os::unix::fs::PermissionsExt; -use tokio::sync::broadcast; +use gadget_sdk::tangle_subxt::parity_scale_codec::DecodeAll; +use gadget_sdk::tangle_subxt::subxt::backend::rpc::RpcClient; +use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types; +use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_testnet_runtime::opaque::SessionKeys; +use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::session::calls::types::set_keys::Proof; use url::Url; #[derive(Clone)] @@ -20,42 +24,36 @@ pub struct BalanceTransferContext { pub address: Address, } -pub async fn register_operator_to_tangle( - env: &GadgetConfiguration, -) -> Result<()> { - // Register Session Key with the Network for the Node - // curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys", "params":[]}' http://localhost:bind_port - +pub async fn proxy_and_stash(env: &GadgetConfiguration) -> Result<()> { let client = env.client().await.map_err(|e| eyre!(e))?; let _ecdsa_pair = env.first_ecdsa_signer().map_err(|e| eyre!(e))?; let sr25519_pair = env.first_sr25519_signer().map_err(|e| eyre!(e))?; let account_id = sr25519_pair.account_id(); - // // ---------- Add Proxy ---------- - // let add_proxy_tx = api::tx().proxy().add_proxy( - // Delegate::from(account_id.clone()), - // ProxyType::NonTransfer, - // Delay::from(0u32), - // ); - // let result = tx::tangle::send(&client, &sr25519_pair, &add_proxy_tx).await?; - // info!("Add Proxy Result: {:?}", result); - // - // // ---------- Stash Account Bonding ---------- - // let bond_stash_tx = api::tx().staking().bond( - // types::bond::Value::from(1000u16), - // types::bond::Payee::Account(account_id), // TODO: Make this not hardcoded? - // ); - // let result = tx::tangle::send(&client, &sr25519_pair, &bond_stash_tx).await?; - // info!("Stash Account Bonding Result: {:?}", result); + // ---------- Add Proxy ---------- + let add_proxy_tx = api::tx().proxy().add_proxy( + Delegate::from(account_id.clone()), + ProxyType::NonTransfer, + Delay::from(0u32), + ); + let result = tx::tangle::send(&client, &sr25519_pair, &add_proxy_tx).await?; + info!("Add Proxy Result: {:?}", result); + + // ---------- Stash Account Bonding ---------- + let bond_stash_tx = api::tx().staking().bond( + types::bond::Value::from(1000u16), + types::bond::Payee::Account(account_id), // TODO: Make this not hardcoded? + ); + let result = tx::tangle::send(&client, &sr25519_pair, &bond_stash_tx).await?; + info!("Stash Account Bonding Result: {:?}", result); Ok(()) } pub async fn update_session_key(env: &GadgetConfiguration) -> Result<()> { - let client = env.client().await.map_err(|e| eyre!(e))?; + let tangle_client = env.client().await.map_err(|e| eyre!(e))?; let _ecdsa_pair = env.first_ecdsa_signer().map_err(|e| eyre!(e))?; let sr25519_pair = env.first_sr25519_signer().map_err(|e| eyre!(e))?; - let account_id = sr25519_pair.account_id(); let url = Url::parse(&env.http_rpc_endpoint).map_err(|e| eyre!(e))?; // First, rotate keys @@ -79,9 +77,39 @@ pub async fn update_session_key(env: &GadgetConfiguration::new( + RpcClient::from_url(env.ws_rpc_endpoint.clone()) + .await + .map_err(|e| eyre!(e))?, + ) + .author_rotate_keys() + .await + .map_err(|e| eyre!(e))?; + if session_keys.len() != 96 { + return Err(eyre!("Invalid session key length")); + } + + let mut babe = &session_keys[0..32]; + let mut grandpa = &session_keys[32..64]; + let mut imonline = &session_keys[64..96]; + // // Set Session Key - // let bond_stash_tx = api::tx() - // let result = tx::tangle::send(&client, &sr25519_pair, &bond_stash_tx).await?; + let set_session_key_tx = api::tx().session().set_keys( + SessionKeys { + babe: runtime_types::sp_consensus_babe::app::Public::decode_all(&mut babe)?, + grandpa: runtime_types::sp_consensus_grandpa::app::Public::decode_all(&mut grandpa)?, + im_online: runtime_types::pallet_im_online::sr25519::app_sr25519::Public::decode_all( + &mut imonline, + )?, + }, + Proof::from(Vec::new()), + ); + + // TODO: This currently fails with a `Metadata(IncompatibleCodegen)` error + let _result = tx::tangle::send(&tangle_client, &sr25519_pair, &set_session_key_tx).await; Ok(()) } @@ -155,7 +183,7 @@ pub async fn generate_keys() -> Result { /// - The binary download fails /// - Setting executable permissions fails /// - The binary execution fails -pub async fn run_tangle_validator() -> Result> { +pub async fn run_tangle_validator() -> Result<()> { let mut manager = GadgetProcessManager::new(); // Check if the binary exists @@ -187,12 +215,16 @@ pub async fn run_tangle_validator() -> Result> { .map_err(|e| eyre!(e.to_string()))?; } + let _node_key = generate_keys().await.map_err(|e| gadget_sdk::Error::Job { + reason: e.to_string(), + })?; + let base_path = "test"; let chain = "local"; let name = "TESTNODE"; let validator = "--validator"; let telemetry_url = "\"wss://telemetry.polkadot.io/submit/ 1\""; - let rpc_port = "9948"; + let _rpc_port = "9948"; let start_node_command = format!( "./tangle-default-linux-amd64 \ @@ -201,14 +233,23 @@ pub async fn run_tangle_validator() -> Result> { --name {name} \ {validator} \ --telemetry-url {telemetry_url}\ - --rpc-port {rpc_port} \ " ); // Start the validator - let validator_stream = manager - .start_process_and_get_output("tangle_validator".into(), start_node_command.as_str()) - .await - .map_err(|e| eyre!(e.to_string()))?; - Ok(validator_stream) + // TODO: Node is dying or getting stuck for some reason + let _validator_task = tokio::spawn(async move { + let _validator_stream = manager + .run("tangle_validator".into(), start_node_command.as_str()) + .await + .map_err(|e| eyre!(e.to_string())) + .unwrap(); + manager + .focus_service_to_completion("tangle_validator".into()) + .await + .map_err(|e| eyre!(e.to_string())) + .unwrap(); + }); + + Ok(()) }