-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for emission balancer actions and adding rows to dat…
…aset
- Loading branch information
Showing
13 changed files
with
1,401 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (C) 2024, Nuklai. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/nuklai/nuklaivm/emission" | ||
"github.com/nuklai/nuklaivm/storage" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/chain/chaintest" | ||
"github.com/ava-labs/hypersdk/codec/codectest" | ||
"github.com/ava-labs/hypersdk/state" | ||
) | ||
|
||
func TestClaimDelegationStakeRewardsActionFailure(t *testing.T) { | ||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 10, StakeRewards: 20}) | ||
|
||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
tests := []chaintest.ActionTest{ | ||
{ | ||
Name: "StakeMissing", | ||
Actor: actor, | ||
Action: &ClaimDelegationStakeRewards{ | ||
NodeID: nodeID, // Non-existent stake | ||
}, | ||
State: chaintest.NewInMemoryStore(), | ||
ExpectedErr: ErrStakeMissing, | ||
}, | ||
{ | ||
Name: "StakeNotStarted", | ||
Actor: actor, | ||
Action: &ClaimDelegationStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
State: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set stake with end block greater than the current block height | ||
require.NoError(t, storage.SetDelegatorStake(context.Background(), store, actor, nodeID, 25, 50, 1000, actor)) | ||
return store | ||
}(), | ||
ExpectedErr: ErrStakeNotStarted, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt.Run(context.Background(), t) | ||
} | ||
} | ||
|
||
func TestClaimDelegationStakeRewardsActionSuccess(t *testing.T) { | ||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20}) | ||
|
||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
tests := []chaintest.ActionTest{ | ||
{ | ||
Name: "ValidClaim", | ||
ActionID: ids.GenerateTestID(), | ||
Actor: actor, | ||
Action: &ClaimDelegationStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
State: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set stake with end block less than the current block height | ||
require.NoError(t, storage.SetDelegatorStake(context.Background(), store, actor, nodeID, 25, 50, 1000, actor)) | ||
return store | ||
}(), | ||
Assertion: func(ctx context.Context, t *testing.T, store state.Mutable) { | ||
// Check if balance is correctly updated | ||
balance, err := storage.GetAssetAccountBalanceNoController(ctx, store, storage.NAIAddress, actor) | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(20), balance) | ||
|
||
// Check if the stake was claimed correctly | ||
exists, _, _, _, rewardAddress, _, _ := storage.GetDelegatorStakeNoController(ctx, store, actor, nodeID) | ||
require.True(t, exists) | ||
require.Equal(t, actor, rewardAddress) | ||
}, | ||
ExpectedOutputs: &ClaimDelegationStakeRewardsResult{ | ||
StakeStartBlock: 25, | ||
StakeEndBlock: 50, | ||
StakedAmount: 1000, | ||
BalanceBeforeClaim: 0, | ||
BalanceAfterClaim: 20, | ||
DistributedTo: actor, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt.Run(context.Background(), t) | ||
} | ||
} | ||
|
||
// BenchmarkClaimDelegationStakeRewards remains unchanged. | ||
func BenchmarkClaimDelegationStakeRewards(b *testing.B) { | ||
require := require.New(b) | ||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20}) | ||
|
||
claimStakeRewardsBenchmark := &chaintest.ActionBenchmark{ | ||
Name: "ClaimStakeRewardsBenchmark", | ||
Actor: actor, | ||
Action: &ClaimDelegationStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
CreateState: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set stake with end block less than the current block height | ||
require.NoError(storage.SetDelegatorStake(context.Background(), store, actor, nodeID, 25, 50, 1000, actor)) | ||
return store | ||
}, | ||
Assertion: func(ctx context.Context, b *testing.B, store state.Mutable) { | ||
// Check if balance is correctly updated | ||
balance, err := storage.GetAssetAccountBalanceNoController(ctx, store, storage.NAIAddress, actor) | ||
require.NoError(err) | ||
require.Equal(b, uint64(20), balance) | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
claimStakeRewardsBenchmark.Run(ctx, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright (C) 2024, Nuklai. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/nuklai/nuklaivm/emission" | ||
"github.com/nuklai/nuklaivm/storage" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/chain/chaintest" | ||
"github.com/ava-labs/hypersdk/codec/codectest" | ||
"github.com/ava-labs/hypersdk/state" | ||
) | ||
|
||
func TestClaimValidatorStakeRewardsActionFailure(t *testing.T) { | ||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 10, StakeRewards: 20}) | ||
|
||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
tests := []chaintest.ActionTest{ | ||
{ | ||
Name: "StakeMissing", | ||
Actor: actor, | ||
Action: &ClaimValidatorStakeRewards{ | ||
NodeID: nodeID, // Non-existent stake | ||
}, | ||
State: chaintest.NewInMemoryStore(), | ||
ExpectedErr: ErrStakeMissing, | ||
}, | ||
{ | ||
Name: "StakeNotStarted", | ||
Actor: actor, | ||
Action: &ClaimValidatorStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
State: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set validator stake with end block greater than the current block height | ||
require.NoError(t, storage.SetValidatorStake(context.Background(), store, nodeID, 25, 50, 5000, 10, actor, actor)) | ||
return store | ||
}(), | ||
ExpectedErr: ErrStakeNotStarted, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt.Run(context.Background(), t) | ||
} | ||
} | ||
|
||
func TestClaimValidatorStakeRewardsActionSuccess(t *testing.T) { | ||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20}) | ||
|
||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
tests := []chaintest.ActionTest{ | ||
{ | ||
Name: "ValidClaim", | ||
ActionID: ids.GenerateTestID(), | ||
Actor: actor, | ||
Action: &ClaimValidatorStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
State: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set validator stake with end block less than the current block height | ||
require.NoError(t, storage.SetValidatorStake(context.Background(), store, nodeID, 25, 50, emission.GetStakingConfig().MinValidatorStake, 10, actor, actor)) | ||
// Set the balance for the validator | ||
require.NoError(t, storage.SetAssetAccountBalance(context.Background(), store, storage.NAIAddress, actor, 0)) | ||
return store | ||
}(), | ||
Assertion: func(ctx context.Context, t *testing.T, store state.Mutable) { | ||
// Check if balance is correctly updated | ||
balance, err := storage.GetAssetAccountBalanceNoController(ctx, store, storage.NAIAddress, actor) | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(20), balance) | ||
|
||
// Check if the stake still exists after claiming rewards | ||
exists, _, _, _, _, rewardAddress, _, _ := storage.GetValidatorStakeNoController(ctx, store, nodeID) | ||
require.True(t, exists) | ||
require.Equal(t, actor, rewardAddress) | ||
}, | ||
ExpectedOutputs: &ClaimValidatorStakeRewardsResult{ | ||
StakeStartBlock: 25, | ||
StakeEndBlock: 50, | ||
StakedAmount: emission.GetStakingConfig().MinValidatorStake, | ||
DelegationFeeRate: 10, | ||
BalanceBeforeClaim: 0, | ||
BalanceAfterClaim: 20, | ||
DistributedTo: actor, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt.Run(context.Background(), t) | ||
} | ||
} | ||
|
||
func BenchmarkClaimValidatorStakeRewards(b *testing.B) { | ||
require := require.New(b) | ||
actor := codectest.NewRandomAddress() | ||
nodeID := ids.GenerateTestNodeID() | ||
|
||
emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20}) | ||
|
||
claimValidatorStakeRewardsBenchmark := &chaintest.ActionBenchmark{ | ||
Name: "ClaimValidatorStakeRewardsBenchmark", | ||
Actor: actor, | ||
Action: &ClaimValidatorStakeRewards{ | ||
NodeID: nodeID, | ||
}, | ||
CreateState: func() state.Mutable { | ||
store := chaintest.NewInMemoryStore() | ||
// Set validator stake with end block less than the current block height | ||
require.NoError(storage.SetValidatorStake(context.Background(), store, nodeID, 25, 50, emission.GetStakingConfig().MinValidatorStake, 10, actor, actor)) | ||
// Set the balance for the validator | ||
require.NoError(storage.SetAssetAccountBalance(context.Background(), store, storage.NAIAddress, actor, 0)) | ||
return store | ||
}, | ||
Assertion: func(ctx context.Context, b *testing.B, store state.Mutable) { | ||
// Check if balance is correctly updated after claiming rewards | ||
balance, err := storage.GetAssetAccountBalanceNoController(ctx, store, storage.NAIAddress, actor) | ||
require.NoError(err) | ||
require.Equal(b, uint64(20), balance) // Reward amount set by emission instance | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
claimValidatorStakeRewardsBenchmark.Run(ctx, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.