I am looking at blockchain and trying to see how Merkle trees (or perhaps Merkle DAGs) can fit to a graph data structure with circular references.
For example, say I have this data model:
var profile = { setting1: 123, email: '[email protected]' }
var user2 = {}
var user3 = {}
var msg1 = { body: 'foo' }
var msg2 = { body: 'bar' }
var group1 = { name: 'Hello' }
var group2 = { name: 'World' }
var user1 = { messages: [ msg1, msg2, ... ] }
profile.user = user
user.profile = profile
msg1.sender = user1
msg1.recipient = user2
msg2.sender = user1
msg2.recipient = user3
group1.members = [ user1, user3 ]
group2.members = [ user1, user2, user3 ]
user1.groups = [ group1, group2 ]
user2.groups = [ group2 ]
user3.groups = [ group1, group2 ]
This is highly cyclic, but it only has really 2 or 3 levels deep of cyclicness. In reality there could be extremely large and complicated cycles, but I don't want to overcomplicate it.
With the Merkle tree, you essentially have a bunch of blobs of data which you make the leafs of a tree, and compute hashes for each pair all the way up to the top part of the tree. But if one of your blobs changes, then you have to recalculate the whole path up to the top again. If you insert a node, you might have to recalculate even more. And this is just for basic blobs of data.
But if you have graph data like I've outlined above, I'm not sure what to do, or if there is a better data structure. So for example, if you change profile.email
, then you would assume "profile has changed". But then I am unsure if we also say "user" has changed, because it is once removed. And likewise, perhaps even "msg1" has changed, or "group" has changed, since user1 is related to those through a few links. So basically, the idea of what is a "single entity" seems to break down in a graph, and I'm not sure what to do. Wondering if
- Merkle trees just aren't good for this type of data structure.
- If there is a way to make Merkle trees work here.
- Or, if there is a better one or two data structures for this sort of data model that can accomplish what the Merkle tree does best (mainly validating that the data hasn't changed).