Skip to content

Commit

Permalink
fix/Do not vote for the same validators list twice (#2443)
Browse files Browse the repository at this point in the history
Closes #2365.
  • Loading branch information
roman-khimov committed Jul 27, 2023
2 parents 782f658 + 1eb6273 commit 12e8b08
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Changelog for NeoFS Node
### Fixed
- `neo-go` RPC connection loss handling (#1337)
- Concurrent morph cache misses (#1248)
- Double voting for validators on IR startup (#2365)

### Removed
- Deprecated `morph.rpc_endpoint` SN and `morph.endpoint.client` IR config sections (#2400)
Expand Down
35 changes: 34 additions & 1 deletion pkg/innerring/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit"
Expand Down Expand Up @@ -108,13 +109,21 @@ func (s *Server) voteForSidechainValidator(prm governance.VoteValidatorPrm) erro
return nil
}

voted, err := s.alreadyVoted(validators)
if err != nil {
return fmt.Errorf("could not check validators state: %w", err)
}

if voted {
return nil
}

epoch := s.EpochCounter()

var (
nonce uint32 = 1
vub uint32
vubP *uint32
err error
)

if prm.Hash != nil {
Expand All @@ -138,6 +147,30 @@ func (s *Server) voteForSidechainValidator(prm governance.VoteValidatorPrm) erro
return nil
}

func (s *Server) alreadyVoted(validatorsToVote keys.PublicKeys) (bool, error) {
currentValidators := make(map[keys.PublicKey]struct{}, len(s.contracts.alphabet))
for letter, contract := range s.contracts.alphabet {
validator, err := s.morphClient.AccountVote(contract)
if err != nil {
return false, fmt.Errorf("receiving %s's vote: %w", letter, err)
}

if validator == nil {
continue
}

currentValidators[*validator] = struct{}{}
}

for _, v := range validatorsToVote {
if _, voted := currentValidators[*v]; !voted {
return false, nil
}
}

return true, nil
}

// VoteForSidechainValidator calls vote method on alphabet contracts with
// the provided list of keys.
func (s *Server) VoteForSidechainValidator(prm governance.VoteValidatorPrm) error {
Expand Down
27 changes: 27 additions & 0 deletions pkg/morph/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
Expand Down Expand Up @@ -418,6 +420,31 @@ func (c *Client) IsValidScript(script []byte, signers []transaction.Signer) (res
return result.State == vmstate.Halt.String(), nil
}

// AccountVote returns a key the provided account has voted with its NEO
// tokens for. Nil key with no error is returned if the account has no NEO
// or if the account hasn't voted for anyone.
func (c *Client) AccountVote(addr util.Uint160) (*keys.PublicKey, error) {
c.switchLock.RLock()
defer c.switchLock.RUnlock()

if c.inactive {
return nil, ErrConnectionLost
}

neoReader := neo.NewReader(invoker.New(c.client, nil))

state, err := neoReader.GetAccountState(addr)
if err != nil {
return nil, fmt.Errorf("can't get vote info: %w", err)
}

if state == nil {
return nil, nil
}

return state.VoteTo, nil
}

// NotificationChannel returns channel than receives subscribed
// notification from the connected RPC node.
// Channel is closed when connection to the RPC node is lost.
Expand Down

0 comments on commit 12e8b08

Please sign in to comment.