Add TryFrom for store Scabbard msg to protocol msg
// See the License for the specific language governing permissions and
// limitations under the License.
+use std::convert::TryFrom;
+
+use crate::protocol::v3::{
+ message::ScabbardMessage,
+ two_phase_commit::{
+ Abort, Commit, DecisionRequest, TwoPhaseCommitMessage, VoteRequest, VoteResponse,
+ },
+};
+
+use crate::protos::{scabbard_v3, IntoBytes, ProtoConversionError};
+
#[derive(Debug, Clone, PartialEq)]
pub enum Scabbard2pcMessage {
VoteRequest(u64, Vec<u8>),
@@ -20,3 +31,39 @@ pub enum Scabbard2pcMessage {
DecisionRequest(u64),
VoteResponse(u64, bool),
}
+
+impl TryFrom<Scabbard2pcMessage> for ScabbardMessage {
+ type Error = ProtoConversionError;
+
+ fn try_from(store_msg: Scabbard2pcMessage) -> Result<Self, Self::Error> {
+ let msg_2pc = match store_msg {
+ Scabbard2pcMessage::VoteRequest(epoch, value) => {
+ TwoPhaseCommitMessage::VoteRequest(VoteRequest { epoch, value })
+ }
+ Scabbard2pcMessage::Commit(epoch) => TwoPhaseCommitMessage::Commit(Commit { epoch }),
+ Scabbard2pcMessage::Abort(epoch) => TwoPhaseCommitMessage::Abort(Abort { epoch }),
+ Scabbard2pcMessage::DecisionRequest(epoch) => {
+ TwoPhaseCommitMessage::DecisionRequest(DecisionRequest { epoch })
+ }
+ Scabbard2pcMessage::VoteResponse(epoch, response) => {
+ TwoPhaseCommitMessage::VoteResponse(VoteResponse { epoch, response })
+ }
+ };
+
+ Ok(ScabbardMessage::ConsensusMessage(IntoBytes::<
+ scabbard_v3::TwoPhaseCommitMessage,
+ >::into_bytes(
+ msg_2pc
+ )?))
+ }
+}
+
+impl TryFrom<Scabbard2pcMessage> for Vec<u8> {
+ type Error = ProtoConversionError;
+
+ fn try_from(store_msg: Scabbard2pcMessage) -> Result<Self, Self::Error> {
+ IntoBytes::<scabbard_v3::ScabbardMessageV3>::into_bytes(ScabbardMessage::try_from(
+ store_msg,
+ )?)
+ }
+}
