From cc89bb1808d965aef327f01e9893527f8a0806b4 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Tue, 13 Aug 2024 12:39:43 +0300 Subject: [PATCH] blank node skolemization should work without BASE --- ng-net/src/app_protocol.rs | 4 +- ng-sdk-js/app-node/index.js | 6 +- ng-verifier/src/commits/transaction.rs | 95 ++++++++++++-------------- 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/ng-net/src/app_protocol.rs b/ng-net/src/app_protocol.rs index 0537d77..bb607ef 100644 --- a/ng-net/src/app_protocol.rs +++ b/ng-net/src/app_protocol.rs @@ -248,7 +248,7 @@ impl NuriV0 { } pub fn repo_skolem( - prefix: &String, + repo_id: &RepoId, peer_id: &Vec, random: u128, ) -> Result { @@ -256,7 +256,7 @@ impl NuriV0 { arr.extend_from_slice(peer_id); arr.extend_from_slice(&random.to_be_bytes()); let sko: SymKey = arr.as_slice().try_into()?; - Ok(format!("{prefix}:u:{sko}")) + Ok(format!("{DID_PREFIX}:o:{repo_id}:u:{sko}")) } pub fn overlay_id(overlay_id: &OverlayId) -> String { diff --git a/ng-sdk-js/app-node/index.js b/ng-sdk-js/app-node/index.js index 234fdc8..c0d89ab 100644 --- a/ng-sdk-js/app-node/index.js +++ b/ng-sdk-js/app-node/index.js @@ -27,7 +27,7 @@ ng.init_headless(config).then( async() => { let user_id = "NnAJWxO-KapuWyCm7RGwO5VszZwaJARGit-i3i1mXbkA"; - let base = "did:ng:o:8mqfhoSprneBjkAASinRk0OYvFpbiyhjMBVHKQIarDEA:v:dmn9xLARD-LrCz1tdmRiTKelikOCadGEvsLklUrwee4A"; + let base = "did:ng:o:8mqfhoSprneBjkAASinRk0OYvFpbiyhjMBVHKQIarDEA"; let session = await ng.session_headless_start(user_id); session_id = session.session_id; @@ -67,8 +67,8 @@ ng.init_headless(config).then( async() => { //await ng.sparql_update(session.session_id, "INSERT DATA { <> . }",base); - await ng.sparql_update(session.session_id, "INSERT DATA { _:_ . _:_ . }", base); - await ng.sparql_update(session.session_id, "INSERT DATA { [ ]. }", base); + await ng.sparql_update(session.session_id, "INSERT DATA { _:_ . _:_ . }"); + await ng.sparql_update(session.session_id, "INSERT DATA { [ ]. }"); sparql_result = await ng.sparql_query(session.session_id, "SELECT ?a WHERE { ?a _:abc. _:abc }", base); console.log(sparql_result); diff --git a/ng-verifier/src/commits/transaction.rs b/ng-verifier/src/commits/transaction.rs index 85dd63d..13157b9 100644 --- a/ng-verifier/src/commits/transaction.rs +++ b/ng-verifier/src/commits/transaction.rs @@ -328,12 +328,12 @@ impl Verifier { &self, quad: &Quad, branches: &mut HashMap, - nuri_branches: &mut HashMap, - ) -> Result<(BranchId, bool), VerifierError> { + nuri_branches: &mut HashMap, + ) -> Result<(RepoId, BranchId, bool), VerifierError> { match &quad.graph_name { GraphName::NamedNode(named_node) => { let graph_name = named_node.as_string(); - log_debug!("graph_name {graph_name}"); + //log_debug!("graph_name {graph_name}"); if let Some(branch_found) = nuri_branches.get(graph_name) { return Ok(branch_found.clone()); } @@ -378,10 +378,12 @@ impl Verifier { token, store.overlay_id, )); - let _ = nuri_branches - .entry(graph_name.clone()) - .or_insert((branch_id, is_publisher)); - Ok((branch_id, is_publisher)) + let _ = nuri_branches.entry(graph_name.clone()).or_insert(( + repo.id, + branch_id, + is_publisher, + )); + Ok((repo.id, branch_id, is_publisher)) } _ => Err(VerifierError::InvalidNamedGraph), } @@ -391,6 +393,7 @@ impl Verifier { &mut self, inserts: Vec, removes: Vec, + peer_id: Vec, ) -> Result<(), VerifierError> { // options when not a publisher on the repo: // - skip @@ -401,11 +404,12 @@ impl Verifier { let mut removes_map: HashMap> = HashMap::with_capacity(1); let mut branches: HashMap = HashMap::with_capacity(1); - let mut nuri_branches: HashMap = HashMap::with_capacity(1); + let mut nuri_branches: HashMap = + HashMap::with_capacity(1); let mut inserts_len = inserts.len(); let mut removes_len = removes.len(); - for insert in inserts { - let (branch_id, is_publisher) = + for mut insert in inserts { + let (repo_id, branch_id, is_publisher) = self.find_branch_and_repo_for_quad(&insert, &mut branches, &mut nuri_branches)?; if !is_publisher { continue; @@ -415,10 +419,32 @@ impl Verifier { inserts_len = 1; set }); + + // changing blank node to skolemized node + + //log_debug!("INSERTING BN {}", quad); + if insert.subject.is_blank_node() { + //log_debug!("INSERTING SUBJECT BN {}", insert.subject); + if let Subject::BlankNode(b) = &insert.subject { + let iri = + NuriV0::repo_skolem(&repo_id, &peer_id, b.as_ref().unique_id().unwrap())?; + insert.subject = Subject::NamedNode(NamedNode::new_unchecked(iri)); + } + } + if insert.object.is_blank_node() { + //log_debug!("INSERTING OBJECT BN {}", insert.object); + if let Term::BlankNode(b) = &insert.object { + let iri = + NuriV0::repo_skolem(&repo_id, &peer_id, b.as_ref().unique_id().unwrap())?; + insert.object = Term::NamedNode(NamedNode::new_unchecked(iri)); + } + } + // TODO deal with triples in subject and object (RDF-STAR) + set.insert(insert.into()); } for remove in removes { - let (branch_id, is_publisher) = + let (repo_id, branch_id, is_publisher) = self.find_branch_and_repo_for_quad(&remove, &mut branches, &mut nuri_branches)?; if !is_publisher { continue; @@ -689,46 +715,13 @@ impl Verifier { if inserts.is_empty() && removes.is_empty() { Ok(()) } else { - let mut new_inserts = Vec::with_capacity(inserts.len()); - for mut quad in inserts.drain() { - //log_debug!("INSERTING BN {}", quad); - if quad.subject.is_blank_node() { - if base.is_none() { - return Err("Cannot insert blank nodes without a base".to_string()); - } - //log_debug!("INSERTING SUBJECT BN {}", quad.subject); - if let Subject::BlankNode(b) = &quad.subject { - let iri = NuriV0::repo_skolem( - base.as_ref().unwrap(), - &peer_id, - b.as_ref().unique_id().unwrap(), - ) - .map_err(|e| e.to_string())?; - quad.subject = Subject::NamedNode(NamedNode::new_unchecked(iri)); - } - } - if quad.object.is_blank_node() { - if base.is_none() { - return Err("Cannot insert blank nodes without a base".to_string()); - } - //log_debug!("INSERTING OBJECT BN {}", quad.object); - if let Term::BlankNode(b) = &quad.object { - let iri = NuriV0::repo_skolem( - base.as_ref().unwrap(), - &peer_id, - b.as_ref().unique_id().unwrap(), - ) - .map_err(|e| e.to_string())?; - quad.object = Term::NamedNode(NamedNode::new_unchecked(iri)); - } - } - // TODO deal with triples in subject and object (RDF-STAR) - new_inserts.push(quad); - } - - self.prepare_sparql_update(new_inserts, Vec::from_iter(removes)) - .await - .map_err(|e| e.to_string()) + self.prepare_sparql_update( + Vec::from_iter(inserts), + Vec::from_iter(removes), + peer_id, + ) + .await + .map_err(|e| e.to_string()) } } }