Skip to content

Commit

Permalink
feat: add test for vesting
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Letang <[email protected]>
  • Loading branch information
jeremyletang committed Oct 18, 2024
1 parent afe6ce9 commit 5336c27
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 35 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
- [11519](https://github.com/vegaprotocol/vega/issues/11519) - Add fees to position API types.
- [11714](https://github.com/vegaprotocol/vega/issues/11714) - Improve `AMM` performance by caching best prices and volumes.
- [11642](https://github.com/vegaprotocol/vega/issues/11642) - `AMMs` with empty price levels are now allowed.
- [11685](https://github.com/vegaprotocol/vega/issues/11685) - Automated purchase support added.
- [11685](https://github.com/vegaprotocol/vega/issues/11685) - Automated purchase support added.
- [11726](https://github.com/vegaprotocol/vega/issues/11726) - Combined `AMM` uncrossing orders for better performance when uncrossing the book.
- [11711](https://github.com/vegaprotocol/vega/issues/11711) - Manage closed team membership by updating the allow list.
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.
- [11744](https://github.com/vegaprotocol/vega/issues/11744) - Staking from collateral bridged assets.

### 🐛 Fixes

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 @@ -392,7 +392,7 @@ func newServices(
svcs.activityStreak.OnEpochRestore,
)

svcs.vesting = vesting.NewSnapshotEngine(svcs.log, svcs.collateral, svcs.activityStreak, svcs.broker, svcs.assets, svcs.partiesEngine, svcs.timeService)
svcs.vesting = vesting.NewSnapshotEngine(svcs.log, svcs.collateral, svcs.activityStreak, svcs.broker, svcs.assets, svcs.partiesEngine, svcs.timeService, svcs.stakingAccounts)
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
2 changes: 2 additions & 0 deletions core/vesting/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func New(
assets Assets,
parties Parties,
t Time,
stakeAccounting StakeAccounting,
) *Engine {
log = log.Named(namedLogger)

Expand All @@ -133,6 +134,7 @@ func New(
state: map[string]*PartyRewards{},
rewardBonusMultiplierCache: map[string]MultiplierAndQuantBalance{},
t: t,
stakeAccounting: stakeAccounting,
}
}

Expand Down
23 changes: 23 additions & 0 deletions core/vesting/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vesting_test
import (
"context"
"testing"
"time"

"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/events"
Expand All @@ -31,6 +32,28 @@ import (
"github.com/stretchr/testify/require"
)

func TestAutomatedStaking(t *testing.T) {
v := getTestEngine(t)

ctx := context.Background()

require.NoError(t, v.OnRewardVestingBaseRateUpdate(ctx, num.MustDecimalFromString("0.9")))
require.NoError(t, v.OnRewardVestingMinimumTransferUpdate(ctx, num.MustDecimalFromString("1")))
require.NoError(t, v.OnStakingAssetUpdate(ctx, "ETH"))

// this is not the most useful test, but at least we know that on payment for the
// staking asset, the staking account are being notified of the amounts as new stake deposits
// via the calls to the mocks
t.Run("On add reward for the staking asset, stake accounting is called", func(t *testing.T) {
// one call to add event, and one call to broadcast it
// one for the time
v.stakeAccounting.EXPECT().AddEvent(gomock.Any(), gomock.Any()).Times(1)
v.broker.EXPECT().Send(gomock.Any()).Times(1)
v.t.EXPECT().GetTimeNow().Times(1).Return(time.Unix(0, 0))
v.AddReward(ctx, "party1", "ETH", num.NewUint(1000), 0)
})
}

func TestDistributeAfterDelay(t *testing.T) {
v := getTestEngine(t)

Expand Down
68 changes: 37 additions & 31 deletions core/vesting/helper_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ import (
type testEngine struct {
*vesting.Engine

ctrl *gomock.Controller
col *collateralMock
asvm *mocks.MockActivityStreakVestingMultiplier
broker *bmocks.MockBroker
assets *mocks.MockAssets
parties *mocks.MockParties
t *mocks.MockTime
ctrl *gomock.Controller
col *collateralMock
asvm *mocks.MockActivityStreakVestingMultiplier
broker *bmocks.MockBroker
assets *mocks.MockAssets
parties *mocks.MockParties
t *mocks.MockTime
stakeAccounting *mocks.MockStakeAccounting
}

func getTestEngine(t *testing.T) *testEngine {
Expand All @@ -59,31 +60,34 @@ func getTestEngine(t *testing.T) *testEngine {
assets := mocks.NewMockAssets(ctrl)
parties := mocks.NewMockParties(ctrl)
tim := mocks.NewMockTime(ctrl)
stakeAccounting := mocks.NewMockStakeAccounting(ctrl)

return &testEngine{
Engine: vesting.New(
logger, col, asvm, broker, assets, parties, tim,
logger, col, asvm, broker, assets, parties, tim, stakeAccounting,
),
ctrl: ctrl,
broker: broker,
col: col,
asvm: asvm,
assets: assets,
parties: parties,
t: tim,
ctrl: ctrl,
broker: broker,
col: col,
asvm: asvm,
assets: assets,
parties: parties,
t: tim,
stakeAccounting: stakeAccounting,
}
}

type testSnapshotEngine struct {
engine *vesting.SnapshotEngine

ctrl *gomock.Controller
col *collateralMock
asvm *mocks.MockActivityStreakVestingMultiplier
broker *bmocks.MockBroker
assets *mocks.MockAssets
parties *mocks.MockParties
t *mocks.MockTime
ctrl *gomock.Controller
col *collateralMock
asvm *mocks.MockActivityStreakVestingMultiplier
broker *bmocks.MockBroker
assets *mocks.MockAssets
parties *mocks.MockParties
t *mocks.MockTime
stakeAccounting *mocks.MockStakeAccounting

currentEpoch uint64
}
Expand All @@ -97,19 +101,21 @@ func newEngine(t *testing.T) *testSnapshotEngine {
assets := mocks.NewMockAssets(ctrl)
parties := mocks.NewMockParties(ctrl)
tim := mocks.NewMockTime(ctrl)
stakeAccounting := mocks.NewMockStakeAccounting(ctrl)

return &testSnapshotEngine{
engine: vesting.NewSnapshotEngine(
logging.NewTestLogger(), col, asvm, broker, assets, parties, tim,
logging.NewTestLogger(), col, asvm, broker, assets, parties, tim, stakeAccounting,
),
ctrl: ctrl,
col: col,
asvm: asvm,
broker: broker,
assets: assets,
parties: parties,
currentEpoch: 10,
t: tim,
ctrl: ctrl,
col: col,
asvm: asvm,
broker: broker,
assets: assets,
parties: parties,
currentEpoch: 10,
t: tim,
stakeAccounting: stakeAccounting,
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/vesting/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func NewSnapshotEngine(
assets Assets,
parties Parties,
t Time,
stakeAccounting StakeAccounting,
) *SnapshotEngine {
se := &SnapshotEngine{
Engine: New(log, c, asvm, broker, assets, parties, t),
Engine: New(log, c, asvm, broker, assets, parties, t, stakeAccounting),
}

return se
Expand Down

0 comments on commit 5336c27

Please sign in to comment.