From 01d62ba311a1dfa0277d13a01db77dea4173eac7 Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Thu, 23 May 2024 18:00:42 +0200 Subject: [PATCH] tweaks --- README.md | 1 + src/collector.ts | 4 ++-- src/consts.ts | 2 +- src/indexer/bootstrap.ts | 25 ++++++++++++++++++++++++- src/indexer/start.ts | 4 +++- src/main.ts | 2 +- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b652c9c..5ef4fcc 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ More detail in the doc below. ## TODO Bugs +- [ ] initialize dd_keys for new node (→ bootstrap) - [ ] fix merging blocked when inode unreachable, timeout seems ignored - [ ] fix pubsub cid can not be fetched triggers pubsub abort (dirty workaround for know) Features diff --git a/src/collector.ts b/src/collector.ts index ebccb0b..ec6d791 100644 --- a/src/collector.ts +++ b/src/collector.ts @@ -12,7 +12,7 @@ import type { Message, SignedMessage } from '@libp2p/interface' export function buildStringPayload(ir: IndexRequest) { let payload = `prefix: Duniter Datapod time: ${ir.time} -kind: ${ir.kind.toV1()} +kind: ${ir.kind} ` if (ir.data) payload += `data: ${ir.data.toV1()} @@ -69,7 +69,7 @@ export function getPubSubHandler( // TODO some validation on timestamp to prevent too much control on the key // example: 0 < Date.now() - timestamp < 1 minute const stringPayload = buildStringPayload(dag) - const isValid = isValidSignature(stringPayload, dag.sig, dag.pubkey) + const isValid = isValidSignature(stringPayload, dag.sig!, dag.pubkey) if (isValid) { // at this point we can apply different treatment based on the key // we could for example have a trust list published in ipfs diff --git a/src/consts.ts b/src/consts.ts index 7bc9ce3..b655b4b 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -11,7 +11,7 @@ export const BASE = 16 export const KEYSIZE = (64 * Math.log(2)) / Math.log(BASE) // empty root cid -export const EMPTY_NODE_CID = 'bafyreicvlp2p65agkxpzcboedba7zit55us4zvtyyq2wesvsdedy6irwfy' +export const EMPTY_NODE_CID = CID.parse('bafyreicvlp2p65agkxpzcboedba7zit55us4zvtyyq2wesvsdedy6irwfy') // document kind of old cesium plus profile imported in the indexer export const CESIUM_PLUS_PROFILE_IMPORT = 'cplus_raw' diff --git a/src/indexer/bootstrap.ts b/src/indexer/bootstrap.ts index 20596ad..84cb5c2 100644 --- a/src/indexer/bootstrap.ts +++ b/src/indexer/bootstrap.ts @@ -1,5 +1,8 @@ +import type { CID } from 'multiformats' import { kubo } from '../kubo' -import { DD_ROOT_OPT, getSelfDdKeys } from './ipns' +import { DD_ROOT_OPT, DD_TAMT_HIST_OPT, getSelfDdKeys } from './ipns' +import { resolveHist } from '../processor' +import type { IndexHist } from '../types' // setup the root index from scratch // after that, an other node can setup its own root index from another peer @@ -8,3 +11,23 @@ export async function publishKeys() { const cid = await kubo.dag.put(keys) await kubo.name.publish(cid, DD_ROOT_OPT) } + +/// check that history is available else initialize it +export async function initHistIfNull(cid: CID): Promise<void> { + try { + await resolveHist() + } catch { + // define first history element + const firstHist: IndexHist = { + last_history: null, + current_index: cid, + number: 0, + timestamp: Date.now() + } + const firstHistCID = await kubo.dag.put(firstHist) + kubo.name + .publish(firstHistCID, DD_TAMT_HIST_OPT) + .then(() => console.log('initialize history to ' + DD_TAMT_HIST_OPT.key)) + } + return +} diff --git a/src/indexer/start.ts b/src/indexer/start.ts index 54ef153..1c08ede 100644 --- a/src/indexer/start.ts +++ b/src/indexer/start.ts @@ -1,5 +1,5 @@ import { TOPIC } from '../consts' -import { timestampToKey, arrayToVinode, publishHistory, mergeInodesSyncCID, resolveHist } from '../processor' +import { timestampToKey, arrayToVinode, mergeInodesSyncCID, resolveHist } from '../processor' import { getPubSubHandler } from '../collector' import { KUBO_RPC, kubo, kubo2 } from '../kubo' import type { IndexHist, IndexRequest } from '../types' @@ -8,6 +8,7 @@ import { events, evtype, indexKnownDiff, indexStart } from './handlers' import type { DiffData } from './types' import { getRootCIDfromArgs } from './utils' import { DD_TAMT_HIST_OPT, DD_TAMT_OPT } from './ipns' +import { initHistIfNull } from './bootstrap' // === HANDLERS === @@ -161,6 +162,7 @@ let isProcessingQueue = false /// global rootCID variable // set it from CLI args let rootCID = await getRootCIDfromArgs(process.argv, trusted_peer_list) +await initHistIfNull(rootCID) // make sure history is available until then let histCID: CID = await resolveHist() let hist: IndexHist = (await kubo.dag.get(histCID)).value diff --git a/src/main.ts b/src/main.ts index e17018a..7b643ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,7 +27,7 @@ function handleMessage(message: any) { kubo.dag.get(cid).then(function (d) { const dag = d.value as IndexRequest const stringPayload = buildStringPayload(dag) - const isValid = isValidSignature(stringPayload, dag.sig, dag.pubkey) + const isValid = isValidSignature(stringPayload, dag.sig!, dag.pubkey) if (isValid) { // here we would do the processing // addToIndex(cid, dag) -- GitLab