github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/keeper/proposal_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"time"
     5  
     6  	ethcmn "github.com/ethereum/go-ethereum/common"
     7  	ethermint "github.com/fibonacci-chain/fbc/app/types"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    10  	"github.com/fibonacci-chain/fbc/x/evm/types"
    11  	govtypes "github.com/fibonacci-chain/fbc/x/gov/types"
    12  	staking_types "github.com/fibonacci-chain/fbc/x/staking/types"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func (suite *KeeperTestSuite) TestProposal_ManageContractDeploymentWhitelistProposal() {
    17  	addr1 := ethcmn.BytesToAddress([]byte{0x0}).Bytes()
    18  	addr2 := ethcmn.BytesToAddress([]byte{0x1}).Bytes()
    19  
    20  	proposal := types.NewManageContractDeploymentWhitelistProposal(
    21  		"default title",
    22  		"default description",
    23  		types.AddressList{addr1, addr2},
    24  		true,
    25  	)
    26  
    27  	minDeposit := suite.app.EvmKeeper.GetMinDeposit(suite.ctx, proposal)
    28  	require.Equal(suite.T(), sdk.SysCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(100))}, minDeposit)
    29  
    30  	maxDepositPeriod := suite.app.EvmKeeper.GetMaxDepositPeriod(suite.ctx, proposal)
    31  	require.Equal(suite.T(), time.Hour*24, maxDepositPeriod)
    32  
    33  	votingPeriod := suite.app.EvmKeeper.GetVotingPeriod(suite.ctx, proposal)
    34  	require.Equal(suite.T(), time.Hour*72, votingPeriod)
    35  
    36  	testCases := []struct {
    37  		msg     string
    38  		prepare func()
    39  	}{
    40  		{
    41  			"pass check",
    42  			func() {},
    43  		},
    44  		{
    45  			"pass check when trying to add addresses already exists in whitelist",
    46  			func() {
    47  				suite.stateDB.SetContractDeploymentWhitelist(types.AddressList{addr1, addr2})
    48  			},
    49  		},
    50  		{
    51  			"pass check when trying to delete addresses from whitelist",
    52  			func() {
    53  				proposal.IsAdded = false
    54  			},
    55  		},
    56  		{
    57  			"pass check when trying to delete addresses from whitelist which contains none of them",
    58  			func() {
    59  				// clear whitelist in the store
    60  				suite.stateDB.DeleteContractDeploymentWhitelist(suite.stateDB.GetContractDeploymentWhitelist())
    61  				suite.Require().Zero(len(suite.stateDB.GetContractDeploymentWhitelist()))
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, tc := range testCases {
    67  		suite.Run(tc.msg, func() {
    68  			tc.prepare()
    69  
    70  			msg := govtypes.NewMsgSubmitProposal(proposal, minDeposit, addr1)
    71  			err := suite.app.EvmKeeper.CheckMsgSubmitProposal(suite.ctx, msg)
    72  			suite.Require().NoError(err)
    73  		})
    74  	}
    75  }
    76  
    77  func (suite *KeeperTestSuite) TestProposal_ManageContractBlockedListProposal() {
    78  	addr1 := ethcmn.BytesToAddress([]byte{0x0}).Bytes()
    79  	addr2 := ethcmn.BytesToAddress([]byte{0x1}).Bytes()
    80  
    81  	proposal := types.NewManageContractBlockedListProposal(
    82  		"default title",
    83  		"default description",
    84  		types.AddressList{addr1, addr2},
    85  		true,
    86  	)
    87  
    88  	minDeposit := suite.app.EvmKeeper.GetMinDeposit(suite.ctx, proposal)
    89  	require.Equal(suite.T(), sdk.SysCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(100))}, minDeposit)
    90  
    91  	maxDepositPeriod := suite.app.EvmKeeper.GetMaxDepositPeriod(suite.ctx, proposal)
    92  	require.Equal(suite.T(), time.Hour*24, maxDepositPeriod)
    93  
    94  	votingPeriod := suite.app.EvmKeeper.GetVotingPeriod(suite.ctx, proposal)
    95  	require.Equal(suite.T(), time.Hour*72, votingPeriod)
    96  
    97  	testCases := []struct {
    98  		msg     string
    99  		prepare func()
   100  	}{
   101  		{
   102  			"pass check",
   103  			func() {},
   104  		},
   105  		{
   106  			"pass check when trying to add addresses already exists in blocked list",
   107  			func() {
   108  				suite.stateDB.SetContractDeploymentWhitelist(types.AddressList{addr1, addr2})
   109  			},
   110  		},
   111  		{
   112  			"pass check when trying to delete addresses from blocked list",
   113  			func() {
   114  				proposal.IsAdded = false
   115  			},
   116  		},
   117  		{
   118  			"pass check when trying to delete addresses from blocked list which contains none of them",
   119  			func() {
   120  				// clear blocked list in the store
   121  				suite.stateDB.DeleteContractBlockedList(suite.stateDB.GetContractBlockedList())
   122  				suite.Require().Zero(len(suite.stateDB.GetContractBlockedList()))
   123  			},
   124  		},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		suite.Run(tc.msg, func() {
   129  			tc.prepare()
   130  
   131  			msg := govtypes.NewMsgSubmitProposal(proposal, minDeposit, addr1)
   132  			err := suite.app.EvmKeeper.CheckMsgSubmitProposal(suite.ctx, msg)
   133  			suite.Require().NoError(err)
   134  		})
   135  	}
   136  }
   137  
   138  func (suite *KeeperTestSuite) TestProposal_ManageContractMethodBlockedListProposal() {
   139  	addr1 := ethcmn.BytesToAddress([]byte{0x0}).Bytes()
   140  	addr2 := ethcmn.BytesToAddress([]byte{0x1}).Bytes()
   141  	bcMethodOne1 := types.BlockedContract{
   142  		Address: addr1,
   143  		BlockMethods: types.ContractMethods{
   144  			types.ContractMethod{
   145  				Sign:  "aaaa",
   146  				Extra: "aaaa()",
   147  			},
   148  		},
   149  	}
   150  	bcMethodTwo1 := types.BlockedContract{
   151  		Address: addr2,
   152  		BlockMethods: types.ContractMethods{
   153  			types.ContractMethod{
   154  				Sign:  "aaaa",
   155  				Extra: "aaaa()",
   156  			},
   157  		},
   158  	}
   159  	bcl := types.BlockedContractList{bcMethodOne1, bcMethodTwo1}
   160  	proposal := types.NewManageContractMethodBlockedListProposal(
   161  		"default title",
   162  		"default description",
   163  		bcl,
   164  		true,
   165  	)
   166  
   167  	minDeposit := suite.app.EvmKeeper.GetMinDeposit(suite.ctx, proposal)
   168  	require.Equal(suite.T(), sdk.SysCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(100))}, minDeposit)
   169  
   170  	maxDepositPeriod := suite.app.EvmKeeper.GetMaxDepositPeriod(suite.ctx, proposal)
   171  	require.Equal(suite.T(), time.Hour*24, maxDepositPeriod)
   172  
   173  	votingPeriod := suite.app.EvmKeeper.GetVotingPeriod(suite.ctx, proposal)
   174  	require.Equal(suite.T(), time.Hour*72, votingPeriod)
   175  
   176  	testCases := []struct {
   177  		msg     string
   178  		prepare func()
   179  		success bool
   180  	}{
   181  		{
   182  			"pass check",
   183  			func() {},
   184  			true,
   185  		},
   186  		{
   187  			"pass check when trying to add addresses already exists in blocked list",
   188  			func() {
   189  				suite.stateDB.InsertContractMethodBlockedList(bcl)
   190  			},
   191  			true,
   192  		},
   193  		{
   194  			"pass check when trying to delete addresses from blocked list",
   195  			func() {
   196  				proposal.IsAdded = false
   197  			},
   198  			true,
   199  		},
   200  		{
   201  			"pass check when trying to delete addresses from blocked list which is empty",
   202  			func() {
   203  				// clear blocked list in the store
   204  				suite.stateDB.DeleteContractMethodBlockedList(suite.stateDB.GetContractMethodBlockedList())
   205  				suite.Require().Zero(len(suite.stateDB.GetContractBlockedList()))
   206  			},
   207  			false,
   208  		},
   209  	}
   210  
   211  	for _, tc := range testCases {
   212  		suite.Run(tc.msg, func() {
   213  			tc.prepare()
   214  
   215  			msg := govtypes.NewMsgSubmitProposal(proposal, minDeposit, addr1)
   216  			err := suite.app.EvmKeeper.CheckMsgSubmitProposal(suite.ctx, msg)
   217  			if tc.success {
   218  				suite.Require().NoError(err)
   219  			} else {
   220  				suite.Require().Error(err)
   221  			}
   222  		})
   223  	}
   224  }
   225  
   226  func (suite *KeeperTestSuite) TestProposal_ManageSysContractAddressProposal() {
   227  	priv := ed25519.GenPrivKeyFromSecret([]byte("ed25519 private key"))
   228  	pub := priv.PubKey()
   229  
   230  	addr1 := ethcmn.BytesToAddress([]byte{0x01}).Bytes()
   231  	proposal := types.NewManageSysContractAddressProposal(
   232  		"default title",
   233  		"default description",
   234  		addr1,
   235  		true,
   236  	)
   237  
   238  	newVal := staking_types.NewValidator(sdk.ValAddress(pub.Address()), pub, staking_types.NewDescription("test description", "", "", ""), staking_types.DefaultMinDelegation)
   239  	validator := newVal.UpdateStatus(sdk.Bonded)
   240  	suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
   241  	suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
   242  	suite.app.StakingKeeper.SetValidatorByPowerIndex(suite.ctx, validator)
   243  
   244  	minDeposit := suite.app.EvmKeeper.GetMinDeposit(suite.ctx, proposal)
   245  	require.Equal(suite.T(), sdk.SysCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, sdk.NewInt(100))}, minDeposit)
   246  
   247  	maxDepositPeriod := suite.app.EvmKeeper.GetMaxDepositPeriod(suite.ctx, proposal)
   248  	require.Equal(suite.T(), time.Hour*24, maxDepositPeriod)
   249  
   250  	votingPeriod := suite.app.EvmKeeper.GetVotingPeriod(suite.ctx, proposal)
   251  	require.Equal(suite.T(), time.Hour*72, votingPeriod)
   252  
   253  	testCases := []struct {
   254  		msg     string
   255  		prepare func()
   256  		success bool
   257  	}{
   258  		{
   259  			"pass check IsAdded is true, but this address is not exist contract address",
   260  			func() {
   261  				proposal.IsAdded = true
   262  			},
   263  			false,
   264  		},
   265  		{
   266  			"pass check IsAdded is false and not exist a sys contract address",
   267  			func() {
   268  				proposal.IsAdded = false
   269  			},
   270  			false,
   271  		},
   272  		{
   273  			"pass check IsAdded is false and exist a sys contract address",
   274  			func() {
   275  				proposal.IsAdded = false
   276  				suite.app.EvmKeeper.SetSysContractAddress(suite.ctx, addr1)
   277  			},
   278  			true,
   279  		},
   280  		{
   281  			"pass check IsAdded is true and exist a sys contract address, this address is a contract address ",
   282  			func() {
   283  				proposal.IsAdded = true
   284  				suite.app.EvmKeeper.SetSysContractAddress(suite.ctx, addr1)
   285  
   286  				acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr1)
   287  				ethAcct, ok := acc.(*ethermint.EthAccount)
   288  				suite.Require().True(ok)
   289  				ethAcct.CodeHash = []byte("123")
   290  				suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
   291  			},
   292  			true,
   293  		},
   294  	}
   295  
   296  	for _, tc := range testCases {
   297  		suite.Run(tc.msg, func() {
   298  			tc.prepare()
   299  
   300  			msg := govtypes.NewMsgSubmitProposal(proposal, minDeposit, sdk.AccAddress(pub.Address()))
   301  			err := suite.app.EvmKeeper.CheckMsgSubmitProposal(suite.ctx, msg)
   302  			if tc.success {
   303  				suite.Require().NoError(err)
   304  			} else {
   305  				suite.Require().Error(err)
   306  			}
   307  		})
   308  	}
   309  }