Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crdx: Improve performance of decryptGraph #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions packages/crdx/src/graph/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,32 @@ export const decryptGraph: DecryptFn = <A extends Action, C>({
keys: KeysetWithSecrets | KeysetWithSecrets[] | Keyring
}): Graph<A, C> => {
const { encryptedLinks, root, childMap = {} } = encryptedGraph

const links = encryptedGraph.links ?? {}
const toVisit = [root]
const visited: Set<Hash> = new Set()
const decryptedLinks: Record<Hash, Link<A, C>> = {}

while (toVisit.length > 0) {
const current = toVisit.pop() as Hash

if (visited.has(current)) {
continue
}

/** Recursively decrypts a link and its children. */
const decrypt = (
hash: Hash,
prevLinks: Record<Hash, Link<A, C>> = {}
): Record<Hash, Link<A, C>> => {
// decrypt this link
const encryptedLink = encryptedLinks[hash]
const encryptedLink = encryptedLinks[current]
const decryptedLink =
links[hash] ?? // if it's already decrypted, don't bother decrypting it again
links[current] ?? // if it's already decrypted, don't bother decrypting it again
decryptLink(encryptedLink, keys)
let newLinks = {
[hash]: decryptedLink,
}

// decrypt its children
const children = childMap[hash] ?? []
for (const hash of children) {
newLinks = { ...newLinks, ...decrypt(hash, newLinks) }
}
decryptedLinks[current] = decryptedLink

return { ...prevLinks, ...newLinks }
const children = childMap[current] ?? []
for (const child of children) {
toVisit.push(child)
}
visited.add(current)
}

const decryptedLinks = decrypt(root)

return {
...encryptedGraph,
links: decryptedLinks,
Expand Down
Loading