Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LEDGER] rm unnecessary mock from txmgr/validator #1094

Closed
wants to merge 1 commit into from

Conversation

cendhu
Copy link
Contributor

@cendhu cendhu commented Apr 15, 2020

Type of change

  • Improvement (improvement to code)

Description

In the serializability validator, though we create a local mock for the TxMgr interface, we use the mock created at txmgr package. Hence, we make the validator to use the local mock and remove the one in the txmgr package.

Further, the validator does not need to create mock for all APIs in the TxMgr interface as it only uses NewTxSimulator(). Hence, we define a local interface to reduce the #LOC.

Additional details

We pass LockBasedTxMgr that implements TxMgr to the validator so that the validator can use it to get a tx simulator to execute the post-order transaction. As this is a side-effect, we need to run the post-order tx execution within the lockbasedtxmgr package and only use validator to perform the serializability check. Note that channel config tx is the only post-order tx execution. When we remove the side-effect, we can remove txSimulatorProvider interface introduced by this PR -- FAB-17758

Related issues

FAB-17759

In the validator, we already create a mock for TxMgr interface
but use the mock created at txmgr package. Hence, we make the
validator to use the mock that present locally.

Further, the validator does not need to create mock for all APIs
in the TxMgr interface as it only uses NewTxSimulator(). Hence,
we define a local interface.

Signed-off-by: senthil <cendhu@gmail.com>
@cendhu cendhu requested a review from a team as a code owner April 15, 2020 11:17
Comment on lines 47 to +48
func NewStatebasedValidator(
txmgr txmgr.TxMgr,
txSimProvider txSimulatorProvider,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exported function receiving an unexported interface. Please don't do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I would like to understand the reasoning so that it would stick in my mind.

My thought process was that valimpl package is not the one that implements this interface. It is just a consumer of the interface. Hence, an unexported interface was defined to implement polymorphism (to restrict API access). As this interface is to be used only by the valimpl package, I defined an unexported interface. If you can pinpoint the problem with the approach, I am sure that it will stick hard in my mind. I agree that it looks awkward to have an unexported interface but an exported function. However, when I consider the complete context, it makes sense to me. If there is specific reasoning for not employing this approach. I would like to understand and change my approach accordingly.

I just tried to export the interface. It then resulted in the import cycle. To fix that, we need to do some package restructuring as mentioned here -- https://stackoverflow.com/questions/50986170/how-to-avoid-import-cycles-in-mock-generation

Is package restructuring justifiable given that we would soon remove this dependency by moving the post-order tx processing to lockbasedtxmgr package FAB-17758? One could ask why we are not doing FAB-17759 first. As it requires a larger refactoring, we would want to do that towards the end once we reduce the folder depth and the number of packages in the ledger. The goal if this CR is to remove the TxMgr interface so that we don't need to have two packages such as txmgr and lockbasedtxmgr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I would like to understand the reasoning so that it would stick in my mind.

An exported API is public and expected to be consumed outside of the package. What this code does is say that my API is exported and you can pass me an argument that satisfies a contract - but I'm not really going to tell you what the contract is. This is inconsistent.

If a caller is supposed to satisfy an interface, the interface should be exposed so the caller can see how to satisfy it. It should also be possible for the caller to create a var of the type that you expect to hold its instance.

So, on public (exported) functions, the types of the arguments should also be public.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this code does is say that my API is exported and you can pass me an argument that satisfies a contract - but I'm not really going to tell you what the contract is. This is inconsistent.

Understood. Thanks.

@manish-sethi Should I move the post-order tx processing to lockbasedtxmgr to avoid the dependency? Once we do that, we can merge this PR as well as #1095 which would enable us to merge txmgr and lockbasedtxmgr packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we would have only one post-order tx, i.e., config block, we can in fact directly pass a simulator instead of TxMgr to create a simulator. When we move the post-order processing to lockbasedtxmgr, we can either retain the simulator or do a refactoring. Wdyt Manish?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the offline discussion with @manish-sethi we decided to remove the side-effect first before removing the TxMgr interface. Hence, I am marking this PR and #1095 as Draft.

@cendhu cendhu marked this pull request as draft April 15, 2020 16:25
@cendhu cendhu closed this Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants