Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Modify parseUint function to use strconv package #358

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions _deploy/p/gnoswap/uint256/uint256.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package uint256

import (
"errors"
"strconv"
"math/bits"
)

Expand Down Expand Up @@ -143,10 +144,10 @@ func (z *Uint) fromDecimal(bs string) error {
if remaining <= 0 {
return nil // Done
} else if remaining > 19 {
num, err = parseUint(bs[remaining-19:remaining], 10, 64)
num, err = strconv.ParseUint(bs[remaining-19:remaining], 10, 64)
} else {
// Final round
num, err = parseUint(bs, 10, 64)
num, err = strconv.ParseUint(bs, 10, 64)
}
if err != nil {
return err
Expand Down
160 changes: 0 additions & 160 deletions _deploy/p/gnoswap/uint256/utils.gno
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
package uint256

// lower(c) is a lower-case letter if and only if
// c is either that lower-case letter or the equivalent upper-case letter.
// Instead of writing c == 'x' || c == 'X' one can write lower(c) == 'x'.
// Note that lower of non-letters can produce other non-letters.
func lower(c byte) byte {
return c | ('x' - 'X')
}

// underscoreOK reports whether the underscores in s are allowed.
// Checking them in this one function lets all the parsers skip over them simply.
// Underscore must appear only between digits or between a base prefix and a digit.
func underscoreOK(s string) bool {
// saw tracks the last character (class) we saw:
// ^ for beginning of number,
// 0 for a digit or base prefix,
// _ for an underscore,
// ! for none of the above.
saw := '^'
i := 0

// Optional sign.
if len(s) >= 1 && (s[0] == '-' || s[0] == '+') {
s = s[1:]
}

// Optional base prefix.
hex := false
if len(s) >= 2 && s[0] == '0' && (lower(s[1]) == 'b' || lower(s[1]) == 'o' || lower(s[1]) == 'x') {
i = 2
saw = '0' // base prefix counts as a digit for "underscore as digit separator"
hex = lower(s[1]) == 'x'
}

// Number proper.
for ; i < len(s); i++ {
// Digits are always okay.
if '0' <= s[i] && s[i] <= '9' || hex && 'a' <= lower(s[i]) && lower(s[i]) <= 'f' {
saw = '0'
continue
}
// Underscore must follow digit.
if s[i] == '_' {
if saw != '0' {
return false
}
saw = '_'
continue
}
// Underscore must also be followed by digit.
if saw == '_' {
return false
}
// Saw non-digit, non-underscore.
saw = '!'
}
return saw != '_'
}

func checkNumberS(input string) error {
const fn = "UnmarshalText"
l := len(input)
Expand All @@ -76,105 +18,3 @@ func checkNumberS(input string) error {
}
return nil
}

// ParseUint is like ParseUint but for unsigned numbers.
//
// A sign prefix is not permitted.
func parseUint(s string, base int, bitSize int) (uint64, error) {
const fnParseUint = "ParseUint"

if s == "" {
return 0, errSyntax(fnParseUint, s)
}

base0 := base == 0

s0 := s
switch {
case 2 <= base && base <= 36:
// valid base; nothing to do

case base == 0:
// Look for octal, hex prefix.
base = 10
if s[0] == '0' {
switch {
case len(s) >= 3 && lower(s[1]) == 'b':
base = 2
s = s[2:]
case len(s) >= 3 && lower(s[1]) == 'o':
base = 8
s = s[2:]
case len(s) >= 3 && lower(s[1]) == 'x':
base = 16
s = s[2:]
default:
base = 8
s = s[1:]
}
}

default:
return 0, errInvalidBase(fnParseUint, base)
}

if bitSize == 0 {
bitSize = uintSize
} else if bitSize < 0 || bitSize > 64 {
return 0, errInvalidBitSize(fnParseUint, bitSize)
}

// Cutoff is the smallest number such that cutoff*base > maxUint64.
// Use compile-time constants for common cases.
var cutoff uint64
switch base {
case 10:
cutoff = MaxUint64/10 + 1
case 16:
cutoff = MaxUint64/16 + 1
default:
cutoff = MaxUint64/uint64(base) + 1
}

maxVal := uint64(1)<<uint(bitSize) - 1

underscores := false
var n uint64
for _, c := range []byte(s) {
var d byte
switch {
case c == '_' && base0:
underscores = true
continue
case '0' <= c && c <= '9':
d = c - '0'
case 'a' <= lower(c) && lower(c) <= 'z':
d = lower(c) - 'a' + 10
default:
return 0, errSyntax(fnParseUint, s0)
}

if d >= byte(base) {
return 0, errSyntax(fnParseUint, s0)
}

if n >= cutoff {
// n*base overflows
return maxVal, errRange(fnParseUint, s0)
}
n *= uint64(base)

n1 := n + uint64(d)
if n1 < n || n1 > maxVal {
// n+d overflows
return maxVal, errRange(fnParseUint, s0)
}
n = n1
}

if underscores && !underscoreOK(s0) {
return 0, errSyntax(fnParseUint, s0)
}

return n, nil
}
115 changes: 94 additions & 21 deletions gov/governance/execute.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package governance

import (
"std"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -472,8 +473,11 @@ func executeParameterChange(msgs []string) {
}

func handleCommunityPoolTransferToken(params []string) (string, std.Address, uint64) {

return params[0], std.Address(params[1]), parseUint(params[2], 10, 64)
p2, err := strconv.ParseUint(params[2], 10, 64)
if err != nil {
panic(err)
}
return params[0], std.Address(params[1]), p2
}

func handleEmissionChangeDistributionPct(params []string) (
Expand All @@ -482,56 +486,125 @@ func handleEmissionChangeDistributionPct(params []string) (
int, uint64,
int, uint64,
) {
target01 := strToInt(params[0])
pct01 := parseUint(params[1], 10, 64)
target01, err := strconv.ParseInt(params[0], 10, 64)
if err != nil {
panic(err)
}
pct01, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
panic(err)
}

target02 := strToInt(params[2])
pct02 := parseUint(params[3], 10, 64)
pct02, err := strconv.ParseUint(params[3], 10, 64)
if err != nil {
panic(err)
}

target03 := strToInt(params[4])
pct03 := parseUint(params[5], 10, 64)
pct03, err := strconv.ParseUint(params[5], 10, 64)
if err != nil {
panic(err)
}

target04 := strToInt(params[6])
pct04 := parseUint(params[7], 10, 64)
pct04, err := strconv.ParseUint(params[7], 10, 64)
if err != nil {
panic(err)
}

return target01, pct01, target02, pct02, target03, pct03, target04, pct04
return int(target01), pct01, target02, pct02, target03, pct03, target04, pct04
}

func handleSetAvgBlockTimeInMs(params []string) int64 {
return int64(parseUint(params[0], 10, 64))
res, err := strconv.ParseInt(params[0], 10, 64)
if err != nil {
panic(err)
}
return res
}

func handleGovernanceReconfigure(params []string) (
uint64, uint64, uint64, uint64, uint64, uint64, uint64,
) {
votingStartDelay := parseUint(params[0], 10, 64)
votingPeriod := parseUint(params[1], 10, 64)
votingWeightSmoothingDuration := parseUint(params[2], 10, 64)
quorum := parseUint(params[3], 10, 64)
proposalCreationThreshold := parseUint(params[4], 10, 64)
executionDelay := parseUint(params[5], 10, 64)
executionWindow := parseUint(params[6], 10, 64)
votingStartDelay, err := strconv.ParseUint(params[0], 10, 64)
if err != nil {
panic(err.Error())
}

votingPeriod, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
panic(err.Error())
}

votingWeightSmoothingDuration, err := strconv.ParseUint(params[2], 10, 64)
if err != nil {
panic(err.Error())
}

quorum, err := strconv.ParseUint(params[3], 10, 64)
if err != nil {
panic(err.Error())
}

proposalCreationThreshold, err := strconv.ParseUint(params[4], 10, 64)
if err != nil {
panic(err.Error())
}

executionDelay, err := strconv.ParseUint(params[5], 10, 64)
if err != nil {
panic(err.Error())
}

executionWindow, err := strconv.ParseUint(params[6], 10, 64)
if err != nil {
panic(err.Error())
}

return votingStartDelay, votingPeriod, votingWeightSmoothingDuration, quorum, proposalCreationThreshold, executionDelay, executionWindow
}

func handlePoolSetFeeProtocol(params []string) (uint8, uint8) {
feeProtocol0 := parseUint(params[0], 10, 64)
feeProtocol1 := parseUint(params[1], 10, 64)
feeProtocol0, err := strconv.ParseUint(params[0], 10, 64)
if err != nil {
panic(err.Error())
}

feeProtocol1, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
panic(err.Error())
}

return uint8(feeProtocol0), uint8(feeProtocol1)
}

func handleSingleUint64(params []string) uint64 {
return parseUint(params[0], 10, 64)
res, err := strconv.ParseUint(params[0], 10, 64)
if err != nil {
panic(err.Error())
}
return res
}

func handlePoolPathTier(params []string) (string, uint64) {
return params[0], parseUint(params[1], 10, 64)
res, err := strconv.ParseUint(params[1], 10, 64)
if err != nil {
panic(err.Error())
}
return params[0], res
}

func handleTwoInt64(params []string) (int64, int64) {
return int64(parseUint(params[0], 10, 64)), int64(parseUint(params[1], 10, 64))
res0, err := strconv.ParseInt(params[0], 10, 64)
if err != nil {
panic(err.Error())
}
res1, err := strconv.ParseInt(params[1], 10, 64)
if err != nil {
panic(err.Error())
}
return res0, res1
}

func handleSingleBool(params []string) bool {
Expand Down
Loading