Skip to content

Commit

Permalink
Merge pull request #195 from gnoswap-labs/GSW-919-more_optimizations
Browse files Browse the repository at this point in the history
GSW-919 more optimizations
  • Loading branch information
notJoon authored Mar 18, 2024
2 parents e26ff5d + 7ef5241 commit c1fb2a0
Show file tree
Hide file tree
Showing 137 changed files with 816 additions and 885 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// github.com/mempooler/int256
// ported from github.com/mempooler/int256

package int256

Expand All @@ -13,10 +13,12 @@ type Int struct {
neg bool
}

// Zero returns a new Int set to 0.
func Zero() *Int {
return NewInt(0)
}

// One returns a new Int set to 1.
func One() *Int {
return NewInt(1)
}
Expand Down Expand Up @@ -536,17 +538,11 @@ func AddDeltaOverflow(z, x *uint256.Uint, y *Int) bool {
return overflow
}

// NIL TO ZERO
func (z *Int) NilToZero() *Int {
if z == nil {
return NewInt(0)
}
return z
}

// OBS, differs from original holiman uint256
// ToString returns the decimal representation of z.
func (z *Int) ToString() string {
if z == nil {
panic("int256: nil pointer")
panic("int256: nil pointer to ToString()")
}

t := z.abs.Dec()
Expand All @@ -555,3 +551,12 @@ func (z *Int) ToString() string {
}
return t
}

// OBS, differs from original holiman uint256
// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z
func (z *Int) NilToZero() *Int {
if z == nil {
return NewInt(0)
}
return z
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Ported from https://github.com/holiman/uint256
// ported from https://github.com/holiman/uint256
package uint256

import (
Expand Down Expand Up @@ -750,6 +750,14 @@ func (z *Uint) And(x, y *Uint) *Uint {
return z
}

func (z *Uint) AndNot(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] &^ y.arr[0]
z.arr[1] = x.arr[1] &^ y.arr[1]
z.arr[2] = x.arr[2] &^ y.arr[2]
z.arr[3] = x.arr[3] &^ y.arr[3]
return z
}

// Xor sets z = x ^ y and returns z.
func (z *Uint) Xor(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] ^ y.arr[0]
Expand Down Expand Up @@ -963,7 +971,7 @@ func (z *Uint) fromDecimal(bs string) error {
z.SetUint64(num)
} else {
base := NewUint(num)
z.UnsafeAdd(z, base.Mul(base, mult))
z.Add(z, base.Mul(base, mult))
}
// Chop off another 19 characters
if remaining > 19 {
Expand Down Expand Up @@ -1186,14 +1194,6 @@ func (z *Uint) Dec() string {
return string(out[pos-len(buf):])
}

func (z *Uint) ToString() string {
if z == nil {
panic("U256 ToString() nil")
}

return z.Dec()
}

// Mod sets z to the modulus x%y for y != 0 and returns z.
// If y == 0, z is set to 0 (OBS: differs from the big.Uint)
func (z *Uint) Mod(x, y *Uint) *Uint {
Expand Down Expand Up @@ -1236,22 +1236,28 @@ func (z *Uint) Clone() *Uint {
return &x
}

// OBS, differs from original holiman uint256
// ToString returns the decimal representation of z.
func (z *Uint) ToString() string {
if z == nil {
panic("uin256: nil pointer to ToString()")
}

return z.Dec()
}

// OBS, differs from original holiman uint256
// IsNil reports whether z is nil
func (z *Uint) IsNil() bool {
return z == nil
}

// OBS, differs from original holiman uint256
// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z
func (z *Uint) NilToZero() *Uint {
if z == nil {
z = NewUint(0)
}

return z
}

func (z *Uint) AndNot(x, y *Uint) *Uint {
z.arr[0] = x.arr[0] &^ y.arr[0]
z.arr[1] = x.arr[1] &^ y.arr[1]
z.arr[2] = x.arr[2] &^ y.arr[2]
z.arr[3] = x.arr[3] &^ y.arr[3]
return z
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions _lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"std"

"gno.land/r/gnoswap/consts"
)

var (
limitCaller bool = true
)

func GetLimitCaller() bool {
return limitCaller
}

func SetLimitCaller(v bool) {
MustCallFromAdmin()
limitCaller = v
}

func MustCallFromAdmin() {
caller := std.GetOrigCaller()
if caller != consts.GNOSWAP_ADMIN {
panic("must be called by admin")
}
}

func DisallowCallFromUser() {
isOrigin := std.IsOriginCall()
if isOrigin {
panic("must be called by realm, not user")
}
}

func AllowCallFromOnly(allowPath string) {
if !limitCaller {
prevPath := std.PrevRealm().PkgPath()

if prevPath != allowPath {
panic("caller is not allowed to call this function")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

import (
"gno.land/r/demo/consts"
"gno.land/r/gnoswap/consts"

u256 "gno.land/p/big/uint256"
)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion _setup/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"

"gno.land/r/demo/consts"
"gno.land/r/gnoswap/consts"
)

const MAXIMUM_SUPPLY = uint64(1_000_000_000_000_000)
Expand Down
2 changes: 1 addition & 1 deletion gov/realm_variables.gno
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func vote(proposalID uint64, address string, option VoteOption) {
vote, ok := p.Voters.Get(address)
if !ok {
// new voter, add to voters map and increase the total voting power
p.updateVotingPower(bigint(0), power)
p.updateVotingPower(0, power)

}

Expand Down
1 change: 0 additions & 1 deletion packages/big/int256/int256_test.gno

This file was deleted.

18 changes: 0 additions & 18 deletions packages/big/uint256/uint256_test.gno

This file was deleted.

Loading

0 comments on commit c1fb2a0

Please sign in to comment.