Skip to content

Commit

Permalink
refactor: Using Generics for calculating Min and Max for numeric type #…
Browse files Browse the repository at this point in the history
…604 (#609)

* refactor: Updating LRU cache to version2 #514

* chore: rename cache variable

* docs: len returns the maximum number of items cache

* refactor: Using Generics for calculating Min and Max for numeric type #604
  • Loading branch information
sadaghiani authored Jul 29, 2023
1 parent 6c36120 commit 239bb98
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 64 deletions.
4 changes: 2 additions & 2 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (exe *Execution) checkFee(trx *tx.Tx, sb sandbox.Sandbox) error {
func calculateFee(amt int64, sb sandbox.Sandbox) int64 {
params := sb.Params()
fee := int64(float64(amt) * params.FeeFraction)
fee = util.Max64(fee, params.MinimumFee)
fee = util.Min64(fee, params.MaximumFee)
fee = util.Max(fee, params.MinimumFee)
fee = util.Min(fee, params.MaximumFee)
return fee
}
6 changes: 2 additions & 4 deletions sync/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
lru "github.com/hashicorp/golang-lru/v2"
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/util"
)

type Cache struct {
Expand Down Expand Up @@ -62,10 +63,7 @@ func (c *Cache) AddCertificate(height uint32, cert *block.Certificate) {

// Len returns the maximum number of items in the blocks and certificates cache.
func (c *Cache) Len() int {
if c.blocks.Len() > c.certs.Len() {
return c.blocks.Len()
}
return c.certs.Len()
return util.Max(c.blocks.Len(), c.certs.Len())
}

func (c *Cache) Clear() {
Expand Down
2 changes: 1 addition & 1 deletion sync/handler_blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (handler *blocksRequestHandler) ParseMessage(m message.Message, initiator p

// Help this peer to sync up
for {
blockToRead := util.MinU32(handler.config.BlockPerMessage, count)
blockToRead := util.Min(handler.config.BlockPerMessage, count)
blocksData := handler.prepareBlocks(height, blockToRead)
if len(blocksData) == 0 {
break
Expand Down
2 changes: 1 addition & 1 deletion sync/peerset/peer_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (ps *PeerSet) UpdateHeight(pid peer.ID, height uint32, lastBlockHash hash.H
p := ps.mustGetPeer(pid)
p.Height = height
p.LastBlockHash = lastBlockHash
ps.maxClaimedHeight = util.MaxU32(ps.maxClaimedHeight, height)
ps.maxClaimedHeight = util.Max(ps.maxClaimedHeight, height)
}

func (ps *PeerSet) UpdateStatus(pid peer.ID, status StatusCode) {
Expand Down
44 changes: 7 additions & 37 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/big"
"os"
"strconv"

"golang.org/x/exp/constraints"
)

const MaxUint16 = ^uint16(0)
Expand All @@ -23,54 +25,22 @@ const MinUint64 = 0
const MaxInt64 = int64(MaxUint64 >> 1)
const MinInt64 = -MaxInt64 - 1

// Max32 returns the biggest of two 32-bits numbers.
func Max32(a, b int32) int32 {
if a < b {
return b
}
return a
}

// Min32 returns the smallest of two 32-bits numbers.
func Min32(a, b int32) int32 {
if a < b {
return a
}
return b
}

// MaxU32 returns the biggest of two 32-bits unsigned numbers.
func MaxU32(a, b uint32) uint32 {
if a < b {
return b
}
return a
}

// MinU32 returns the smallest of two 32-bits unsigned numbers.
func MinU32(a, b uint32) uint32 {
if a < b {
// Max returns the biggest of two integer numbers.
func Max[T constraints.Integer](a, b T) T {
if a > b {
return a
}
return b
}

// Min64 returns the smallest of two 64-bits numbers.
func Min64(a, b int64) int64 {
// Min returns the smallest of two integer numbers.
func Min[T constraints.Integer](a, b T) T {
if a < b {
return a
}
return b
}

// Max64 returns the biggest of two 64-bits numbers.
func Max64(a, b int64) int64 {
if a < b {
return b
}
return a
}

// RandInt32 returns a random int16 in between 0 and max.
// If max set to zero or negative, the max will set to MaxInt16.
func RandInt16(max int16) int16 {
Expand Down
34 changes: 17 additions & 17 deletions util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ import (
)

func TestUtils(t *testing.T) {
assert.Equal(t, Min32(1, 1), int32(1))
assert.Equal(t, Min32(1, 2), int32(1))
assert.Equal(t, Min32(2, 1), int32(1))
assert.Equal(t, Max32(2, 2), int32(2))
assert.Equal(t, Max32(1, 2), int32(2))
assert.Equal(t, Max32(2, 1), int32(2))

assert.Equal(t, MinU32(1, 1), uint32(1))
assert.Equal(t, MinU32(1, 2), uint32(1))
assert.Equal(t, MinU32(2, 1), uint32(1))
assert.Equal(t, MaxU32(2, 2), uint32(2))
assert.Equal(t, MaxU32(1, 2), uint32(2))
assert.Equal(t, MaxU32(2, 1), uint32(2))
assert.Equal(t, Min(int32(1), 1), int32(1))
assert.Equal(t, Min(int32(1), 2), int32(1))
assert.Equal(t, Min(2, int32(1)), int32(1))
assert.Equal(t, Max(int32(2), 2), int32(2))
assert.Equal(t, Max(1, int32(2)), int32(2))
assert.Equal(t, Max(int32(2), 1), int32(2))

assert.Equal(t, Min(uint32(1), 1), uint32(1))
assert.Equal(t, Min(uint32(1), 2), uint32(1))
assert.Equal(t, Min(2, uint32(1)), uint32(1))
assert.Equal(t, Max(uint32(2), 2), uint32(2))
assert.Equal(t, Max(1, uint32(2)), uint32(2))
assert.Equal(t, Max(uint32(2), 1), uint32(2))

assert.Equal(t, MaxUint32, uint32(0xffffffff))
assert.Equal(t, MaxUint64, uint64(0xffffffffffffffff))
assert.Equal(t, MaxInt32, int32(0x7fffffff))
assert.Equal(t, MaxInt64, int64(0x7fffffffffffffff))
assert.Equal(t, Max64(MaxInt64, 1), MaxInt64)
assert.Equal(t, Max64(MinInt64, MaxInt64), MaxInt64)
assert.Equal(t, Min64(MaxInt64, 1), int64(1))
assert.Equal(t, Min64(MinInt64, MaxInt64), MinInt64)
assert.Equal(t, Max(MaxInt64, 1), MaxInt64)
assert.Equal(t, Max(MinInt64, MaxInt64), MaxInt64)
assert.Equal(t, Min(MaxInt64, 1), int64(1))
assert.Equal(t, Min(MinInt64, MaxInt64), MinInt64)
}

func TestSetFlags(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ func (w *Wallet) BroadcastTransaction(trx *tx.Tx) (string, error) {
func calcFee(amount int64) int64 {
params := param.DefaultParams() // TODO: Get parameter from the node
fee := int64(float64(amount) * params.FeeFraction)
fee = util.Max64(fee, params.MinimumFee)
fee = util.Min64(fee, params.MaximumFee)
fee = util.Max(fee, params.MinimumFee)
fee = util.Min(fee, params.MaximumFee)
return fee
}

Expand Down

0 comments on commit 239bb98

Please sign in to comment.