-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move rate limiter to zetaclient and add metrics (#2110)
* initial commit of grpc pending cctx query with rate limiter * replace big.Float with sdk.Dec and update mock rate limiter flags * split big loop into backwards loop and forwards loop to be more accurate * adjust zetaclient code to query pending cctx with rate limit * update change log and add one more rate limiter flag test * use outboun amount for calculation * some minimum code refactor * created separate file for cctx query with rate limit * improved a few error handlling * use old cctx query as fallback when rate limiter is disabled; some renaming * fixed unit test compile * added unit test for fallback query * added unit tests for cctx value conversion * add changelog entry * added unit tests for query pending cctxs within rate limit * added total value in rate limiter window for monitoring purpose * Update x/crosschain/keeper/grpc_query_cctx_rate_limit.go Co-authored-by: Lucas Bertrand <[email protected]> * change variable name fCoin to foreignCoin * Update x/fungible/keeper/foreign_coins.go Co-authored-by: Lucas Bertrand <[email protected]> * Update x/crosschain/keeper/grpc_query_cctx_rate_limit_test.go Co-authored-by: Lucas Bertrand <[email protected]> * converted rate limiter query unit tests to table test * handle edge case when pending cctxs span wider block range than sliding window * added zero rate check; added comment to make unit test clearer * added unit test and note for method GetAllForeignCoinMap * treat Rate as average block rate; stop outbound when current rate limit exceeds Rate; updated metrics * refactor: allow zeta deposits to new zevm address (#2076) * allow zevm coin deposit to unknow addresses * add e2e tests * add changelog * add comments * add commented unit tests back * replace sdk.Dec with sdkmath.Int to represent cctx value in azeta * test(e2e): add rate limiter admin E2E test (#2063) * refactor and create Withdraw ZETA general function * new rate limiter test * use rate limiter for admin test * fix the test: single approval and add liquidity * make generate * fix liquidity * fix uniswap pool * change localnet chain params * fix lint * add cli query * add nil check * fix nil point * modify tests * eliminate nil pending nonce issue * fix query * set flags * Update e2e/runner/evm.go Co-authored-by: Charlie Chen <[email protected]> * add back other advanced tests * make generate * add comment * fix eth liquidity cap test * fix withdraw count --------- Co-authored-by: Charlie Chen <[email protected]> Co-authored-by: Charlie Chen <[email protected]> * removed incorrect Note * initiated rate limiter refactor and added metrics * added more unit tests and updated changelog * print more details from rate limiter output * reorder observer methods declaration * fix unit test * moved state irrelevant methods to types; renaming and cleaning * code indentation and update variable * replace with * fix unit test * added extra unit tests and improved func name --------- Co-authored-by: Lucas Bertrand <[email protected]> Co-authored-by: Tanmay <[email protected]>
- Loading branch information
1 parent
b460556
commit 7c98376
Showing
31 changed files
with
3,359 additions
and
620 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package math | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
// Percentage calculates the percentage of A over B. | ||
func Percentage(a, b *big.Int) *big.Float { | ||
// cannot calculate if either a or b is nil | ||
if a == nil || b == nil { | ||
return nil | ||
} | ||
|
||
// if a is zero, return nil to avoid division by zero | ||
if b.Cmp(big.NewInt(0)) == 0 { | ||
return nil | ||
} | ||
|
||
// convert a and b to big.Float | ||
floatA := new(big.Float).SetInt(a) | ||
floatB := new(big.Float).SetInt(b) | ||
|
||
// calculate the percentage of a over b | ||
percentage := new(big.Float).Quo(floatA, floatB) | ||
percentage.Mul(percentage, big.NewFloat(100)) | ||
|
||
return percentage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPercentage(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
numerator *big.Int | ||
denominator *big.Int | ||
percentage *big.Float | ||
fail bool | ||
}{ | ||
{ | ||
name: "positive percentage", | ||
numerator: big.NewInt(165), | ||
denominator: big.NewInt(1000), | ||
percentage: big.NewFloat(16.5), | ||
fail: false, | ||
}, | ||
{ | ||
name: "negative percentage", | ||
numerator: big.NewInt(-165), | ||
denominator: big.NewInt(1000), | ||
percentage: big.NewFloat(-16.5), | ||
fail: false, | ||
}, | ||
{ | ||
name: "zero denominator", | ||
numerator: big.NewInt(1), | ||
denominator: big.NewInt(0), | ||
percentage: nil, | ||
fail: true, | ||
}, | ||
{ | ||
name: "nil numerator", | ||
numerator: nil, | ||
denominator: big.NewInt(1000), | ||
percentage: nil, | ||
fail: true, | ||
}, | ||
{ | ||
name: "nil denominator", | ||
numerator: big.NewInt(165), | ||
denominator: nil, | ||
percentage: nil, | ||
fail: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
percentage := Percentage(tc.numerator, tc.denominator) | ||
fmt.Printf("percentage: %v\n", percentage) | ||
if tc.fail { | ||
require.Nil(t, percentage) | ||
} else { | ||
require.True(t, percentage.Cmp(tc.percentage) == 0) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.