Skip to content

Commit c2d70ce

Browse files
committed
Store FMD flag ciphertexts in extra data secs
1 parent 113d025 commit c2d70ce

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

crates/node/src/bench_utils.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl BenchShellInner {
192192

193193
if let Some(sections) = extra_sections {
194194
for section in sections {
195-
if let Section::ExtraData(_) | Section::Data(_) = section {
195+
if let Section::ExtraData(_) = section {
196196
tx.add_section(section);
197197
}
198198
}
@@ -1098,9 +1098,13 @@ impl Client for BenchShell {
10981098
});
10991099
let masp_fmd_event =
11001100
tx.sections.iter().find_map(|section| match section {
1101-
sec @ Section::Data(Data { data, .. })
1102-
if <Vec<FlagCiphertext>>::try_from_slice(data)
1103-
.is_ok() =>
1101+
sec @ Section::ExtraData(extra_data)
1102+
if extra_data.id().is_some_and(|extra_data| {
1103+
<Vec<FlagCiphertext>>::try_from_slice(
1104+
extra_data,
1105+
)
1106+
.is_ok()
1107+
}) =>
11041108
{
11051109
Some(AbciEvent::from(Event::from(
11061110
MaspEvent::FlagCiphertexts {
@@ -1312,7 +1316,8 @@ impl BenchShieldedCtx {
13121316
)
13131317
.expect("MASP must have shielded part");
13141318

1315-
let fmd_section = Section::Data(Data::from_borsh_encoded(&fmd_flags));
1319+
let fmd_section =
1320+
Section::ExtraData(Code::from_borsh_encoded(&fmd_flags));
13161321
let shielded_data = MaspTxData {
13171322
masp_tx_id: shielded.txid().into(),
13181323
flag_ciphertext_sechash: fmd_section.get_hash(),
@@ -1430,7 +1435,7 @@ impl BenchShieldedCtx {
14301435
.into_iter()
14311436
.collect();
14321437
let masp_tx = tx.tx.get_masp_section(&masp_tx_id).unwrap().clone();
1433-
let fmd_section = Section::Data(Data::from_borsh_encoded(
1438+
let fmd_section = Section::ExtraData(Code::from_borsh_encoded(
14341439
&std::iter::repeat_with(FlagCiphertext::default)
14351440
.take(
14361441
masp_tx

crates/sdk/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ pub mod testing {
901901
arb_withdraw,
902902
};
903903
use crate::tx::{
904-
Authorization, Code, Commitment, Data, Header, MaspBuilder, Section,
904+
Authorization, Code, Commitment, Header, MaspBuilder, Section,
905905
TxCommitments,
906906
};
907907

@@ -1146,10 +1146,8 @@ pub mod testing {
11461146
if let Some((shielded_transfer, asset_types, build_params)) = aux {
11471147
let shielded_section_hash =
11481148
tx.add_masp_tx_section(shielded_transfer.masp_tx).1;
1149-
tx.add_section(
1150-
Section::Data(
1151-
Data::from_borsh_encoded(&shielded_transfer.fmd_flags),
1152-
),
1149+
tx.add_fmd_flag_ciphertexts(
1150+
&shielded_transfer.fmd_flags,
11531151
);
11541152
tx.add_masp_builder(MaspBuilder {
11551153
asset_types: asset_types.into_keys().collect(),

crates/sdk/src/tx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4351,7 +4351,7 @@ fn proposal_to_vec(proposal: OnChainProposal) -> Result<Vec<u8>> {
43514351
}
43524352

43534353
fn create_fmd_section(fmd_flags: Vec<FlagCiphertext>) -> (Section, Hash) {
4354-
let fmd_section = Section::Data(Data::from_borsh_encoded(&fmd_flags));
4354+
let fmd_section = Section::ExtraData(Code::from_borsh_encoded(&fmd_flags));
43554355
let fmd_sechash = fmd_section.get_hash();
43564356

43574357
(fmd_section, fmd_sechash)

crates/token/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ pub mod testing {
536536
.take(builder.sapling_outputs().len())
537537
.collect();
538538
let fmd_sechash = {
539-
let sec = namada_tx::Section::Data(
540-
namada_tx::Data::from_borsh_encoded(&fmd_flags),
539+
let sec = namada_tx::Section::ExtraData(
540+
namada_tx::Code::from_borsh_encoded(&fmd_flags),
541541
);
542542
sec.get_hash()
543543
};

crates/tx/src/section.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl Data {
277277

278278
/// Make a new data section with the given borsh encodable data
279279
#[inline]
280-
pub fn from_borsh_encoded<T: BorshSerialize>(data: &T) -> Self {
280+
pub fn from_borsh_encoded<T: BorshSerialize + ?Sized>(data: &T) -> Self {
281281
Self::new(data.serialize_to_vec())
282282
}
283283

@@ -376,6 +376,21 @@ impl Code {
376376
}
377377
}
378378

379+
/// Return the code data, if it is present verbatim.
380+
pub fn id(&self) -> Option<&[u8]> {
381+
if let Commitment::Id(code) = &self.code {
382+
Some(&code[..])
383+
} else {
384+
None
385+
}
386+
}
387+
388+
/// Make a new code section with the given borsh encodable data
389+
#[inline]
390+
pub fn from_borsh_encoded<T: BorshSerialize + ?Sized>(data: &T) -> Self {
391+
Self::new(data.serialize_to_vec(), None)
392+
}
393+
379394
/// Make a new code section with the given hash
380395
pub fn from_hash(
381396
hash: namada_core::hash::Hash,

crates/tx/src/types.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,40 @@ impl Tx {
290290
None
291291
}
292292

293+
/// Add an FMD flag ciphertext section to the transaction
294+
pub fn add_fmd_flag_ciphertexts(
295+
&mut self,
296+
flag_ciphertexts: &[FlagCiphertext],
297+
) -> &mut Self {
298+
self.add_section(Section::ExtraData(Code::from_borsh_encoded(
299+
flag_ciphertexts,
300+
)));
301+
self
302+
}
303+
293304
/// Get the FMD flag ciphertext with the given hash
294305
pub fn get_fmd_flag_ciphertexts(
295306
&self,
296307
hash: &namada_core::hash::Hash,
297308
) -> Result<Option<Vec<FlagCiphertext>>, DecodeError> {
298309
let maybe_section = self.get_section(hash);
299310

300-
let data = match maybe_section.as_ref().map(Cow::as_ref) {
301-
Some(Section::Data(Data { data, .. })) => data,
302-
Some(_) => {
311+
let code = match maybe_section.as_ref().map(Cow::as_ref) {
312+
Some(Section::ExtraData(code)) => code,
313+
Some(sec) => {
303314
return Err(DecodeError::InvalidFlagCiphertexts(
304315
HEXUPPER.encode(&hash.0),
305316
));
306317
}
307318
None => return Ok(None),
308319
};
309320

321+
let Some(data) = code.id() else {
322+
return Err(DecodeError::InvalidFlagCiphertexts(
323+
HEXUPPER.encode(&hash.0),
324+
));
325+
};
326+
310327
let decoded =
311328
BorshDeserialize::try_from_slice(data).map_err(|_err| {
312329
DecodeError::InvalidFlagCiphertexts(HEXUPPER.encode(&hash.0))

0 commit comments

Comments
 (0)