Skip to content

Commit

Permalink
refactor(proto): use slices instead of maps (#69)
Browse files Browse the repository at this point in the history
hashmap might be a cause of inconsistent AppHash

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Aug 3, 2023
1 parent 60498f7 commit 9f6946b
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 285 deletions.
39 changes: 25 additions & 14 deletions go/node/deployment/v1beta3/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package v1beta3

import (
math "math"
"fmt"
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
Expand All @@ -26,8 +27,8 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {

func DefaultParams() Params {
return Params{
MinDeposits: map[string]uint32{
"uakt": 5000000,
MinDeposits: sdk.Coins{
sdk.NewCoin("uakt", sdk.NewInt(5000000)),
},
}
}
Expand All @@ -54,27 +55,37 @@ func (p Params) ValidateDeposit(amt sdk.Coin) error {
}

func (p Params) MinDepositFor(denom string) (sdk.Coin, error) {
val, ok := p.MinDeposits[denom]
if !ok {
return sdk.NewInt64Coin(denom, math.MaxInt64), errors.Wrapf(ErrInvalidDeposit, "Invalid deposit denomination %v", denom)
for _, minDeposit := range p.MinDeposits {
if minDeposit.Denom == denom {
return sdk.NewCoin(minDeposit.Denom, minDeposit.Amount), nil
}
}
return sdk.NewCoin(denom, sdk.NewInt(int64(val))), nil

return sdk.NewInt64Coin(denom, math.MaxInt64), errors.Wrapf(ErrInvalidDeposit, "Invalid deposit denomination %v", denom)
}

func validateMinDeposits(i interface{}) error {
vals, ok := i.(map[string]uint32)
vals, ok := i.(sdk.Coins)
if !ok {
return errors.Wrapf(ErrInvalidParam, "Min Deposits - invalid type: %T", i)
}

if _, ok := vals["uakt"]; !ok {
return errors.Wrapf(ErrInvalidParam, "Min Deposits - uakt not given: %#v", vals)
}
check := make(map[string]bool)

for denom, val := range vals {
if val >= math.MaxInt32 {
return errors.Wrapf(ErrInvalidParam, "Min Deposit (%v) - too large: %v", denom, val)
for _, minDeposit := range vals {
if _, exists := check[minDeposit.Denom]; exists {
return fmt.Errorf("duplicate Min Deposit for denom (%#v)", minDeposit)
}

check[minDeposit.Denom] = true

if minDeposit.Amount.Uint64() >= math.MaxInt32 {
return errors.Wrapf(ErrInvalidParam, "Min Deposit (%v) - too large: %v", minDeposit.Denom, minDeposit.Amount.Uint64())
}
}

if _, exists := check["uakt"]; !exists {
return errors.Wrapf(ErrInvalidParam, "Min Deposits - uakt not given: %#v", vals)
}

return nil
Expand Down
162 changes: 39 additions & 123 deletions go/node/deployment/v1beta3/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions go/node/take/v1beta3/denom_take_rate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package v1beta3

import (
"sort"
)

type DenomTakeRates []DenomTakeRate

var _ sort.Interface = (*DenomTakeRates)(nil)

func (u DenomTakeRates) Len() int {
return len(u)
}

func (u DenomTakeRates) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}

func (u DenomTakeRates) Less(i, j int) bool {
return u[i].Denom < u[j].Denom
}
32 changes: 22 additions & 10 deletions go/node/take/v1beta3/params.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package v1beta3

import (
"github.com/pkg/errors"
"fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/pkg/errors"
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -27,8 +28,11 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
func DefaultParams() Params {
return Params{
DefaultTakeRate: 20,
DenomTakeRates: map[string]uint32{
"uakt": 0,
DenomTakeRates: DenomTakeRates{
{
Denom: "uakt",
Rate: 0,
},
},
}
}
Expand All @@ -50,26 +54,34 @@ func validateTakeRate(i interface{}) error {
return errors.Wrapf(ErrInvalidParam, "%T", i)
}
if val > 100 {
return fmt.Errorf("Invalid Take Rate (%#v)", val)
return fmt.Errorf("invalid Take Rate (%#v)", val)
}
return nil
}

func validateDenomTakeRates(i interface{}) error {
val, ok := i.(map[string]uint32)
takeRates, ok := i.(DenomTakeRates)
if !ok {
return errors.Wrapf(ErrInvalidParam, "%T", i)
}

for k, v := range(val) {
if v > 100 {
return fmt.Errorf("Invalid Denom Take Rate (%v=%#v)", k,v)
check := make(map[string]uint32)

for k, v := range takeRates {
if _, exists := check[v.Denom]; exists {
return fmt.Errorf("duplicate Denom Take Rate (%#v)", v)
}

check[v.Denom] = v.Rate

if v.Rate > 100 {
return fmt.Errorf("invalid Denom Take Rate (%v=%#v)", k, v)
}
}

// must have uakt=0
if v, ok := val["uakt"]; !ok || v != 0 {
return fmt.Errorf("Invalid Denom Take Rate - uakt must be 0 (%#v)", v)
if rate, exists := check["uakt"]; !exists || rate != 0 {
return fmt.Errorf("invalid Denom Take Rate - uakt must be 0")
}

return nil
Expand Down
Loading

0 comments on commit 9f6946b

Please sign in to comment.