automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.75k stars 467 forks source link

Question: deterministic serialization #281

Closed nolash closed 4 years ago

nolash commented 4 years ago

First of all, thanks for an awesome project.

I've implemented a setup where every update adds a second update with a signature on the previous.

I understand that the interpreted data is deterministic, it terms of the values set. But on dicts, is the serialization actually deterministic too? As far as I know, json intepreters do not have to enforce any order in dicts. In my case it's imperative that:

Can this be guaranteed to work, no matter js engine etc?

ept commented 4 years ago

Hi @nolash, the result of Automerge.save() is not deterministic, so the exact procedure you describe won't work. However, it is possible to make signatures work like this:

Even though JSON does not guarantee any particular order of field names, this should work, since the signature is on the encoded string. Will this do what you need?

nolash commented 4 years ago

Yeah it will, it does bloat the graph, however.

What would be cool was if the save function could modified (or a hook be added) to indeed guarantee eg. alphabetical dict keys in serialization.

... or of course can do it in the application, too, by loading the save serialization and re-serialize in order. What I'm after needs only to be application specific - as in the alphabetical ordering can be part of the protocol.

ept commented 4 years ago

The binary encoding format being developed on #253 is designed with deterministic serialisation in mind. At the moment its implementation is not yet fully deterministic, but it's working towards that direction. Hopefully this will do what you need in the future.