Skip to content

Commit c1fb2a0

Browse files
authored
Merge pull request #195 from gnoswap-labs/GSW-919-more_optimizations
GSW-919 more optimizations
2 parents e26ff5d + 7ef5241 commit c1fb2a0

File tree

137 files changed

+816
-885
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+816
-885
lines changed
File renamed without changes.

packages/big/int256/int256.gno renamed to _lib_package/big/int256/int256.gno

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// github.com/mempooler/int256
1+
// ported from github.com/mempooler/int256
22

33
package int256
44

@@ -13,10 +13,12 @@ type Int struct {
1313
neg bool
1414
}
1515

16+
// Zero returns a new Int set to 0.
1617
func Zero() *Int {
1718
return NewInt(0)
1819
}
1920

21+
// One returns a new Int set to 1.
2022
func One() *Int {
2123
return NewInt(1)
2224
}
@@ -536,17 +538,11 @@ func AddDeltaOverflow(z, x *uint256.Uint, y *Int) bool {
536538
return overflow
537539
}
538540

539-
// NIL TO ZERO
540-
func (z *Int) NilToZero() *Int {
541-
if z == nil {
542-
return NewInt(0)
543-
}
544-
return z
545-
}
546-
541+
// OBS, differs from original holiman uint256
542+
// ToString returns the decimal representation of z.
547543
func (z *Int) ToString() string {
548544
if z == nil {
549-
panic("int256: nil pointer")
545+
panic("int256: nil pointer to ToString()")
550546
}
551547

552548
t := z.abs.Dec()
@@ -555,3 +551,12 @@ func (z *Int) ToString() string {
555551
}
556552
return t
557553
}
554+
555+
// OBS, differs from original holiman uint256
556+
// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z
557+
func (z *Int) NilToZero() *Int {
558+
if z == nil {
559+
return NewInt(0)
560+
}
561+
return z
562+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/big/uint256/uint256.gno renamed to _lib_package/big/uint256/uint256.gno

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Ported from https://github.com/holiman/uint256
1+
// ported from https://github.com/holiman/uint256
22
package uint256
33

44
import (
@@ -750,6 +750,14 @@ func (z *Uint) And(x, y *Uint) *Uint {
750750
return z
751751
}
752752

753+
func (z *Uint) AndNot(x, y *Uint) *Uint {
754+
z.arr[0] = x.arr[0] &^ y.arr[0]
755+
z.arr[1] = x.arr[1] &^ y.arr[1]
756+
z.arr[2] = x.arr[2] &^ y.arr[2]
757+
z.arr[3] = x.arr[3] &^ y.arr[3]
758+
return z
759+
}
760+
753761
// Xor sets z = x ^ y and returns z.
754762
func (z *Uint) Xor(x, y *Uint) *Uint {
755763
z.arr[0] = x.arr[0] ^ y.arr[0]
@@ -963,7 +971,7 @@ func (z *Uint) fromDecimal(bs string) error {
963971
z.SetUint64(num)
964972
} else {
965973
base := NewUint(num)
966-
z.UnsafeAdd(z, base.Mul(base, mult))
974+
z.Add(z, base.Mul(base, mult))
967975
}
968976
// Chop off another 19 characters
969977
if remaining > 19 {
@@ -1186,14 +1194,6 @@ func (z *Uint) Dec() string {
11861194
return string(out[pos-len(buf):])
11871195
}
11881196

1189-
func (z *Uint) ToString() string {
1190-
if z == nil {
1191-
panic("U256 ToString() nil")
1192-
}
1193-
1194-
return z.Dec()
1195-
}
1196-
11971197
// Mod sets z to the modulus x%y for y != 0 and returns z.
11981198
// If y == 0, z is set to 0 (OBS: differs from the big.Uint)
11991199
func (z *Uint) Mod(x, y *Uint) *Uint {
@@ -1236,22 +1236,28 @@ func (z *Uint) Clone() *Uint {
12361236
return &x
12371237
}
12381238

1239+
// OBS, differs from original holiman uint256
1240+
// ToString returns the decimal representation of z.
1241+
func (z *Uint) ToString() string {
1242+
if z == nil {
1243+
panic("uin256: nil pointer to ToString()")
1244+
}
1245+
1246+
return z.Dec()
1247+
}
1248+
1249+
// OBS, differs from original holiman uint256
1250+
// IsNil reports whether z is nil
12391251
func (z *Uint) IsNil() bool {
12401252
return z == nil
12411253
}
12421254

1255+
// OBS, differs from original holiman uint256
1256+
// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z
12431257
func (z *Uint) NilToZero() *Uint {
12441258
if z == nil {
12451259
z = NewUint(0)
12461260
}
12471261

12481262
return z
12491263
}
1250-
1251-
func (z *Uint) AndNot(x, y *Uint) *Uint {
1252-
z.arr[0] = x.arr[0] &^ y.arr[0]
1253-
z.arr[1] = x.arr[1] &^ y.arr[1]
1254-
z.arr[2] = x.arr[2] &^ y.arr[2]
1255-
z.arr[3] = x.arr[3] &^ y.arr[3]
1256-
return z
1257-
}
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.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package common
2+
3+
import (
4+
"std"
5+
6+
"gno.land/r/gnoswap/consts"
7+
)
8+
9+
var (
10+
limitCaller bool = true
11+
)
12+
13+
func GetLimitCaller() bool {
14+
return limitCaller
15+
}
16+
17+
func SetLimitCaller(v bool) {
18+
MustCallFromAdmin()
19+
limitCaller = v
20+
}
21+
22+
func MustCallFromAdmin() {
23+
caller := std.GetOrigCaller()
24+
if caller != consts.GNOSWAP_ADMIN {
25+
panic("must be called by admin")
26+
}
27+
}
28+
29+
func DisallowCallFromUser() {
30+
isOrigin := std.IsOriginCall()
31+
if isOrigin {
32+
panic("must be called by realm, not user")
33+
}
34+
}
35+
36+
func AllowCallFromOnly(allowPath string) {
37+
if !limitCaller {
38+
prevPath := std.PrevRealm().PkgPath()
39+
40+
if prevPath != allowPath {
41+
panic("caller is not allowed to call this function")
42+
}
43+
}
44+
}

packages/gnoswap/common/liquidity_amounts.gno renamed to _lib_realm/gnoswap/common/liquidity_amounts.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package common
22

33
import (
4-
"gno.land/r/demo/consts"
4+
"gno.land/r/gnoswap/consts"
55

66
u256 "gno.land/p/big/uint256"
77
)
File renamed without changes.

_setup/gns/gns.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"gno.land/p/demo/ufmt"
99
"gno.land/r/demo/users"
1010

11-
"gno.land/r/demo/consts"
11+
"gno.land/r/gnoswap/consts"
1212
)
1313

1414
const MAXIMUM_SUPPLY = uint64(1_000_000_000_000_000)

gov/realm_variables.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func vote(proposalID uint64, address string, option VoteOption) {
4949
vote, ok := p.Voters.Get(address)
5050
if !ok {
5151
// new voter, add to voters map and increase the total voting power
52-
p.updateVotingPower(bigint(0), power)
52+
p.updateVotingPower(0, power)
5353

5454
}
5555

packages/big/int256/int256_test.gno

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/big/uint256/uint256_test.gno

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)