diff --git a/README.md b/README.md index b652c9c66db9d63f117558178b3b9715dd45629f..5ef4fcca44cbe3d5b8084c40d014bc24683deda8 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 ebccb0b3f2e936e1be47f3d8165c6b1b92982cd4..ec6d791981950df1dd3310cb1c666db80090cd4a 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 7bc9ce32783ec6fcc36bb05698c7c8c614241e15..b655b4bb7dfab0290ed007f56ccc06932ce3e54a 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 20596ada86eaad4b697b8a28ef03905a2335831b..84cb5c2d10ca7d65d86552c00bbdbe3bb4b02bf5 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 54ef153d90290c8ba85f64b503f625e23322a026..1c08ede1f549aa34dc5593c3d413510a857ab946 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 e17018a067adeb80f34bd53e9a48277712624204..7b643ae8bc447cbe0eb97eef734d200d03a3de13 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)