Skip to content

Commit c0bde6a

Browse files
committed
Remove use of macro
Signed-off-by: Jacinta Ferrant <jacinta.ferrant@gmail.com>
1 parent cac00cc commit c0bde6a

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

stackslib/src/chainstate/burn/operations/mod.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub fn blockstack_op_extended_serialize_opt<S: Serializer>(
418418
}
419419

420420
/// Deserialize the burnchain op that was serialized with blockstack_op_to_json
421-
pub fn deserialize_extended_blockstack_op<'de, D>(
421+
pub fn blockstack_op_extended_deserialize<'de, D>(
422422
deserializer: D,
423423
) -> Result<Option<BlockstackOperationType>, D::Error>
424424
where
@@ -476,40 +476,29 @@ where
476476
.map_err(serde::de::Error::custom)
477477
}
478478

479-
macro_rules! normalize_common_fields {
480-
($map:ident, $de:ident) => {{
481-
normalize_hex_field::<$de, _>(&mut $map, "burn_header_hash", |s| {
482-
BurnchainHeaderHash::from_hex(s).map_err(DeError::custom)
483-
})?;
484-
rename_field(&mut $map, "burn_txid", "txid");
485-
rename_field(&mut $map, "burn_block_height", "block_height");
486-
}};
487-
}
488-
489-
// Utility function to normalize a hex string to a BurnchainHeaderHash JSON value
490-
fn normalize_hex_field<'de, D, T>(
479+
fn normalize_common_fields<'de, D>(
491480
map: &mut serde_json::Map<String, serde_json::Value>,
492-
field: &str,
493-
from_hex: fn(&str) -> Result<T, D::Error>,
494481
) -> Result<(), D::Error>
495482
where
496483
D: Deserializer<'de>,
497-
T: serde::Serialize,
498484
{
499-
if let Some(hex_str) = map.get(field).and_then(serde_json::Value::as_str) {
485+
if let Some(hex_str) = map
486+
.get("burn_header_hash")
487+
.and_then(serde_json::Value::as_str)
488+
{
500489
let cleaned = hex_str.strip_prefix("0x").unwrap_or(hex_str);
501-
let val = from_hex(cleaned).map_err(DeError::custom)?;
490+
let val = BurnchainHeaderHash::from_hex(cleaned).map_err(DeError::custom)?;
502491
let ser_val = serde_json::to_value(val).map_err(DeError::custom)?;
503-
map.insert(field.to_string(), ser_val);
492+
map.insert("burn_header_hash".to_string(), ser_val);
504493
}
505-
Ok(())
506-
}
507494

508-
// Normalize renamed field
509-
fn rename_field(map: &mut serde_json::Map<String, serde_json::Value>, from: &str, to: &str) {
510-
if let Some(val) = map.remove(from) {
511-
map.insert(to.to_string(), val);
495+
if let Some(val) = map.remove("burn_txid") {
496+
map.insert("txid".to_string(), val);
497+
}
498+
if let Some(val) = map.remove("burn_block_height") {
499+
map.insert("block_height".to_string(), val);
512500
}
501+
Ok(())
513502
}
514503

515504
impl BlockstackOperationType {
@@ -615,25 +604,25 @@ impl BlockstackOperationType {
615604

616605
// Replace all the normalize_* functions with minimal implementations
617606
fn normalize_pre_stx_fields<'de, D>(
618-
mut map: &mut serde_json::Map<String, serde_json::Value>,
607+
map: &mut serde_json::Map<String, serde_json::Value>,
619608
) -> Result<(), D::Error>
620609
where
621610
D: Deserializer<'de>,
622611
{
623-
normalize_common_fields!(map, D);
612+
normalize_common_fields::<D>(map)?;
624613
if let Some(serde_json::Value::Object(obj)) = map.get_mut("output") {
625614
normalize_stacks_addr_fields::<D>(obj)?;
626615
}
627616
Ok(())
628617
}
629618

630619
fn normalize_stack_stx_fields<'de, D>(
631-
mut map: &mut serde_json::Map<String, serde_json::Value>,
620+
map: &mut serde_json::Map<String, serde_json::Value>,
632621
) -> Result<(), D::Error>
633622
where
634623
D: Deserializer<'de>,
635624
{
636-
normalize_common_fields!(map, D);
625+
normalize_common_fields::<D>(map)?;
637626
if let Some(serde_json::Value::Object(obj)) = map.get_mut("sender") {
638627
normalize_stacks_addr_fields::<D>(obj)?;
639628
}
@@ -650,12 +639,12 @@ impl BlockstackOperationType {
650639
}
651640

652641
fn normalize_transfer_stx_fields<'de, D>(
653-
mut map: &mut serde_json::Map<String, serde_json::Value>,
642+
map: &mut serde_json::Map<String, serde_json::Value>,
654643
) -> Result<(), D::Error>
655644
where
656645
D: Deserializer<'de>,
657646
{
658-
normalize_common_fields!(map, D);
647+
normalize_common_fields::<D>(map)?;
659648
for field in ["recipient", "sender"] {
660649
if let Some(serde_json::Value::Object(obj)) = map.get_mut(field) {
661650
normalize_stacks_addr_fields::<D>(obj)?;
@@ -671,12 +660,12 @@ impl BlockstackOperationType {
671660
}
672661

673662
fn normalize_delegate_stx_fields<'de, D>(
674-
mut map: &mut serde_json::Map<String, serde_json::Value>,
663+
map: &mut serde_json::Map<String, serde_json::Value>,
675664
) -> Result<(), D::Error>
676665
where
677666
D: Deserializer<'de>,
678667
{
679-
normalize_common_fields!(map, D);
668+
normalize_common_fields::<D>(map)?;
680669
if let Some(serde_json::Value::Array(arr)) = map.get("reward_addr") {
681670
if arr.len() == 2 {
682671
let index = arr[0]
@@ -701,12 +690,12 @@ impl BlockstackOperationType {
701690
}
702691

703692
fn normalize_vote_for_aggregate_key_fields<'de, D>(
704-
mut map: &mut serde_json::Map<String, serde_json::Value>,
693+
map: &mut serde_json::Map<String, serde_json::Value>,
705694
) -> Result<(), D::Error>
706695
where
707696
D: Deserializer<'de>,
708697
{
709-
normalize_common_fields!(map, D);
698+
normalize_common_fields::<D>(map)?;
710699
for field in ["aggregate_key", "signer_key"] {
711700
if let Some(hex_str) = map.get(field).and_then(serde_json::Value::as_str) {
712701
let cleaned = hex_str.strip_prefix("0x").unwrap_or(hex_str);

stackslib/src/chainstate/burn/operations/test/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::burnchains::bitcoin::{
1717
};
1818
use crate::burnchains::{BurnchainBlockHeader, BurnchainSigner, BurnchainTransaction, Txid};
1919
use crate::chainstate::burn::operations::{
20-
blockstack_op_extended_serialize_opt, deserialize_extended_blockstack_op,
20+
blockstack_op_extended_deserialize, blockstack_op_extended_serialize_opt,
2121
BlockstackOperationType, DelegateStxOp, LeaderBlockCommitOp, LeaderKeyRegisterOp, PreStxOp,
2222
StackStxOp, TransferStxOp, VoteForAggregateKeyOp,
2323
};
@@ -104,7 +104,7 @@ fn serde_blockstack_ops() {
104104
struct TestOpHolder {
105105
#[serde(
106106
serialize_with = "blockstack_op_extended_serialize_opt",
107-
deserialize_with = "deserialize_extended_blockstack_op"
107+
deserialize_with = "blockstack_op_extended_deserialize"
108108
)]
109109
burnchain_op: Option<BlockstackOperationType>,
110110
}

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rusqlite::{params, Connection};
3737
use serde_json::json;
3838
use stacks::burnchains::{PoxConstants, Txid};
3939
use stacks::chainstate::burn::operations::{
40-
blockstack_op_extended_serialize_opt, deserialize_extended_blockstack_op,
40+
blockstack_op_extended_deserialize, blockstack_op_extended_serialize_opt,
4141
BlockstackOperationType,
4242
};
4343
use stacks::chainstate::burn::ConsensusHash;
@@ -359,7 +359,7 @@ pub struct TransactionEventPayload<'a> {
359359
/// The burnchain op
360360
#[serde(
361361
serialize_with = "blockstack_op_extended_serialize_opt",
362-
deserialize_with = "deserialize_extended_blockstack_op"
362+
deserialize_with = "blockstack_op_extended_deserialize"
363363
)]
364364
pub burnchain_op: Option<BlockstackOperationType>,
365365
/// The transaction execution cost

0 commit comments

Comments
 (0)