Skip to content

Commit

Permalink
refactor: add MoreInfo error
Browse files Browse the repository at this point in the history
  • Loading branch information
edytapawlak committed May 7, 2024
1 parent b17f312 commit 33a6e1f
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 66 deletions.
2 changes: 0 additions & 2 deletions components/controller/src/controller/verifying.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use cesrox::{parse_many, payload::Payload, ParsedData};
use itertools::Itertools;
use keri_core::{
error::Error,
event_message::signature::{get_signatures, Signature},
oobi::Oobi,
processor::validator::{EventValidator, VerificationError},
};
use thiserror::Error;

use crate::{error::ControllerError, known_events::KnownEvents};

Expand Down
6 changes: 5 additions & 1 deletion components/controller/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use keri_core::{
};
use thiserror::Error;

use crate::{communication::SendingError, identifier::mechanics::MechanicsError};
use crate::{communication::SendingError, identifier::{mechanics::MechanicsError, query::WatcherResponseError}};

#[derive(Error, Debug)]
pub enum ControllerError {
Expand Down Expand Up @@ -52,4 +52,8 @@ pub enum ControllerError {

#[error(transparent)]
Mechanic(#[from] MechanicsError),

#[error("Watcher response error: {0}")]
WatcherResponseError(#[from] WatcherResponseError)

}
7 changes: 0 additions & 7 deletions components/controller/src/identifier/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ mod test {

use crate::{
config::ControllerConfig, controller::Controller, error::ControllerError,
identifier::query::QueryResponse,
};

#[async_std::test]
Expand Down Expand Up @@ -219,12 +218,6 @@ mod test {

assert_eq!(identifier.notify_witnesses().await?, 0);

dbg!(witness1
.witness_data
.event_storage
.get_kel_messages_with_receipts(&identifier.id, None)?
.unwrap()
.as_slice());
assert!(matches!(
witness1.witness_data.event_storage.get_kel_messages_with_receipts(&identifier.id, None)?.unwrap().as_slice(),
[Notice::Event(evt), Notice::NontransferableRct(rct)]
Expand Down
36 changes: 35 additions & 1 deletion components/controller/src/identifier/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::collections::HashSet;

use crate::communication::SendingError;
use crate::error::ControllerError;
use crate::known_events::OobiRetrieveError;
use keri_core::actor::error::ActorError;
use keri_core::actor::prelude::HashFunctionCode;
use keri_core::oobi::Scheme;
use keri_core::prefix::IndexedSignature;
use keri_core::query::mailbox::{MailboxQuery, MailboxRoute};
use keri_core::query::query_event::SignedKelQuery;
use keri_core::transport::TransportError;
use keri_core::{
actor::{prelude::SerializationFormats, simple_controller::PossibleResponse},
event::sections::seal::EventSeal,
Expand All @@ -25,6 +28,37 @@ pub enum QueryResponse {
NoUpdates,
}

#[derive(thiserror::Error, Debug)]
pub enum WatcherResponseError {
#[error("Unexpected watcher response")]
UnexpectedResponse,
#[error("Watcher doesn't have OOBI of {0}. Can't find KEL")]
UnknownIdentifierOobi(IdentifierPrefix),
#[error("Watcher response processing error: {0}")]
ResponseProcessingError(#[from] keri_core::error::Error),
#[error("Watcher internal error: {0}")]
WatcherError(#[from] ActorError),
#[error("Transport error: {0}")]
Transport(#[from] TransportError),
#[error("OOBI error: {0}")]
Oobi(#[from] OobiRetrieveError)
}

impl From<SendingError> for WatcherResponseError {
fn from(value: SendingError) -> Self {
match value {
SendingError::TransportError(TransportError::RemoteError(
ActorError::NoIdentState { prefix },
)) => WatcherResponseError::UnknownIdentifierOobi(prefix),
SendingError::TransportError(TransportError::RemoteError(err)) => {
WatcherResponseError::WatcherError(err)
}
SendingError::TransportError(err) => WatcherResponseError::Transport(err),
SendingError::OobiError(e) => e.into(),
}
}
}

impl Identifier {
/// Generates query message of route `mbx` to query own identifier mailbox.
pub fn query_mailbox(
Expand Down Expand Up @@ -82,7 +116,7 @@ impl Identifier {
pub async fn finalize_query(
&mut self,
queries: Vec<(QueryEvent, SelfSigningPrefix)>,
) -> Result<QueryResponse, ControllerError> {
) -> Result<QueryResponse, WatcherResponseError> {
let mut updates = QueryResponse::NoUpdates;
let mut possibly_updated_ids: HashSet<IdentifierPrefix> = HashSet::new();
for (qry, sig) in queries {
Expand Down
4 changes: 2 additions & 2 deletions components/watcher/src/watcher_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ mod test {
actor::{
error::ActorError,
parse_event_stream, parse_op_stream,
simple_controller::{parse_response, PossibleResponse},
simple_controller::PossibleResponse,
},
event_message::signed_event_message::{Message, Op},
oobi::{Oobi, Role},
prefix::IdentifierPrefix,
query::query_event::{QueryRoute, SignedKelQuery, SignedQueryMessage},
query::query_event::{QueryRoute, SignedQueryMessage},
};

#[async_trait::async_trait]
Expand Down
2 changes: 1 addition & 1 deletion components/witness/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ pub fn test_delegated_multisig() -> Result<(), ActorError> {
// TODO return event with attached receipts
let ixn = delegator
.storage
.get_event_at_sn(delegator.prefix(), 1)?
.get_event_at_sn(delegator.prefix(), 1)
.unwrap()
.signed_event_message;
let attached_witness_sig = ixn_receipt.signatures;
Expand Down
2 changes: 1 addition & 1 deletion components/witness/src/witness_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod test {
}
},
SignedQueryMessage::MailboxQuery(qry) => match qry.query.data.data {
query::mailbox::MailboxRoute::Mbx { reply_route, args } => {
query::mailbox::MailboxRoute::Mbx { reply_route: _, args: _ } => {
let resp = String::from_utf8(resp.to_vec()).unwrap();
let resp = parse_response(&resp).unwrap();
Ok(resp)
Expand Down
4 changes: 2 additions & 2 deletions keriox_core/src/processor/escrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
},
},
prefix::{BasicPrefix, IdentifierPrefix, SelfSigningPrefix},
processor::validator::VerificationError,
processor::validator::{MoreInfoError, VerificationError},
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -738,7 +738,7 @@ impl ReplyEscrow {
self.0.remove_escrowed_reply(&id, &sig_rep)?;
}
Err(Error::EventOutOfOrderError)
| Err(Error::VerificationError(VerificationError::EventNotFound)) => (), // keep in escrow,
| Err(Error::VerificationError(VerificationError::MoreInfo(MoreInfoError::EventNotFound(_)))) => (), // keep in escrow,
Err(e) => return Err(e),
};
}
Expand Down
12 changes: 6 additions & 6 deletions keriox_core/src/processor/event_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ impl EventStorage {
&self,
id: &IdentifierPrefix,
sn: u64,
) -> Result<Option<TimestampedSignedEventMessage>, Error> {
) -> Option<TimestampedSignedEventMessage> {
if let Some(mut events) = self.db.get_kel_finalized_events(id) {
Ok(events.find(|event| event.signed_event_message.event_message.data.get_sn() == sn))
events.find(|event| event.signed_event_message.event_message.data.get_sn() == sn)
} else {
Ok(None)
None
}
}

Expand Down Expand Up @@ -281,7 +281,7 @@ impl EventStorage {
sn: u64,
event_digest: &SelfAddressingIdentifier,
) -> Result<Option<KeyConfig>, Error> {
if let Ok(Some(event)) = self.get_event_at_sn(id, sn) {
if let Some(event) = self.get_event_at_sn(id, sn) {
// if it's the event we're looking for
if event
.signed_event_message
Expand All @@ -304,10 +304,10 @@ impl EventStorage {
},
))
} else {
Err(Error::SemanticError("Event digests doesn't match".into()))
Ok(None)
}
} else {
Err(Error::EventOutOfOrderError)
Ok(None)
}
}

Expand Down
4 changes: 3 additions & 1 deletion keriox_core/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ impl EventProcessor {

#[cfg(feature = "query")]
pub fn process_op_reply(&self, rpy: &SignedReply) -> Result<(), Error> {
use crate::processor::validator::MoreInfoError;

use self::validator::VerificationError;

match rpy.reply.get_route() {
Expand All @@ -98,7 +100,7 @@ impl EventProcessor {
self.db
.update_accepted_reply(rpy.clone(), &rpy.reply.get_prefix())?;
}
Err(Error::VerificationError(VerificationError::EventNotFound)) => {
Err(Error::VerificationError(VerificationError::MoreInfo(MoreInfoError::EventNotFound(_)))) => {
self.publisher
.notify(&Notification::KsnOutOfOrder(rpy.clone()))?;
}
Expand Down
17 changes: 8 additions & 9 deletions keriox_core/src/processor/processor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn test_process() -> Result<(), Error> {

// Check if processed event is in kel.
let icp_from_db = event_storage.get_event_at_sn(&id, 0).unwrap();
let re_serialized = icp_from_db.unwrap().signed_event_message.encode().unwrap();
let re_serialized = icp_from_db.signed_event_message.encode().unwrap();
assert_eq!(icp_raw.to_vec(), re_serialized);

let rot_raw = br#"{"v":"KERI10JSON00021c_","t":"rot","d":"EHjzZj4i_-RpTN2Yh-NocajFROJ_GkBtlByhRykqiXgz","i":"EBfxc4RiVY6saIFmUfEtETs1FcqmktZW88UkbnOg0Qen","s":"1","p":"EBfxc4RiVY6saIFmUfEtETs1FcqmktZW88UkbnOg0Qen","kt":"2","k":["DCjxOXniUc5EUzDqERlXdptfKPHy6jNo_ZGsS4Vd8fAE","DNZHARO4dCJlluv0qezEMRmErIWWc-lzOzolBOQ15tHV","DOCQ4KN1jUlKbfjRteDYt9fxgpq1NK9_MqO5IA7shpED"],"nt":"2","n":["EN8l6yJC2PxribTN0xfri6bLz34Qvj-x3cNwcV3DvT2m","EATiZAHl0kzKID6faaQP2O7zB3Hj7eH3bE-vgKVAtsyU","EG6e7dJhh78ZqeIZ-eMbe-OB3TwFMPmrSsh9k75XIjLP"],"bt":"0","br":[],"ba":[],"a":[]}-AADAAAqV6xpsAAEB_FJP5UdYO5qiJphz8cqXbTjB9SRy8V0wIim-lgafF4o-b7TW0spZtzx2RXUfZLQQCIKZsw99k8AABBP8nfF3t6bf4z7eNoBgUJR-hdhw7wnlljMZkeY5j2KFRI_s8wqtcOFx1A913xarGJlO6UfrqFWo53e9zcD8egIACB8DKLMZcCGICuk98RCEVuS0GsqVngi1d-7gAX0jid42qUcR3aiYDMp2wJhqJn-iHJVvtB-LK7TRTggBtMDjuwB"#;
Expand All @@ -62,7 +62,7 @@ fn test_process() -> Result<(), Error> {

// Process rotation event.
event_processor.process(&deserialized_rot.clone())?;
let rot_from_db = event_storage.get_event_at_sn(&id, 1).unwrap().unwrap();
let rot_from_db = event_storage.get_event_at_sn(&id, 1).unwrap();
assert_eq!(rot_from_db.signed_event_message.encode().unwrap(), rot_raw);

// Process the same rotation event one more time.
Expand All @@ -81,7 +81,7 @@ fn test_process() -> Result<(), Error> {
event_processor.process(&deserialized_ixn)?;

// Check if processed event is in db.
let ixn_from_db = event_storage.get_event_at_sn(&id, 2).unwrap().unwrap();
let ixn_from_db = event_storage.get_event_at_sn(&id, 2).unwrap();
match deserialized_ixn {
Message::Notice(Notice::Event(evt)) => assert_eq!(
ixn_from_db.signed_event_message.event_message.data,
Expand Down Expand Up @@ -121,7 +121,7 @@ fn test_process() -> Result<(), Error> {

// Check if processed ixn event is in kel. It shouldn't because of not enough signatures.
let ixn_from_db = event_storage.get_event_at_sn(&id, 3);
assert!(matches!(ixn_from_db, Ok(None)));
assert!(matches!(ixn_from_db, None));

// Out of order event.
let out_of_order_rot_raw = br#"{"v":"KERI10JSON000190_","t":"rot","d":"EG3e42rBNZJ_ijLq6Ch2eNRUGRANwEHohGmnR2U_lH92","i":"EBfxc4RiVY6saIFmUfEtETs1FcqmktZW88UkbnOg0Qen","s":"4","p":"ECS66nEGuig1H1gM88HntPIN0fPQomkQPj7CizREZOEx","kt":"2","k":["DOCQ4KN1jUlKbfjRteDYt9fxgpq1NK9_MqO5IA7shpED","DFY1nGjV9oApBzo5Oq5JqjwQsZEQqsCCftzo3WJjMMX-","DE9ZxA3qXegkgDAhOzWP45S3Ruv5ilJSkv5lvthyWNYY"],"nt":"0","n":[],"bt":"0","br":[],"ba":[],"a":[]}-AADAAAyif3K8mg9JE0p98CASi-c9vOhbGqOMUd-CfZGUOTPk3_qfvA-IDLDjm2QDmR6yhAGyhC-6HZRTq8ChC6fIp8OABAHpYJJpsNfNQw6V7QzDWjJ9hfQYq3RlV1XcbxWIXHhwI2nRHxlxyGwufRNeFANZdP10MqcR4IX6nDkdp9YN6IHACBh9wl7YbutrnKfKI-8tCaztpCifUFuR5XY6rOVucWgLXYVJwCYmkl95LMUBJPee4v2pImB0Vftmwt5FJ2lPY8O"#;
Expand All @@ -137,7 +137,7 @@ fn test_process() -> Result<(), Error> {

// Check if processed event is in kel. It shouldn't.
let raw_from_db = event_storage.get_event_at_sn(&id, 4);
assert!(matches!(raw_from_db, Ok(None)));
assert!(matches!(raw_from_db, None));

let id: IdentifierPrefix = "EBfxc4RiVY6saIFmUfEtETs1FcqmktZW88UkbnOg0Qen".parse()?;
let mut kel = Vec::new();
Expand Down Expand Up @@ -203,7 +203,6 @@ fn test_process_delegated() -> Result<(), Error> {
// Check if processed event is in db.
let ixn_from_db = event_storage
.get_event_at_sn(&delegator_prefix, 1)
.unwrap()
.unwrap();
assert_eq!(
ixn_from_db.signed_event_message.event_message.encode()?,
Expand All @@ -214,7 +213,7 @@ fn test_process_delegated() -> Result<(), Error> {
event_processor.process(&deserialized_dip)?;

// Check if processed dip event is in db.
let dip_from_db = event_storage.get_event_at_sn(&child_prefix, 0)?.unwrap();
let dip_from_db = event_storage.get_event_at_sn(&child_prefix, 0).unwrap();

assert_eq!(
dip_from_db.signed_event_message.event_message.encode()?,
Expand All @@ -230,7 +229,7 @@ fn test_process_delegated() -> Result<(), Error> {

// Check if processed event is in db.
let ixn_from_db = event_storage
.get_event_at_sn(&delegator_prefix, 2)?
.get_event_at_sn(&delegator_prefix, 2)
.unwrap();
assert_eq!(
ixn_from_db.signed_event_message.event_message.encode()?,
Expand All @@ -246,7 +245,7 @@ fn test_process_delegated() -> Result<(), Error> {
event_processor.process(&deserialized_drt)?;

// Check if processed drt event is in db.
let drt_from_db = event_storage.get_event_at_sn(&child_prefix, 1)?.unwrap();
let drt_from_db = event_storage.get_event_at_sn(&child_prefix, 1).unwrap();
assert_eq!(
drt_from_db.signed_event_message.event_message.encode()?,
raw_parsed(deserialized_drt)?
Expand Down
Loading

0 comments on commit 33a6e1f

Please sign in to comment.