!5275odin/slasher-address-compare → developa826b2ada1

Slasher compares validator addresses as bytes, not hex strings

AI-generatedEverything on this page was written by an AI agent from the diff: the summaries, the narration, the diagrams, the flags and the glossary. It can be wrong or incomplete, so check it against the code before you rely on it.

2commits
2files
41lines changed
7lines to read
3comparisons changed
1.6%block CPU removed (author)

Where the diff is

1-minute explainer

What changed

Hot loop

Each vote scanned every pair, hex-encoding two addresses per check

111 votes× 55 pairs≈ 12k allocs/block

Same answer, no strings

bytes.Equal replaces String() == String() in three handlers

HandleDoubleSignHandleSuccessfulSignHandleMissingSign

Skip test

A decoy pair first; handlers must skip it

decoy ✗signer ✓missed 5→4→5

Look here first

BeginBlock path: equality must match String()HexBytes.String is upper hex of the bytes, so bytes.Equal is equivalent; nil and empty both compare equal either way.manager_slasher_current.go:167 ↗careful
158159 }
159160
160161 return s.keeper.SetNodeAccount(ctx, na)
161162 }
162163
163164 // HandleSuccessfulSign - decrement missing blocks from a validator for signing a block
164165 func (s *SlasherImpl) HandleSuccessfulSign(ctx cosmos.Context, addr crypto.Address, constAccessor constants.ConstantValues, validatorAddresses []nodeAddressValidatorAddressPair) error {
165166 for _, pair := range validatorAddresses {
166− if addr.String() != pair.validatorAddress.String() {
167+ if !bytes.Equal(addr, pair.validatorAddress) {
167168 continue
168169 }
169170
170171 na, err := s.keeper.GetNodeAccount(ctx, pair.nodeAddress)
171172 if err != nil {
172173 return err
173174 }
174175
175176 if na.MissingBlocks == 0 {
176177 return nil
177178 }
178179
179180 // decrement the number of blocks that weren't signed
Double-sign lookup now bytes.EqualSame equivalence; the bonded-validator fallback below still uses its own comparison, unchanged.manager_slasher_current.go:123 ↗look
114115 }
115116
116117 doubleBlockSignSlashPoints := s.keeper.GetConfigInt64(ctx, constants.DoubleBlockSignSlashPoints)
117118
118119 // First search active validators, then fall back to all validators with bond
119120 // to handle churned-out validators whose evidence arrives after churn.
120121 var nodeAddress cosmos.AccAddress
121122 for _, pair := range validatorAddresses {
122− if addr.String() == pair.validatorAddress.String() {
123+ if bytes.Equal(addr, pair.validatorAddress) {
123124 nodeAddress = pair.nodeAddress
124125 break
125126 }
126127 }
127128 if nodeAddress.Empty() {
128129 allValidators, err := s.keeper.ListValidatorsWithBond(ctx)
129130 if err != nil {
130131 return fmt.Errorf("fail to list validators with bond: %w", err)
131132 }
132133 for _, na := range allValidators {
133134 pk, err := cosmos.GetPubKeyFromBech32(cosmos.Bech32PubKeyTypeConsPub, na.ValidatorConsPubKey)
134135 if err != nil {
135136 continue
Missing-sign skip branchSame swap; slash points and missed-block writes are untouched.manager_slasher_current.go:195 ↗look
186187 }
187188
188189 // HandleMissingSign - slashes a validator for not signing a block
189190 func (s *SlasherImpl) HandleMissingSign(ctx cosmos.Context, addr crypto.Address, constAccessor constants.ConstantValues, validatorAddresses []nodeAddressValidatorAddressPair) error {
190191 missBlockSignSlashPoints := s.keeper.GetConfigInt64(ctx, constants.MissBlockSignSlashPoints)
191192 maxTrack := s.keeper.GetConfigInt64(ctx, constants.MaxTrackMissingBlock)
192193
193194 for _, pair := range validatorAddresses {
194− if addr.String() != pair.validatorAddress.String() {
195+ if !bytes.Equal(addr, pair.validatorAddress) {
195196 continue
196197 }
197198
198199 na, err := s.keeper.GetNodeAccount(ctx, pair.nodeAddress)
199200 if err != nil {
200201 return err
201202 }
202203
203204 slashCtx := ctx.WithContext(context.WithValue(ctx.Context(), constants.CtxMetricLabels, []metrics.Label{
204205 telemetry.NewLabel("address", na.NodeAddress.String()),
205206 telemetry.NewLabel("reason", "miss_block_sign"),
206207 }))
207208 if err := s.keeper.IncNodeAccountSlashPoints(slashCtx, na.NodeAddress, missBlockSignSlashPoints); err != nil {
Decoy-first test covers mismatch branchFake keeper errors on the decoy's node account, so a broken skip fails the test.manager_slasher_current_test.go:734 ↗look
725725
726726 randomAddr := GetRandomBech32Addr().String()
727727 err := slasher.HandleSuccessfulSign(ctx, []byte(randomAddr), constAccessor, []nodeAddressValidatorAddressPair{})
728728 c.Assert(err, NotNil)
729729 c.Assert(err.Error(), Matches, "could not find active node account with validator address: .*")
730730 }
731731
732+// Both handlers skip pairs whose validator address differs: the decoy comes
733+// first and its node account is unknown to the keeper, so acting on it fails.
734+func (s *SlashingSuite) TestSignHandlersSkipOtherValidators(c *C) {
735+ ctx, _ := setupKeeperForTest(c)
736+ constAccessor := constants.GetConstantValues(GetCurrentVersion())
737+
738+ na := GetRandomValidatorNode(NodeActive)
739+ na.MissingBlocks = 5
740+ keeper := &TestDoubleSlashKeeper{
741+ na: na,
742+ network: NewNetwork(),
743+ slashPoints: make(map[string]int64),
744+ constants: make(map[string]int64),
745+ }
746+ keeper.constants["MissBlockSignSlashPoints"] = 1
747+ keeper.constants["MaxTrackMissingBlock"] = 10

Ask an AI about this change

Chat assistantClaude, ChatGPT, … (reads a link)

Coding agentClaude Code, Cursor, … (reads the repo)

The context file is context.md. Use Copy with context for an AI that can't open links; it pastes the whole file in.

Glossary

BeginBlock
Per-block hook run before transactions; here it feeds each vote to the sign handlers, so its cost repeats every block.
HexBytes
CometBFT byte-slice type for addresses; its String() returns the upper-case hex, allocating a new string each call.
Slash points
Penalty counters on node accounts that reduce their rewards.
Slasher
THORNode manager that adds slash points and tracks missed blocks for validators each block. manager_slasher_current.go ↗