diff --git a/x/payment/types/payfordata.go b/x/payment/types/payfordata.go index a768b36bd4..66430b5894 100644 --- a/x/payment/types/payfordata.go +++ b/x/payment/types/payfordata.go @@ -184,9 +184,8 @@ func powerOf2MountainRange(l, k uint64) []uint64 { // NextHighestPowerOf2 returns the next lowest power of 2 unless the input is a power // of two, in which case it returns the input func NextHighestPowerOf2(v uint64) uint64 { - if v == 1 { - return 1 - } + // keep track of the value to check if its the same later + i := v // find the next highest power using bit mashing v-- @@ -198,7 +197,11 @@ func NextHighestPowerOf2(v uint64) uint64 { v |= v >> 32 v++ - // return the next highest power + // force the value to the next highest power of two if its the same + if v == i { + return 2 * v + } + return v } diff --git a/x/payment/types/payfordata_test.go b/x/payment/types/payfordata_test.go index 0dabeaed4f..cf89eee8c8 100644 --- a/x/payment/types/payfordata_test.go +++ b/x/payment/types/payfordata_test.go @@ -74,6 +74,39 @@ func TestNextLowestPowerOf2(t *testing.T) { } } +func TestNextHighestPowerOf2(t *testing.T) { + type test struct { + input uint64 + expected uint64 + } + tests := []test{ + { + input: 2, + expected: 4, + }, + { + input: 11, + expected: 16, + }, + { + input: 511, + expected: 512, + }, + { + input: 1, + expected: 2, + }, + { + input: 0, + expected: 0, + }, + } + for _, tt := range tests { + res := NextHighestPowerOf2(tt.input) + assert.Equal(t, tt.expected, res) + } +} + func TestPowerOf2(t *testing.T) { type test struct { input uint64