Skip to content

Commit e5d099e

Browse files
committed
sqldb/sqlc: add graph channel schemas
In this commit, we define the SQL schemas for storing graph channel data. This includes a new `channels` table and a new `channel_features` table along with various indices.
1 parent bff2f24 commit e5d099e

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

sqldb/sqlc/migrations/000007_graph.down.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ DROP INDEX IF EXISTS node_extra_types_unique;
44
DROP INDEX IF EXISTS node_features_unique;
55
DROP INDEX IF EXISTS node_addresses_unique;
66
DROP INDEX IF EXISTS source_nodes_unique;
7+
DROP INDEX IF EXISTS channels_node_id_1_idx;
8+
DROP INDEX IF EXISTS channels_node_id_2_idx;
9+
DROP INDEX IF EXISTS channels_unique;
10+
DROP INDEX IF EXISTS channels_version_outpoint_idx;
11+
DROP INDEX IF EXISTS channel_features_unique;
12+
DROP INDEX IF EXISTS channel_extra_types_unique;
713

814
-- Drop tables in order of reverse dependencies.
915
DROP TABLE IF EXISTS source_nodes;
1016
DROP TABLE IF EXISTS node_addresses;
1117
DROP TABLE IF EXISTS node_features;
1218
DROP TABLE IF EXISTS node_extra_types;
13-
DROP TABLE IF EXISTS nodes;
19+
DROP TABLE IF EXISTS nodes;
20+
DROP TABLE IF EXISTS channels;
21+
DROP TABLE IF EXISTS channel_features;
22+
DROP TABLE IF EXISTS channel_extra_types;

sqldb/sqlc/migrations/000007_graph.up.sql

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,117 @@ CREATE TABLE IF NOT EXISTS source_nodes (
9494
);
9595
CREATE UNIQUE INDEX IF NOT EXISTS source_nodes_unique ON source_nodes (
9696
node_id
97-
);
97+
);
98+
99+
/* ─────────────────────────────────────────────
100+
channel data tables
101+
─────────────────────────────────────────────
102+
*/
103+
104+
-- channels stores all teh channels that we are aware of in the graph.
105+
CREATE TABLE IF NOT EXISTS channels (
106+
-- The db ID of the channel.
107+
id INTEGER PRIMARY KEY,
108+
109+
-- The protocol version that this node was gossiped on.
110+
version SMALLINT NOT NULL,
111+
112+
-- The channel id (short channel id) of the channel.
113+
scid BLOB NOT NULL,
114+
115+
-- A reference to a node in the nodes table for the node_1 node in
116+
-- the channel announcement.
117+
node_id_1 BIGINT NOT NULL REFERENCES nodes(id) ON DELETE CASCADE,
118+
119+
-- A reference to a node in the nodes table for the node_2 node in
120+
-- the channel announcement.
121+
node_id_2 BIGINT NOT NULL REFERENCES nodes(id) ON DELETE CASCADE,
122+
123+
-- The outpoint of the funding transaction. We chose to store this
124+
-- in a string format for the sake of readability and user queries on
125+
-- outpoint.
126+
outpoint TEXT NOT NULL,
127+
128+
-- The capacity of the channel in millisatoshis. This is nullable since
129+
-- (for the v1 protocol at least), the capacity is not necessarily known as
130+
-- it is not gossiped in the channel announcement.
131+
capacity BIGINT,
132+
133+
-- bitcoin_key_1 is the public key owned by node_1 that is used to create
134+
-- a funding transaction. It is nullable since future protocol version may
135+
-- not make use of this field.
136+
bitcoin_key_1 BLOB,
137+
138+
-- bitcoin_key_2 is the public key owned by node_2 that is used to create
139+
-- the funding transaction. It is nullable since future protocol version may
140+
-- not make use of this field.
141+
bitcoin_key_2 BLOB,
142+
143+
-- node_1_signature is the signature of the serialised channel announcement
144+
-- using the node_1 public key. It is nullable since future protocol
145+
-- versions may not make use of this field _and_ because for this node's own
146+
-- channels, the signature will only be known after the initial record
147+
-- creation.
148+
node_1_signature BLOB,
149+
150+
-- node_2_signature is the signature of the serialised channel announcement
151+
-- using the node_2 public key. It is nullable since future protocol
152+
-- versions may not make use of this field _and_ because for this node's own
153+
-- channels, the signature will only be known after the initial record
154+
-- creation.
155+
node_2_signature BLOB,
156+
157+
-- bitcoin_1_signature is the signature of the serialised channel
158+
-- announcement using bitcion_key_1. It is nullable since future protocol
159+
-- versions may not make use of this field _and_ because for this node's
160+
-- own channels, the signature will only be known after the initial record
161+
-- creation.
162+
bitcoin_1_signature BLOB,
163+
164+
-- bitcoin_2_signature is the signature of the serialised channel
165+
-- announcement using bitcion_key_2. It is nullable since future protocol
166+
-- versions may not make use of this field _and_ because for this node's
167+
-- own channels, the signature will only be known after the initial record
168+
-- creation.
169+
bitcoin_2_signature BLOB
170+
);
171+
-- We'll want to lookup all the channels owned by a node, so we create
172+
-- indexes on the node_id_1 and node_id_2 columns.
173+
CREATE INDEX IF NOT EXISTS channels_node_id_1_idx ON channels(node_id_1);
174+
CREATE INDEX IF NOT EXISTS channels_node_id_2_idx ON channels(node_id_2);
175+
176+
-- A channel (identified by a short channel id) can only have one active
177+
-- channel announcement per protocol version. We also order the index by
178+
-- scid in descending order so that we have an idea of the latest channel
179+
-- announcement we know of.
180+
CREATE UNIQUE INDEX IF NOT EXISTS channels_unique ON channels(version, scid DESC);
181+
CREATE INDEX IF NOT EXISTS channels_version_outpoint_idx ON channels(version, outpoint);
182+
183+
-- channel_features contains the feature bits of a channel.
184+
CREATE TABLE IF NOT EXISTS channel_features (
185+
-- The channel id this feature belongs to.
186+
channel_id BIGINT NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
187+
188+
-- The feature bit value.
189+
feature_bit INTEGER NOT NULL
190+
);
191+
CREATE UNIQUE INDEX IF NOT EXISTS channel_features_unique ON channel_features (
192+
channel_id, feature_bit
193+
);
194+
195+
-- channel_extra_types stores any extra TLV fields covered by a channels
196+
-- announcement that we do not have an explicit column for in the channels
197+
-- table.
198+
CREATE TABLE IF NOT EXISTS channel_extra_types (
199+
-- The channel id this TLV field belongs to.
200+
channel_id BIGINT NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
201+
202+
-- The Type field.
203+
type BIGINT NOT NULL,
204+
205+
-- The value field.
206+
value BLOB
207+
);
208+
CREATE UNIQUE INDEX IF NOT EXISTS channel_extra_types_unique ON channel_extra_types (
209+
type, channel_id
210+
);

sqldb/sqlc/models.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)