Skip to content

Commit

Permalink
csv: fix parsing of bitfileds of types
Browse files Browse the repository at this point in the history
The parser was working fine when we found simple bitfileds
but when we find bitfileds of types (e.g: Signature) we
was not able to parse it.

This fixing the parsing of the bolt2

Link: #31
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Mar 27, 2024
1 parent bae2af6 commit 937b7d1
Show file tree
Hide file tree
Showing 11 changed files with 630 additions and 23 deletions.
6 changes: 6 additions & 0 deletions lncodegen-codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub trait CodeGen<'g> {

fn write_point(&mut self, field: &LNMsData);

fn build_sha256(&mut self, field: &LNMsData);

fn write_sha256(&mut self, field: &LNMsData);

fn build_bitfield(&mut self, filed: &LNMsData);

fn write_bitfiled(&mut self, field: &LNMsData);
Expand All @@ -84,6 +88,7 @@ pub trait CodeGen<'g> {
LNMsData::ShortChannelId(_) => self.build_short_channel_id(field),
LNMsData::Signature(_) => self.build_signature(field),
LNMsData::Point(_) => self.build_point(field),
LNMsData::Sha256(_) => self.build_sha256(field),
LNMsData::BitfieldStream(_, _) => self.build_bitfield(field),
LNMsData::TLVinit(tlv_name, _) => {
let tlv = symbol_table.get(tlv_name).unwrap();
Expand All @@ -110,6 +115,7 @@ pub trait CodeGen<'g> {
LNMsData::ShortChannelId(_) => self.write_short_channel_id(field),
LNMsData::Signature(_) => self.write_signature(field),
LNMsData::Point(_) => self.write_point(field),
LNMsData::Sha256(_) => self.write_sha256(field),
LNMsData::BitfieldStream(_, _) => self.write_bitfiled(field),
LNMsData::TLVinit(tlv_name, _) => {
let tlv = symbol_table.get(tlv_name).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions lncodegen-codegen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ impl<'g> CodeGen<'g> for RustCodeGen {
}
}

fn build_sha256(&mut self, field: &LNMsData) {
if let LNMsData::Sha256(name) = field {
let code = fmt_struct_filed!(name, "Sha256");
self.file_content += self.add_identation_to_code(&code).as_str();
}
}

fn write_sha256(&mut self, _: &LNMsData) {}

fn write_point(&mut self, _: &LNMsData) {}

fn build_signature(&mut self, filed: &LNMsData) {
Expand Down
43 changes: 28 additions & 15 deletions lncodegen-csvlang/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl Parser {
tokens.get(self.pos - 2)
}

fn lookup_next<'p>(&self, tokens: &'p [CSVToken]) -> Option<&'p CSVToken> {

Check warning on line 61 in lncodegen-csvlang/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

method `lookup_next` is never used

Check warning on line 61 in lncodegen-csvlang/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

method `lookup_next` is never used
tokens.get(self.pos + 1)
}

/// Parse a message type line of the csv file, where the format looks like
/// the following one:
///
Expand Down Expand Up @@ -137,8 +141,12 @@ impl Parser {
LNMsData::ChannelId(msg_val.val.to_owned())
}
CSVTokenType::Signature => {
let msg_val = self.lookup_last(tokens).unwrap();
LNMsData::Signature(msg_val.val.to_owned())
if self.is_bitfield(tokens) {
self.make_bitfield(tokens)
} else {
let msg_val = self.lookup_last(tokens).unwrap();
LNMsData::Signature(msg_val.val.to_owned())
}
}
CSVTokenType::ShortChannelId => {
let msg_val = self.lookup_last(tokens).unwrap();
Expand All @@ -152,18 +160,10 @@ impl Parser {
let msg_val = self.lookup_last(tokens).unwrap();
LNMsData::Sha256(msg_val.val.to_owned())
}
CSVTokenType::Byte => {
let tok = self.lookup_last(tokens).unwrap();
let size = if !self.peek_and_check_if_type_declaration(tokens) {
self.advance(tokens).val.to_owned()
} else {
"1".to_string()
};
trace!("bytes name {:?}\n", tok);
LNMsData::BitfieldStream(tok.val.to_owned(), size)
}
CSVTokenType::Byte => self.make_bitfield(tokens),
// FIXME: this is a start point for a tlv stream
CSVTokenType::LiteralString => {
log::debug!("token ****** `{:?}`", token);
// be compatible with cln csv
//
// The difference is that cln use some alias type
Expand All @@ -172,8 +172,6 @@ impl Parser {
if ["amount_sat"].contains(&token.val.to_string().as_str()) {
let tok = self.lookup_last(tokens).unwrap();
LNMsData::Uint16(tok.val.to_owned())
// FIXME: an array of any type, so we should be able to fix
// this in some way and automatically fill thsi.
} else if ["u8", "witness"].contains(&token.val.as_str()) {
let tok = self.lookup_last(tokens).unwrap();
let size = if !self.peek_and_check_if_type_declaration(tokens) {
Expand All @@ -198,6 +196,7 @@ impl Parser {
return;
}
}

trace!("Append msg data {:?} to msg {:?}", msg_data, target_msg);
target_msg.add_msg_data(&msg_data);
}
Expand Down Expand Up @@ -295,6 +294,21 @@ impl Parser {
typ.ty_data = fake_lnmessage.msg_data;
}

fn make_bitfield(&mut self, tokens: &[CSVToken]) -> LNMsData {
let tok = self.lookup_last(tokens).unwrap();
let size = if !self.peek_and_check_if_type_declaration(tokens) {
self.advance(tokens).val.to_owned()
} else {
"1".to_string()
};
trace!("bytes name {:?}\n", tok);
LNMsData::BitfieldStream(tok.val.to_owned(), size)
}

fn is_bitfield(&mut self, tokens: &[CSVToken]) -> bool {
!self.peek_and_check_if_type_declaration(tokens)
}

/// Entry point of the parser!
pub fn parse(&mut self, tokens: &[CSVToken]) {
while self.peek(tokens).ty != CSVTokenType::EOF {
Expand All @@ -303,7 +317,6 @@ impl Parser {
CSVTokenType::SubTy => self.parse_subtype(tokens),
CSVTokenType::TlvType => self.parse_tlv(tokens),
_ => {
trace!("parser status {:#?}", self.symbol_table);
trace!("look up token {:?}", self.lookup_last(tokens));
trace!("previous token {:?}", tokens.get(self.pos - 1).unwrap());
panic!("Unknown Token {:?}", self.peek(tokens))
Expand Down
17 changes: 17 additions & 0 deletions lncodegen-csvlang/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ mod test {
token::CSVToken {
ty: token::CSVTokenType::MsgTy,
val: "msgtype".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "init".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::Number,
val: "16".to_string(),
code_line: None,
},
];
for c in 0..expected.len() - 1 {
Expand All @@ -88,18 +91,22 @@ mod test {
token::CSVToken {
ty: token::CSVTokenType::MsgData,
val: "msgdata".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "init".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "gflen".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::U16,
val: "u16".to_string(),
code_line: None,
},
];
for c in 0..expected.len() - 1 {
Expand All @@ -121,26 +128,32 @@ mod test {
token::CSVToken {
ty: token::CSVTokenType::EOF,
val: "EOF".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "gflen".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::Byte,
val: "byte".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "globalfeatures".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "init".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::MsgData,
val: "msgdata".to_string(),
code_line: None,
},
];
for c in 0..expected.len() - 1 {
Expand Down Expand Up @@ -170,18 +183,22 @@ mod test {
token::CSVToken {
ty: token::CSVTokenType::MsgData,
val: "msgdata".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "init".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::LiteralString,
val: "gflen".to_string(),
code_line: None,
},
token::CSVToken {
ty: token::CSVTokenType::U16,
val: "u16".to_string(),
code_line: None,
},
];
for c in 0..expected.len() - 1 {
Expand Down
Loading

0 comments on commit 937b7d1

Please sign in to comment.