Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

[Consensus] Add state objects to CM and CHT for inspection #2724

Closed
wants to merge 12 commits into from

Conversation

fassadlr
Copy link
Collaborator

@fassadlr fassadlr commented Nov 9, 2018

Acquiring and/or inspecting the state of various ConsensusManager and ChainedHeaderTree properties/fields is extremely limited and makes unit tests basically impossible unless reflection is used,

This PR introduces a ConsenusManagerState and ChainedHeaderTreeState class that can easily be added to, without changing the access modifiers of internal or private fields of CM or CHT.

I don't see the point of using locks around these as they will mostly be used in tests for now. Should we want to use CHT properties outside of CHT we can look at how to lock them. @dangershony thoughts on this?

@fassadlr
Copy link
Collaborator Author

fassadlr commented Nov 9, 2018

@Neurosploit this can then be used in your PR #2620

@Neurosploit
Copy link
Contributor

Great I'll try to merge when this is in. Is that ok?

zeptin
zeptin previously approved these changes Nov 10, 2018
{
var state = new ChainedHeaderTreeState
{
ChainedHeadersByHash = this.chainedHeadersByHash,
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably actually want to create copies of each collection no? (or the intention is to get the changes as they are reflected on the tree)

Also these methods should be internal so they will not get abused.

Copy link
Collaborator Author

@fassadlr fassadlr Nov 18, 2018

Choose a reason for hiding this comment

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

@dangershony these methods can't be internal as they sit on the IConsensusManager interface ;) We need public access to them.

I will make copies of the collections though 👍

@dangershony
Copy link
Contributor

@Neurosploit if you agree with my comments you can modify this PR directly.

@Neurosploit
Copy link
Contributor

Neurosploit commented Nov 16, 2018

Thanks @dangershony @fassadlr I'll try it out over the weekend, first i'll make these states internal and then create copies, trying out a test merge with a copy of my branch on the way to see if I run into anything and get back to you.

@fassadlr
Copy link
Collaborator Author

@Neurosploit I've made the changes as suggested by @dangershony 👍

@Neurosploit
Copy link
Contributor

Neurosploit commented Nov 18, 2018

@fassadlr No we can do both. We don't need it to be public yet. We can do that once that need arises. There are however some tests that require me to manipulate the CallbacksByBlocksRequestedHash and ExpectedBlockSizes collections. To fix that in a nice way I need to create an internal consensusmanager constructor that allows me to pass in these collections and have the default public constructor call that. In my testconsensusmanager i can then call the internal constructor and keep track of the collection and manipulate it for my needs and then I can work with the copies of the state in the rest of the tests to verify those internals. That makes all my tests pass with this rework.
@dangershony FYI

@fassadlr
Copy link
Collaborator Author

@Neurosploit We will greatly benefit from having this on the interface as there are looooads of tests that can be amended to rather use this instead of getting values via reflection.

@Neurosploit
Copy link
Contributor

Neurosploit commented Nov 18, 2018

Ok, I've already finished my rework in a testbranch so once this is merged i'll merge my PR with master and reapply my rework to my PR. For me it doesn't really matter if the state methods are internal or public both work for me.

Neurosploit
Neurosploit previously approved these changes Nov 19, 2018
Copy link
Contributor

@noescape00 noescape00 left a comment

Choose a reason for hiding this comment

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

I strongly disagree with exposing private structures of CHT.

CHT is not thread-safe and it's thread safety is being ensured by locks inside CM, no other component should access CHT directly.

Also such change breaks the architecture.

For tests we agreed to use reflection over changing production code. If such change is needed for something not tests-related we can implement it without exposing internal structures of CHT.

@noescape00
Copy link
Contributor

noescape00 commented Nov 27, 2018

For unit tests or integration tests you can create TestCHT which will inherit from CHT and have those structures exposed- in that case you won't even need reflection.

@fassadlr
Copy link
Collaborator Author

@noescape00 please note that we are not exposing internal properties of CHT or CM. We are merely creating copies of them.

@noescape00
Copy link
Contributor

@noescape00 please note that we are not exposing internal properties of CHT or CM. We are merely creating copies of them.

That's also exposure, just read-only one (except for chained headers- they are references, not copies).
And very expensive one too if it's ever used on production code.

@fassadlr
Copy link
Collaborator Author

@Neurosploit we have decided not to go this route. We don't want to put the State on the interface as it might be misued. @noescape00 will create a PR where we will be using an overridden ChainedHeaderTree class that exposes the private properties of CHT.

@fassadlr fassadlr closed this Nov 28, 2018
@Neurosploit
Copy link
Contributor

Alright just reference it to my issue and I will have a look again. @fassadlr @noescape00.

@fassadlr fassadlr deleted the consensus-state branch November 29, 2018 07:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants