Skip to content

HAL-06 iteration over map source of non-determinism #3257

@juniuszhou

Description

@juniuszhou

Description

The codebase contains instances of iteration over maps. As maps are not ordered in Go, iterations over maps are non-deterministic. If the consensus logic expects a deterministic result from the iteration, errors can occur where different nodes reach different states due to the differences in map ordering. If the nodes reach differing states, this can cause consensus issues, which could result in a chain halt.

Code Location:

x/tokenregistry/types/types.pb.go, Lines 582-601
Listing 8: Iteration over map (Line 582)
582 for k := range m. DoublePeggedNetworkMap {
583 v := m. DoublePeggedNetworkMap [k]
584 baseI := i
585 i- -
586 if v {
587 dAtA [i] = 1
588 } else {
589 dAtA [i] = 0
590 }
591 i- -
592 dAtA [i] = 0 x10
593 i = encodeVarintTypes (dAtA , i, uint64 (k))
594 i- -
595 dAtA [i] = 0x8
596 i = encodeVarintTypes (dAtA , i, uint64 (baseI -i))
597 i- -
598 dAtA [i] = 0x1
599 i- -
600 dAtA [i] = 0 x9a
601 }

Recommendation

Ensure that iterations over maps do not contain any logic that depends on a fixed ordering of the map. Alternatively, make use of an alternative data structure that has a deterministic ordering or ensure that maps are sorted before iterating over them.

Metadata

Metadata

Labels

Peggy 2.0An issue blocking the Peggy 2.0 releasePeggy TeamPeggy team task

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions