Skip to content
Snippets Groups Projects
Commit 04b2522c authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

fix unchanged node

parent fab786d9
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ import type { PubSub } from '@libp2p/interface'
export const local_peer = [
'/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWF44SaSomGuUSNycgpkRwQcxcsMYeNbtn6XCHPR2ojodv',
'/ip4/127.0.0.1/udp/4001/quic-v1/p2p/12D3KooWF44SaSomGuUSNycgpkRwQcxcsMYeNbtn6XCHPR2ojodv',
'/ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEiCQEENr7FShHOkuCAUCgW-oahGIGj0Mk3tOHXQjrsWGWQ/certhash/uEiCG3LMdW9dtRC_kXft2TlE66kZ4RX1MzQksLwxRgbJMVw/p2p/12D3KooWF44SaSomGuUSNycgpkRwQcxcsMYeNbtn6XCHPR2ojodv'
'/ip4/127.0.0.1/udp/4001/quic-v1/webtransport/certhash/uEiCG3LMdW9dtRC_kXft2TlE66kZ4RX1MzQksLwxRgbJMVw/certhash/uEiC6q4csXlqjvPg_z28SK2fu59NKwOLBDud7j-6Cdn958Q/p2p/12D3KooWF44SaSomGuUSNycgpkRwQcxcsMYeNbtn6XCHPR2ojodv'
]
export const TOPIC = 'ddd'
......
......@@ -12,19 +12,26 @@ export async function addToIndexQueue(cid: CID, request: IndexRequest) {
// https://polkadot-blockchain-academy.github.io/pba-book/substrate/storage/slides.html#/
// key size can be expressed in x chars depending on base
const size = 64 * Math.log(2) / Math.log(16)
const size = (64 * Math.log(2)) / Math.log(16)
const key = request.timestamp.toString(BASE).padStart(size, '0')
console.log('key: ' + key)
// console.log('cid: ' + cid.toString())
for await (const name of kubo.name.resolve(IPNS, { nocache: true })) {
console.log('initializing root node from ipns')
// console.log('initializing root node from ipns')
const rootCID = CID.parse(name.slice(6))
const dag: IndexInode = (await kubo.dag.get(rootCID)).value
const newRootCID = await processInode(dag, key, cid)
await kubo.name.publish(newRootCID, { ttl: '1s' })
console.log('new root cid published: ' + newRootCID.toString())
}
}
async function processInode(node: IndexInode, key: string, val: CID): Promise<CID> {
// console.log("key: " + key)
// bucket
const b = parseInt(key[0], BASE)
// if bucket is available, place leaf in it
......@@ -34,28 +41,42 @@ async function processInode(node: IndexInode, key: string, val: CID): Promise<CI
// must share bucket with a node
const [k1, cid1] = node.children[b] as [string, CID]
const comp = compareKey(k1, key)
// console.log(comp)
switch (comp.type) {
case resultType.Enter:
const e = comp as enterResult
const enterNode = (await kubo.dag.get(cid1)).value as IndexInode
node.children[b] = [e.common, await processInode(enterNode, e.nk, val)]
console.log('enter "' + e.common + '" key "' + e.nk + '"')
const enterNode: IndexInode | IndexLeaf = (await kubo.dag.get(cid1)).value
const enterNodeAsLeaf = enterNode as IndexLeaf
const enterNodeAsInode = enterNode as IndexInode
if (enterNodeAsLeaf.leaf) {
// we can not enter a leaf at this stage
throw Error('should not enter a leaf, this should have been an end')
}
node.children[b] = [e.common, await processInode(enterNodeAsInode, e.nk, val)]
break
case resultType.End:
console.log('end')
const otherLeaf = (await kubo.dag.get(cid1)).value as IndexLeaf
node.children[b] = [k1, await processLeaf(otherLeaf, val)]
break
case resultType.Diff:
const c = comp as diffResult
console.log('diff on "' + c.common + '" keys "' + c.nk1 + '" / "' + c.nk2 + '"')
const newNode = emptyInode()
newNode.children[c.b1] = [c.nk1, cid1]
newNode.children[c.b2] = [c.nk2, await processLeaf(emptyLeaf(), val)]
const newNodeCid = await kubo.dag.put(newNode)
node.children[b] = [c.common, newNodeCid]
break
}
}
// now that we have the new node save it and return
const newCid: CID = await kubo.dag.put(node)
console.log('new inode: ' + newCid.toString())
return newCid
}
......@@ -119,7 +140,7 @@ function compareKey(k1: string, k2: string): compResult {
type: resultType.Enter,
common,
b,
nk: k2.slice(b)
nk: k2.slice(common.length)
}
} else {
// we reached the end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment