Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
Revert release "10.0.0"
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This reverts commits in release 10.0.0
  • Loading branch information
oetyng committed Mar 30, 2021
1 parent 2bf8f37 commit 158e3a3
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 420 deletions.
13 changes: 0 additions & 13 deletions src/client/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,9 @@ pub enum Error {
/// Node failed to delete the requested data for some reason.
#[error("Failed to delete requested data")]
FailedToDelete,
/// Node does not manage any section funds.
#[error("Node does not currently manage any section funds")]
NoSectionFunds,
/// Node does not manage any metadata, so is likely not a fully prepared elder yet.
#[error("Node does not currently manage any section metadata")]
NoSectionMetaData,
/// Node does not manage any immutable chunks.
#[error("Node does not currently manage any immutable chunks")]
NoImmutableChunks,
/// Node is currently churning so cannot perform the request.
#[error("Cannot complete request due to churning of funds")]
NodeChurningFunds,
/// The node hasn't left the section, and was not marked for relocation during reward operations
#[error("Node is not being relocated")]
NodeWasNotRelocated,

/// There was an error in the target section of a message. Probably related to section keys.
#[error("Target section error")]
TargetSection(#[from] TargetSectionError),
Expand Down
190 changes: 67 additions & 123 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,6 @@ use std::{
convert::TryFrom,
fmt,
};
use threshold_crypto::PublicKey as BlsPublicKey;
use xor_name::XorName;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum Message {
Process(ProcessMsg),
ProcessingError(ProcessingError),
}

/// Our LazyMesssage error. Recipient was unable to process this message for some reason.
/// The original message should be returned in full, and context can optionally be added via
/// reason.
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct ProcessingError {
/// Optional reason for the error. This should help recveiving node handle the error
pub reason: Option<Error>,
/// Message that triggered this error
pub source_message: Option<ProcessMsg>,
/// MessageId
pub id: MessageId,
}

impl ProcessingError {
pub fn id(&self) -> MessageId {
self.id
}
}

/// Message envelope containing a Safe message payload,
/// This struct also provides utilities to obtain the serialized bytes
Expand All @@ -89,7 +60,7 @@ impl Message {
/// It returns an error if the bytes don't correspond to a client message.
pub fn from(bytes: Bytes) -> crate::Result<Self> {
let deserialized = WireMsg::deserialize(bytes)?;
if let MessageType::ClientMessage { msg, .. } = deserialized {
if let MessageType::ClientMessage(msg) = deserialized {
Ok(msg)
} else {
Err(crate::Error::FailedToParse(
Expand All @@ -98,49 +69,16 @@ impl Message {
}
}

/// Serialize this Message into bytes ready to be sent over the wire.
pub fn serialize(&self, dest: XorName, dest_section_pk: BlsPublicKey) -> crate::Result<Bytes> {
WireMsg::serialize_client_msg(self, dest, dest_section_pk)
}

/// Gets the message ID.
pub fn id(&self) -> MessageId {
match self {
Self::Process(ProcessMsg::Cmd { id, .. })
| Self::Process(ProcessMsg::Query { id, .. })
| Self::Process(ProcessMsg::Event { id, .. })
| Self::Process(ProcessMsg::QueryResponse { id, .. })
| Self::Process(ProcessMsg::CmdError { id, .. })
| Self::Process(ProcessMsg::NodeCmd { id, .. })
| Self::Process(ProcessMsg::NodeEvent { id, .. })
| Self::Process(ProcessMsg::NodeQuery { id, .. })
| Self::Process(ProcessMsg::NodeCmdError { id, .. })
| Self::Process(ProcessMsg::NodeQueryResponse { id, .. })
| Self::ProcessingError(ProcessingError { id, .. }) => *id,
}
}

/// return ProcessMessage if any
pub fn get_process(&self) -> Option<&ProcessMsg> {
match self {
Self::Process(msg) => Some(msg),
Self::ProcessingError(_) => None,
}
}

/// return ProcessMessage if any
pub fn get_processing_error(&self) -> Option<&ProcessingError> {
match self {
Self::Process(_) => None,
Self::ProcessingError(error) => Some(error),
}
/// serialize this Message into bytes ready to be sent over the wire.
pub fn serialize(&self) -> crate::Result<Bytes> {
WireMsg::serialize_client_msg(self)
}
}

///
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum ProcessMsg {
pub enum Message {
/// A Cmd is leads to a write / change of state.
/// We expect them to be successful, and only return a msg
/// if something went wrong.
Expand All @@ -149,13 +87,17 @@ pub enum ProcessMsg {
cmd: Cmd,
/// Message ID.
id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// Queries is a read-only operation.
Query {
/// Query.
query: Query,
/// Message ID.
id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// An Event is a fact about something that happened.
Event {
Expand All @@ -165,6 +107,8 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing cmd.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// The response to a query, containing the query result.
QueryResponse {
Expand All @@ -174,6 +118,8 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing query.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// Cmd error.
CmdError {
Expand All @@ -183,13 +129,17 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing cmd.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// Cmds only sent internally in the network.
NodeCmd {
/// NodeCmd.
cmd: NodeCmd,
/// Message ID.
id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// An error of a NodeCmd.
NodeCmdError {
Expand All @@ -199,6 +149,8 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing cmd.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// Events only sent internally in the network.
NodeEvent {
Expand All @@ -208,13 +160,17 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing cmd.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// Queries is a read-only operation.
NodeQuery {
/// Query.
query: NodeQuery,
/// Message ID.
id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
/// The response to a query, containing the query result.
NodeQueryResponse {
Expand All @@ -224,20 +180,12 @@ pub enum ProcessMsg {
id: MessageId,
/// ID of causing query.
correlation_id: MessageId,
/// Target section's current PublicKey
target_section_pk: Option<PublicKey>,
},
}

impl ProcessMsg {
pub fn create_processing_error(&self, reason: Option<Error>) -> ProcessingError {
ProcessingError {
source_message: Some(self.clone()),
id: MessageId::new(),
reason,
}
}
}

impl ProcessMsg {
impl Message {
/// Gets the message ID.
pub fn id(&self) -> MessageId {
match self {
Expand All @@ -253,6 +201,42 @@ impl ProcessMsg {
| Self::NodeQueryResponse { id, .. } => *id,
}
}

/// Gets the message's expected section PublicKey.
pub fn target_section_pk(&self) -> Option<PublicKey> {
match self {
Self::Cmd {
target_section_pk, ..
}
| Self::Query {
target_section_pk, ..
}
| Self::Event {
target_section_pk, ..
}
| Self::QueryResponse {
target_section_pk, ..
}
| Self::CmdError {
target_section_pk, ..
}
| Self::NodeCmd {
target_section_pk, ..
}
| Self::NodeEvent {
target_section_pk, ..
}
| Self::NodeQuery {
target_section_pk, ..
}
| Self::NodeCmdError {
target_section_pk, ..
}
| Self::NodeQueryResponse {
target_section_pk, ..
} => *target_section_pk,
}
}
}

///
Expand Down Expand Up @@ -531,7 +515,7 @@ mod tests {
}

#[test]
fn debug_format_functional() -> Result<()> {
fn debug_format() -> Result<()> {
if let Some(key) = gen_keys().first() {
let errored_response = QueryResponse::GetSequence(Err(Error::AccessDenied(*key)));
assert!(format!("{:?}", errored_response)
Expand All @@ -541,45 +525,6 @@ mod tests {
Err(anyhow!("Could not generate public key"))
}
}
#[test]
fn generate_processing_error() -> Result<()> {
if let Some(key) = gen_keys().first() {
let msg = ProcessMsg::Query {
query: Query::Transfer(TransferQuery::GetBalance(*key)),
id: MessageId::new(),
};
let lazy_error = msg.create_processing_error(Some(Error::NoSuchData));

assert!(format!("{:?}", lazy_error).contains("TransferQuery::GetBalance"));
assert!(format!("{:?}", lazy_error).contains("ProcessingError"));
assert!(format!("{:?}", lazy_error).contains("NoSuchData"));

Ok(())
} else {
Err(anyhow!("Could not generate public key"))
}
}

#[test]
fn debug_format_processing_error() -> Result<()> {
if let Some(key) = gen_keys().first() {
let errored_response = ProcessingError {
reason: Some(Error::NoSuchData),
source_message: Some(ProcessMsg::Query {
id: MessageId::new(),
query: Query::Transfer(TransferQuery::GetBalance(*key)),
}),
id: MessageId::new(),
};

assert!(format!("{:?}", errored_response).contains("TransferQuery::GetBalance"));
assert!(format!("{:?}", errored_response).contains("ProcessingError"));
assert!(format!("{:?}", errored_response).contains("NoSuchData"));
Ok(())
} else {
Err(anyhow!("Could not generate public key"))
}
}

#[test]
fn try_from() -> Result<()> {
Expand Down Expand Up @@ -632,15 +577,14 @@ mod tests {

let random_xor = xor_name::XorName::random();
let id = MessageId(random_xor);
let message = Message::Process(ProcessMsg::Query {
let message = Message::Query {
query: Query::Transfer(TransferQuery::GetBalance(pk)),
id,
});
target_section_pk: None,
};

// test msgpack serialization
let dest = XorName::random();
let dest_section_pk = threshold_crypto::SecretKey::random().public_key();
let serialized = message.serialize(dest, dest_section_pk)?;
let serialized = message.serialize()?;
let deserialized = Message::from(serialized)?;
assert_eq!(deserialized, message);

Expand Down

0 comments on commit 158e3a3

Please sign in to comment.