kns & app_store: add reset

This commit is contained in:
bitful-pannul 2024-12-19 01:40:22 +02:00
parent a6bb05173c
commit 557d15c5a7
12 changed files with 143 additions and 4 deletions

11
Cargo.lock generated
View File

@ -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"

View File

@ -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",

View File

@ -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),
}

View File

@ -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::<ChainResponses>(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 {

View File

@ -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<u64> {
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(())
}

View File

@ -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"

View File

@ -3,6 +3,7 @@ resolver = "2"
members = [
"get-block",
"kns-indexer",
"reset",
]
[profile.release]

View File

@ -14,11 +14,15 @@ interface kns-indexer {
/// returns an Option<KnsUpdate>
/// 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<string>),
node-info(option<wit-kns-update>),
reset-ok,
}
record namehash-to-name-request {

View File

@ -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!");
}
}
}
}

View File

@ -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
}
}

View File

@ -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"

View File

@ -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();
}