Skip to content
Snippets Groups Projects
Commit de4b5098 authored by Éloïs's avatar Éloïs
Browse files

[fix] documents:tx: some weird(manual) outputs poorly parsed

parent 4de69b54
No related branches found
No related tags found
1 merge request!144Elois/fix gt sync
...@@ -273,6 +273,7 @@ dependencies = [ ...@@ -273,6 +273,7 @@ dependencies = [
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
......
...@@ -24,6 +24,7 @@ serde = "1.0.*" ...@@ -24,6 +24,7 @@ serde = "1.0.*"
serde_derive = "1.0.*" serde_derive = "1.0.*"
serde_json = "1.0.*" serde_json = "1.0.*"
log = "0.4.*" log = "0.4.*"
unwrap = "1.2.1"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.5.1" pretty_assertions = "0.5.1"
......
...@@ -22,6 +22,7 @@ use pest::iterators::Pairs; ...@@ -22,6 +22,7 @@ use pest::iterators::Pairs;
use pest::Parser; use pest::Parser;
use std::ops::{Add, Deref, Sub}; use std::ops::{Add, Deref, Sub};
use std::str::FromStr; use std::str::FromStr;
use unwrap::unwrap;
use crate::blockstamp::Blockstamp; use crate::blockstamp::Blockstamp;
use crate::documents::*; use crate::documents::*;
...@@ -116,7 +117,7 @@ impl FromStr for TransactionInput { ...@@ -116,7 +117,7 @@ impl FromStr for TransactionInput {
pairs.next().unwrap().into_inner(), pairs.next().unwrap().into_inner(),
)), )),
Err(_) => Err(TextDocumentParseError::InvalidInnerFormat( Err(_) => Err(TextDocumentParseError::InvalidInnerFormat(
"Invalid unlocks !", "Invalid unlocks !".to_owned(),
)), )),
} }
} }
...@@ -239,7 +240,7 @@ impl FromStr for TransactionInputUnlocks { ...@@ -239,7 +240,7 @@ impl FromStr for TransactionInputUnlocks {
pairs.next().unwrap().into_inner(), pairs.next().unwrap().into_inner(),
)), )),
Err(_) => Err(TextDocumentParseError::InvalidInnerFormat( Err(_) => Err(TextDocumentParseError::InvalidInnerFormat(
"Invalid unlocks !", "Invalid unlocks !".to_owned(),
)), )),
} }
} }
...@@ -466,13 +467,42 @@ impl FromStr for TransactionOutput { ...@@ -466,13 +467,42 @@ impl FromStr for TransactionOutput {
type Err = TextDocumentParseError; type Err = TextDocumentParseError;
fn from_str(source: &str) -> Result<Self, Self::Err> { fn from_str(source: &str) -> Result<Self, Self::Err> {
match DocumentsParser::parse(Rule::tx_output, source) { let output_parts: Vec<&str> = source.split(':').collect();
Ok(mut utxo_pairs) => Ok(TransactionOutput::from_pest_pair( let amount = output_parts.get(0);
utxo_pairs.next().unwrap().into_inner(), let base = output_parts.get(1);
)), let conditions_origin_str = output_parts.get(2);
Err(_) => Err(TextDocumentParseError::InvalidInnerFormat(
"Invalid output !", let str_to_parse = if amount.is_some() && base.is_some() && conditions_origin_str.is_some()
)), {
format!(
"{}:{}:({})",
unwrap!(amount),
unwrap!(base),
unwrap!(conditions_origin_str)
)
} else {
source.to_owned()
};
match DocumentsParser::parse(Rule::tx_output, &str_to_parse) {
Ok(mut utxo_pairs) => {
let mut output =
TransactionOutput::from_pest_pair(utxo_pairs.next().unwrap().into_inner());
output.conditions.origin_str = conditions_origin_str.map(ToString::to_string);
Ok(output)
}
Err(_) => match DocumentsParser::parse(Rule::tx_output, source) {
Ok(mut utxo_pairs) => {
let mut output =
TransactionOutput::from_pest_pair(utxo_pairs.next().unwrap().into_inner());
output.conditions.origin_str = conditions_origin_str.map(ToString::to_string);
Ok(output)
}
Err(e) => Err(TextDocumentParseError::InvalidInnerFormat(format!(
"Invalid output : {}",
e
))),
},
} }
} }
} }
...@@ -578,8 +608,10 @@ impl TransactionDocument { ...@@ -578,8 +608,10 @@ impl TransactionDocument {
} else { } else {
fatal_error!("Try to compute_hash of tx with None text !") fatal_error!("Try to compute_hash of tx with None text !")
}; };
hashing_text.push_str(&self.signatures[0].to_string()); for sig in &self.signatures {
hashing_text.push_str(&sig.to_string());
hashing_text.push_str("\n"); hashing_text.push_str("\n");
}
//println!("tx_text_hasing={}", hashing_text); //println!("tx_text_hasing={}", hashing_text);
self.hash = Some(Hash::compute_str(&hashing_text)); self.hash = Some(Hash::compute_str(&hashing_text));
self.hash.expect("Try to get hash of a reduce tx !") self.hash.expect("Try to get hash of a reduce tx !")
......
...@@ -76,8 +76,8 @@ pub trait TextDocumentParser<R: RuleType> { ...@@ -76,8 +76,8 @@ pub trait TextDocumentParser<R: RuleType> {
#[derive(Debug, Clone, Fail)] #[derive(Debug, Clone, Fail)]
pub enum TextDocumentParseError { pub enum TextDocumentParseError {
/// The given source don't have a valid specific document format (document type). /// The given source don't have a valid specific document format (document type).
#[fail(display = "TextDocumentParseError: Invalid inner format.")] #[fail(display = "TextDocumentParseError: Invalid inner format: {}", _0)]
InvalidInnerFormat(&'static str), InvalidInnerFormat(String),
/// Error with pest parser /// Error with pest parser
#[fail(display = "TextDocumentParseError: PestError.")] #[fail(display = "TextDocumentParseError: PestError.")]
PestError(String), PestError(String),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment