@@ -240,14 +240,75 @@ CREATE TABLE market_offline_urls (
240
240
unique (piece_cid)
241
241
);
242
242
243
- CREATE TABLE libp2p_keys (
243
+ -- This table is used for coordinating libp2p nodes
244
+ CREATE TABLE libp2p (
244
245
sp_id BIGINT NOT NULL ,
245
246
priv_key BYTEA NOT NULL ,
246
- listen_address TEXT NOT NULL ,
247
- announce_address TEXT NOT NULL ,
248
- no_announce_address TEXT NOT NULL
247
+ listen_address TEXT DEFAULT NULL ,
248
+ announce_address TEXT DEFAULT NULL ,
249
+ no_announce_address TEXT DEFAULT NULL ,
250
+ running_on TEXT DEFAULT NULL ,
251
+ updated_at TIMESTAMPTZ DEFAULT NULL ,
252
+
253
+ -- sp_id, priv_key must be unique for miner<>peerID combo uniqueness
254
+ -- announce address should be unique to avoid having 2 peerID reachable on same address
255
+ constraint market_libp2p_identity_key unique (sp_id, priv_key, announce_address)
249
256
);
250
257
258
+ -- -- Function used to update the libp2p table
259
+ CREATE OR REPLACE FUNCTION insert_or_update_libp2p (
260
+ _sp_id BIGINT ,
261
+ _listen_address TEXT ,
262
+ _announce_address TEXT ,
263
+ _no_announce_address TEXT ,
264
+ _running_on TEXT
265
+ )
266
+ RETURNS BYTEA AS $$
267
+ DECLARE
268
+ _priv_key BYTEA ;
269
+ _current_running_on TEXT ;
270
+ _current_updated_at TIMESTAMPTZ ;
271
+ BEGIN
272
+ -- Check if the sp_id exists and retrieve the current values
273
+ SELECT priv_key, running_on, updated_at INTO _priv_key, _current_running_on, _current_updated_at
274
+ FROM libp2p
275
+ WHERE sp_id = _sp_id;
276
+
277
+ -- Raise an exception if no row was found
278
+ IF NOT FOUND THEN
279
+ RAISE EXCEPTION ' libp2p key for sp_id "%" does not exist' , _sp_id;
280
+ END IF;
281
+
282
+ -- If the sp_id exists and running_on is NULL or matches _running_on
283
+ IF _current_running_on IS NULL OR _current_running_on = _running_on THEN
284
+ -- Update the record with the provided values and set updated_at to NOW
285
+ UPDATE libp2p
286
+ SET
287
+ listen_address = _listen_address,
288
+ announce_address = _announce_address,
289
+ no_announce_address = _no_announce_address,
290
+ running_on = _running_on,
291
+ updated_at = NOW() AT TIME ZONE ' UTC'
292
+ WHERE sp_id = _sp_id;
293
+ ELSIF _current_updated_at > NOW() - INTERVAL ' 10 seconds' THEN
294
+ -- Raise an exception if running_on is different and updated_at is recent
295
+ RAISE EXCEPTION ' Libp2p node already running on "%"' , _current_running_on;
296
+ ELSE
297
+ -- Update running_on and other columns if updated_at is older than 10 seconds
298
+ UPDATE libp2p
299
+ SET
300
+ listen_address = _listen_address,
301
+ announce_address = _announce_address,
302
+ no_announce_address = _no_announce_address,
303
+ running_on = _running_on,
304
+ updated_at = NOW() AT TIME ZONE ' UTC'
305
+ WHERE sp_id = _sp_id;
306
+ END IF;
307
+
308
+ RETURN _priv_key;
309
+ END;
310
+ $$ LANGUAGE plpgsql;
311
+
251
312
-- Add host column to allow local file based
252
313
-- piece park
253
314
ALTER TABLE parked_piece_refs
@@ -275,9 +336,6 @@ CREATE TABLE market_legacy_deals (
275
336
publish_cid TEXT NOT NULL ,
276
337
chain_deal_id BIGINT NOT NULL ,
277
338
278
- piece_cid TEXT NOT NULL ,
279
- piece_size BIGINT NOT NULL ,
280
-
281
339
fast_retrieval BOOLEAN NOT NULL ,
282
340
283
341
created_at TIMESTAMPTZ NOT NULL ,
0 commit comments