Skip to content

refactor(consensus): optimize initialize priv-validator#512

Merged
shotonoff merged 2 commits intov0.10-devfrom
optimaze-set-priv-validator
Nov 29, 2022
Merged

refactor(consensus): optimize initialize priv-validator#512
shotonoff merged 2 commits intov0.10-devfrom
optimaze-set-priv-validator

Conversation

@shotonoff
Copy link
Collaborator

@shotonoff shotonoff commented Nov 25, 2022

Issue being fixed or feature implemented

Some of the logic corresponding to the private-validator in consensus.State is redundant and cannot be used

What was done?

This PR includes some changes of related to usage of private-validator in consensus.State.

  1. Gather private-validator and its pro-tx-hash in one structure
  2. Remove the field private-validator-type as a redundant
  3. Remove redundant updatePrivValidatorProTxHash method, because pro-tx-hash can't be changed
  4. Simplify State.SetPrivValidator method

How Has This Been Tested?

Unit and E2E tests

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Copy link
Collaborator

@lklimek lklimek left a comment

Choose a reason for hiding this comment

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

Please update PR description.

Please see linter issues in internal/consensus/invalid_test.go

case *tmcons.Vote:
r.state.mtx.RLock()
isValidator := r.state.Validators.HasProTxHash(r.state.privValidatorProTxHash)
isValidator := r.state.Validators.HasProTxHash(r.state.privValidator.ProTxHash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

did you test this on a full node that is not a validator? I guess we can get panic here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since fullnode never vote, it should work

privValidator types.PrivValidator // for signing votes
privValidatorType types.PrivValidatorType
config *config.ConsensusConfig
privValidator *privValidator
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of using pointer here, we can add IsZero() func to privValidator.

func (cs *State) isProposer() bool {
proTxHash := cs.privValidatorProTxHash
return proTxHash != nil && bytes.Equal(cs.Validators.GetProposer().ProTxHash.Bytes(), proTxHash.Bytes())
return cs.privValidator.IsProTxHashEqual(cs.Validators.GetProposer().ProTxHash)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it possible for cs.privValidator to be nil? (full node, light node, ...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

isProposer is called in enterPropose method, this functionality is available for validator only

ProTxHash types.ProTxHash
}

func (pv *privValidator) IsProTxHashEqual(proTxHash types.ProTxHash) bool {
Copy link
Collaborator

@lklimek lklimek Nov 28, 2022

Choose a reason for hiding this comment

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

I don't understand why you created this privValidator struct.
Can't we just implement it in types.PrivValidator, instead of creating this wrapper struct?

Copy link
Collaborator Author

@shotonoff shotonoff Nov 28, 2022

Choose a reason for hiding this comment

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

To add this method to priv-validator, we have to modify PrivValidator interface and support this method at each implementation. I compared the effort between this structure and other modifications and decided to stay with this.

Copy link
Collaborator Author

@shotonoff shotonoff left a comment

Choose a reason for hiding this comment

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

please have a look at the answers

case *tmcons.Vote:
r.state.mtx.RLock()
isValidator := r.state.Validators.HasProTxHash(r.state.privValidatorProTxHash)
isValidator := r.state.Validators.HasProTxHash(r.state.privValidator.ProTxHash)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since fullnode never vote, it should work

func (cs *State) isProposer() bool {
proTxHash := cs.privValidatorProTxHash
return proTxHash != nil && bytes.Equal(cs.Validators.GetProposer().ProTxHash.Bytes(), proTxHash.Bytes())
return cs.privValidator.IsProTxHashEqual(cs.Validators.GetProposer().ProTxHash)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

isProposer is called in enterPropose method, this functionality is available for validator only

@shotonoff shotonoff requested a review from lklimek November 28, 2022 12:18
@lklimek lklimek changed the title refactor(consensus): optimaze initialize priv-validator refactor(consensus): optimize initialize priv-validator Nov 28, 2022
@shotonoff shotonoff merged commit fd58973 into v0.10-dev Nov 29, 2022
@shotonoff shotonoff deleted the optimaze-set-priv-validator branch November 29, 2022 09:19
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