use hiero_sdk_proto::services;
use hiero_sdk_proto::services::address_book_service_client::AddressBookServiceClient;
use tonic::transport::Channel;
use crate::ledger_id::RefLedgerId;
use crate::protobuf::FromProtobuf;
use crate::transaction::{
AnyTransactionData,
ChunkInfo,
ToSchedulableTransactionDataProtobuf,
ToTransactionDataProtobuf,
TransactionData,
TransactionExecute,
};
use crate::{
BoxGrpcFuture,
Error,
ToProtobuf,
Transaction,
ValidateChecksums,
};
pub type NodeDeleteTransaction = Transaction<NodeDeleteTransactionData>;
#[derive(Debug, Clone, Default)]
pub struct NodeDeleteTransactionData {
node_id: u64,
}
impl NodeDeleteTransaction {
#[must_use]
pub fn get_node_id(&self) -> u64 {
self.data().node_id
}
pub fn node_id(&mut self, node_id: u64) -> &mut Self {
self.data_mut().node_id = node_id;
self
}
}
impl TransactionData for NodeDeleteTransactionData {}
impl TransactionExecute for NodeDeleteTransactionData {
fn execute(
&self,
channel: Channel,
request: services::Transaction,
) -> BoxGrpcFuture<'_, services::TransactionResponse> {
Box::pin(async { AddressBookServiceClient::new(channel).delete_node(request).await })
}
}
impl ValidateChecksums for NodeDeleteTransactionData {
fn validate_checksums(&self, _ledger_id: &RefLedgerId) -> Result<(), Error> {
Ok(())
}
}
impl ToTransactionDataProtobuf for NodeDeleteTransactionData {
fn to_transaction_data_protobuf(
&self,
chunk_info: &ChunkInfo,
) -> services::transaction_body::Data {
let _ = chunk_info.assert_single_transaction();
services::transaction_body::Data::NodeDelete(self.to_protobuf())
}
}
impl ToSchedulableTransactionDataProtobuf for NodeDeleteTransactionData {
fn to_schedulable_transaction_data_protobuf(
&self,
) -> services::schedulable_transaction_body::Data {
services::schedulable_transaction_body::Data::NodeDelete(self.to_protobuf())
}
}
impl From<NodeDeleteTransactionData> for AnyTransactionData {
fn from(transaction: NodeDeleteTransactionData) -> Self {
Self::NodeDelete(transaction)
}
}
impl FromProtobuf<services::NodeDeleteTransactionBody> for NodeDeleteTransactionData {
fn from_protobuf(pb: services::NodeDeleteTransactionBody) -> crate::Result<Self> {
Ok(Self { node_id: pb.node_id })
}
}
impl ToProtobuf for NodeDeleteTransactionData {
type Protobuf = services::NodeDeleteTransactionBody;
fn to_protobuf(&self) -> Self::Protobuf {
services::NodeDeleteTransactionBody { node_id: self.node_id }
}
}
#[cfg(test)]
mod tests {
use expect_test::expect_file;
use hiero_sdk_proto::services;
use super::NodeDeleteTransaction;
use crate::address_book::NodeDeleteTransactionData;
use crate::protobuf::FromProtobuf;
use crate::transaction::test_helpers::{
check_body,
transaction_body,
};
use crate::AnyTransaction;
fn make_transaction() -> NodeDeleteTransaction {
let mut tx = NodeDeleteTransaction::new_for_tests();
tx.node_id(1).freeze().unwrap();
tx
}
#[test]
fn serialize() {
let tx = make_transaction();
let tx = transaction_body(tx);
let tx = check_body(tx);
expect_file!["./snapshots/node_delete_transaction/serialize.txt"].assert_debug_eq(&tx);
}
#[test]
fn to_from_bytes() {
let tx = make_transaction();
let tx2 = AnyTransaction::from_bytes(&tx.to_bytes().unwrap()).unwrap();
let tx = transaction_body(tx);
let tx2 = transaction_body(tx2);
assert_eq!(tx, tx2)
}
#[test]
fn from_proto_body() {
let tx = services::NodeDeleteTransactionBody { node_id: 1 };
let data = NodeDeleteTransactionData::from_protobuf(tx).unwrap();
assert_eq!(data.node_id, 1);
}
#[test]
fn get_set_node_id() {
let mut tx = NodeDeleteTransaction::new();
tx.node_id(1);
assert_eq!(tx.get_node_id(), 1);
}
#[test]
#[should_panic]
fn get_set_node_id_frozen_panic() {
make_transaction().node_id(1);
}
}