From 1b5531588d4fd08b9033136bfb1eca59c5995524 Mon Sep 17 00:00:00 2001 From: 0xTopaz Date: Thu, 16 Jan 2025 21:53:25 +0900 Subject: [PATCH] fix: emission callback panic issue --- _deploy/r/gnoswap/gns/errors.gno | 5 +++-- _deploy/r/gnoswap/gns/gns.gno | 3 --- _deploy/r/gnoswap/gns/halving.gno | 11 +++++------ _deploy/r/gnoswap/gns/halving_test.gno | 14 +++++++++++++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/_deploy/r/gnoswap/gns/errors.gno b/_deploy/r/gnoswap/gns/errors.gno index af5bb4e8c..de2dc57a4 100644 --- a/_deploy/r/gnoswap/gns/errors.gno +++ b/_deploy/r/gnoswap/gns/errors.gno @@ -7,8 +7,9 @@ import ( ) var ( - errInvalidYear = errors.New("[GNOSWAP-GNS-001] invalid year") - errTooManyEmission = errors.New("[GNOSWAP-GNS-002] too many emission reward") + errInvalidYear = errors.New("[GNOSWAP-GNS-001] invalid year") + errTooManyEmission = errors.New("[GNOSWAP-GNS-002] too many emission reward") + errCallbackEmissionChangeIsNil = errors.New("[GNOSWAP-GNS-003] callback emission change is nil") ) func addDetailToError(err error, detail string) string { diff --git a/_deploy/r/gnoswap/gns/gns.gno b/_deploy/r/gnoswap/gns/gns.gno index d661978bf..039b215ee 100644 --- a/_deploy/r/gnoswap/gns/gns.gno +++ b/_deploy/r/gnoswap/gns/gns.gno @@ -205,9 +205,6 @@ func checkErr(err error) { // It calculates the amount of gns to mint for each halving year for block range. // It also handles the left emission amount if the current block range includes halving year end block. func calculateAmountToMint(fromHeight, toHeight int64) uint64 { - prevHeight := fromHeight - currentHeight := toHeight - // if toHeight is greater than emission end height, set toHeight to emission end height endH := GetEndHeight() if toHeight > endH { diff --git a/_deploy/r/gnoswap/gns/halving.gno b/_deploy/r/gnoswap/gns/halving.gno index ce89f1540..e93d64947 100644 --- a/_deploy/r/gnoswap/gns/halving.gno +++ b/_deploy/r/gnoswap/gns/halving.gno @@ -7,7 +7,6 @@ import ( "gno.land/p/demo/avl" "gno.land/p/demo/json" - "gno.land/r/gnoswap/v1/common" "gno.land/r/gnoswap/v1/consts" ) @@ -62,7 +61,6 @@ type HalvingData struct { amountPerBlock []uint64 } - func (h *HalvingData) getStartBlockHeight(year int64) int64 { if year == 0 { return 0 @@ -389,7 +387,10 @@ func setAvgBlockTimeInMs(ms int64) { // how much reward should be minted per block for current halving year adjustedAmountPerBlock := amountLeft / uint64(blockLeft) // update it - setAmountPerBlockPerHalvingYear(currentYear, adjustedAmountPerBlock) + setAmountPerBlockPerHalvingYear(currentYear, adjustedAmountPerBlock) + if callbackEmissionChange == nil { + panic(errCallbackEmissionChangeIsNil.Error()) + } callbackEmissionChange(adjustedAmountPerBlock) for year := HALVING_START_YEAR; year <= HALVING_END_YEAR; year++ { @@ -418,8 +419,6 @@ func setAvgBlockTimeInMs(ms int64) { } avgBlockTimeMs = ms - - } // GetAmountByHeight returns the amount of gns to mint by height @@ -605,4 +604,4 @@ func getHalvingYearAndEndTimestamp(timestamp int64) (int64, int64) { year += 1 // since we subtract startTimestamp at line 215, we need to add 1 to get the correct year return year, startTimestamp + (consts.TIMESTAMP_YEAR * year) -} \ No newline at end of file +} diff --git a/_deploy/r/gnoswap/gns/halving_test.gno b/_deploy/r/gnoswap/gns/halving_test.gno index 478fade0f..93b57fa51 100644 --- a/_deploy/r/gnoswap/gns/halving_test.gno +++ b/_deploy/r/gnoswap/gns/halving_test.gno @@ -156,36 +156,48 @@ func TestGetHalvingInfo(t *testing.T) { func TestSetAvgBlockTimeInMsByAdmin(t *testing.T) { t.Run("panic if caller is not admin", func(t *testing.T) { + oldCallback := callbackEmissionChange + callbackEmissionChange = func(amount uint64) {} uassert.PanicsWithMessage(t, "caller(g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm) has no permission", func() { SetAvgBlockTimeInMsByAdmin(1) }, ) + callbackEmissionChange = oldCallback }) t.Run("success if caller is admin", func(t *testing.T) { + oldCallback := callbackEmissionChange + callbackEmissionChange = func(amount uint64) {} std.TestSkipHeights(1) - std.TestSetRealm(adminRealm) + std.TestSetRealm(std.NewUserRealm(consts.ADMIN)) SetAvgBlockTimeInMsByAdmin(2) uassert.Equal(t, GetAvgBlockTimeInMs(), int64(2)) + callbackEmissionChange = oldCallback }) } func TestSetAvgBlockTimeInMs(t *testing.T) { t.Run("panic if caller is not governance contract", func(t *testing.T) { + oldCallback := callbackEmissionChange + callbackEmissionChange = func(amount uint64) {} uassert.PanicsWithMessage(t, "caller(g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm) has no permission", func() { SetAvgBlockTimeInMs(3) }, ) + callbackEmissionChange = oldCallback }) t.Run("success if caller is governance contract", func(t *testing.T) { + oldCallback := callbackEmissionChange + callbackEmissionChange = func(amount uint64) {} std.TestSkipHeights(3) std.TestSetRealm(govRealm) SetAvgBlockTimeInMs(4) uassert.Equal(t, GetAvgBlockTimeInMs(), int64(4)) + callbackEmissionChange = oldCallback }) }