Skip to content

Commit

Permalink
Merge branch 'master' into arwen-upgrade-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianMincu authored Jun 17, 2021
2 parents c2042f5 + 5180b53 commit 93d6a2d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion cmd/node/config/economics.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
TopUpFactor = 0.25 # fraction of value 0.25 - 25%

[[RewardsSettings.RewardsConfigByEpoch]]
EpochEnable = 2
EpochEnable = 4
LeaderPercentage = 0.1 #fraction of value 0.1 - 10%
DeveloperPercentage = 0.3 #fraction of value 0.3 - 30%
ProtocolSustainabilityPercentage = 0.1 #fraction of value 0.1 - 10%
Expand Down
3 changes: 1 addition & 2 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2198,8 +2198,7 @@ func newMetaBlockProcessor(
},

StakingDataProvider: stakingDataProvider,
TopUpRewardFactor: economicsData.RewardsTopUpFactor(),
TopUpGradientPoint: economicsData.RewardsTopUpGradientPoint(),
RewardsHandler: economicsData,
EconomicsDataProvider: economicsDataProvider,
EpochEnableV2: systemSCConfig.StakingSystemSCConfig.StakingV2Epoch,
}
Expand Down
6 changes: 2 additions & 4 deletions epochStart/metachain/rewardsCreatorProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type RewardsCreatorProxyArgs struct {
BaseRewardsCreatorArgs
StakingDataProvider epochStart.StakingDataProvider
EconomicsDataProvider epochStart.EpochEconomicsDataProvider
TopUpRewardFactor float64
TopUpGradientPoint *big.Int
RewardsHandler process.RewardsHandler
EpochEnableV2 uint32
}

Expand Down Expand Up @@ -184,8 +183,7 @@ func (rcp *rewardsCreatorProxy) createRewardsCreatorV2() (*rewardsCreatorV2, err
BaseRewardsCreatorArgs: rcp.args.BaseRewardsCreatorArgs,
StakingDataProvider: rcp.args.StakingDataProvider,
EconomicsDataProvider: rcp.args.EconomicsDataProvider,
TopUpRewardFactor: rcp.args.TopUpRewardFactor,
TopUpGradientPoint: rcp.args.TopUpGradientPoint,
RewardsHandler: rcp.args.RewardsHandler,
}

return NewRewardsCreatorV2(argsV2)
Expand Down
14 changes: 12 additions & 2 deletions epochStart/metachain/rewardsCreatorProxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ElrondNetwork/elrond-go/epochStart"
"github.com/ElrondNetwork/elrond-go/epochStart/mock"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/testscommon/economicsmocks"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -352,12 +353,21 @@ func createDefaultBlockBody() *block.Body {

func createDefaultRewardsCreatorProxyArgs() RewardsCreatorProxyArgs {
rewardsTopUpGradientPoint, _ := big.NewInt(0).SetString("3000000000000000000000000", 10)

rewardsHandler := &economicsmocks.EconomicsHandlerStub{
RewardsTopUpGradientPointCalled: func() *big.Int {
return rewardsTopUpGradientPoint
},
RewardsTopUpFactorCalled: func() float64 {
return 0.25
},
}

return RewardsCreatorProxyArgs{
BaseRewardsCreatorArgs: getBaseRewardsArguments(),
StakingDataProvider: &mock.StakingDataProviderStub{},
EconomicsDataProvider: NewEpochEconomicsStatistics(),
TopUpRewardFactor: 0.25,
TopUpGradientPoint: rewardsTopUpGradientPoint,
RewardsHandler: rewardsHandler,
}
}

Expand Down
33 changes: 20 additions & 13 deletions epochStart/metachain/rewardsV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ type RewardsCreatorArgsV2 struct {
BaseRewardsCreatorArgs
StakingDataProvider epochStart.StakingDataProvider
EconomicsDataProvider epochStart.EpochEconomicsDataProvider
TopUpRewardFactor float64
TopUpGradientPoint *big.Int
RewardsHandler process.RewardsHandler
}

type rewardsCreatorV2 struct {
*baseRewardsCreator
stakingDataProvider epochStart.StakingDataProvider
economicsDataProvider epochStart.EpochEconomicsDataProvider
topUpRewardFactor float64
topUpGradientPoint *big.Int
rewardsHandler process.RewardsHandler
}

// NewRewardsCreatorV2 creates a new rewards creator object
Expand All @@ -56,19 +54,15 @@ func NewRewardsCreatorV2(args RewardsCreatorArgsV2) (*rewardsCreatorV2, error) {
if check.IfNil(args.EconomicsDataProvider) {
return nil, epochStart.ErrNilEconomicsDataProvider
}
if args.TopUpGradientPoint.Cmp(zero) < 0 {
return nil, epochStart.ErrInvalidRewardsTopUpGradientPoint
}
if args.TopUpRewardFactor < 0 || args.TopUpRewardFactor > 1 {
return nil, epochStart.ErrInvalidRewardsTopUpFactor
if check.IfNil(args.RewardsHandler) {
return nil, epochStart.ErrNilRewardsHandler
}

rc := &rewardsCreatorV2{
baseRewardsCreator: brc,
economicsDataProvider: args.EconomicsDataProvider,
stakingDataProvider: args.StakingDataProvider,
topUpRewardFactor: args.TopUpRewardFactor,
topUpGradientPoint: args.TopUpGradientPoint,
rewardsHandler: args.RewardsHandler,
}

return rc, nil
Expand Down Expand Up @@ -387,21 +381,34 @@ func (rc *rewardsCreatorV2) computeTopUpRewards(totalToDistribute *big.Int, tota
return big.NewInt(0)
}

log.Debug("computeTopUpRewards", "totalToDistribute", totalToDistribute.String())
log.Debug("computeTopUpRewards", "totalTopUpEligible", totalTopUpEligible.String())

// k = c * economics.TotalToDistribute, c = top-up reward factor (constant)
k := core.GetIntTrimmedPercentageOfValue(totalToDistribute, rc.topUpRewardFactor)
k := core.GetIntTrimmedPercentageOfValue(totalToDistribute, rc.rewardsHandler.RewardsTopUpFactor())
log.Debug("computeTopUpRewards", "topUpFactor", rc.rewardsHandler.RewardsTopUpFactor())
log.Debug("computeTopUpRewards", "k", k.String())

// p is the cumulative eligible stake where rewards per day reach 1/2 of k (constant)
// x/p - argument for atan
totalTopUpEligibleFloat := big.NewFloat(0).SetInt(totalTopUpEligible)
topUpGradientPointFloat := big.NewFloat(0).SetInt(rc.topUpGradientPoint)
topUpGradientPointFloat := big.NewFloat(0).SetInt(rc.rewardsHandler.RewardsTopUpGradientPoint())
log.Debug("computeTopUpRewards", "topUpGradientPoint", rc.rewardsHandler.RewardsTopUpGradientPoint().String())

floatArg, _ := big.NewFloat(0).Quo(totalTopUpEligibleFloat, topUpGradientPointFloat).Float64()
log.Debug("computeTopUpRewards", "x/p", floatArg)

// atan(x/p)
res1 := math.Atan(floatArg)
log.Debug("computeTopUpRewards", "atan(x/p)", res1)
// 2*k/pi
res2 := big.NewFloat(0).SetInt(big.NewInt(0).Mul(k, big.NewInt(2)))
res2 = big.NewFloat(0).Quo(res2, big.NewFloat(math.Pi))
log.Debug("computeTopUpRewards", "2k/pi", res2)

// topUpReward:= (2*k/pi)*atan(x/p)
topUpRewards, _ := big.NewFloat(0).Mul(big.NewFloat(res1), res2).Int(nil)
log.Debug("computeTopUpRewards", "topUpRewards", topUpRewards.String())

return topUpRewards
}
Expand Down
57 changes: 27 additions & 30 deletions epochStart/metachain/rewardsV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ElrondNetwork/elrond-go/epochStart"
"github.com/ElrondNetwork/elrond-go/epochStart/mock"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/testscommon/economicsmocks"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -72,37 +73,15 @@ func TestNewRewardsCreator_NilEconomicsDataProvider(t *testing.T) {
require.Equal(t, epochStart.ErrNilEconomicsDataProvider, err)
}

func TestNewRewardsCreator_NegativeGradientPointShouldErr(t *testing.T) {
func TestNewRewardsCreator_NilRewardsHandlerShouldErr(t *testing.T) {
t.Parallel()

args := getRewardsCreatorV2Arguments()
args.TopUpGradientPoint = big.NewInt(-1)
args.RewardsHandler = nil

rwd, err := NewRewardsCreatorV2(args)
require.True(t, check.IfNil(rwd))
require.Equal(t, epochStart.ErrInvalidRewardsTopUpGradientPoint, err)
}

func TestNewRewardsCreator_NegativeTopUpRewardFactorShouldErr(t *testing.T) {
t.Parallel()

args := getRewardsCreatorV2Arguments()
args.TopUpRewardFactor = -1

rwd, err := NewRewardsCreatorV2(args)
require.True(t, check.IfNil(rwd))
require.Equal(t, epochStart.ErrInvalidRewardsTopUpFactor, err)
}

func TestNewRewardsCreator_SupraUnitaryTopUpRewardFactorShouldErr(t *testing.T) {
t.Parallel()

args := getRewardsCreatorV2Arguments()
args.TopUpRewardFactor = 1.5

rwd, err := NewRewardsCreatorV2(args)
require.True(t, check.IfNil(rwd))
require.Equal(t, epochStart.ErrInvalidRewardsTopUpFactor, err)
require.Equal(t, epochStart.ErrNilRewardsHandler, err)
}

func TestNewRewardsCreatorOK(t *testing.T) {
Expand Down Expand Up @@ -588,7 +567,7 @@ func TestNewRewardsCreatorV2_computeTopUpRewards(t *testing.T) {
require.NotNil(t, rwd)

totalToDistribute, _ := big.NewInt(0).SetString("3000000000000000000000", 10)
topUpRewardsLimit := core.GetApproximatePercentageOfValue(totalToDistribute, rwd.topUpRewardFactor)
topUpRewardsLimit := core.GetApproximatePercentageOfValue(totalToDistribute, rwd.rewardsHandler.RewardsTopUpFactor())

totalTopUpEligible, _ := big.NewInt(0).SetString("2000000000000000000000000", 10)
topUpRewards := rwd.computeTopUpRewards(totalToDistribute, totalTopUpEligible)
Expand Down Expand Up @@ -1789,23 +1768,41 @@ func TestNewRewardsCreatorV2_CreateRewardsMiniBlocks2169Nodes(t *testing.T) {

func getRewardsCreatorV2Arguments() RewardsCreatorArgsV2 {
rewardsTopUpGradientPoint, _ := big.NewInt(0).SetString("3000000000000000000000000", 10)
topUpRewardFactor := 0.25

rewardsHandler := &economicsmocks.EconomicsHandlerStub{
RewardsTopUpGradientPointCalled: func() *big.Int {
return big.NewInt(0).Set(rewardsTopUpGradientPoint)
},
RewardsTopUpFactorCalled: func() float64 {
return topUpRewardFactor
},
}
return RewardsCreatorArgsV2{
BaseRewardsCreatorArgs: getBaseRewardsArguments(),
StakingDataProvider: &mock.StakingDataProviderStub{},
EconomicsDataProvider: NewEpochEconomicsStatistics(),
TopUpRewardFactor: 0.25,
TopUpGradientPoint: rewardsTopUpGradientPoint,
RewardsHandler: rewardsHandler,
}
}

func getRewardsCreatorV35Arguments() RewardsCreatorArgsV2 {
rewardsTopUpGradientPoint, _ := big.NewInt(0).SetString("2000000000000000000000000", 10)
topUpRewardFactor := 0.5

rewardsHandler := &economicsmocks.EconomicsHandlerStub{
RewardsTopUpGradientPointCalled: func() *big.Int {
return big.NewInt(0).Set(rewardsTopUpGradientPoint)
},
RewardsTopUpFactorCalled: func() float64 {
return topUpRewardFactor
},
}
return RewardsCreatorArgsV2{
BaseRewardsCreatorArgs: getBaseRewardsArguments(),
StakingDataProvider: &mock.StakingDataProviderStub{},
EconomicsDataProvider: NewEpochEconomicsStatistics(),
TopUpRewardFactor: 0.5,
TopUpGradientPoint: rewardsTopUpGradientPoint,
RewardsHandler: rewardsHandler,
}
}

Expand Down
3 changes: 1 addition & 2 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1915,8 +1915,7 @@ func (tpn *TestProcessorNode) initBlockProcessor(stateCheckpointModulus uint) {
UserAccountsDB: tpn.AccntState,
},
StakingDataProvider: stakingDataProvider,
TopUpGradientPoint: tpn.EconomicsData.RewardsTopUpGradientPoint(),
TopUpRewardFactor: tpn.EconomicsData.RewardsTopUpFactor(),
RewardsHandler: tpn.EconomicsData,
EconomicsDataProvider: economicsDataProvider,
EpochEnableV2: StakingV2Epoch,
}
Expand Down

0 comments on commit 93d6a2d

Please sign in to comment.