mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
refactor(x/precisebank): Replace FractionalAmount wrapper with func (#1961)
Removal of unnecessary wrapper type, along with using conversionFactor-1 instead of maxFractionalAmount
This commit is contained in:
parent
23ce7d8169
commit
ce6aac3a72
@ -48,7 +48,7 @@ func (k *Keeper) SetFractionalBalance(
|
|||||||
|
|
||||||
// Ensure the fractional balance is valid before setting it. Use the
|
// Ensure the fractional balance is valid before setting it. Use the
|
||||||
// NewFractionalAmountFromInt wrapper to use its Validate() method.
|
// NewFractionalAmountFromInt wrapper to use its Validate() method.
|
||||||
if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil {
|
if err := types.ValidateFractionalAmount(amount); err != nil {
|
||||||
panic(fmt.Errorf("amount is invalid: %w", err))
|
panic(fmt.Errorf("amount is invalid: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ func ValidFractionalAmountsInvariant(k Keeper) sdk.Invariant {
|
|||||||
)
|
)
|
||||||
|
|
||||||
k.IterateFractionalBalances(ctx, func(addr sdk.AccAddress, amount sdkmath.Int) bool {
|
k.IterateFractionalBalances(ctx, func(addr sdk.AccAddress, amount sdkmath.Int) bool {
|
||||||
if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil {
|
if err := types.ValidateFractionalAmount(amount); err != nil {
|
||||||
count++
|
count++
|
||||||
msg += fmt.Sprintf("\t%s has an invalid fractional amount of %s\n", addr, amount)
|
msg += fmt.Sprintf("\t%s has an invalid fractional amount of %s\n", addr, amount)
|
||||||
}
|
}
|
||||||
@ -133,7 +133,7 @@ func ValidRemainderAmountInvariant(k Keeper) sdk.Invariant {
|
|||||||
|
|
||||||
if !remainderAmount.IsZero() {
|
if !remainderAmount.IsZero() {
|
||||||
// Only validate if non-zero, as zero is default value
|
// Only validate if non-zero, as zero is default value
|
||||||
if err := types.NewFractionalAmountFromInt(remainderAmount).Validate(); err != nil {
|
if err := types.ValidateFractionalAmount(remainderAmount); err != nil {
|
||||||
broken = true
|
broken = true
|
||||||
msg = fmt.Sprintf("remainder amount is invalid: %s", err)
|
msg = fmt.Sprintf("remainder amount is invalid: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func (k *Keeper) SetRemainderAmount(
|
|||||||
|
|
||||||
// Ensure the remainder is valid before setting it. Follows the same
|
// Ensure the remainder is valid before setting it. Follows the same
|
||||||
// validation as FractionalBalance with the same value range.
|
// validation as FractionalBalance with the same value range.
|
||||||
if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil {
|
if err := types.ValidateFractionalAmount(amount); err != nil {
|
||||||
panic(fmt.Errorf("remainder amount is invalid: %w", err))
|
panic(fmt.Errorf("remainder amount is invalid: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
package types
|
|
||||||
|
|
||||||
import (
|
|
||||||
fmt "fmt"
|
|
||||||
|
|
||||||
sdkmath "cosmossdk.io/math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FractionalAmount represents a fractional amount between the valid range of 1
|
|
||||||
// and maxFractionalAmount. This wraps an sdkmath.Int to provide additional
|
|
||||||
// validation methods so it can be re-used in multiple places.
|
|
||||||
type FractionalAmount struct {
|
|
||||||
sdkmath.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFractionalAmountFromInt creates a new FractionalAmount from an sdkmath.Int.
|
|
||||||
func NewFractionalAmountFromInt(i sdkmath.Int) FractionalAmount {
|
|
||||||
return FractionalAmount{i}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFractionalAmount creates a new FractionalAmount from an int64.
|
|
||||||
func NewFractionalAmount(i int64) FractionalAmount {
|
|
||||||
return FractionalAmount{sdkmath.NewInt(i)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate checks if the FractionalAmount is valid.
|
|
||||||
func (f FractionalAmount) Validate() error {
|
|
||||||
if f.IsNil() {
|
|
||||||
return fmt.Errorf("nil amount")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !f.IsPositive() {
|
|
||||||
return fmt.Errorf("non-positive amount %v", f)
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.GT(maxFractionalAmount) {
|
|
||||||
return fmt.Errorf("amount %v exceeds max of %v", f, maxFractionalAmount)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,18 +1,15 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
fmt "fmt"
|
||||||
|
|
||||||
sdkmath "cosmossdk.io/math"
|
sdkmath "cosmossdk.io/math"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// conversionFactor is used to convert the fractional balance to integer
|
||||||
// conversionFactor is used to convert the fractional balance to integer
|
// balances.
|
||||||
// balances.
|
var conversionFactor = sdkmath.NewInt(1_000_000_000_000)
|
||||||
conversionFactor = sdkmath.NewInt(1_000_000_000_000)
|
|
||||||
// maxFractionalAmount is the largest valid value in a FractionalBalance amount.
|
|
||||||
// This is for direct internal use so that there are no extra allocations.
|
|
||||||
maxFractionalAmount = conversionFactor.SubRaw(1)
|
|
||||||
)
|
|
||||||
|
|
||||||
// ConversionFactor returns a copy of the conversionFactor used to convert the
|
// ConversionFactor returns a copy of the conversionFactor used to convert the
|
||||||
// fractional balance to integer balances. This is also 1 greater than the max
|
// fractional balance to integer balances. This is also 1 greater than the max
|
||||||
@ -39,5 +36,24 @@ func (fb FractionalBalance) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate the amount with the FractionalAmount wrapper
|
// Validate the amount with the FractionalAmount wrapper
|
||||||
return NewFractionalAmountFromInt(fb.Amount).Validate()
|
return ValidateFractionalAmount(fb.Amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateFractionalAmount checks if an sdkmath.Int is a valid fractional
|
||||||
|
// amount, ensuring it is positive and less than or equal to the maximum
|
||||||
|
// fractional amount.
|
||||||
|
func ValidateFractionalAmount(amt sdkmath.Int) error {
|
||||||
|
if amt.IsNil() {
|
||||||
|
return fmt.Errorf("nil amount")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !amt.IsPositive() {
|
||||||
|
return fmt.Errorf("non-positive amount %v", amt)
|
||||||
|
}
|
||||||
|
|
||||||
|
if amt.GTE(conversionFactor) {
|
||||||
|
return fmt.Errorf("amount %v exceeds max of %v", amt, conversionFactor.SubRaw(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func (gs *GenesisState) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if gs.Remainder.GTE(conversionFactor) {
|
if gs.Remainder.GTE(conversionFactor) {
|
||||||
return fmt.Errorf("remainder %v exceeds max of %v", gs.Remainder, maxFractionalAmount)
|
return fmt.Errorf("remainder %v exceeds max of %v", gs.Remainder, conversionFactor.SubRaw(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if sum(fractionalBalances) + remainder = whole integer value
|
// Determine if sum(fractionalBalances) + remainder = whole integer value
|
||||||
|
Loading…
Reference in New Issue
Block a user