diff --git a/consensus/block.go b/consensus/block.go index 8bd8687..0015a5c 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -7,7 +7,7 @@ package consensus import ( "fmt" - "sort" + "slices" "forge.lthn.ai/core/go-blockchain/config" "forge.lthn.ai/core/go-blockchain/types" @@ -50,7 +50,7 @@ func CheckTimestamp(blockTimestamp uint64, flags uint8, adjustedTime uint64, rec func medianTimestamp(timestamps []uint64) uint64 { sorted := make([]uint64, len(timestamps)) copy(sorted, timestamps) - sort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] }) + slices.Sort(sorted) n := len(sorted) if n == 0 { diff --git a/wallet/builder.go b/wallet/builder.go index d66e368..ef05301 100644 --- a/wallet/builder.go +++ b/wallet/builder.go @@ -11,9 +11,10 @@ package wallet import ( "bytes" + "cmp" "errors" "fmt" - "sort" + "slices" "forge.lthn.ai/core/go-blockchain/config" "forge.lthn.ai/core/go-blockchain/crypto" @@ -158,8 +159,8 @@ func (b *V1Builder) buildInput(src *Transfer) (types.TxInputToKey, inputMeta, er }) // Sort by global index (consensus rule). - sort.Slice(ring, func(a, b int) bool { - return ring[a].GlobalIndex < ring[b].GlobalIndex + slices.SortFunc(ring, func(a, b RingMember) int { + return cmp.Compare(a.GlobalIndex, b.GlobalIndex) }) // Find real index after sorting. diff --git a/wallet/wallet.go b/wallet/wallet.go index 0c0687a..1df06e9 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -10,9 +10,10 @@ package wallet import ( + "cmp" "errors" "fmt" - "sort" + "slices" "strconv" "forge.lthn.ai/core/go-blockchain/chain" @@ -193,8 +194,8 @@ func (w *Wallet) Send(destinations []Destination, fee uint64) (*types.Transactio spendable = append(spendable, tr) } } - sort.Slice(spendable, func(i, j int) bool { - return spendable[i].Amount > spendable[j].Amount + slices.SortFunc(spendable, func(a, b Transfer) int { + return cmp.Compare(b.Amount, a.Amount) // descending }) var selected []Transfer