Skip to content

Commit

Permalink
made the mining boost flow require the exact amount expected in the t…
Browse files Browse the repository at this point in the history
…ransaction; added a 4 digit random number at the end of the price users must pay
  • Loading branch information
ice-ares committed Jun 20, 2024
1 parent d93cd85 commit c072e08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ require (
github.com/ethereum/go-ethereum v1.14.5
github.com/goccy/go-json v0.10.3
github.com/hashicorp/go-multierror v1.1.1
github.com/ice-blockchain/eskimo v1.327.0
github.com/ice-blockchain/eskimo v1.328.0
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb
github.com/ice-blockchain/wintr v1.141.0
github.com/ice-blockchain/wintr v1.142.0
github.com/imroc/req/v3 v3.43.7
github.com/oklog/ulid/v2 v2.1.0
github.com/pkg/errors v0.9.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ice-blockchain/eskimo v1.327.0 h1:6NS4+DaTQaJl1cIdwYj5nzDRbBUhseOWewJUUTTgfbo=
github.com/ice-blockchain/eskimo v1.327.0/go.mod h1:KSzTrn3HCDjwPZKk30NFi5S2vpcyJRJzfVFGaz6mTPQ=
github.com/ice-blockchain/eskimo v1.328.0 h1:/JN5sPSVJkDlM0I0S4wZIfKAOSv32hJ8oF0Otme6uxg=
github.com/ice-blockchain/eskimo v1.328.0/go.mod h1:KSzTrn3HCDjwPZKk30NFi5S2vpcyJRJzfVFGaz6mTPQ=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb h1:8TnFP3mc7O+tc44kv2e0/TpZKnEVUaKH+UstwfBwRkk=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb/go.mod h1:ZsQU7i3mxhgBBu43Oev7WPFbIjP4TniN/b1UPNGbrq8=
github.com/ice-blockchain/wintr v1.141.0 h1:JkQkjXoXLmnLh3hoRzr1SbiVXvdzNaWrkHjRxQqS1yQ=
github.com/ice-blockchain/wintr v1.141.0/go.mod h1:6INwquKAzF28T1DwAh/n3fPTpuyGQQz9cSUbDjxSCAY=
github.com/ice-blockchain/wintr v1.142.0 h1:pojlgGyNsbcMh3Hv8v0tD7ahsekneYyeU4UZs5tINnw=
github.com/ice-blockchain/wintr v1.142.0/go.mod h1:7pWkpbd52lS8dc/9FRSzT9M6uyfKAZOYvQoP8YidLnw=
github.com/imroc/req/v3 v3.43.7 h1:dOcNb9n0X83N5/5/AOkiU+cLhzx8QFXjv5MhikazzQA=
github.com/imroc/req/v3 v3.43.7/go.mod h1:SQIz5iYop16MJxbo8ib+4LnostGCok8NQf8ToyQc2xA=
github.com/ip2location/ip2location-go/v9 v9.7.0 h1:ipwl67HOWcrw+6GOChkEXcreRQR37NabqBd2ayYa4Q0=
Expand Down
41 changes: 26 additions & 15 deletions tokenomics/mining_boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ package tokenomics

import (
"context"
"crypto/rand"
"fmt"
"math"
"math/big"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
stdlibtime "time"

Expand Down Expand Up @@ -100,7 +102,8 @@ func (r *repository) InitializeMiningBoostUpgrade(ctx context.Context, miningBoo
upgradePrice := (*r.cfg.MiningBoost.levels.Load())[miningBoostLevelIndex].icePrice - previousLevelPrice
storedPrice := strconv.FormatFloat(upgradePrice, 'f', miningBoostPricePrecision, 64)
key := fmt.Sprintf("mining_boost_upgrades:%v", id)
val := fmt.Sprintf("%v:%v", miningBoostLevelIndex, storedPrice)
randomPart := fmt.Sprint(random(4))
val := fmt.Sprintf("%v:%v", miningBoostLevelIndex, storedPrice+randomPart)
result, err := r.db.Set(ctx, key, val, r.cfg.MiningBoost.SessionLength).Result()
if err != nil {
return nil, errors.Wrapf(err, "failed to set new mining_boost_upgrade for userID:%v", userID)
Expand All @@ -111,7 +114,7 @@ func (r *repository) InitializeMiningBoostUpgrade(ctx context.Context, miningBoo
icePrice := strconv.FormatFloat(upgradePrice*(1+(float64(r.cfg.MiningBoost.PriceDelta)/100)), 'f', miningBoostPricePrecision, 64)
return &PendingMiningBoostUpgrade{
ExpiresAt: time.New(stdlibtime.Now().Add(r.cfg.MiningBoost.SessionLength)),
ICEPrice: icePrice,
ICEPrice: icePrice + randomPart,
PaymentAddress: r.cfg.MiningBoost.PaymentAddress,
}, nil
}
Expand Down Expand Up @@ -181,21 +184,10 @@ func (r *repository) FinalizeMiningBoostUpgrade(ctx context.Context, network Blo
amount += *res[0].MiningBoostAmountBurnt
}
var newMiningBoostLevelIndex *model.FlexibleUint64 = nil
if icePrice-burntAmount <= 0 {
if icePrice-burntAmount == 0 {
targetLevel := model.FlexibleUint64(miningBoostLevelIndex)
newMiningBoostLevelIndex = &targetLevel
}
if extraBurntAmount := burntAmount - icePrice; extraBurntAmount > 0 {
for ix, level := range *r.cfg.MiningBoost.levels.Load() {
if ix > int(miningBoostLevelIndex) {
extraBurntAmount -= level.icePrice - (*r.cfg.MiningBoost.levels.Load())[ix-1].icePrice
}
if extraBurntAmount >= 0 {
extraLevel := model.FlexibleUint64(ix)
newMiningBoostLevelIndex = &extraLevel
}
}
}
var prestakingBonus, prestakingAllocation float64
switch {
case newMiningBoostLevelIndex != nil:
Expand Down Expand Up @@ -253,7 +245,7 @@ func (r *repository) FinalizeMiningBoostUpgrade(ctx context.Context, network Blo
}
}

if icePrice-burntAmount <= 0 {
if icePrice-burntAmount == 0 {
return nil, nil
}
initiallyProposedToPayAmount := icePrice * (1 + (float64(r.cfg.MiningBoost.PriceDelta) / 100))
Expand Down Expand Up @@ -396,3 +388,22 @@ func (r *repository) buildMiningBoostLevels() *[]*MiningBoostLevel {

return &levels
}

var secureRandomMx sync.Mutex

func random(digits int) uint64 {
secureRandomMx.Lock()
defer secureRandomMx.Unlock()

minVal := big.NewInt(int64(1))
for i := 1; i < digits; i++ {
minVal = minVal.Mul(minVal, big.NewInt(10))
}
maxVal := big.NewInt(0).Mul(minVal, big.NewInt(10))
rangeVal := big.NewInt(0).Sub(maxVal, minVal)

rnd, err := rand.Int(rand.Reader, rangeVal)
log.Panic(err)

return rnd.Uint64() + minVal.Uint64()
}

0 comments on commit c072e08

Please sign in to comment.