Skip to content

Commit

Permalink
Merge pull request #11301 from vegaprotocol/feat-11264
Browse files Browse the repository at this point in the history
Vesting reward bonus multiplier for derived party ID
  • Loading branch information
karlem authored May 23, 2024
2 parents 46437ac + f48e448 commit cc310d7
Show file tree
Hide file tree
Showing 18 changed files with 2,579 additions and 1,993 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [11217](https://github.com/vegaprotocol/vega/issues/11217) - Allow market proposals to override risk factors.
- [11285](https://github.com/vegaprotocol/vega/issues/11285) - Add support for trading transaction ordering.
- [11282](https://github.com/vegaprotocol/vega/issues/11282) - Allow a party to withdraw rewards from an AMM vested account.
- [11301](https://github.com/vegaprotocol/vega/issues/11301) - Sum vesting reward bonus multiplier across all derived keys for party.

### 🐛 Fixes

Expand Down
2 changes: 1 addition & 1 deletion core/integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func newExecutionTestSetup() *executionTestSetup {
execsetup.activityStreak = activitystreak.New(execsetup.log, execsetup.executionEngine, execsetup.broker)
execsetup.epochEngine.NotifyOnEpoch(execsetup.activityStreak.OnEpochEvent, execsetup.activityStreak.OnEpochRestore)

execsetup.vesting = vesting.New(execsetup.log, execsetup.collateralEngine, execsetup.activityStreak, execsetup.broker, execsetup.assetsEngine)
execsetup.vesting = vesting.New(execsetup.log, execsetup.collateralEngine, execsetup.activityStreak, execsetup.broker, execsetup.assetsEngine, execsetup.profilesEngine)
execsetup.rewardsEngine = rewards.New(execsetup.log, rewards.NewDefaultConfig(), execsetup.broker, execsetup.delegationEngine, execsetup.epochEngine, execsetup.collateralEngine, execsetup.timeService, execsetup.marketActivityTracker, execsetup.topology, execsetup.vesting, execsetup.banking, execsetup.activityStreak)

// register this after the rewards engine is created to make sure the on epoch is called in the right order.
Expand Down
37 changes: 29 additions & 8 deletions core/parties/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"golang.org/x/exp/maps"
)

var (
Expand All @@ -50,11 +52,11 @@ func (e *Engine) AssignDeriveKey(party types.PartyID, derivedKey string) {
e.profiles[party] = &types.PartyProfile{
PartyID: party,
Metadata: map[string]string{},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
}
}

e.profiles[party].DerivedKeys = append(e.profiles[party].DerivedKeys, derivedKey)
e.profiles[party].DerivedKeys[derivedKey] = struct{}{}
}

func (e *Engine) CheckDerivedKeyOwnership(party types.PartyID, derivedKey string) bool {
Expand All @@ -63,7 +65,27 @@ func (e *Engine) CheckDerivedKeyOwnership(party types.PartyID, derivedKey string
return false
}

return slices.Contains(partyProfile.DerivedKeys, derivedKey)
_, ok = partyProfile.DerivedKeys[derivedKey]
return ok
}

// RelatedKeys returns all keys related to the specified key.
// If a derived key is provided, it returns all other derived keys and the party key.
// If a party key is provided, it returns all derived keys and the party key itself.
// The keys will be in an indeterminate order.
func (e *Engine) RelatedKeys(key string) (*types.PartyID, []string) {
profile, ok := e.profiles[types.PartyID(key)]
if ok {
return &profile.PartyID, maps.Keys(profile.DerivedKeys)
}

for _, profile := range e.profiles {
if _, ok := profile.DerivedKeys[key]; ok {
return &profile.PartyID, maps.Keys(profile.DerivedKeys)
}
}

return nil, nil
}

func (e *Engine) CheckSufficientBalanceToUpdateProfile(party types.PartyID, balance *num.Uint) error {
Expand All @@ -82,7 +104,7 @@ func (e *Engine) UpdateProfile(ctx context.Context, partyID types.PartyID, cmd *
if !exists {
profile = &types.PartyProfile{
PartyID: partyID,
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
}
e.profiles[partyID] = profile
}
Expand Down Expand Up @@ -111,10 +133,9 @@ func (e *Engine) loadPartiesFromSnapshot(partiesPayload *types.PayloadParties) {
profile.Metadata[m.Key] = m.Value
}

if profilePayload.DerivedKeys == nil {
profile.DerivedKeys = []string{}
} else {
profile.DerivedKeys = profilePayload.DerivedKeys
profile.DerivedKeys = map[string]struct{}{}
for _, val := range profilePayload.DerivedKeys {
profile.DerivedKeys[val] = struct{}{}
}

e.profiles[profile.PartyID] = profile
Expand Down
8 changes: 4 additions & 4 deletions core/parties/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ func TestUpdatingProfiles(t *testing.T) {
"key2": "value2",
"key3": "value3",
},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
},
{
PartyID: party2,
Alias: "test2",
Metadata: map[string]string{
"key1": "value1",
},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
},
}, te.engine.ListProfiles())

Expand Down Expand Up @@ -177,15 +177,15 @@ func TestAssigningDerivedKeys(t *testing.T) {
"key2": "value2",
"key3": "value3",
},
DerivedKeys: []string{"derivedKey1", "derivedKey2"},
DerivedKeys: map[string]struct{}{"derivedKey1": {}, "derivedKey2": {}},
},
{
PartyID: party2,
Alias: "test2",
Metadata: map[string]string{
"key1": "value1",
},
DerivedKeys: []string{"derivedKey3"},
DerivedKeys: map[string]struct{}{"derivedKey3": {}},
},
}, te.engine.ListProfiles())
}
8 changes: 4 additions & 4 deletions core/parties/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (e *SnapshottedEngine) serialiseParties() ([]byte, error) {
return strings.Compare(a.Key, b.Key)
})

profileSnapshot.DerivedKeys = append(profileSnapshot.DerivedKeys, profile.DerivedKeys...)
for k := range profile.DerivedKeys {
profileSnapshot.DerivedKeys = append(profileSnapshot.DerivedKeys, k)
}

// Ensure deterministic order among the derived keys.
slices.SortStableFunc(profileSnapshot.DerivedKeys, func(a, b string) int {
return strings.Compare(a, b)
})
slices.Sort(profileSnapshot.DerivedKeys)

profilesSnapshot = append(profilesSnapshot, profileSnapshot)
}
Expand Down
6 changes: 3 additions & 3 deletions core/parties/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
Metadata: map[string]string{
"key1": "value1",
},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
},
{
PartyID: party2,
Alias: "test2",
Metadata: map[string]string{
"key1": "value1",
},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
},
{
PartyID: party3,
Alias: "test3",
Metadata: map[string]string{
"key1": "value1",
},
DerivedKeys: []string{},
DerivedKeys: map[string]struct{}{},
},
}, te.engine.ListProfiles())
}
Expand Down
2 changes: 1 addition & 1 deletion core/protocol/all_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func newServices(
svcs.activityStreak.OnEpochRestore,
)

svcs.vesting = vesting.NewSnapshotEngine(svcs.log, svcs.collateral, svcs.activityStreak, svcs.broker, svcs.assets)
svcs.vesting = vesting.NewSnapshotEngine(svcs.log, svcs.collateral, svcs.activityStreak, svcs.broker, svcs.assets, svcs.partiesEngine)
svcs.timeService.NotifyOnTick(svcs.vesting.OnTick)
svcs.rewards = rewards.New(svcs.log, svcs.conf.Rewards, svcs.broker, svcs.delegation, svcs.epochService, svcs.collateral, svcs.timeService, svcs.marketActivityTracker, svcs.topology, svcs.vesting, svcs.banking, svcs.activityStreak)

Expand Down
7 changes: 4 additions & 3 deletions core/rewards/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/vesting"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/protos/vega"
Expand Down Expand Up @@ -89,7 +90,7 @@ type Teams interface {

type Vesting interface {
AddReward(party, asset string, amount *num.Uint, lockedForEpochs uint64)
GetRewardBonusMultiplier(party string) (num.Decimal, num.Decimal)
GetSingleAndSummedRewardBonusMultipliers(party string) (vesting.MultiplierAndQuantBalance, vesting.MultiplierAndQuantBalance)
}

type ActivityStreak interface {
Expand Down Expand Up @@ -357,8 +358,8 @@ func (e *Engine) convertTakerFeesToRewardAsset(takerFees map[string]*num.Uint, f

func (e *Engine) getRewardMultiplierForParty(party string) num.Decimal {
asMultiplier := e.activityStreak.GetRewardsDistributionMultiplier(party)
_, vsMultiplier := e.vesting.GetRewardBonusMultiplier(party)
return asMultiplier.Mul(vsMultiplier)
_, summed := e.vesting.GetSingleAndSummedRewardBonusMultipliers(party)
return asMultiplier.Mul(summed.Multiplier)
}

func filterEligible(ps []*types.PartyContributionScore) []*types.PartyContributionScore {
Expand Down
17 changes: 9 additions & 8 deletions core/rewards/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/types/party.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ type PartyProfile struct {
PartyID PartyID
Alias string
Metadata map[string]string
DerivedKeys []string
DerivedKeys map[string]struct{}
}
Loading

0 comments on commit cc310d7

Please sign in to comment.