diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 3a881bd28..dd6469862 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -12,6 +12,11 @@ BREAKING CHANGES: * Go API * \#2310 Mempool.ReapMaxBytes -> Mempool.ReapMaxBytesMaxGas * \#2431 Remove Word256 code in libs/common, due to lack of use + * \#2452 Remove the following methods from libs/common due to lack of use: + * byteslice.go: cmn.IsZeros, cmn.RightPadBytes, cmn.LeftPadBytes, cmn.PrefixEndBytes + * strings.go: cmn.IsHex, cmn.StripHex + * int.go: Uint64Slice, all put/get int64 methods + * Blockchain Protocol * P2P Protocol diff --git a/libs/common/byteslice.go b/libs/common/byteslice.go index 57b3a8a2b..af2d7949d 100644 --- a/libs/common/byteslice.go +++ b/libs/common/byteslice.go @@ -1,9 +1,5 @@ package common -import ( - "bytes" -) - // Fingerprint returns the first 6 bytes of a byte slice. // If the slice is less than 6 bytes, the fingerprint // contains trailing zeroes. @@ -12,62 +8,3 @@ func Fingerprint(slice []byte) []byte { copy(fingerprint, slice) return fingerprint } - -func IsZeros(slice []byte) bool { - for _, byt := range slice { - if byt != byte(0) { - return false - } - } - return true -} - -func RightPadBytes(slice []byte, l int) []byte { - if l < len(slice) { - return slice - } - padded := make([]byte, l) - copy(padded[0:len(slice)], slice) - return padded -} - -func LeftPadBytes(slice []byte, l int) []byte { - if l < len(slice) { - return slice - } - padded := make([]byte, l) - copy(padded[l-len(slice):], slice) - return padded -} - -func TrimmedString(b []byte) string { - trimSet := string([]byte{0}) - return string(bytes.TrimLeft(b, trimSet)) - -} - -// PrefixEndBytes returns the end byteslice for a noninclusive range -// that would include all byte slices for which the input is the prefix -func PrefixEndBytes(prefix []byte) []byte { - if prefix == nil { - return nil - } - - end := make([]byte, len(prefix)) - copy(end, prefix) - finished := false - - for !finished { - if end[len(end)-1] != byte(255) { - end[len(end)-1]++ - finished = true - } else { - end = end[:len(end)-1] - if len(end) == 0 { - end = nil - finished = true - } - } - } - return end -} diff --git a/libs/common/byteslice_test.go b/libs/common/byteslice_test.go deleted file mode 100644 index 98085d125..000000000 --- a/libs/common/byteslice_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package common - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestPrefixEndBytes(t *testing.T) { - assert := assert.New(t) - - var testCases = []struct { - prefix []byte - expected []byte - }{ - {[]byte{byte(55), byte(255), byte(255), byte(0)}, []byte{byte(55), byte(255), byte(255), byte(1)}}, - {[]byte{byte(55), byte(255), byte(255), byte(15)}, []byte{byte(55), byte(255), byte(255), byte(16)}}, - {[]byte{byte(55), byte(200), byte(255)}, []byte{byte(55), byte(201)}}, - {[]byte{byte(55), byte(255), byte(255)}, []byte{byte(56)}}, - {[]byte{byte(255), byte(255), byte(255)}, nil}, - {nil, nil}, - } - - for _, test := range testCases { - end := PrefixEndBytes(test.prefix) - assert.Equal(test.expected, end) - } -} diff --git a/libs/common/int.go b/libs/common/int.go index a8a5f1e00..845dc97f6 100644 --- a/libs/common/int.go +++ b/libs/common/int.go @@ -1,59 +1,5 @@ package common -import ( - "encoding/binary" - "sort" -) - -// Sort for []uint64 - -type Uint64Slice []uint64 - -func (p Uint64Slice) Len() int { return len(p) } -func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } -func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p Uint64Slice) Sort() { sort.Sort(p) } - -func SearchUint64s(a []uint64, x uint64) int { - return sort.Search(len(a), func(i int) bool { return a[i] >= x }) -} - -func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) } - -//-------------------------------------------------------------------------------- - -func PutUint64LE(dest []byte, i uint64) { - binary.LittleEndian.PutUint64(dest, i) -} - -func GetUint64LE(src []byte) uint64 { - return binary.LittleEndian.Uint64(src) -} - -func PutUint64BE(dest []byte, i uint64) { - binary.BigEndian.PutUint64(dest, i) -} - -func GetUint64BE(src []byte) uint64 { - return binary.BigEndian.Uint64(src) -} - -func PutInt64LE(dest []byte, i int64) { - binary.LittleEndian.PutUint64(dest, uint64(i)) -} - -func GetInt64LE(src []byte) int64 { - return int64(binary.LittleEndian.Uint64(src)) -} - -func PutInt64BE(dest []byte, i int64) { - binary.BigEndian.PutUint64(dest, uint64(i)) -} - -func GetInt64BE(src []byte) int64 { - return int64(binary.BigEndian.Uint64(src)) -} - // IntInSlice returns true if a is found in the list. func IntInSlice(a int, list []int) bool { for _, b := range list { diff --git a/libs/common/string.go b/libs/common/string.go index e341b49e8..e125043d1 100644 --- a/libs/common/string.go +++ b/libs/common/string.go @@ -1,28 +1,10 @@ package common import ( - "encoding/hex" "fmt" "strings" ) -// IsHex returns true for non-empty hex-string prefixed with "0x" -func IsHex(s string) bool { - if len(s) > 2 && strings.EqualFold(s[:2], "0x") { - _, err := hex.DecodeString(s[2:]) - return err == nil - } - return false -} - -// StripHex returns hex string without leading "0x" -func StripHex(s string) string { - if IsHex(s) { - return s[2:] - } - return s -} - // StringInSlice returns true if a is found the list. func StringInSlice(a string, list []string) bool { for _, b := range list { diff --git a/libs/common/string_test.go b/libs/common/string_test.go index 0fc677a91..5e7ae98cc 100644 --- a/libs/common/string_test.go +++ b/libs/common/string_test.go @@ -13,30 +13,12 @@ func TestStringInSlice(t *testing.T) { assert.False(t, StringInSlice("", []string{})) } -func TestIsHex(t *testing.T) { - notHex := []string{ - "", " ", "a", "x", "0", "0x", "0X", "0x ", "0X ", "0X a", - "0xf ", "0x f", "0xp", "0x-", - "0xf", "0XBED", "0xF", "0xbed", // Odd lengths - } - for _, v := range notHex { - assert.False(t, IsHex(v), "%q is not hex", v) - } - hex := []string{ - "0x00", "0x0a", "0x0F", "0xFFFFFF", "0Xdeadbeef", "0x0BED", - "0X12", "0X0A", - } - for _, v := range hex { - assert.True(t, IsHex(v), "%q is hex", v) - } -} - func TestIsASCIIText(t *testing.T) { notASCIIText := []string{ "", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t", } for _, v := range notASCIIText { - assert.False(t, IsHex(v), "%q is not ascii-text", v) + assert.False(t, IsASCIIText(v), "%q is not ascii-text", v) } asciiText := []string{ " ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",