|
4 | 4 | "bytes" |
5 | 5 | "encoding/hex" |
6 | 6 | "errors" |
| 7 | + "fmt" |
7 | 8 | "math" |
8 | 9 | "testing" |
9 | 10 |
|
@@ -5503,3 +5504,142 @@ func Test_ClaimStaking_LoadAccountError(t *testing.T) { |
5503 | 5504 | require.Error(t, err) |
5504 | 5505 | assert.Equal(t, transaction.Transaction_LoadAccountError, code) |
5505 | 5506 | } |
| 5507 | + |
| 5508 | +func Test_ClaimStaking_MaxSupplyExceeded(t *testing.T) { |
| 5509 | + accountsKapp := setupAccountsKapp(t, config.EnableEpochs{}) |
| 5510 | + |
| 5511 | + receiptsCtx := &commonMock.ReceiptsContextStub{} |
| 5512 | + ctx := &commonMock.KAppContextStub{ |
| 5513 | + ReceiptsCalled: func() kapp.ReceiptsContext { return receiptsCtx }, |
| 5514 | + ContractIDCalled: func() int { return 1 }, |
| 5515 | + BlockCalled: func() *block.Block { |
| 5516 | + return &block.Block{Header: &block.BlockHeader{Timestamp: 1000, Epoch: 1}} |
| 5517 | + }, |
| 5518 | + } |
| 5519 | + |
| 5520 | + _ = accountsKapp.SetKAppController(&kvmStub.KAppControllerStub{ |
| 5521 | + GetCurrentKAppContextCalled: func() kapp.KappContext { return ctx }, |
| 5522 | + GetKDAKAppCalled: func() kapp.KDAKapp { |
| 5523 | + return &kvmStub.KDAKappStub{ |
| 5524 | + GetStakingCalled: func(assetID []byte) (state.KAppAccountHandler, *kapps.StakingData, error) { |
| 5525 | + return nil, &kapps.StakingData{}, nil |
| 5526 | + }, |
| 5527 | + GetKDACalled: func(assetID []byte) (state.KAppAccountHandler, *kapps.KDAData, error) { |
| 5528 | + return nil, &kapps.KDAData{AssetType: kapps.KDAData_Fungible}, nil |
| 5529 | + }, |
| 5530 | + } |
| 5531 | + }, |
| 5532 | + }) |
| 5533 | + |
| 5534 | + _ = accountsKapp.SetAccountsCacher(&commonMock.AccountsCacherStub{ |
| 5535 | + GetExistingUserCalled: func(address []byte) (state.UserAccountHandler, error) { |
| 5536 | + return &commonMock.UserAccountHandlerStub{ |
| 5537 | + GetUserKDACalled: func(assetID, nonce []byte, checkDirtData bool) (*kapps.UserKDA, error) { |
| 5538 | + return &kapps.UserKDA{}, nil |
| 5539 | + }, |
| 5540 | + ClaimCalled: func(claimType transaction.ClaimContract_EnumClaimType, assetID []byte, epoch uint32, blockTime int64, staking *kapps.StakingData, kda *kapps.KDAData, userKDA *kapps.UserKDA, forkController core.ForkController) (map[string]int64, error) { |
| 5541 | + return nil, common.ErrMaxSupplyExceeded |
| 5542 | + }, |
| 5543 | + }, nil |
| 5544 | + }, |
| 5545 | + }) |
| 5546 | + |
| 5547 | + tc := &transaction.ClaimContract{ |
| 5548 | + ClaimType: transaction.ClaimContract_StakingClaim, |
| 5549 | + ID: kdautils.KLVIdentifier, |
| 5550 | + } |
| 5551 | + |
| 5552 | + code, err := accountsKapp.ClaimStaking(txSender, tc) |
| 5553 | + require.ErrorIs(t, err, common.ErrMaxSupplyExceeded) |
| 5554 | + assert.Equal(t, transaction.Transaction_MaxSupplyExceeded, code) |
| 5555 | +} |
| 5556 | + |
| 5557 | +func Test_ClaimStaking_MaxSupplyExceeded_Wrapped(t *testing.T) { |
| 5558 | + accountsKapp := setupAccountsKapp(t, config.EnableEpochs{}) |
| 5559 | + wrapped := fmt.Errorf("ctx: %w", common.ErrMaxSupplyExceeded) |
| 5560 | + |
| 5561 | + receiptsCtx := &commonMock.ReceiptsContextStub{} |
| 5562 | + ctx := &commonMock.KAppContextStub{ |
| 5563 | + ReceiptsCalled: func() kapp.ReceiptsContext { return receiptsCtx }, |
| 5564 | + ContractIDCalled: func() int { return 1 }, |
| 5565 | + BlockCalled: func() *block.Block { |
| 5566 | + return &block.Block{Header: &block.BlockHeader{Timestamp: 1000, Epoch: 1}} |
| 5567 | + }, |
| 5568 | + } |
| 5569 | + |
| 5570 | + _ = accountsKapp.SetKAppController(&kvmStub.KAppControllerStub{ |
| 5571 | + GetCurrentKAppContextCalled: func() kapp.KappContext { return ctx }, |
| 5572 | + GetKDAKAppCalled: func() kapp.KDAKapp { |
| 5573 | + return &kvmStub.KDAKappStub{ |
| 5574 | + GetStakingCalled: func(assetID []byte) (state.KAppAccountHandler, *kapps.StakingData, error) { |
| 5575 | + return nil, &kapps.StakingData{}, nil |
| 5576 | + }, |
| 5577 | + GetKDACalled: func(assetID []byte) (state.KAppAccountHandler, *kapps.KDAData, error) { |
| 5578 | + return nil, &kapps.KDAData{AssetType: kapps.KDAData_Fungible}, nil |
| 5579 | + }, |
| 5580 | + } |
| 5581 | + }, |
| 5582 | + }) |
| 5583 | + |
| 5584 | + _ = accountsKapp.SetAccountsCacher(&commonMock.AccountsCacherStub{ |
| 5585 | + GetExistingUserCalled: func(address []byte) (state.UserAccountHandler, error) { |
| 5586 | + return &commonMock.UserAccountHandlerStub{ |
| 5587 | + GetUserKDACalled: func(assetID, nonce []byte, checkDirtData bool) (*kapps.UserKDA, error) { |
| 5588 | + return &kapps.UserKDA{}, nil |
| 5589 | + }, |
| 5590 | + ClaimCalled: func(claimType transaction.ClaimContract_EnumClaimType, assetID []byte, epoch uint32, blockTime int64, staking *kapps.StakingData, kda *kapps.KDAData, userKDA *kapps.UserKDA, forkController core.ForkController) (map[string]int64, error) { |
| 5591 | + return nil, wrapped |
| 5592 | + }, |
| 5593 | + }, nil |
| 5594 | + }, |
| 5595 | + }) |
| 5596 | + |
| 5597 | + tc := &transaction.ClaimContract{ |
| 5598 | + ClaimType: transaction.ClaimContract_StakingClaim, |
| 5599 | + ID: kdautils.KLVIdentifier, |
| 5600 | + } |
| 5601 | + |
| 5602 | + code, err := accountsKapp.ClaimStaking(txSender, tc) |
| 5603 | + require.ErrorIs(t, err, common.ErrMaxSupplyExceeded) |
| 5604 | + assert.Equal(t, transaction.Transaction_MaxSupplyExceeded, code) |
| 5605 | +} |
| 5606 | + |
| 5607 | +func Test_claimErrorResultCode(t *testing.T) { |
| 5608 | + t.Parallel() |
| 5609 | + |
| 5610 | + tests := []struct { |
| 5611 | + name string |
| 5612 | + err error |
| 5613 | + fallback transaction.Transaction_TXResultCode |
| 5614 | + want transaction.Transaction_TXResultCode |
| 5615 | + }{ |
| 5616 | + { |
| 5617 | + name: "direct ErrMaxSupplyExceeded -> MaxSupplyExceeded", |
| 5618 | + err: common.ErrMaxSupplyExceeded, |
| 5619 | + fallback: transaction.Transaction_ClaimError, |
| 5620 | + want: transaction.Transaction_MaxSupplyExceeded, |
| 5621 | + }, |
| 5622 | + { |
| 5623 | + name: "wrapped ErrMaxSupplyExceeded -> MaxSupplyExceeded", |
| 5624 | + err: errWrap("ctx", common.ErrMaxSupplyExceeded), |
| 5625 | + fallback: transaction.Transaction_ClaimError, |
| 5626 | + want: transaction.Transaction_MaxSupplyExceeded, |
| 5627 | + }, |
| 5628 | + { |
| 5629 | + name: "unrelated error -> fallback", |
| 5630 | + err: errors.New("something else"), |
| 5631 | + fallback: transaction.Transaction_ClaimError, |
| 5632 | + want: transaction.Transaction_ClaimError, |
| 5633 | + }, |
| 5634 | + } |
| 5635 | + |
| 5636 | + for _, tt := range tests { |
| 5637 | + t.Run(tt.name, func(t *testing.T) { |
| 5638 | + assert.Equal(t, tt.want, claimErrorResultCode(tt.err, tt.fallback)) |
| 5639 | + }) |
| 5640 | + } |
| 5641 | +} |
| 5642 | + |
| 5643 | +func errWrap(msg string, err error) error { |
| 5644 | + return fmt.Errorf("%s: %w", msg, err) |
| 5645 | +} |
0 commit comments