Skip to content

Commit

Permalink
fix: emission callback panic issue
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 16, 2025
1 parent 7f845bc commit 1b55315
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 3 additions & 2 deletions _deploy/r/gnoswap/gns/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions _deploy/r/gnoswap/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions _deploy/r/gnoswap/gns/halving.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -62,7 +61,6 @@ type HalvingData struct {
amountPerBlock []uint64
}


func (h *HalvingData) getStartBlockHeight(year int64) int64 {
if year == 0 {
return 0
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -418,8 +419,6 @@ func setAvgBlockTimeInMs(ms int64) {
}

avgBlockTimeMs = ms


}

// GetAmountByHeight returns the amount of gns to mint by height
Expand Down Expand Up @@ -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)
}
}
14 changes: 13 additions & 1 deletion _deploy/r/gnoswap/gns/halving_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

0 comments on commit 1b55315

Please sign in to comment.