Skip to content

Commit

Permalink
fix mqtt todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Nov 16, 2023
1 parent b39643c commit 7340a82
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ where
}
let payload = match &topic_event.payload {
MqttPayload::Json(val) => serde_json::to_string(&val).expect("failed to serialize MqttPayload::Json"),
MqttPayload::BlockBody(block_body) => {
serde_json::to_string(block_body).expect("failed to serialize MqttPayload::BlockBody")
MqttPayload::Block(block) => {
serde_json::to_string(block).expect("failed to serialize MqttPayload::Block")
}
e => panic!("received unknown mqtt type: {e:?}"),
};
Expand Down
6 changes: 2 additions & 4 deletions bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{
Expand Down Expand Up @@ -153,8 +151,8 @@ async fn client_from_wallet() -> Result<()> {
// .await;

// let block = match response {
// Response::Block(block_body) => {
// match &block_body.block {
// Response::Block(block) => {
// match &block.body {
// BlockBodyDto::Basic(b) => assert_eq!(b.payload.as_ref(), Some(&payload)),
// BlockBodyDto::Validation(v) => panic!("unexpected block {v:?}"),
// }
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/07_mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<()> {
println!("> Topic: {}", event.topic);
match &event.payload {
MqttPayload::Json(val) => println!("{}", serde_json::to_string(&val).unwrap()),
MqttPayload::BlockBody(block_body) => println!("{block_body:?}"),
MqttPayload::Block(block) => println!("{block:?}"),
e => println!("unknown event received: {e:?}"),
}
tx.send(()).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/client/node_api/mqtt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::sync::watch::Receiver as WatchReceiver;
pub use self::{error::Error, types::*};
use crate::{
client::{Client, ClientInner},
types::block::BlockBody,
types::block::Block,
};

impl Client {
Expand Down Expand Up @@ -190,13 +190,13 @@ fn poll_mqtt(client: &Client, mut event_loop: EventLoop) {
let payload = &*p.payload;
let protocol_parameters = &client.network_info.read().await.protocol_parameters;

match BlockBody::unpack_verified(payload, protocol_parameters) {
Ok(block_body) => Ok(TopicEvent {
match Block::unpack_verified(payload, protocol_parameters) {
Ok(block) => Ok(TopicEvent {
topic: p.topic.clone(),
payload: MqttPayload::BlockBody((&block_body).into()),
payload: MqttPayload::Block((&block).into()),
}),
Err(e) => {
warn!("Block body unpacking failed: {:?}", e);
warn!("Block unpacking failed: {:?}", e);
Err(())
}
}
Expand Down
8 changes: 3 additions & 5 deletions sdk/src/client/node_api/mqtt/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{de::Error as _, Deserialize, Deserializer, Serialize};
use serde_json::Value;

use super::Error;
use crate::types::block::BlockBodyDto;
use crate::types::block::BlockDto;

type TopicHandler = Box<dyn Fn(&TopicEvent) + Send + Sync>;

Expand All @@ -33,10 +33,8 @@ pub struct TopicEvent {
pub enum MqttPayload {
/// In case it contains JSON.
Json(Value),
// TODO: should we still call this variant `Block`? It's less exact, but the mqtt topic will still be called
// `blocks` instead of `block_bodies`.
/// In case it contains a `BlockBody` object.
BlockBody(BlockBodyDto),
/// In case it contains a `Block` object.
Block(BlockDto),
}

/// Mqtt events.
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/client/node_api/mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn test_mqtt() {
],
move |evt| {
match &evt.payload {
MqttPayload::BlockBody(_) => {
MqttPayload::Block(_) => {
assert_eq!(evt.topic, "blocks");
}
MqttPayload::Json(_) => {
Expand Down

0 comments on commit 7340a82

Please sign in to comment.