github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/keeper/internal/abci_test.go (about)

     1  package internal_test
     2  
     3  import (
     4  	abci "github.com/tendermint/tendermint/abci/types"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  	"github.com/Finschia/finschia-sdk/x/foundation"
     8  	"github.com/Finschia/finschia-sdk/x/foundation/keeper/internal"
     9  )
    10  
    11  func (s *KeeperTestSuite) TestBeginBlocker() {
    12  	for name, tc := range map[string]struct {
    13  		taxRatio sdk.Dec
    14  		valid    bool
    15  	}{
    16  		"valid ratio": {
    17  			taxRatio: sdk.OneDec(),
    18  			valid:    true,
    19  		},
    20  		"ratio > 1": {
    21  			taxRatio: sdk.MustNewDecFromStr("1.00000001"),
    22  		},
    23  	} {
    24  		s.Run(name, func() {
    25  			ctx, _ := s.ctx.CacheContext()
    26  
    27  			// collect
    28  			testing := func() {
    29  				s.impl.SetParams(ctx, foundation.Params{
    30  					FoundationTax: tc.taxRatio,
    31  				})
    32  				internal.BeginBlocker(ctx, s.impl)
    33  			}
    34  			if !tc.valid {
    35  				s.Require().Panics(testing)
    36  				return
    37  			}
    38  			s.Require().NotPanics(testing)
    39  
    40  			if s.deterministic {
    41  				expectedEvents := sdk.Events{sdk.Event{Type: "coin_spent", Attributes: []abci.EventAttribute{{Key: []uint8{0x73, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x72}, Value: []uint8{0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x37, 0x78, 0x70, 0x66, 0x76, 0x61, 0x6b, 0x6d, 0x32, 0x61, 0x6d, 0x67, 0x39, 0x36, 0x32, 0x79, 0x6c, 0x73, 0x36, 0x66, 0x38, 0x34, 0x7a, 0x33, 0x6b, 0x65, 0x6c, 0x6c, 0x38, 0x63, 0x35, 0x6c, 0x39, 0x68, 0x72, 0x7a, 0x73, 0x34}, Index: false}, {Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x73, 0x74, 0x61, 0x6b, 0x65}, Index: false}}}, sdk.Event{Type: "coin_received", Attributes: []abci.EventAttribute{{Key: []uint8{0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x72}, Value: []uint8{0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x6d, 0x61, 0x66, 0x6c, 0x38, 0x66, 0x33, 0x73, 0x36, 0x75, 0x75, 0x7a, 0x77, 0x6e, 0x78, 0x6b, 0x71, 0x7a, 0x30, 0x65, 0x7a, 0x61, 0x34, 0x37, 0x76, 0x36, 0x65, 0x63, 0x6e, 0x30, 0x74, 0x75, 0x77, 0x72, 0x36, 0x79, 0x6b}, Index: false}, {Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x73, 0x74, 0x61, 0x6b, 0x65}, Index: false}}}, sdk.Event{Type: "transfer", Attributes: []abci.EventAttribute{{Key: []uint8{0x72, 0x65, 0x63, 0x69, 0x70, 0x69, 0x65, 0x6e, 0x74}, Value: []uint8{0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x6d, 0x61, 0x66, 0x6c, 0x38, 0x66, 0x33, 0x73, 0x36, 0x75, 0x75, 0x7a, 0x77, 0x6e, 0x78, 0x6b, 0x71, 0x7a, 0x30, 0x65, 0x7a, 0x61, 0x34, 0x37, 0x76, 0x36, 0x65, 0x63, 0x6e, 0x30, 0x74, 0x75, 0x77, 0x72, 0x36, 0x79, 0x6b}, Index: false}, {Key: []uint8{0x73, 0x65, 0x6e, 0x64, 0x65, 0x72}, Value: []uint8{0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x37, 0x78, 0x70, 0x66, 0x76, 0x61, 0x6b, 0x6d, 0x32, 0x61, 0x6d, 0x67, 0x39, 0x36, 0x32, 0x79, 0x6c, 0x73, 0x36, 0x66, 0x38, 0x34, 0x7a, 0x33, 0x6b, 0x65, 0x6c, 0x6c, 0x38, 0x63, 0x35, 0x6c, 0x39, 0x68, 0x72, 0x7a, 0x73, 0x34}, Index: false}, {Key: []uint8{0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74}, Value: []uint8{0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x73, 0x74, 0x61, 0x6b, 0x65}, Index: false}}}}
    42  				s.Require().Equal(expectedEvents, ctx.EventManager().Events())
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  func (s *KeeperTestSuite) TestEndBlocker() {
    49  	ctx, _ := s.ctx.CacheContext()
    50  
    51  	// check preconditions
    52  	for name, tc := range map[string]struct {
    53  		id     uint64
    54  		status foundation.ProposalStatus
    55  	}{
    56  		"active proposal": {
    57  			s.activeProposal,
    58  			foundation.PROPOSAL_STATUS_SUBMITTED,
    59  		},
    60  		"voted proposal": {
    61  			s.votedProposal,
    62  			foundation.PROPOSAL_STATUS_SUBMITTED,
    63  		},
    64  		"withdrawn proposal": {
    65  			s.withdrawnProposal,
    66  			foundation.PROPOSAL_STATUS_WITHDRAWN,
    67  		},
    68  		"invalid proposal": {
    69  			s.invalidProposal,
    70  			foundation.PROPOSAL_STATUS_SUBMITTED,
    71  		},
    72  	} {
    73  		s.Run(name, func() {
    74  			proposal, err := s.impl.GetProposal(ctx, tc.id)
    75  			s.Require().NoError(err)
    76  			s.Require().NotNil(proposal)
    77  			s.Require().Equal(tc.status, proposal.Status)
    78  		})
    79  	}
    80  
    81  	// voting periods end
    82  	votingPeriod := s.impl.GetFoundationInfo(ctx).GetDecisionPolicy().GetVotingPeriod()
    83  	ctx = ctx.WithBlockTime(ctx.BlockTime().Add(votingPeriod))
    84  	internal.EndBlocker(ctx, s.impl)
    85  
    86  	for name, tc := range map[string]struct {
    87  		id      uint64
    88  		removed bool
    89  		status  foundation.ProposalStatus
    90  	}{
    91  		"active proposal": {
    92  			id:     s.activeProposal,
    93  			status: foundation.PROPOSAL_STATUS_ACCEPTED,
    94  		},
    95  		"voted proposal": {
    96  			id:     s.votedProposal,
    97  			status: foundation.PROPOSAL_STATUS_REJECTED,
    98  		},
    99  		"withdrawn proposal": {
   100  			id:      s.withdrawnProposal,
   101  			removed: true,
   102  		},
   103  		"invalid proposal": {
   104  			id:     s.invalidProposal,
   105  			status: foundation.PROPOSAL_STATUS_ACCEPTED,
   106  		},
   107  	} {
   108  		s.Run(name, func() {
   109  			proposal, err := s.impl.GetProposal(ctx, tc.id)
   110  			if tc.removed {
   111  				s.Require().Error(err)
   112  				return
   113  			}
   114  			s.Require().NoError(err)
   115  			s.Require().NotNil(proposal)
   116  			s.Require().Equal(tc.status, proposal.Status)
   117  		})
   118  	}
   119  
   120  	// proposals expire
   121  	maxExecutionPeriod := foundation.DefaultConfig().MaxExecutionPeriod
   122  	ctx = ctx.WithBlockTime(ctx.BlockTime().Add(maxExecutionPeriod))
   123  	internal.EndBlocker(ctx, s.impl)
   124  
   125  	// all proposals must be pruned
   126  	s.Require().Empty(s.impl.GetProposals(ctx))
   127  
   128  	if s.deterministic {
   129  		expectedEvents := sdk.Events{}
   130  		s.Require().Equal(expectedEvents, ctx.EventManager().Events())
   131  	}
   132  }