Skip to content

Commit

Permalink
Split up the test cases for failure and success
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Oct 2, 2024
1 parent 3917fa6 commit 5cca241
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 60 deletions.
48 changes: 27 additions & 21 deletions actions/claim_delegation_stake_rewards_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (C) 2024, Nuklai. All rights reserved.
// See the file LICENSE for licensing terms.

package actions

import (
Expand All @@ -17,12 +14,8 @@ import (
"github.com/ava-labs/hypersdk/state"
)

func TestClaimDelegationStakeRewardsAction(t *testing.T) {
// Mock emission balancer instance
_, err := emission.MockNewEmission(&emission.MockEmission{
LastAcceptedBlockHeight: 50, // Mock block height
StakeRewards: 10, // Mock stake rewards
})
func TestClaimDelegationStakeRewardsActionFailure(t *testing.T) {
_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 10, StakeRewards: 20})
require.NoError(t, err)

addr := codectest.NewRandomAddress()
Expand All @@ -47,11 +40,26 @@ func TestClaimDelegationStakeRewardsAction(t *testing.T) {
State: func() state.Mutable {
store := chaintest.NewInMemoryStore()
// Set stake with end block greater than the current block height
require.NoError(t, storage.SetDelegateUserStake(context.Background(), store, addr, nodeID, 101, 150, 1000, addr))
require.NoError(t, storage.SetDelegateUserStake(context.Background(), store, addr, nodeID, 25, 50, 1000, addr))
return store
}(),
ExpectedErr: ErrStakeNotStarted,
},
}

for _, tt := range tests {
tt.Run(context.Background(), t)
}
}

func TestClaimDelegationStakeRewardsActionSuccess(t *testing.T) {
_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20})
require.NoError(t, err)

addr := codectest.NewRandomAddress()
nodeID := ids.GenerateTestNodeID()

tests := []chaintest.ActionTest{
{
Name: "ValidClaim",
ActionID: ids.GenerateTestID(),
Expand All @@ -62,26 +70,26 @@ func TestClaimDelegationStakeRewardsAction(t *testing.T) {
State: func() state.Mutable {
store := chaintest.NewInMemoryStore()
// Set stake with end block less than the current block height
require.NoError(t, storage.SetDelegateUserStake(context.Background(), store, addr, nodeID, 50, 75, 1000, addr))
require.NoError(t, storage.SetDelegateUserStake(context.Background(), store, addr, nodeID, 25, 50, 1000, addr))
return store
}(),
Assertion: func(ctx context.Context, t *testing.T, store state.Mutable) {
// Check if balance is correctly updated
balance, err := storage.GetBalance(ctx, store, addr, ids.Empty)
require.NoError(t, err)
require.Equal(t, uint64(10), balance) // Reward amount set by emission instance
require.Equal(t, uint64(20), balance)

// Check if the stake was claimed correctly
exists, _, _, _, rewardAddress, _, _ := storage.GetDelegateUserStake(ctx, store, addr, nodeID)
require.True(t, exists)
require.Equal(t, addr, rewardAddress)
},
ExpectedOutputs: &ClaimDelegationStakeRewardsResult{
StakeStartBlock: 50,
StakeEndBlock: 75,
StakeStartBlock: 25,
StakeEndBlock: 50,
StakedAmount: 1000,
BalanceBeforeClaim: 0,
BalanceAfterClaim: 10,
BalanceAfterClaim: 20,
DistributedTo: addr,
},
},
Expand All @@ -92,15 +100,13 @@ func TestClaimDelegationStakeRewardsAction(t *testing.T) {
}
}

// BenchmarkClaimDelegationStakeRewards remains unchanged.
func BenchmarkClaimDelegationStakeRewards(b *testing.B) {
require := require.New(b)
actor := codectest.NewRandomAddress()
nodeID := ids.GenerateTestNodeID()

// Mock emission balancer instance
_, err := emission.MockNewEmission(&emission.MockEmission{
LastAcceptedBlockHeight: 100, // Mock block height
})
_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20})
require.NoError(err)

claimStakeRewardsBenchmark := &chaintest.ActionBenchmark{
Expand All @@ -112,14 +118,14 @@ func BenchmarkClaimDelegationStakeRewards(b *testing.B) {
CreateState: func() state.Mutable {
store := chaintest.NewInMemoryStore()
// Set stake with end block less than the current block height
require.NoError(storage.SetDelegateUserStake(context.Background(), store, actor, nodeID, 50, 75, 1000, actor))
require.NoError(storage.SetDelegateUserStake(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.GetBalance(ctx, store, actor, ids.Empty)
require.NoError(err)
require.Equal(b, uint64(10), balance) // Reward amount set by emission instance
require.Equal(b, uint64(20), balance)
},
}

Expand Down
141 changes: 141 additions & 0 deletions actions/claim_validator_stake_rewards_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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) {
_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 10, StakeRewards: 20})
require.NoError(t, err)

addr := codectest.NewRandomAddress()
nodeID := ids.GenerateTestNodeID()

tests := []chaintest.ActionTest{
{
Name: "StakeMissing",
Actor: addr,
Action: &ClaimValidatorStakeRewards{
NodeID: nodeID, // Non-existent stake
},
State: chaintest.NewInMemoryStore(),
ExpectedErr: ErrStakeMissing,
},
{
Name: "StakeNotStarted",
Actor: addr,
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.SetRegisterValidatorStake(context.Background(), store, nodeID, 25, 50, 5000, 10, addr, addr))
return store
}(),
ExpectedErr: ErrStakeNotStarted,
},
}

for _, tt := range tests {
tt.Run(context.Background(), t)
}
}

func TestClaimValidatorStakeRewardsActionSuccess(t *testing.T) {
_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20})
require.NoError(t, err)

addr := codectest.NewRandomAddress()
nodeID := ids.GenerateTestNodeID()

tests := []chaintest.ActionTest{
{
Name: "ValidClaim",
ActionID: ids.GenerateTestID(),
Actor: addr,
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.SetRegisterValidatorStake(context.Background(), store, nodeID, 25, 50, emission.GetStakingConfig().MinValidatorStake, 10, addr, addr))
// Set the balance for the validator
require.NoError(t, storage.SetBalance(context.Background(), store, addr, ids.Empty, 0))
return store
}(),
Assertion: func(ctx context.Context, t *testing.T, store state.Mutable) {
// Check if balance is correctly updated
balance, err := storage.GetBalance(ctx, store, addr, ids.Empty)
require.NoError(t, err)
require.Equal(t, uint64(20), balance)

// Check if the stake still exists after claiming rewards
exists, _, _, _, _, rewardAddress, _, _ := storage.GetRegisterValidatorStake(ctx, store, nodeID)
require.True(t, exists)
require.Equal(t, addr, rewardAddress)
},
ExpectedOutputs: &ClaimValidatorStakeRewardsResult{
StakeStartBlock: 25,
StakeEndBlock: 50,
StakedAmount: emission.GetStakingConfig().MinValidatorStake,
DelegationFeeRate: 10,
BalanceBeforeClaim: 0,
BalanceAfterClaim: 20,
DistributedTo: addr,
},
},
}

for _, tt := range tests {
tt.Run(context.Background(), t)
}
}

func BenchmarkClaimValidatorStakeRewards(b *testing.B) {
require := require.New(b)
actor := codectest.NewRandomAddress()
nodeID := ids.GenerateTestNodeID()

_, err := emission.MockNewEmission(&emission.MockEmission{LastAcceptedBlockHeight: 51, StakeRewards: 20})
require.NoError(err)

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.SetRegisterValidatorStake(context.Background(), store, nodeID, 25, 50, emission.GetStakingConfig().MinValidatorStake, 10, actor, actor))
// Set the balance for the validator
require.NoError(storage.SetBalance(context.Background(), store, actor, ids.Empty, 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.GetBalance(ctx, store, actor, ids.Empty)
require.NoError(err)
require.Equal(b, uint64(20), balance) // Reward amount set by emission instance
},
}

ctx := context.Background()
claimValidatorStakeRewardsBenchmark.Run(ctx, b)
}
4 changes: 0 additions & 4 deletions actions/delegate_user_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package actions
import (
"context"
"errors"
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/nuklai/nuklaivm/emission"
Expand Down Expand Up @@ -90,9 +89,6 @@ func (s *DelegateUserStake) Execute(
emissionInstance := emission.GetEmission()

// Check if stakeStartBlock is smaller than the current block height
fmt.Println(emissionInstance.GetLastAcceptedBlockHeight())
fmt.Println(s.StakeStartBlock)
fmt.Println(stakeEndBlock)
if s.StakeStartBlock < emissionInstance.GetLastAcceptedBlockHeight() || s.StakeStartBlock >= stakeEndBlock {
return nil, ErrInvalidStakeStartBlock
}
Expand Down
Loading

0 comments on commit 5cca241

Please sign in to comment.