Cycles in IPFS DAGs

I have done the Blog tutorial on Protoschool, and one thing is bugging me. At some point in the tutorial, we create a DAG with a “Post” node that is linked to a “Tag” node which in turn is also linked to the same “Post” node, thus creating a cycle in the graph. DAG stands for directed acyclic graph, why is this allowed?

Links are oriented, so you can’t have a directed cycle (following arrows and find the same data node)? Like in this image: several cycles, zero directed cycles.
image

No it is a directed cycle, you have the Post “Computers” which has in its tag list the CID of the Tag “Hobby”, and the Tag “Hobby” has the list of CIDs of all the Posts it is associated to, especially the “Computer” Post. If you start from the “Computer” Post and follow the links, you can end up again on the same post.

You create Post node which has hash QmA, you create Tag node which is linked to Post creating hash QmB. You then link Post to Tag which creates a new object with hash QmC.

The process of linking Post to Tag creates an entirely new object, which is why the circular references are prevented. When you link Post to Tag it’s not pointing to the "newly linked Post" node, its pointing to the “old” Post node.

1 Like

@postables Thank you. I see, so QmC (Post with link to Tag) points to QmB (Tag with link to another Post) which points to QmA (Post without link). The trick is that QmC and QmA are both Posts with the name “Computer” but they’re not the same object because QmC has a link whereas QmA doesn’t.

It means that if we really wanted to make a blog website with IPFS DAGs using the method described in the tutorial it wouldn’t work properly, because if we wanted to see all the other posts sharing a tag with a particular post, it would show us an outdated list. I think the solution to that would be to have a new type of objects, Link, that would connect two different objects so that we have a bidirectional association. For example:

Link {
  obj1: "QmA",
  obj2: "QmB"
}

That sounds equivalent to storing a directory in IPFS, which you can already do. I believe a directory in IPFS is just a list of filenames and subdirectories and their IPFS links.

If you access a directory through IPFS in a browser that contains a.html and b.html, then you can access a.html through the directory’s ipfs link + “/a.html”, and then if a.html contains a link to “b.html”, it will just work, because when you click it, your browser will navigate to the directory’s ipfs link + “/b.html”.

1 Like

@Macil
Yes, that’s a more intuitive way to solve it, but a directory wouldn’t really be equivalent to a Link. We would rather create a directory named “Hobby” that contains all the Posts with the Tag “Hobby”, like the Post “Computers”. If this Post has several tags, its CID (QmA) will be present in multiple directories.

To access the list of all the Posts under the “Hobby” Tag in the HTML file of the “Computer” Post, you would have a link to the URI /tags/hobby. But it would mean that a single Post could have different URIs, which is not particulary good. For example, the “Computer” Post can be accessed by /tags/hobby/computer.html and by /tags/technology/computer.html.

What I think would be good to do in IPFS is an object that redirects you to another URI. For example, we are in the /tags/hobby directory and we click on computer.html and it redirects us to /posts/computer.html. A hack to do that would be to create a computer.html file in the /tags/hobby directory that would contain that:

<!DOCTYPE html>
<script>
window.location.replace("/posts/computers.html");
<script>

But it would be good to have a native means to do that instead.