Skip to content

Commit 2495240

Browse files
Alessandro KonradAlessandro Konrad
authored andcommitted
changed is_valid to default true; allowing deserialization with 3 or 4 arguments
1 parent f40a047 commit 2495240

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

rust/src/fees.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ mod tests {
7676
let signed_tx = Transaction::new(
7777
&body,
7878
&w,
79-
true,
8079
None,
8180
);
8281

@@ -123,7 +122,6 @@ mod tests {
123122
let signed_tx = Transaction::new(
124123
&body,
125124
&w,
126-
true,
127125
None,
128126
);
129127

@@ -194,7 +192,6 @@ mod tests {
194192
let signed_tx = Transaction::new(
195193
&body,
196194
&w,
197-
true,
198195
None,
199196
);
200197

@@ -291,7 +288,6 @@ mod tests {
291288
let signed_tx = Transaction::new(
292289
&body,
293290
&w,
294-
true,
295291
None,
296292
);
297293

@@ -486,7 +482,6 @@ mod tests {
486482
let signed_tx = Transaction::new(
487483
&body,
488484
&w,
489-
true,
490485
None,
491486
);
492487

rust/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,19 @@ impl Transaction {
121121
self.auxiliary_data.clone()
122122
}
123123

124+
pub fn set_is_valid(&mut self, valid: bool) {
125+
self.is_valid = valid
126+
}
127+
124128
pub fn new(
125129
body: &TransactionBody,
126130
witness_set: &TransactionWitnessSet,
127-
is_valid: bool,
128131
auxiliary_data: Option<AuxiliaryData>,
129132
) -> Self {
130133
Self {
131134
body: body.clone(),
132135
witness_set: witness_set.clone(),
133-
is_valid: is_valid.clone(),
136+
is_valid: true,
134137
auxiliary_data: auxiliary_data.clone(),
135138
}
136139
}

rust/src/serialization.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,48 @@ impl DeserializeEmbeddedGroup for Transaction {
9595
let witness_set = (|| -> Result<_, DeserializeError> {
9696
Ok(TransactionWitnessSet::deserialize(raw)?)
9797
})().map_err(|e| e.annotate("witness_set"))?;
98+
let mut checked_auxiliary_data = false;
99+
let mut auxiliary_data = None;
98100
let is_valid = (|| -> Result<_, DeserializeError> {
99101
match raw.cbor_type()? == CBORType::Special {
100102
true => {
101-
if let CBORSpecial::Bool(b) = raw.special()? {
103+
// if it's special it can be either a bool or null. if it's null, then it's empty auxiliary data, otherwise not a valid encoding
104+
let special = raw.special()?;
105+
if let CBORSpecial::Bool(b) = special {
102106
return Ok(b);
107+
} else if special == CBORSpecial::Null {
108+
checked_auxiliary_data = true;
109+
return Ok(true);
110+
} else {
111+
return Err(DeserializeFailure::ExpectedBool.into());
103112
}
104-
return Err(DeserializeFailure::ExpectedBool.into());
105113
},
106-
_ => return Err(DeserializeFailure::ExpectedBool.into())
114+
false => {
115+
// if no special symbol was detected, it must have auxiliary data
116+
auxiliary_data = (|| -> Result<_, DeserializeError> {
117+
Ok(Some(AuxiliaryData::deserialize(raw)?))
118+
})().map_err(|e| e.annotate("auxiliary_data"))?;
119+
checked_auxiliary_data = true;
120+
return Ok(true);
121+
}
107122
}
108123
})().map_err(|e| e.annotate("is_valid"))?;
109-
let auxiliary_data = (|| -> Result<_, DeserializeError> {
110-
Ok(match raw.cbor_type()? != CBORType::Special {
111-
true => {
112-
Some(AuxiliaryData::deserialize(raw)?)
113-
},
114-
false => {
115-
if raw.special()? != CBORSpecial::Null {
116-
return Err(DeserializeFailure::ExpectedNull.into());
124+
if (!checked_auxiliary_data) {
125+
// this branch is reached, if the 3rd argument was a bool. then it simply follows the rules for checking auxiliary data
126+
auxiliary_data = (|| -> Result<_, DeserializeError> {
127+
Ok(match raw.cbor_type()? != CBORType::Special {
128+
true => {
129+
Some(AuxiliaryData::deserialize(raw)?)
130+
},
131+
false => {
132+
if raw.special()? != CBORSpecial::Null {
133+
return Err(DeserializeFailure::ExpectedNull.into());
134+
}
135+
None
117136
}
118-
None
119-
}
120-
})
121-
})().map_err(|e| e.annotate("auxiliary_data"))?;
137+
})
138+
})().map_err(|e| e.annotate("auxiliary_data"))?;
139+
}
122140
Ok(Transaction {
123141
body,
124142
witness_set,

0 commit comments

Comments
 (0)