MinaProtocol / mina

Mina is a cryptocurrency protocol with a constant size blockchain, improving scaling while maintaining decentralization and security.
https://minaprotocol.com
Apache License 2.0
1.97k stars 522 forks source link

Document Mina daemon's databases #15767

Open georgeee opened 1 week ago

georgeee commented 1 week ago

Produce a document describing what are databases that Daemon uses.

Result of this task is expected to be a table of databases and descriptions.

Table's rows, I imagine, would be:

Relates to #13971

georgeee commented 1 week ago

This document is needed to analyze usage of RocksDB in block production/processing.

Major focus should be on frontier and ledger-related DBs.

georgeee commented 1 week ago

AFAIU all DBs we have are key-value storages.

And we probably have more than one key-value space within some of these DBs, let's have one "key-value space" per row in a resuting table.

To start with the task I suggest one launching a Mina node that connects to mainnet. And then checking what you have in .mina-config. Ideally we'd be able to inspect every DB with manual tool and check that there are no keys unaccounted, but it might be too tedious, so maybe just invetigating usages in codebase would be alright.

What I see on a mainnet's node:

Geometer1729 commented 2 days ago

root

The root/snarked_ledger location is defined here: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/persistent_root/persistent_root.ml#L40 This path is given to Ledger.Db where Ledger is Mina_ledger.Ledger. So the snarked_ledger Db type gets defined here: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/mina_ledger/ledger.ml#L108-L120 which calls this functor: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/merkle_ledger/database.ml#L1

On this module: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/mina_ledger/ledger.ml#L89-L106

The Kvbd.t type it takes seems to be a generic database type with Bigstring.t for both key and value. But from calls to Kvdb like this: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/merkle_ledger/database.ml#L101-L102 It looks like the keys represent serialized Location.ts which I think is this enum: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/merkle_ledger/location.ml#L54-L55

and the value appears to be the Toeken_id.Set.t type from the inputs. I'm not sure where Token_id comes from, there are a few opens at the top of the file.

The root/root location is defined here and seems to store a hash of the genesis state.

Geometer1729 commented 2 days ago

trust

src/lib/trust_system/peer_trust.ml

Has the key as Peer_id.t which I believe refers to this: https://github.com/MinaProtocol/mina/blob/10a0bf9b8b5c27407b349f02ecaeea964e14690e/src/lib/trust_system/peer_trust.ml#L252-L256

and the value Record.t which I beleive reffers to this https://github.com/MinaProtocol/mina/blob/10a0bf9b8b5c27407b349f02ecaeea964e14690e/src/lib/trust_system/record.ml#L5-L12

Geometer1729 commented 1 day ago

frontier

It looks like this module: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/persistent_frontier/database.ml#L212

calls this functor: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/rocksdb/serializable.ml#L53

With this argument: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/persistent_frontier/database.ml#L29

I think that results in the key type being this private Enum: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/persistent_frontier/database.ml#L49-L56

Here Root_data.Minimal.Stable.V2 refers to this type: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/frontier_base/root_data.ml#L114

and I think this defines a type family like thing for what the value types is depending on the key: https://github.com/MinaProtocol/mina/blob/4495af5caea5e1bb2f98f92592c065f93a586ade/src/lib/transition_frontier/persistent_frontier/database.ml#L74-L86