Skip to content

Commit 7d09c06

Browse files
pompon0philipsu522
authored andcommitted
extra checks in BitArray methods (CON-131) (#2572)
* Also removed some unused code from BitArray * Also fixed bugs introduced in #2558
1 parent 3c4849e commit 7d09c06

File tree

3 files changed

+13
-111
lines changed

3 files changed

+13
-111
lines changed

sei-tendermint/internal/consensus/reactor.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,7 @@ func (r *Reactor) gossipVotesRoutine(ctx context.Context, ps *PeerState) error {
598598
// into play for liveness when there's a signature DDoS attack happening.
599599
func (r *Reactor) queryMaj23Routine(ctx context.Context, ps *PeerState) error {
600600
stateCh := r.channels.state
601-
timer := time.NewTimer(0)
602601
for {
603-
if _, err := utils.Recv(ctx, timer.C); err != nil {
604-
return err
605-
}
606-
607602
// TODO create more reliable copies of these
608603
// structures so the following go routines don't race
609604
rs := r.getRoundState()
@@ -657,6 +652,9 @@ func (r *Reactor) queryMaj23Routine(ctx context.Context, ps *PeerState) error {
657652
}
658653
}
659654
}
655+
if err := utils.Sleep(ctx, r.state.config.PeerQueryMaj23SleepDuration); err != nil {
656+
return err
657+
}
660658
}
661659
}
662660

sei-tendermint/libs/bits/bit_array.go

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bits
22

33
import (
4-
"encoding/binary"
54
"errors"
65
"fmt"
76
"math"
@@ -139,7 +138,7 @@ func (bA *BitArray) Or(o *BitArray) *BitArray {
139138
o.mtx.Lock()
140139
c := bA.copyBits(tmmath.MaxInt(bA.Bits, o.Bits))
141140
smaller := tmmath.MinInt(len(bA.Elems), len(o.Elems))
142-
for i := 0; i < smaller; i++ {
141+
for i := range smaller {
143142
c.Elems[i] |= o.Elems[i]
144143
}
145144
bA.mtx.Unlock()
@@ -207,7 +206,7 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
207206
// If bA is longer, then skipping those iterations is equivalent
208207
// to right padding with 0's
209208
smaller := tmmath.MinInt(len(bA.Elems), len(o.Elems))
210-
for i := 0; i < smaller; i++ {
209+
for i := range smaller {
211210
// &^ is and not in golang
212211
c.Elems[i] &^= o.Elems[i]
213212
}
@@ -216,42 +215,6 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
216215
return c
217216
}
218217

219-
// IsEmpty returns true iff all bits in the bit array are 0
220-
func (bA *BitArray) IsEmpty() bool {
221-
if bA == nil {
222-
return true // should this be opposite?
223-
}
224-
bA.mtx.Lock()
225-
defer bA.mtx.Unlock()
226-
for _, e := range bA.Elems {
227-
if e > 0 {
228-
return false
229-
}
230-
}
231-
return true
232-
}
233-
234-
// IsFull returns true iff all bits in the bit array are 1.
235-
func (bA *BitArray) IsFull() bool {
236-
if bA == nil {
237-
return true
238-
}
239-
bA.mtx.Lock()
240-
defer bA.mtx.Unlock()
241-
242-
// Check all elements except the last
243-
for _, elem := range bA.Elems[:len(bA.Elems)-1] {
244-
if (^elem) != 0 {
245-
return false
246-
}
247-
}
248-
249-
// Check that the last element has (lastElemBits) 1's
250-
lastElemBits := (bA.Bits+63)%64 + 1
251-
lastElem := bA.Elems[len(bA.Elems)-1]
252-
return (lastElem+1)&((uint64(1)<<uint(lastElemBits))-1) == 0
253-
}
254-
255218
// PickRandom returns a random index for a set bit in the bit array.
256219
// If there is no such value, it returns 0, false.
257220
// It uses math/rand's global randomness Source to get this index.
@@ -278,6 +241,9 @@ func (bA *BitArray) PickRandom() (int, bool) {
278241
}
279242

280243
func (bA *BitArray) getTrueIndices() []int {
244+
if bA.Size() == 0 {
245+
return nil
246+
}
281247
trueIndices := make([]int, 0, bA.Bits)
282248
curBit := 0
283249
numElems := len(bA.Elems)
@@ -288,7 +254,7 @@ func (bA *BitArray) getTrueIndices() []int {
288254
curBit += 64
289255
continue
290256
}
291-
for j := 0; j < 64; j++ {
257+
for j := range 64 {
292258
if (elem & (uint64(1) << uint64(j))) > 0 {
293259
trueIndices = append(trueIndices, curBit)
294260
}
@@ -298,7 +264,7 @@ func (bA *BitArray) getTrueIndices() []int {
298264
// handle last element
299265
lastElem := bA.Elems[numElems-1]
300266
numFinalBits := bA.Bits - curBit
301-
for i := 0; i < numFinalBits; i++ {
267+
for i := range numFinalBits {
302268
if (lastElem & (uint64(1) << uint64(i))) > 0 {
303269
trueIndices = append(trueIndices, curBit)
304270
}
@@ -354,21 +320,6 @@ func (bA *BitArray) stringIndented(indent string) string {
354320
return fmt.Sprintf("BA{%v:%v}", bA.Bits, strings.Join(lines, indent))
355321
}
356322

357-
// Bytes returns the byte representation of the bits within the bitarray.
358-
func (bA *BitArray) Bytes() []byte {
359-
bA.mtx.Lock()
360-
defer bA.mtx.Unlock()
361-
362-
numBytes := (bA.Bits + 7) / 8
363-
bytes := make([]byte, numBytes)
364-
for i := 0; i < len(bA.Elems); i++ {
365-
elemBytes := [8]byte{}
366-
binary.LittleEndian.PutUint64(elemBytes[:], bA.Elems[i])
367-
copy(bytes[i*8:], elemBytes[:])
368-
}
369-
return bytes
370-
}
371-
372323
// Update sets the bA's bits to be that of the other bit array.
373324
// The copying begins from the begin of both bit arrays.
374325
func (bA *BitArray) Update(o *BitArray) {
@@ -427,7 +378,7 @@ func (bA *BitArray) UnmarshalJSON(bz []byte) error {
427378
numBits := len(bits)
428379

429380
bA.reset(numBits)
430-
for i := 0; i < numBits; i++ {
381+
for i := range numBits {
431382
if bits[i] == 'x' {
432383
bA.SetIndex(i, true)
433384
}

sei-tendermint/libs/bits/bit_array_test.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bits
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"math"
@@ -17,8 +16,8 @@ import (
1716
func randBitArray(bits int) *BitArray {
1817
src := tmrand.Bytes((bits + 7) / 8)
1918
bA := NewBitArray(bits)
20-
for i := 0; i < len(src); i++ {
21-
for j := 0; j < 8; j++ {
19+
for i := range src {
20+
for j := range 8 {
2221
if i*8+j >= bits {
2322
return bA
2423
}
@@ -144,52 +143,6 @@ func TestPickRandom(t *testing.T) {
144143
}
145144
}
146145

147-
func TestBytes(t *testing.T) {
148-
bA := NewBitArray(4)
149-
bA.SetIndex(0, true)
150-
check := func(bA *BitArray, bz []byte) {
151-
require.True(t, bytes.Equal(bA.Bytes(), bz),
152-
"Expected %X but got %X", bz, bA.Bytes())
153-
}
154-
check(bA, []byte{0x01})
155-
bA.SetIndex(3, true)
156-
check(bA, []byte{0x09})
157-
158-
bA = NewBitArray(9)
159-
check(bA, []byte{0x00, 0x00})
160-
bA.SetIndex(7, true)
161-
check(bA, []byte{0x80, 0x00})
162-
bA.SetIndex(8, true)
163-
check(bA, []byte{0x80, 0x01})
164-
165-
bA = NewBitArray(16)
166-
check(bA, []byte{0x00, 0x00})
167-
bA.SetIndex(7, true)
168-
check(bA, []byte{0x80, 0x00})
169-
bA.SetIndex(8, true)
170-
check(bA, []byte{0x80, 0x01})
171-
bA.SetIndex(9, true)
172-
check(bA, []byte{0x80, 0x03})
173-
174-
require.False(t, bA.SetIndex(-1, true))
175-
}
176-
177-
func TestEmptyFull(t *testing.T) {
178-
ns := []int{47, 123}
179-
for _, n := range ns {
180-
bA := NewBitArray(n)
181-
if !bA.IsEmpty() {
182-
t.Fatal("Expected bit array to be empty")
183-
}
184-
for i := 0; i < n; i++ {
185-
bA.SetIndex(i, true)
186-
}
187-
if !bA.IsFull() {
188-
t.Fatal("Expected bit array to be full")
189-
}
190-
}
191-
}
192-
193146
func TestUpdateNeverPanics(t *testing.T) {
194147
newRandBitArray := func(n int) *BitArray { return randBitArray(n) }
195148
pairs := []struct {

0 commit comments

Comments
 (0)