hyperledger-iroha / iroha

Iroha - A simple, enterprise-grade decentralized ledger
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
445 stars 277 forks source link

The "immutable" design of `iroha_data_model` causes inconvenience #5050

Open 0x009922 opened 2 months ago

0x009922 commented 2 months ago

This topic was raised many times before, and so I decided to make a dedicated issue for it.

Problems with decomposition

Given the immutability of the structures, it is impossible to decompose them in most cases. The only way to access them is to get immutable references. Explorer re-wraps everything in its own structures. It could be done either by lots of clones, or by lots of lifetimes. It seems a bit insane given that in the context of Explorer there is not point of holding original data model structures and making its reflections for api responses. It would be convenient to own and move internal data directly.

It could be workarounded with the transparent_api feature, but it is supposed to be for internal use only.

- https://github.com/soramitsu/iroha2-block-explorer-backend/pull/44#issuecomment-2277192800

"Forgetting" to expose some getters

While using iroha_data_model crate without transparent_api feature, I repeatedly and very often stumble upon the same issue: some structure doesn't expose getters for its fields, for all or only some of them.

This happens because it is done manually in most cases and is not covered by some sort of static check for consistency.

Moreover, in CI iroha_data_model seems to always compile with transparent_api feature enabled, thus CI doesn't catch even internal possible inconsistencies. For example - #5049: iroha_genesis uses iroha_data_model without transparent_api feature enabled while accessing private fields. It works fine in Iroha repository, but doesn't work from the outside.

Such things are really minor to fix, but it is too much effort and time to open PRs for each of them. Example of such a PR: #4992

In a sense, I find this "forgetting" issue to be similar to the issue with errors: #4974. Errors are also mostly done manually and mostly never really tested.

Related data-model refactoring issues

nxsaken commented 2 months ago

On destructuring and transparent_api, can we have data model structs with private fields and a pair of (fn from_parts(...) -> Self, fn into_parts(self) -> ...) for going in and out of the data model context? Even with this, I think that data model types should provide methods instead of relying on something outside using their fields in a correct way. This seems to be related to data model anemia (leaking internals instead of providing the needed functionality through the types).

mversic commented 1 month ago

@nxsaken I think you mean such as described in #4360 ?