Skip to content

Commit

Permalink
meta: add a way to self generate types
Browse files Browse the repository at this point in the history
This is a change for the gossip map that use a compiler
that generate the messages that are used inside the gossip
map.

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Apr 1, 2024
1 parent 3ee46cb commit a549b69
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 119 deletions.
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CC=cargo
FMT=fmt

OPTIONS=
# Please keep me update with the https://github.com/ElementsProject/lightning/blob/master/Makefile#L29
BOLTDIR := /tmp/
OPTIONS := ""

default: fmt
$(CC) build --all-features
Expand All @@ -20,3 +22,20 @@ example:

clean:
$(CC) clean

# FIXME: we should apply the diff over the csv file.
genfile: check_compiler
cd "$(BOLTDIR)" && git clone https://github.com/lightning/bolts.git
cd /tmp && git clone https://github.com/ElementsProject/lightning.git
cd $(BOLTDIR)/bolts && tools/extract-formats.py 07-routing-gossip.md > bolt7.csv
cp /tmp/lightning/gossipd/gossip_store_wire.csv gossip_map/spec/gossip_store_wire.csv
cp /tmp/bolts/bolt7.csv gossip_map/spec/bolt7.csv
@make update-genfile

update-genfile:
lncodegen-cli -l rust generate -b gossip_map/spec/gossip_store_wire.csv gossip_map/src/gossip_stor_wiregen.rs
lncodegen-cli -l rust generate -b gossip_map/spec/bolt7.csv gossip_map/src/bolt7.rs


check_compiler:
@command -v lncodegen-cli --help >/dev/null 2>&1 || (echo "`lncodegen-cli` not found, installing..." && cargo install lncodegen-cli --git https://github.com/lnspec-tools/lncodegen.git)
8 changes: 8 additions & 0 deletions gossip_map/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ version = "0.1.0"
edition = "2021"

[dependencies]
log = "0.4.21"
byteorder = "1.4.3"
hex = "0.4.3"
bitcoin = { version = "0.30.0" }
fundamentals = { git = "https://github.com/lnspec-tools/ln-fundamentals.git" }
fundamentals-derive = { git = "https://github.com/lnspec-tools/ln-fundamentals.git" }
lightning = "0.0.121"

[dev-dependencies]
anyhow = "1.0.70"
colored = "1.9"
log = { version = "0.4", features = ["std"] }
chrono = { version = "0.4", features = ["std"], default-features = false }
81 changes: 81 additions & 0 deletions gossip_map/spec/bolt7.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
msgtype,announcement_signatures,259
msgdata,announcement_signatures,channel_id,channel_id,
msgdata,announcement_signatures,short_channel_id,short_channel_id,
msgdata,announcement_signatures,node_signature,signature,
msgdata,announcement_signatures,bitcoin_signature,signature,
msgtype,channel_announcement,256
msgdata,channel_announcement,node_signature_1,signature,
msgdata,channel_announcement,node_signature_2,signature,
msgdata,channel_announcement,bitcoin_signature_1,signature,
msgdata,channel_announcement,bitcoin_signature_2,signature,
msgdata,channel_announcement,len,u16,
msgdata,channel_announcement,features,byte,len
msgdata,channel_announcement,chain_hash,chain_hash,
msgdata,channel_announcement,short_channel_id,short_channel_id,
msgdata,channel_announcement,node_id_1,point,
msgdata,channel_announcement,node_id_2,point,
msgdata,channel_announcement,bitcoin_key_1,point,
msgdata,channel_announcement,bitcoin_key_2,point,
msgtype,node_announcement,257
msgdata,node_announcement,signature,signature,
msgdata,node_announcement,flen,u16,
msgdata,node_announcement,features,byte,flen
msgdata,node_announcement,timestamp,u32,
msgdata,node_announcement,node_id,point,
msgdata,node_announcement,rgb_color,byte,3
msgdata,node_announcement,alias,byte,32
msgdata,node_announcement,addrlen,u16,
msgdata,node_announcement,addresses,byte,addrlen
msgtype,channel_update,258
msgdata,channel_update,signature,signature,
msgdata,channel_update,chain_hash,chain_hash,
msgdata,channel_update,short_channel_id,short_channel_id,
msgdata,channel_update,timestamp,u32,
msgdata,channel_update,message_flags,byte,
msgdata,channel_update,channel_flags,byte,
msgdata,channel_update,cltv_expiry_delta,u16,
msgdata,channel_update,htlc_minimum_msat,u64,
msgdata,channel_update,fee_base_msat,u32,
msgdata,channel_update,fee_proportional_millionths,u32,
msgdata,channel_update,htlc_maximum_msat,u64,
msgtype,query_short_channel_ids,261
msgdata,query_short_channel_ids,chain_hash,chain_hash,
msgdata,query_short_channel_ids,len,u16,
msgdata,query_short_channel_ids,encoded_short_ids,byte,len
msgdata,query_short_channel_ids,tlvs,query_short_channel_ids_tlvs,
tlvtype,query_short_channel_ids_tlvs,query_flags,1
tlvdata,query_short_channel_ids_tlvs,query_flags,encoding_type,byte,
tlvdata,query_short_channel_ids_tlvs,query_flags,encoded_query_flags,byte,...
msgtype,reply_short_channel_ids_end,262
msgdata,reply_short_channel_ids_end,chain_hash,chain_hash,
msgdata,reply_short_channel_ids_end,full_information,byte,
msgtype,query_channel_range,263
msgdata,query_channel_range,chain_hash,chain_hash,
msgdata,query_channel_range,first_blocknum,u32,
msgdata,query_channel_range,number_of_blocks,u32,
msgdata,query_channel_range,tlvs,query_channel_range_tlvs,
tlvtype,query_channel_range_tlvs,query_option,1
tlvdata,query_channel_range_tlvs,query_option,query_option_flags,bigsize,
msgtype,reply_channel_range,264
msgdata,reply_channel_range,chain_hash,chain_hash,
msgdata,reply_channel_range,first_blocknum,u32,
msgdata,reply_channel_range,number_of_blocks,u32,
msgdata,reply_channel_range,sync_complete,byte,
msgdata,reply_channel_range,len,u16,
msgdata,reply_channel_range,encoded_short_ids,byte,len
msgdata,reply_channel_range,tlvs,reply_channel_range_tlvs,
tlvtype,reply_channel_range_tlvs,timestamps_tlv,1
tlvdata,reply_channel_range_tlvs,timestamps_tlv,encoding_type,byte,
tlvdata,reply_channel_range_tlvs,timestamps_tlv,encoded_timestamps,byte,...
tlvtype,reply_channel_range_tlvs,checksums_tlv,3
tlvdata,reply_channel_range_tlvs,checksums_tlv,checksums,channel_update_checksums,...
subtype,channel_update_timestamps
subtypedata,channel_update_timestamps,timestamp_node_id_1,u32,
subtypedata,channel_update_timestamps,timestamp_node_id_2,u32,
subtype,channel_update_checksums
subtypedata,channel_update_checksums,checksum_node_id_1,u32,
subtypedata,channel_update_checksums,checksum_node_id_2,u32,
msgtype,gossip_timestamp_filter,265
msgdata,gossip_timestamp_filter,chain_hash,chain_hash,
msgdata,gossip_timestamp_filter,first_timestamp,u32,
msgdata,gossip_timestamp_filter,timestamp_range,u32,
29 changes: 29 additions & 0 deletions gossip_map/spec/gossip_store_wire.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# gossip_store messages: messages persisted in the gossip_store
# We store raw messages here, so these numbers must not overlap with
# 256/257/258.
#include <common/amount.h>
#include <common/node_id.h>

# This always follows the channel_announce / private_announce
msgtype,gossip_store_channel_amount,4101
msgdata,gossip_store_channel_amount,satoshis,amount_sat,

# Mimics a channel_announce, except signatures are all-zero
msgtype,gossip_store_private_channel_obs,4104
msgdata,gossip_store_private_channel_obs,satoshis,amount_sat,
msgdata,gossip_store_private_channel_obs,len,u16,
msgdata,gossip_store_private_channel_obs,announcement,u8,len

msgtype,gossip_store_private_update_obs,4102
msgdata,gossip_store_private_update_obs,len,u16,
msgdata,gossip_store_private_update_obs,update,u8,len

msgtype,gossip_store_delete_chan,4103
msgdata,gossip_store_delete_chan,scid,short_channel_id,

msgtype,gossip_store_ended,4105
msgdata,gossip_store_ended,equivalent_offset,u64,

msgtype,gossip_store_chan_dying,4106
msgdata,gossip_store_chan_dying,scid,short_channel_id,
msgdata,gossip_store_chan_dying,blockheight,u32,
112 changes: 112 additions & 0 deletions gossip_map/src/bolt7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// code generated with the lngen, please not edit this file.
use std::io::{Read, Write};

use fundamentals_derive::{DecodeWire, EncodeWire};

use crate::core::{FromWire, ToWire};
use crate::prelude::*;

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct AnnouncementSignatures {
#[msg_type = 259]
pub ty: u16,
pub channel_id: ChannelId,
pub short_channel_id: ShortChannelId,
pub node_signature: Signature,
pub bitcoin_signature: Signature,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct ChannelAnnouncement {
#[msg_type = 256]
pub ty: u16,
pub node_signature_1: Signature,
pub node_signature_2: Signature,
pub bitcoin_signature_1: Signature,
pub bitcoin_signature_2: Signature,
pub features: BitFlag,
pub chain_hash: ChainHash,
pub short_channel_id: ShortChannelId,
pub node_id_1: Point,
pub node_id_2: Point,
pub bitcoin_key_1: Point,
pub bitcoin_key_2: Point,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct ChannelUpdate {
#[msg_type = 258]
pub ty: u16,
pub signature: Signature,
pub chain_hash: ChainHash,
pub short_channel_id: ShortChannelId,
pub timestamp: u32,
pub message_flags: BitFlag,
pub channel_flags: BitFlag,
pub cltv_expiry_delta: u16,
pub htlc_minimum_msat: u64,
pub fee_base_msat: u32,
pub fee_proportional_millionths: u32,
pub htlc_maximum_msat: u64,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipTimestampFilter {
#[msg_type = 265]
pub ty: u16,
pub chain_hash: ChainHash,
pub first_timestamp: u32,
pub timestamp_range: u32,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct NodeAnnouncement {
#[msg_type = 257]
pub ty: u16,
pub signature: Signature,
pub features: BitFlag,
pub timestamp: u32,
pub node_id: Point,
pub rgb_color: BitFlag,
pub alias: BitFlag,
pub addresses: BitFlag,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct QueryChannelRange {
#[msg_type = 263]
pub ty: u16,
pub chain_hash: ChainHash,
pub first_blocknum: u32,
pub number_of_blocks: u32,
pub query_channel_range_tlvs: Stream,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct QueryShortChannelIds {
#[msg_type = 261]
pub ty: u16,
pub chain_hash: ChainHash,
pub encoded_short_ids: BitFlag,
pub query_short_channel_ids_tlvs: Stream,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct ReplyChannelRange {
#[msg_type = 264]
pub ty: u16,
pub chain_hash: ChainHash,
pub first_blocknum: u32,
pub number_of_blocks: u32,
pub sync_complete: BitFlag,
pub encoded_short_ids: BitFlag,
pub reply_channel_range_tlvs: Stream,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct ReplyShortChannelIdsEnd {
#[msg_type = 262]
pub ty: u16,
pub chain_hash: ChainHash,
pub full_information: BitFlag,
}
51 changes: 51 additions & 0 deletions gossip_map/src/gossip_stor_wiregen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// code generated with the lngen, please not edit this file.
use std::io::{Read, Write};

use fundamentals_derive::{DecodeWire, EncodeWire};

use crate::core::{FromWire, ToWire};
use crate::prelude::*;

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStoreChanDying {
#[msg_type = 4106]
pub ty: u16,
pub scid: ShortChannelId,
pub blockheight: u32,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStoreChannelAmount {
#[msg_type = 4101]
pub ty: u16,
pub satoshis: u64,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStoreDeleteChan {
#[msg_type = 4103]
pub ty: u16,
pub scid: ShortChannelId,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStoreEnded {
#[msg_type = 4105]
pub ty: u16,
pub equivalent_offset: u64,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStorePrivateChannelObs {
#[msg_type = 4104]
pub ty: u16,
pub satoshis: u64,
pub announcement: BitFlag,
}

#[derive(DecodeWire, EncodeWire, Debug, Clone)]
pub struct GossipStorePrivateUpdateObs {
#[msg_type = 4102]
pub ty: u16,
pub update: BitFlag,
}
57 changes: 0 additions & 57 deletions gossip_map/src/gossip_store_msg.rs

This file was deleted.

Loading

0 comments on commit a549b69

Please sign in to comment.