!5287odin/voter-signer-index → develop5f5da1624d

Network-fee and solvency voters count signers through the node index

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
4files
145lines changed
58lines to read
−4.3%mainnet replay / block
0state/gas diffs

Where the diff is

2-minute explainer

What changed

Old path: decode + scan

Every call decoded bech32 and scanned all nodes per signer

GetSigners()bech32 decode ×NIsNodeKeys ×N×M

countActiveSigners

Cached decode, one map lookup per signer; repeats still counted

signerSignerAddressStringactiveNodeIndex slotcount++

signersInclude

HasSigned compares cached bytes instead of allocating addresses

non-empty decode ⇒ Equals is byte equalityundecodable signers skipped, as before

Proof of equivalence

Randomized parity test, regtest identical, replay 4.3% faster

state =events =gas_used =

Look here first

Repeated signers still counted each timeUnlike getConsensusTx's distinct count, this must keep duplicates counting, or quorum results change. Matches old GetSigners+IsNodeKeys loop.type_observed_tx.go:244 ↗verify
235+// signer counts each time, as when IsNodeKeys is called per GetSigners entry,
236+// but each signer is a map lookup instead of a decode and a linear scan.
237+func countActiveSigners(signers []string, accounts NodeAccounts) int {
238+ var idx activeNodeIndex
239+ count := 0
240+ for _, signer := range signers {
241+ if !idx.built {
242+ idx.build(accounts)
243+ }
244+ if idx.signerSlot(signer) >= 0 {
245+ count++
246+ }
247+ }
248+ return count
249+}
250+
251+// signersInclude reports whether any of signers decodes, as GetSigners decodes
252+// it, to signer. Decoded signers are non-empty, so AccAddress.Equals on them is
253+// byte equality.
254+func signersInclude(signers []string, signer cosmos.AccAddress) bool {
255+ for _, a := range signers {
256+ if addr, ok := common.SignerAddressString(a); ok && addr == string(signer) {
257+ return true
Byte equality replaces AccAddress.EqualsEqual only because decoded signers are non-empty; nil/empty argument never matches, as before.type_observed_tx.go:256 ↗careful
247+ }
248+ return count
249+}
250+
251+// signersInclude reports whether any of signers decodes, as GetSigners decodes
252+// it, to signer. Decoded signers are non-empty, so AccAddress.Equals on them is
253+// byte equality.
254+func signersInclude(signers []string, signer cosmos.AccAddress) bool {
255+ for _, a := range signers {
256+ if addr, ok := common.SignerAddressString(a); ok && addr == string(signer) {
257+ return true
258+ }
259+ }
260+ return false
261+}
262+
233263 func (m *ObservedTxVoter) getConsensusTx(accounts NodeAccounts, final bool, idx *activeNodeIndex) common.ObservedTx {
234264 // accounts.IsNodeKeys(signer) for every signer is a linear scan; index the
235265 // active node addresses once. Signers decode to non-empty addresses, for
236266 // which AccAddress.Equals is byte equality.
237267 for _, txFinal := range m.Txs {
238268 if txFinal.IsFinal() != final {
239269 continue
Solvency quorum now via shared helperHasMinority threshold uses len(nas) unchanged; only the numerator's computation moved. Old GetSigners used uncached AccAddressFromBech32.type_solvency_voter.go:47 ↗careful
4944
5045 // HasConsensus determine if this errata has enough signers
5146 func (m *SolvencyVoter) HasConsensus(nas NodeAccounts) bool {
52− var count int
53− for _, signer := range m.GetSigners() {
54− if nas.IsNodeKeys(signer) {
55− count++
56− }
57− }
47+ count := countActiveSigners(m.Signers, nas)
5848 return HasMinority(count, len(nas))
5949 }
6050
6151 // Empty check whether TxID or Chain is empty
6252 func (m *SolvencyVoter) Empty() bool {
6353 return m.Id.IsEmpty() || m.Chain.IsEmpty() || m.Height <= 0 || len(m.Signers) == 0
6454 }
6555
6656 // String implement fmt.Stinger , return a string representation of solvency tx voter
6757 func (m *SolvencyVoter) String() string {
6858 return m.Id.String()
6959 }
Network-fee supermajority via indexSame swap; index built only when signers exist, so empty voters skip the build.type_observed_network_fee_voter.go:50 ↗look
5247
5348 // HasConsensus Determine if this errata has enough signers
5449 func (m *ObservedNetworkFeeVoter) HasConsensus(nas NodeAccounts) bool {
55− var count int
56− for _, signer := range m.GetSigners() {
57− if nas.IsNodeKeys(signer) {
58− count++
59− }
60− }
50+ count := countActiveSigners(m.Signers, nas)
6151 return HasSuperMajority(count, len(nas))
6252 }
6353
6454 // IsEmpty return true when chain is empty and block height is 0
6555 func (m *ObservedNetworkFeeVoter) IsEmpty() bool {
6656 return m.Chain.IsEmpty() && m.ReportBlockHeight == 0
6757 }
6858
6959 // String implement fmt.Stringer
7060 func (m *ObservedNetworkFeeVoter) String() string {
7161 if m.FeeRate > 0 {
7262 return fmt.Sprintf("%s-%d-%d", m.Chain.String(), m.ReportBlockHeight, m.FeeRate)
7363 }
Index keyed on active statuses onlybuild counts only NodeStatus_Active, matching IsNodeKeys' status filter; empty node addresses never match a decoded signer.type_observed_tx.go:197 ↗look
unchanged in this MR · context
193193 }
194194 return -1
195195 }
196196
197197 func (idx *activeNodeIndex) build(accounts NodeAccounts) {
198198 idx.built = true
199199 size, count := 0, 0
200200 for i := range accounts {
201201 if accounts[i].Status == NodeStatus_Active {
202202 size += len(accounts[i].NodeAddress)
203203 count++
204204 }
205205 }
206206 // one copy of all active addresses backs every key, instead of one string
207207 // allocation per address
208208 var all strings.Builder
209209 all.Grow(size)
210210 for i := range accounts {
Randomized parity test, fixed seedCompares helpers and voters with reference copies of the old loops across odd signer inputs.type_voter_signers_test.go:45 ↗look
36+ continue
37+ }
38+ if addr.Equals(signer) {
39+ return true
40+ }
41+ }
42+ return false
43+}
44+
45+func (VoterSignersSuite) TestMatchesDecodeAndScan(c *C) {
46+ rng := rand.New(rand.NewSource(7)) // #nosec G404 deterministic test data
47+ addrs := make([]cosmos.AccAddress, 12)
48+ for i := range addrs {
49+ addrs[i] = GetRandomBech32Addr()
50+ }
51+ statuses := []NodeStatus{NodeStatus_Active, NodeStatus_Active, NodeStatus_Standby, NodeStatus_Disabled}
52+ for round := 0; round < 500; round++ {
53+ var nas NodeAccounts
54+ for i := rng.Intn(10); i > 0; i-- {
55+ na := NodeAccount{Status: statuses[rng.Intn(len(statuses))]}
56+ if rng.Intn(10) > 0 {
57+ na.NodeAddress = addrs[rng.Intn(len(addrs))] // repeats included
58+ }

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

activeNodeIndex
Map from each active node address to a slot, built once per call, already used by ObservedTxVoter's consensus path. type_observed_tx.go ↗
Bech32
Human-readable address encoding (thor1…). Signers are stored as bech32 strings and must be decoded to bytes to compare.
IsNodeKeys
Linear scan of node accounts for an active node with the given address; the old per-signer check. type_node_account.go ↗
SignerAddressString
Memoized bech32 decode returning the address bytes as a string; false when the signer does not decode. common/type_observed_tx.go ↗
Solvency voter
Collects node attestations that a chain's vault balances are (in)solvent; enough votes halt that chain. type_solvency_voter.go ↗
Supermajority / minority
Quorum thresholds over all node accounts: network fee needs 2/3, solvency needs 1/3 of nodes. types.go ↗