From 557d15c5a738deae30392d5764c0b753465718db Mon Sep 17 00:00:00 2001 From: bitful-pannul Date: Thu, 19 Dec 2024 01:40:22 +0200 Subject: [PATCH] kns & app_store: add reset --- Cargo.lock | 11 +++++++++ Cargo.toml | 2 +- .../app-store/api/app-store:sys-v1.wit | 6 +++++ .../app-store/app-store/src/http_api.rs | 23 +++++++++++++++++++ kinode/packages/app-store/chain/src/lib.rs | 19 +++++++++++++-- kinode/packages/kns-indexer/Cargo.lock | 11 +++++++++ kinode/packages/kns-indexer/Cargo.toml | 1 + .../kns-indexer/api/kns-indexer:sys-v0.wit | 4 ++++ .../kns-indexer/kns-indexer/src/lib.rs | 15 +++++++++++- kinode/packages/kns-indexer/pkg/scripts.json | 12 ++++++++++ kinode/packages/kns-indexer/reset/Cargo.toml | 20 ++++++++++++++++ kinode/packages/kns-indexer/reset/src/lib.rs | 23 +++++++++++++++++++ 12 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 kinode/packages/kns-indexer/reset/Cargo.toml create mode 100644 kinode/packages/kns-indexer/reset/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 63707f15..d80242b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5684,6 +5684,17 @@ dependencies = [ "windows-registry", ] +[[package]] +name = "reset" +version = "0.1.0" +dependencies = [ + "kinode_process_lib 0.10.0", + "process_macros", + "serde", + "serde_json", + "wit-bindgen 0.36.0", +] + [[package]] name = "rfc6979" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 65a72d73..44701286 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ members = [ "kinode/packages/chess/chess", "kinode/packages/contacts/contacts", "kinode/packages/homepage/homepage", - "kinode/packages/kns-indexer/kns-indexer", "kinode/packages/kns-indexer/get-block", "kinode/packages/settings/settings", + "kinode/packages/kns-indexer/kns-indexer", "kinode/packages/kns-indexer/get-block", "kinode/packages/settings/settings", "kinode/packages/kns-indexer/reset", "kinode/packages/terminal/terminal", "kinode/packages/terminal/alias", "kinode/packages/terminal/cat", "kinode/packages/terminal/echo", "kinode/packages/terminal/help", "kinode/packages/terminal/hi", "kinode/packages/terminal/kfetch", diff --git a/kinode/packages/app-store/api/app-store:sys-v1.wit b/kinode/packages/app-store/api/app-store:sys-v1.wit index 6e317c15..0f1ec18f 100644 --- a/kinode/packages/app-store/api/app-store:sys-v1.wit +++ b/kinode/packages/app-store/api/app-store:sys-v1.wit @@ -134,6 +134,10 @@ interface chain { /// /// lazy-load-blob: none. stop-auto-update(package-id), + /// Reset app-store db + /// + /// lazy-load-blob: none. + reset, } /// Responses from the chain component @@ -149,6 +153,8 @@ interface chain { /// lazy-load-blob: none. auto-update-stopped, /// lazy-load-blob: none. + /// successful reset + reset-ok, err(chain-error), } diff --git a/kinode/packages/app-store/app-store/src/http_api.rs b/kinode/packages/app-store/app-store/src/http_api.rs index 5395325c..8ad2a408 100644 --- a/kinode/packages/app-store/app-store/src/http_api.rs +++ b/kinode/packages/app-store/app-store/src/http_api.rs @@ -40,6 +40,7 @@ pub fn init_frontend(our: &Address, http_server: &mut server::HttpServer) { "/apps/:id/install", // install a downloaded app "/downloads/:id/mirror", // start mirroring a version of a downloaded app "/downloads/:id/remove", // remove a downloaded app + "/reset", // reset chain state, re-index "/apps/:id/auto-update", // set auto-updating a version of a downloaded app "/updates/:id/clear", // clear update info for an app. "/mirrorcheck/:node", // check if a node/mirror is online/offline @@ -726,6 +727,28 @@ fn serve_paths( updates.save(); Ok((StatusCode::OK, None, vec![])) } + // POST reset chain state, re-index + "/reset" => { + if method != Method::POST { + return Ok(( + StatusCode::METHOD_NOT_ALLOWED, + None, + format!("Invalid method {method} for {bound_path}").into_bytes(), + )); + } + let chain = Address::from_str("our@chain:app-store:sys")?; + + let resp = Request::new() + .target(chain) + .body(&ChainRequests::Reset) + .send_and_await_response(5)??; + let msg = serde_json::from_slice::(resp.body())?; + if let ChainResponses::ResetOk = msg { + Ok((StatusCode::OK, None, vec![])) + } else { + Ok((StatusCode::INTERNAL_SERVER_ERROR, None, vec![])) + } + } // GET online/offline mirrors for a listed app "/mirrorcheck/:node" => { if method != Method::GET { diff --git a/kinode/packages/app-store/chain/src/lib.rs b/kinode/packages/app-store/chain/src/lib.rs index 5e25916a..a89b55e2 100644 --- a/kinode/packages/app-store/chain/src/lib.rs +++ b/kinode/packages/app-store/chain/src/lib.rs @@ -107,6 +107,12 @@ impl DB { Ok(Self { inner }) } + pub fn reset(&self, our: &Address) { + if let Err(e) = sqlite::remove_db(our.package_id(), "app_store_chain.sqlite", None) { + println!("failed to reset app_store DB: {e}"); + } + } + pub fn get_last_saved_block(&self) -> anyhow::Result { let query = "SELECT value FROM meta WHERE key = 'last_saved_block'"; let rows = self.inner.read(query.into(), vec![])?; @@ -419,7 +425,7 @@ fn handle_message(our: &Address, state: &mut State, message: &Message) -> anyhow } } Req::Request(chains) => { - handle_local_request(state, chains)?; + handle_local_request(our, state, chains)?; } } } @@ -427,7 +433,11 @@ fn handle_message(our: &Address, state: &mut State, message: &Message) -> anyhow Ok(()) } -fn handle_local_request(state: &mut State, req: ChainRequests) -> anyhow::Result<()> { +fn handle_local_request( + our: &Address, + state: &mut State, + req: ChainRequests, +) -> anyhow::Result<()> { match req { ChainRequests::GetApp(package_id) => { let pid = package_id.clone().to_process_lib(); @@ -480,6 +490,11 @@ fn handle_local_request(state: &mut State, req: ChainRequests) -> anyhow::Result Response::new().body(&error_response).send()?; } } + ChainRequests::Reset => { + state.db.reset(&our); + Response::new().body(&ChainResponses::ResetOk).send()?; + panic!("resetting state, restarting!"); + } } Ok(()) } diff --git a/kinode/packages/kns-indexer/Cargo.lock b/kinode/packages/kns-indexer/Cargo.lock index 8d5c6fad..1c2bb637 100644 --- a/kinode/packages/kns-indexer/Cargo.lock +++ b/kinode/packages/kns-indexer/Cargo.lock @@ -2383,6 +2383,17 @@ dependencies = [ "windows-registry", ] +[[package]] +name = "reset" +version = "0.1.0" +dependencies = [ + "kinode_process_lib", + "process_macros", + "serde", + "serde_json", + "wit-bindgen", +] + [[package]] name = "rfc6979" version = "0.4.0" diff --git a/kinode/packages/kns-indexer/Cargo.toml b/kinode/packages/kns-indexer/Cargo.toml index 8e902472..35d4c4f3 100644 --- a/kinode/packages/kns-indexer/Cargo.toml +++ b/kinode/packages/kns-indexer/Cargo.toml @@ -3,6 +3,7 @@ resolver = "2" members = [ "get-block", "kns-indexer", + "reset", ] [profile.release] diff --git a/kinode/packages/kns-indexer/api/kns-indexer:sys-v0.wit b/kinode/packages/kns-indexer/api/kns-indexer:sys-v0.wit index f49ba4ac..4d9e2f42 100644 --- a/kinode/packages/kns-indexer/api/kns-indexer:sys-v0.wit +++ b/kinode/packages/kns-indexer/api/kns-indexer:sys-v0.wit @@ -14,11 +14,15 @@ interface kns-indexer { /// returns an Option /// set block to 0 if you just want to get the current state of the indexer node-info(node-info-request), + /// resets and re-indexes the chain + /// returns a reset-ok + reset, } variant indexer-response { name(option), node-info(option), + reset-ok, } record namehash-to-name-request { diff --git a/kinode/packages/kns-indexer/kns-indexer/src/lib.rs b/kinode/packages/kns-indexer/kns-indexer/src/lib.rs index 6dc74763..b1615301 100644 --- a/kinode/packages/kns-indexer/kns-indexer/src/lib.rs +++ b/kinode/packages/kns-indexer/kns-indexer/src/lib.rs @@ -106,6 +106,14 @@ impl State { state } + /// Reset by removing the database and reloading fresh state + fn reset(&self, our: &Address) { + // Remove the entire database + if let Err(e) = kv::remove_db(our.package_id(), "kns_indexer", None) { + println!("Warning: error removing kns_indexer database: {e:?}"); + } + } + fn meta_version_key() -> String { "meta:version".to_string() } @@ -364,7 +372,6 @@ fn main(our: Address, mut state: State) -> anyhow::Result<()> { .body(IndexerResponse::Name(state.get_name(hash))) .send()?; } - IndexerRequest::NodeInfo(NodeInfoRequest { ref name, .. }) => { Response::new() .body(&IndexerResponse::NodeInfo( @@ -374,6 +381,12 @@ fn main(our: Address, mut state: State) -> anyhow::Result<()> { )) .send()?; } + IndexerRequest::Reset => { + // Reload state fresh - this will create new db + state.reset(&our); + Response::new().body(IndexerResponse::ResetOk).send()?; + panic!("resetting state, restarting!"); + } } } } diff --git a/kinode/packages/kns-indexer/pkg/scripts.json b/kinode/packages/kns-indexer/pkg/scripts.json index 25798ef7..a9706f02 100644 --- a/kinode/packages/kns-indexer/pkg/scripts.json +++ b/kinode/packages/kns-indexer/pkg/scripts.json @@ -10,5 +10,17 @@ "eth:distro:sys" ], "wit_version": 1 + }, + "reset.wasm": { + "root": false, + "public": false, + "request_networking": false, + "request_capabilities": [ + "kns-indexer:kns-indexer:sys" + ], + "grant_capabilities": [ + "kns-indexer:kns-indexer:sys" + ], + "wit_version": 1 } } diff --git a/kinode/packages/kns-indexer/reset/Cargo.toml b/kinode/packages/kns-indexer/reset/Cargo.toml new file mode 100644 index 00000000..d338f61b --- /dev/null +++ b/kinode/packages/kns-indexer/reset/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "reset" +version = "0.1.0" +edition = "2021" + +[features] +simulation-mode = [] + +[dependencies] +kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "d97e012" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +process_macros = "0.1" +wit-bindgen = "0.36.0" + +[lib] +crate-type = ["cdylib"] + +[package.metadata.component] +package = "kinode:process" diff --git a/kinode/packages/kns-indexer/reset/src/lib.rs b/kinode/packages/kns-indexer/reset/src/lib.rs new file mode 100644 index 00000000..78cc4d7b --- /dev/null +++ b/kinode/packages/kns-indexer/reset/src/lib.rs @@ -0,0 +1,23 @@ +use std::str::FromStr; + +use kinode::process::kns_indexer::IndexerRequest; +use kinode_process_lib::{call_init, Address, Request}; + +wit_bindgen::generate!({ + path: "target/wit", + world: "kns-indexer-sys-v0", + generate_unused_types: true, + additional_derives: [serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto], +}); + +call_init!(init); +fn init(_our: Address) { + // request timeout of 5s + let kns = Address::from_str("our@kns-indexer:kns-indexer:sys").unwrap(); + + let _resp = Request::to(kns) + .body(IndexerRequest::Reset) + .send_and_await_response(5) + .unwrap() + .unwrap(); +}