From b4e4567c2b57330117b109d9d8e09682b5518c5d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 26 Aug 2024 20:42:09 +0300 Subject: [PATCH] *: don't use CompareTo name for three-way comparison functions "Compare" is almost a standard one now, although math/big uses Cmp for historic reasons (keys.PublicKey does so too). This also fixes Fixed8 since int64 to int conversion is lossy. Signed-off-by: Roman Khimov --- pkg/core/mempool/mem_pool.go | 12 ++++++------ pkg/core/mempool/mem_pool_test.go | 26 +++++++++++++------------- pkg/encoding/fixedn/fixed8.go | 13 +++++++------ pkg/encoding/fixedn/fixed8_test.go | 4 ++-- pkg/network/server.go | 4 ++-- pkg/util/uint256.go | 4 ++-- pkg/util/uint256_test.go | 2 +- 7 files changed, 33 insertions(+), 32 deletions(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index 17f2b483d..b9fdbbf3c 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -84,13 +84,13 @@ type Pool struct { func (p items) Len() int { return len(p) } func (p items) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p items) Less(i, j int) bool { return p[i].CompareTo(p[j]) < 0 } +func (p items) Less(i, j int) bool { return p[i].Compare(p[j]) < 0 } -// CompareTo returns the difference between two items. +// Compare returns the difference between two items. // difference < 0 implies p < otherP. // difference = 0 implies p = otherP. // difference > 0 implies p > otherP. -func (p item) CompareTo(otherP item) int { +func (p item) Compare(otherP item) int { pHigh := p.txn.HasAttribute(transaction.HighPriority) otherHigh := otherP.txn.HasAttribute(transaction.HighPriority) if pHigh && !otherHigh { @@ -238,7 +238,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer, data ...any) error { // transactions with the same priority and appending to the end of the // slice is always more efficient. n := sort.Search(len(mp.verifiedTxes), func(n int) bool { - return pItem.CompareTo(mp.verifiedTxes[n]) > 0 + return pItem.Compare(mp.verifiedTxes[n]) > 0 }) // Changing sort.Search to slices.BinarySearchFunc() is not recommended // above, as of Go 1.23 this results in @@ -487,14 +487,14 @@ func (mp *Pool) TryGetData(hash util.Uint256) (any, bool) { if tx, ok := mp.verifiedMap[hash]; ok { itm := item{txn: tx} n := sort.Search(len(mp.verifiedTxes), func(n int) bool { - return itm.CompareTo(mp.verifiedTxes[n]) >= 0 + return itm.Compare(mp.verifiedTxes[n]) >= 0 }) if n < len(mp.verifiedTxes) { for i := n; i < len(mp.verifiedTxes); i++ { // items may have equal priority, so `n` is the left bound of the items which are as prioritized as the desired `itm`. if mp.verifiedTxes[i].txn.Hash() == hash { return mp.verifiedTxes[i].data, ok } - if itm.CompareTo(mp.verifiedTxes[i]) != 0 { + if itm.Compare(mp.verifiedTxes[i]) != 0 { break } } diff --git a/pkg/core/mempool/mem_pool_test.go b/pkg/core/mempool/mem_pool_test.go index d2a100199..1bc0ead53 100644 --- a/pkg/core/mempool/mem_pool_test.go +++ b/pkg/core/mempool/mem_pool_test.go @@ -117,7 +117,7 @@ func TestOverCapacity(t *testing.T) { mp := New(mempoolSize, 0, false, nil) var checkPoolIsSorted = func() { - require.True(t, slices.IsSortedFunc(mp.verifiedTxes, func(a, b item) int { return -a.CompareTo(b) })) + require.True(t, slices.IsSortedFunc(mp.verifiedTxes, func(a, b item) int { return -a.Compare(b) })) } for i := 0; i < mempoolSize; i++ { @@ -347,18 +347,18 @@ func TestMempoolItemsOrder(t *testing.T) { tx4.Signers = []transaction.Signer{{Account: sender0}} item4 := item{txn: tx4} - require.True(t, item1.CompareTo(item2) > 0) - require.True(t, item2.CompareTo(item1) < 0) - require.True(t, item1.CompareTo(item3) > 0) - require.True(t, item3.CompareTo(item1) < 0) - require.True(t, item1.CompareTo(item4) > 0) - require.True(t, item4.CompareTo(item1) < 0) - require.True(t, item2.CompareTo(item3) > 0) - require.True(t, item3.CompareTo(item2) < 0) - require.True(t, item2.CompareTo(item4) > 0) - require.True(t, item4.CompareTo(item2) < 0) - require.True(t, item3.CompareTo(item4) > 0) - require.True(t, item4.CompareTo(item3) < 0) + require.True(t, item1.Compare(item2) > 0) + require.True(t, item2.Compare(item1) < 0) + require.True(t, item1.Compare(item3) > 0) + require.True(t, item3.Compare(item1) < 0) + require.True(t, item1.Compare(item4) > 0) + require.True(t, item4.Compare(item1) < 0) + require.True(t, item2.Compare(item3) > 0) + require.True(t, item3.Compare(item2) < 0) + require.True(t, item2.Compare(item4) > 0) + require.True(t, item4.Compare(item2) < 0) + require.True(t, item3.Compare(item4) > 0) + require.True(t, item4.Compare(item3) < 0) } func TestMempoolAddRemoveOracleResponse(t *testing.T) { diff --git a/pkg/encoding/fixedn/fixed8.go b/pkg/encoding/fixedn/fixed8.go index ad28d5133..46adfeb21 100644 --- a/pkg/encoding/fixedn/fixed8.go +++ b/pkg/encoding/fixedn/fixed8.go @@ -1,6 +1,7 @@ package fixedn import ( + "cmp" "strconv" "strings" @@ -158,10 +159,10 @@ func (f Fixed8) Equal(g Fixed8) bool { return f == g } -// CompareTo returns the difference between the f and g. -// difference < 0 implies f < g. -// difference = 0 implies f = g. -// difference > 0 implies f > g. -func (f Fixed8) CompareTo(g Fixed8) int { - return int(f - g) +// Compare performs three-way comparison between f and g. +// - -1 implies f < g. +// - 0 implies f = g. +// - 1 implies f > g. +func (f Fixed8) Compare(g Fixed8) int { + return cmp.Compare(f, g) } diff --git a/pkg/encoding/fixedn/fixed8_test.go b/pkg/encoding/fixedn/fixed8_test.go index db94c3062..283374177 100644 --- a/pkg/encoding/fixedn/fixed8_test.go +++ b/pkg/encoding/fixedn/fixed8_test.go @@ -164,8 +164,8 @@ func TestFixed8_Arith(t *testing.T) { assert.True(t, u1.LessThan(u2)) assert.True(t, u2.GreaterThan(u1)) assert.True(t, u1.Equal(u1)) - assert.NotZero(t, u1.CompareTo(u2)) - assert.Zero(t, u1.CompareTo(u1)) + assert.NotZero(t, u1.Compare(u2)) + assert.Zero(t, u1.Compare(u1)) assert.EqualValues(t, Fixed8(2), u2.Div(3)) } diff --git a/pkg/network/server.go b/pkg/network/server.go index 73ee2752b..556ac4e37 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -1169,7 +1169,7 @@ txloop: var cbList = s.txCbList.Load() if cbList != nil { var list = cbList.([]util.Uint256) - _, found := slices.BinarySearchFunc(list, tx.Hash(), util.Uint256.CompareTo) + _, found := slices.BinarySearchFunc(list, tx.Hash(), util.Uint256.Compare) if found { txCallback(tx) } @@ -1484,7 +1484,7 @@ func (s *Server) RequestTx(hashes ...util.Uint256) { } var sorted = slices.Clone(hashes) - slices.SortFunc(sorted, util.Uint256.CompareTo) + slices.SortFunc(sorted, util.Uint256.Compare) s.txCbList.Store(sorted) for i := 0; i <= len(hashes)/payload.MaxHashesCount; i++ { diff --git a/pkg/util/uint256.go b/pkg/util/uint256.go index e5a835c4e..b6d7cf68a 100644 --- a/pkg/util/uint256.go +++ b/pkg/util/uint256.go @@ -121,12 +121,12 @@ func (u Uint256) MarshalJSON() ([]byte, error) { return r, nil } -// CompareTo compares two Uint256 with each other. Possible output: 1, -1, 0 +// Compare performs three-way comparison of two Uint256. Possible output: 1, -1, 0 // // 1 implies u > other. // -1 implies u < other. // 0 implies u = other. -func (u Uint256) CompareTo(other Uint256) int { return bytes.Compare(u[:], other[:]) } +func (u Uint256) Compare(other Uint256) int { return bytes.Compare(u[:], other[:]) } // EncodeBinary implements the io.Serializable interface. func (u *Uint256) EncodeBinary(w *io.BinWriter) { diff --git a/pkg/util/uint256_test.go b/pkg/util/uint256_test.go index cbbeda216..c4e943240 100644 --- a/pkg/util/uint256_test.go +++ b/pkg/util/uint256_test.go @@ -83,7 +83,7 @@ func TestUInt256Equals(t *testing.T) { require.NoError(t, err) assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub) assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua) - assert.Zero(t, ua.CompareTo(ua), "%s and %s must be equal", ua, ua) + assert.Zero(t, ua.Compare(ua), "%s and %s must be equal", ua, ua) } func TestUint256_Serializable(t *testing.T) {