github.com/platonnetwork/platon-go@v0.7.6/cases/tests/ppos/test_token_init.py (about)

     1  import math
     2  import time
     3  
     4  import pytest
     5  import allure
     6  from dacite import from_dict
     7  from common.key import get_pub_key, mock_duplicate_sign
     8  from common.log import log
     9  from client_sdk_python import Web3
    10  from decimal import Decimal
    11  from tests.conftest import get_clients_noconsensus
    12  from tests.lib import (EconomicConfig,
    13                         Genesis,
    14                         check_node_in_list,
    15                         assert_code,
    16                         von_amount,
    17                         get_governable_parameter_value,
    18                         get_pledge_list, HexBytes,
    19                         wait_block_number)
    20  
    21  
    22  @pytest.mark.P0
    23  def test_IT_IA_002_to_007(new_genesis_env):
    24      """
    25      IT_IA_002:链初始化-查看token发行总量账户初始值
    26      IT_IA_003:链初始化-查看platON基金会账户初始值
    27      IT_IA_004:链初始化-查看激励池账户
    28      IT_IA_005:链初始化-查看剩余总账户
    29      IT_IA_006:链初始化-查看锁仓账户余额
    30      IT_IA_007:链初始化-查看质押账户余额
    31      :return:验证链初始化后token各内置账户初始值
    32      """
    33      # Initialization genesis file Initial amount
    34      node_count = len(new_genesis_env.consensus_node_list)
    35      default_pledge_amount = Web3.toWei(node_count * 1500000, 'ether')
    36      node = new_genesis_env.get_rand_node()
    37      community_amount = default_pledge_amount + 259096239000000000000000000 + 62215742000000000000000000
    38      genesis = from_dict(data_class=Genesis, data=new_genesis_env.genesis_config)
    39      genesis.economicModel.innerAcc.cdfBalance = community_amount
    40      surplus_amount = str(EconomicConfig.TOKEN_TOTAL - community_amount - 200000000000000000000000000)
    41      genesis.alloc = {
    42          "1000000000000000000000000000000000000003": {
    43              "balance": "200000000000000000000000000"
    44          },
    45          "0x2e95E3ce0a54951eB9A99152A6d5827872dFB4FD": {
    46              "balance": surplus_amount
    47          }
    48      }
    49      new_file = new_genesis_env.cfg.env_tmp + "/genesis.json"
    50      genesis.to_file(new_file)
    51      new_genesis_env.deploy_all(new_file)
    52  
    53      # Verify the amount of each built-in account
    54      foundation_louckup = node.eth.getBalance(EconomicConfig.FOUNDATION_LOCKUP_ADDRESS)
    55      log.info('Initial lock up contract address: {} amount:{}'.format(EconomicConfig.FOUNDATION_LOCKUP_ADDRESS,
    56                                                                       foundation_louckup))
    57      incentive_pool = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
    58      log.info('Incentive pool address:{} amount:{}'.format(EconomicConfig.INCENTIVEPOOL_ADDRESS, incentive_pool))
    59      staking = node.eth.getBalance(EconomicConfig.STAKING_ADDRESS)
    60      log.info('Address of pledge contract:{} amount:{}'.format(EconomicConfig.STAKING_ADDRESS, staking))
    61      foundation = node.eth.getBalance(node.web3.toChecksumAddress(EconomicConfig.FOUNDATION_ADDRESS))
    62      log.info('PlatON Foundation address:{} amount:{}'.format(EconomicConfig.FOUNDATION_ADDRESS, foundation))
    63      remain = node.eth.getBalance(node.web3.toChecksumAddress(EconomicConfig.REMAIN_ACCOUNT_ADDRESS))
    64      log.info('Remaining total account address:{} amount:{}'.format(EconomicConfig.REMAIN_ACCOUNT_ADDRESS, remain))
    65      develop = node.eth.getBalance(node.web3.toChecksumAddress(EconomicConfig.DEVELOPER_FOUNDATAION_ADDRESS))
    66      log.info('Community developer foundation address:{} amount:{}'.format(EconomicConfig.DEVELOPER_FOUNDATAION_ADDRESS,
    67                                                                            develop))
    68      reality_total = foundation_louckup + incentive_pool + staking + foundation + remain + develop
    69      log.info("Total issuance of Chuangshi block:{}".format(reality_total))
    70      log.info("--------------Dividing line---------------")
    71      assert foundation == 0, "ErrMsg:Initial amount of foundation {}".format(foundation)
    72      assert foundation_louckup == 259096239000000000000000000, "ErrMsg:Initial lock up amount of foundation {}".format(
    73          foundation_louckup)
    74      assert staking == default_pledge_amount, "ErrMsg:Amount of initial pledge account: {}".format(staking)
    75      assert incentive_pool == 262215742000000000000000000, "ErrMsg:Initial amount of incentive pool {}".format(
    76          incentive_pool)
    77      assert remain == int(surplus_amount), "ErrMsg:Initial amount of remaining total account {}".format(remain)
    78      assert develop == 0, "ErrMsg:Community developer foundation account amount {}".format(develop)
    79      assert reality_total == EconomicConfig.TOKEN_TOTAL, "ErrMsg:Initialize release value {}".format(reality_total)
    80  
    81  
    82  @allure.title("Two distribution-Transfer amount:{value}")
    83  @pytest.mark.P0
    84  @pytest.mark.parametrize('value', [1000, 0.000000000000000001, 100000000])
    85  def test_IT_SD_004_to_006(client_consensus, value):
    86      """
    87      IT_SD_006:二次分配:普通钱包转keyshard钱包
    88      IT_SD_004:二次分配:转账金额为1von
    89      IT_SD_005:二次分配:转账金额为1亿LAT
    90      :param client_consensus:
    91      :param value:
    92      :return:
    93      """
    94      value = client_consensus.node.web3.toWei(value, 'ether')
    95      address, _ = client_consensus.economic.account.generate_account(client_consensus.node.web3, value)
    96      balance = client_consensus.node.eth.getBalance(address)
    97      log.info("transaction address:{},account:{}".format(address, balance))
    98      assert balance == value, "ErrMsg:Transfer amount {}".format(balance)
    99  
   100  
   101  @pytest.mark.P1
   102  @pytest.mark.parametrize('value', [2000, 1000])
   103  def test_IT_SD_002_003(global_test_env, value):
   104      """
   105      IT_SD_002:二次分配:账户余额不足
   106      IT_SD_003:二次分配:转账手续费不足
   107      :param global_test_env:
   108      :param value:
   109      :return:
   110      """
   111      node = global_test_env.get_rand_node()
   112      address, _ = global_test_env.account.generate_account(node.web3, node.web3.toWei(1000, 'ether'))
   113      status = True
   114      # Account balance insufficient transfer
   115      try:
   116          address1, _ = global_test_env.account.generate_account(node.web3, 0)
   117          transfer_amount = node.web3.toWei(value, 'ether')
   118          result = global_test_env.account.sendTransaction(node.web3, '', address, address1, node.web3.platon.gasPrice,
   119                                                           21000, transfer_amount)
   120          log.info("result: {}".format(result))
   121          status = False
   122      except Exception as e:
   123          log.info("Use case success, exception information:{} ".format(str(e)))
   124      assert status, "ErrMsg:Transfer result {}".format(status)
   125  
   126  
   127  @pytest.mark.P1
   128  def test_IT_SD_011(global_test_env):
   129      """
   130      账户转账校验:转账gas费不足
   131      :param global_test_env:
   132      :return:
   133      """
   134      node = global_test_env.get_rand_node()
   135      address, _ = global_test_env.account.generate_account(node.web3, node.web3.toWei(1000, 'ether'))
   136      status = True
   137      # Insufficient gas fee for transfer
   138      try:
   139          address1, _ = global_test_env.account.generate_account(node.web3, 0)
   140          global_test_env.account.sendTransaction(node.web3, '', address,
   141                                                  address1,
   142                                                  node.web3.platon.gasPrice, 2100, 500)
   143          status = False
   144      except Exception as e:
   145          log.info("Use case success, exception information:{} ".format(str(e)))
   146      assert status, "ErrMsg:Transfer result {}".format(status)
   147  
   148  
   149  @pytest.mark.P2
   150  def test_IT_SD_007(global_test_env):
   151      """
   152      账户转账校验:本账户转本账户
   153      :return:
   154      """
   155      node = global_test_env.get_rand_node()
   156      value = node.web3.toWei(1000, 'ether')
   157      address, _ = global_test_env.account.generate_account(node.web3, value)
   158      balance = node.eth.getBalance(address)
   159      log.info("Account balance before transfer:{}".format(balance))
   160      result = global_test_env.account.sendTransaction(node.web3, '', address, address, node.eth.gasPrice, 21000, 100)
   161      assert result is not None, "ErrMsg:Transfer result {}".format(result)
   162      balance1 = node.eth.getBalance(address)
   163      log.info("Account balance after transfer: {}".format(balance1))
   164      log.info("Transaction fee: {}".format(node.web3.platon.gasPrice * 21000))
   165      assert balance == balance1 + node.web3.platon.gasPrice * 21000, "ErrMsg:Account balance after transfer:{}".format(
   166          balance1)
   167  
   168  
   169  @pytest.mark.P0
   170  def test_IT_SD_008(global_test_env):
   171      """
   172      二次分配:普通账户转platON基金会账户
   173      :return:
   174      """
   175      node = global_test_env.get_rand_node()
   176      value = node.web3.toWei(1000, 'ether')
   177      address, _ = global_test_env.account.generate_account(node.web3, value)
   178      balance = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   179      result = global_test_env.account.sendTransaction(node.web3, '', address, EconomicConfig.INCENTIVEPOOL_ADDRESS,
   180                                                       node.eth.gasPrice, 21000, node.web3.toWei(100, 'ether'))
   181      assert result is not None, "ErrMsg:Transfer result {}".format(result)
   182      balance1 = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   183      log.info("Account balance after transfer: {}".format(balance1))
   184      log.info("Transaction fee: {}".format(node.web3.platon.gasPrice * 21000))
   185      assert balance1 == balance + node.web3.toWei(100,
   186                                                   'ether') + node.web3.platon.gasPrice * 21000, "ErrMsg:Account balance after transfer:{}".format(
   187          balance1)
   188  
   189  
   190  def sendTransaction_input_nonce(client, data, from_address, to_address, gasPrice, gas, value, nonce,
   191                                  check_address=True):
   192      node = client.node
   193      account = client.economic.account.accounts[from_address]
   194      print(account)
   195      if check_address:
   196          to_address = Web3.toChecksumAddress(to_address)
   197      tmp_from_address = Web3.toChecksumAddress(from_address)
   198  
   199      transaction_dict = {
   200          "to": to_address,
   201          "gasPrice": gasPrice,
   202          "gas": gas,
   203          "nonce": nonce,
   204          "data": data,
   205          "chainId": client.economic.account.chain_id,
   206          "value": value,
   207          'from': tmp_from_address,
   208      }
   209      signedTransactionDict = node.eth.account.signTransaction(
   210          transaction_dict, account['prikey']
   211      )
   212      data = signedTransactionDict.rawTransaction
   213      result = HexBytes(node.eth.sendRawTransaction(data)).hex()
   214      res = node.eth.waitForTransactionReceipt(result)
   215  
   216      return res
   217  
   218  
   219  @pytest.mark.P2
   220  def test_IT_SD_009(client_consensus):
   221      """
   222      同一时间多次转账
   223      :return:
   224      """
   225      client = client_consensus
   226      economic = client.economic
   227      node = client.node
   228      economic.env.deploy_all()
   229      address, _ = economic.account.generate_account(node.web3, node.web3.toWei(1000, 'ether'))
   230      address1, _ = economic.account.generate_account(node.web3, 0)
   231      nonce = node.eth.getTransactionCount(address)
   232      print('nonce: ', nonce)
   233      balance = node.eth.getBalance(address1)
   234      log.info("balance: {}".format(balance))
   235      sendTransaction_input_nonce(client, '', address, address1, node.eth.gasPrice, 21000, node.web3.toWei(100, 'ether'),
   236                                  nonce)
   237      sendTransaction_input_nonce(client, '', address, address1, node.eth.gasPrice, 21000, node.web3.toWei(100, 'ether'),
   238                                  nonce + 1)
   239      time.sleep(3)
   240      balance1 = node.eth.getBalance(address1)
   241      log.info("Account balance after transfer: {}".format(balance1))
   242      assert balance1 == balance + node.web3.toWei(200, 'ether'), "ErrMsg:Account balance after transfer:{}".format(
   243          balance1)
   244  
   245  
   246  @pytest.mark.P2
   247  def test_IT_SD_010(client_consensus):
   248      """
   249      同一时间多次转账,余额不足
   250      :return:
   251      """
   252      client = client_consensus
   253      economic = client.economic
   254      node = client.node
   255      economic.env.deploy_all()
   256      address, _ = economic.account.generate_account(node.web3, node.web3.toWei(1000, 'ether'))
   257      address1, _ = economic.account.generate_account(node.web3, 0)
   258      balance = node.eth.getBalance(address1)
   259      log.info("balance: {}".format(balance))
   260      try:
   261          nonce = node.eth.getTransactionCount(address)
   262          log.info('nonce: {}'.format(nonce))
   263          sendTransaction_input_nonce(client, '', address, address1, node.eth.gasPrice, 21000,
   264                                      node.web3.toWei(500, 'ether'), nonce)
   265          sendTransaction_input_nonce(client, '', address, address1, node.eth.gasPrice, 21000,
   266                                      node.web3.toWei(600, 'ether'), nonce + 1)
   267      except Exception as e:
   268          log.info("Use case success, exception information:{} ".format(str(e)))
   269          time.sleep(3)
   270          balance1 = node.eth.getBalance(address1)
   271          log.info("Account balance after transfer: {}".format(balance1))
   272          assert balance1 == balance + node.web3.toWei(500, 'ether'), "ErrMsg:Account balance after transfer:{}".format(
   273              balance1)
   274  
   275  
   276  def consensus_node_pledge_award_assertion(client, address):
   277      """
   278      内置节点质押奖励断言
   279      :param client:
   280      :param address:
   281      :return:
   282      """
   283      blockNumber = client.node.eth.blockNumber
   284      log.info("Current block height:{}".format(blockNumber))
   285      incentive_pool_balance = client.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   286      log.info("Balance of incentive pool:{}".format(incentive_pool_balance))
   287      CandidateInfo = client.ppos.getCandidateInfo(client.node.node_id)
   288      log.info("Pledgor node information:{}".format(CandidateInfo))
   289  
   290      # wait settlement block
   291      client.economic.wait_settlement_blocknum(client.node)
   292      time.sleep(5)
   293      VerifierList = client.ppos.getVerifierList()
   294      log.info("Current settlement cycle verifier list:{}".format(VerifierList))
   295      ValidatorList = client.ppos.getValidatorList()
   296      log.info("Current consensus cycle verifier list:{}".format(ValidatorList))
   297      # Application for withdrew staking
   298      result = client.staking.withdrew_staking(address)
   299      assert_code(result, 0)
   300      # wait settlement block
   301      client.economic.wait_settlement_blocknum(client.node)
   302      # view incentive pool amonut
   303      incentive_pool_balance2 = client.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   304      log.info(
   305          "incentive pool address:{} amount:{}".format(EconomicConfig.INCENTIVEPOOL_ADDRESS, incentive_pool_balance2))
   306      assert incentive_pool_balance2 - incentive_pool_balance < client.node.web3.toWei(1,
   307                                                                                       'ether'), "ErrMsg:Balance of incentive pool:{}".format(
   308          incentive_pool_balance2)
   309  
   310  
   311  def no_consensus_node_pledge_award_assertion(client, benifit_address, from_address):
   312      """
   313      非内置节点质押奖励断言
   314      :param client:
   315      :param benifit_address:
   316      :param from_address:
   317      :return:
   318      """
   319      CandidateInfo = client.ppos.getCandidateInfo(client.node.node_id)
   320      log.info("Pledgor node information:{}".format(CandidateInfo))
   321      balance = client.node.eth.getBalance(benifit_address)
   322      log.info("benifit address:{} amount: {}".format(benifit_address, balance))
   323  
   324      # wait settlement block
   325      client.economic.wait_settlement_blocknum(client.node)
   326      time.sleep(5)
   327      VerifierList = client.ppos.getVerifierList()
   328      log.info("Current settlement cycle verifier list:{}".format(VerifierList))
   329      ValidatorList = client.ppos.getValidatorList()
   330      log.info("Current consensus cycle verifier list:{}".format(ValidatorList))
   331      block_reward, staking_reward = client.economic.get_current_year_reward(client.node)
   332      for i in range(4):
   333          result = check_node_in_list(client.node.node_id, client.ppos.getValidatorList)
   334          log.info("Current node in consensus list status:{}".format(result))
   335          if result:
   336              # wait consensus block
   337              client.economic.wait_consensus_blocknum(client.node)
   338              # Application for withdrew staking
   339              result = client.staking.withdrew_staking(from_address)
   340              assert_code(result, 0)
   341              incentive_pool_balance1 = client.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   342              log.info("incentive pool amount:{}".format(incentive_pool_balance1))
   343              # wait settlement block
   344              client.economic.wait_settlement_blocknum(client.node)
   345              # Count the number of blocks out of pledge node
   346              blocknumber = client.economic.get_block_count_number(client.node, 5)
   347              log.info("blocknumber: {}".format(blocknumber))
   348              balance1 = client.node.eth.getBalance(benifit_address)
   349              log.info("benifit address:{} amount:{}".format(benifit_address, balance1))
   350  
   351              # Verify block rewards
   352              log.info("Expected bonus:{}".format(Decimal(str(block_reward)) * blocknumber))
   353              assert balance + Decimal(str(block_reward)) * blocknumber - balance1 < client.node.web3.toWei(
   354                  1, 'ether'), "ErrMsg:benifit address:{} amount:{}".format(
   355                  benifit_address, balance1)
   356              break
   357          else:
   358              # wait consensus block
   359              client.economic.wait_consensus_blocknum(client.node)
   360  
   361  
   362  @pytest.mark.p1
   363  def test_AL_IE_001(client_consensus):
   364      """
   365      查看初始激励池总额
   366      :param client_consensus:
   367      :return:
   368      """
   369      client = client_consensus
   370      economic = client.economic
   371      node = client.node
   372      # 初始化环境
   373      client.economic.env.deploy_all()
   374      # 查询激励池初始金额
   375      incentive_pool = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, 0)
   376      assert incentive_pool == 262215742000000000000000000, "ErrMsg:Initial amount of incentive pool {}".format(
   377          incentive_pool)
   378  
   379  
   380  @pytest.mark.P2
   381  def test_AL_IE_002(clients_new_node):
   382      """
   383      转账到激励池
   384      :param clients_new_node:
   385      :return:
   386      """
   387      client1 = clients_new_node[0]
   388      client2 = clients_new_node[1]
   389      economic = client1.economic
   390      node = client1.node
   391      log.info("Node ID:{}".format(node.node_id))
   392      log.info("Current connection node: {}".format(node.node_mark))
   393      address, _ = client1.economic.account.generate_account(node.web3, von_amount(economic.create_staking_limit, 4))
   394      address1, _ = client1.economic.account.generate_account(node.web3, 0)
   395      address2, _ = client1.economic.account.generate_account(node.web3, 0)
   396      log.info("staking address: {}".format(address))
   397      # Free amount application pledge node
   398      result = client1.staking.create_staking(0, address1, address)
   399      assert_code(result, 0)
   400      # Wait for the settlement round to end
   401      economic.wait_settlement_blocknum(node)
   402      # 获取当前结算周期验证人
   403      verifier_list = node.ppos.getVerifierList()
   404      log.info("verifier_list: {}".format(verifier_list))
   405      # view block_reward
   406      block_reward, staking_reward = economic.get_current_year_reward(node)
   407      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   408      # view account amount
   409      benifit_balance = node.eth.getBalance(address1)
   410      log.info("benifit_balance: {}".format(benifit_balance))
   411      # view benifit reward
   412      blocknumber = view_benifit_reward(client1, address)
   413      # view account amount again
   414      benifit_balance1 = node.eth.getBalance(address1)
   415      log.info("benifit_balance: {}".format(benifit_balance1))
   416      reward = int(blocknumber * Decimal(str(block_reward)))
   417      assert benifit_balance1 == staking_reward + reward, "ErrMsg:benifit_balance: {}".format(benifit_balance1)
   418      # Transfer to the incentive pool
   419      result = client1.economic.account.sendTransaction(node.web3, '', address, EconomicConfig.INCENTIVEPOOL_ADDRESS,
   420                                                        node.eth.gasPrice, 21000, node.web3.toWei(1000, 'ether'))
   421      assert result is not None, "ErrMsg:Transfer result {}".format(result)
   422      time.sleep(5)
   423      # Free amount application pledge node
   424      result = client2.staking.create_staking(0, address2, address, amount=von_amount(economic.create_staking_limit, 2))
   425      assert_code(result, 0)
   426      # Wait for the settlement round to end
   427      economic.wait_settlement_blocknum(client2.node)
   428      # view account amount
   429      benifit_balance2 = client2.node.eth.getBalance(address2)
   430      log.info("benifit_balance: {}".format(benifit_balance2))
   431      # view benifit reward
   432      blocknumber = view_benifit_reward(client2, address)
   433      # view account amount again
   434      benifit_balance3 = client2.node.eth.getBalance(address2)
   435      log.info("benifit_balance: {}".format(benifit_balance3))
   436      reward = int(blocknumber * Decimal(str(block_reward)))
   437      assert benifit_balance3 == staking_reward + reward, "ErrMsg:benifit_balance: {}".format(benifit_balance3)
   438  
   439  
   440  @pytest.mark.P1
   441  def test_AL_IE_003(clients_new_node):
   442      """
   443      自由账户创建质押节点且收益地址为激励池
   444      :param clients_new_node:
   445      :return:
   446      """
   447      log.info("Node ID:{}".format(clients_new_node[0].node.node_id))
   448      address, _ = clients_new_node[0].economic.account.generate_account(clients_new_node[0].node.web3,
   449                                                                         clients_new_node[
   450                                                                             0].economic.create_staking_limit * 2)
   451      log.info("staking address: {}".format(address))
   452      # Free amount application pledge node
   453      result = clients_new_node[0].staking.create_staking(0, EconomicConfig.INCENTIVEPOOL_ADDRESS, address)
   454      assert_code(result, 0)
   455      consensus_node_pledge_award_assertion(clients_new_node[0], address)
   456  
   457  
   458  @pytest.mark.P1
   459  def test_AL_IE_004(clients_new_node):
   460      """
   461      锁仓账户创建质押节点且收益地址为激励池
   462      :param clients_new_node:
   463      :return:
   464      """
   465      log.info("Node ID:{}".format(clients_new_node[1].node.node_id))
   466      address, _ = clients_new_node[1].economic.account.generate_account(clients_new_node[1].node.web3,
   467                                                                         clients_new_node[
   468                                                                             1].economic.create_staking_limit * 2)
   469      log.info("staking address: {}".format(address))
   470      # Create restricting plan
   471      staking_amount = clients_new_node[1].economic.create_staking_limit
   472      log.info("staking amonut:{}".format(staking_amount))
   473      plan = [{'Epoch': 1, 'Amount': staking_amount}]
   474      result = clients_new_node[1].restricting.createRestrictingPlan(address, plan, address)
   475      assert_code(result, 0)
   476      # Lock in amount application pledge node
   477      result = clients_new_node[1].staking.create_staking(1, EconomicConfig.INCENTIVEPOOL_ADDRESS, address)
   478      assert_code(result, 0)
   479      consensus_node_pledge_award_assertion(clients_new_node[1], address)
   480  
   481  
   482  @pytest.mark.P1
   483  def test_AL_BI_001(client_consensus):
   484      """
   485      出块手续费奖励(内置验证人)
   486      :param client_consensus:
   487      :return:
   488      """
   489      incentive_pool_balance = client_consensus.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   490      log.info("incentive_pool_balance: {}".format(incentive_pool_balance))
   491      # create account
   492      address1, _ = client_consensus.economic.account.generate_account(client_consensus.node.web3, 100)
   493      # view incentive account
   494      incentive_pool_balance1 = client_consensus.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   495      log.info("incentive_pool_balance: {}".format(incentive_pool_balance1))
   496      assert incentive_pool_balance1 == incentive_pool_balance + 21000 * client_consensus.node.eth.gasPrice, "ErrMsg:incentive_pool balance: {}".format(
   497          incentive_pool_balance1)
   498  
   499  
   500  @pytest.mark.P1
   501  def test_AL_BI_002(new_genesis_env, staking_cfg):
   502      """
   503      节点出块率为0被处罚,激励池金额增加
   504      :param new_genesis_env:
   505      :param staking_cfg:
   506      :return:
   507      """
   508      # Change configuration parameters
   509      genesis = from_dict(data_class=Genesis, data=new_genesis_env.genesis_config)
   510      genesis.economicModel.slashing.slashBlocksReward = 5
   511      new_file = new_genesis_env.cfg.env_tmp + "/genesis.json"
   512      genesis.to_file(new_file)
   513      new_genesis_env.deploy_all(new_file)
   514      client_noc_list_obj = get_clients_noconsensus(new_genesis_env, staking_cfg)
   515      client1 = client_noc_list_obj[0]
   516      client2 = client_noc_list_obj[1]
   517      economic = client1.economic
   518      node = client1.node
   519      log.info("nodeid: {}".format(node.node_id))
   520      # create account
   521      address, _ = economic.account.generate_account(node.web3, von_amount(economic.create_staking_limit, 2))
   522      # create staking
   523      result = client1.staking.create_staking(0, address, address)
   524      assert_code(result, 0)
   525      # Waiting for a settlement round
   526      client2.economic.wait_settlement_blocknum(client2.node)
   527      # view incentive account
   528      incentive_pool_balance = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   529      log.info("incentive_pool_balance: {}".format(incentive_pool_balance))
   530      # view block_reward
   531      block_reward, staking_reward = economic.get_current_year_reward(node)
   532      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   533      # stop node
   534      node.stop()
   535      # Waiting for 2 consensus round
   536      client2.economic.wait_consensus_blocknum(client2.node, 2)
   537      # view verifier list
   538      verifier_list = client2.ppos.getVerifierList()
   539      log.info("verifier_list: {}".format(verifier_list))
   540      slash_blocks = get_governable_parameter_value(client2, 'slashBlocksReward')
   541      log.info("slash_blocks".format(slash_blocks))
   542      # Get the penalty amount
   543      penalty_amount = int(Decimal(str(block_reward)) * Decimal(str(slash_blocks)))
   544      log.info("penalty_amount: {}".format(penalty_amount))
   545      # view incentive account again
   546      incentive_pool_balance1 = client2.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   547      log.info("incentive_pool_balance1: {}".format(incentive_pool_balance1))
   548      assert incentive_pool_balance1 == incentive_pool_balance + penalty_amount, "ErrMsg: incentive_pool_balance: {}".format(
   549          incentive_pool_balance1)
   550  
   551  
   552  @pytest.mark.P1
   553  def test_AL_BI_003(client_consensus):
   554      """
   555      初始内置账户没有基金会Staking奖励和出块奖励
   556      :param client_consensus:
   557      :return:
   558      """
   559      # view incentive account
   560      incentive_pool_balance = client_consensus.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   561      log.info("incentive_pool_balance: {}".format(incentive_pool_balance))
   562  
   563      # wait settlement block
   564      client_consensus.economic.wait_settlement_blocknum(client_consensus.node)
   565  
   566      # view incentive account again
   567      incentive_pool_balance1 = client_consensus.node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS)
   568      log.info("incentive_pool_balance: {}".format(incentive_pool_balance1))
   569  
   570      assert incentive_pool_balance1 == incentive_pool_balance, "ErrMsg: incentive account: {}".format(
   571          incentive_pool_balance1)
   572  
   573  
   574  @pytest.mark.P1
   575  def test_AL_BI_004(client_consensus):
   576      """
   577      初始验证人退出后重新质押进来
   578      :param client_consensus:
   579      :return:
   580      """
   581      client = client_consensus
   582      economic = client.economic
   583      node = client.node
   584      # Reset environment
   585      economic.env.deploy_all()
   586      # Query Developer Fund Amount
   587      log.info("nodeid: {}".format(node.node_id))
   588      developer_foundation_balance = node.eth.getBalance(EconomicConfig.DEVELOPER_FOUNDATAION_ADDRESS)
   589      log.info("incentive_pool_balance: {}".format(developer_foundation_balance))
   590      staking_balance = client_consensus.node.eth.getBalance(EconomicConfig.STAKING_ADDRESS)
   591      log.info("staking_balance: {}".format(staking_balance))
   592      # Built in node return pledge
   593      result = client.staking.withdrew_staking(EconomicConfig.DEVELOPER_FOUNDATAION_ADDRESS)
   594      assert_code(result, 0)
   595      # Waiting for the end of the 2 settlement
   596      economic.wait_settlement_blocknum(node, 2)
   597      # create account
   598      address, _ = economic.account.generate_account(node.web3, von_amount(economic.create_staking_limit, 2))
   599      address1, _ = economic.account.generate_account(node.web3, 0)
   600      # Check account balance
   601      balance = node.eth.getBalance(address1)
   602      log.info("Account Balance: {}".format(balance))
   603      # Node pledge again
   604      result = client.staking.create_staking(0, address1, address)
   605      assert_code(result, 0)
   606      # Waiting for the end of the settlement
   607      economic.wait_settlement_blocknum(node)
   608      # view block_reward
   609      block_reward, staking_reward = economic.get_current_year_reward(node)
   610      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   611      # withdrew of pledge
   612      result = client.staking.withdrew_staking(address)
   613      assert_code(result, 0)
   614      # wait settlement block
   615      client.economic.wait_settlement_blocknum(client.node)
   616      # wait consensus block
   617      client.economic.wait_consensus_blocknum(client.node)
   618      # count the number of blocks
   619      blocknumber = client.economic.get_block_count_number(client.node, 10)
   620      log.info("blocknumber: {}".format(blocknumber))
   621      # Check account balance again
   622      balance1 = node.eth.getBalance(address1)
   623      log.info("Account Balance: {}".format(balance1))
   624      # Pledged income account to get the bonus amount
   625      total_reward = int(Decimal(str(block_reward)) * blocknumber) + staking_reward
   626      assert balance1 == balance + total_reward, "ErrMsg:benifit_balance: {}".format(balance1)
   627  
   628  
   629  def create_pledge_node(client, base, multiple=2):
   630      """
   631      create pledge node return benifit balance
   632      :param client:
   633      :param base:
   634      :param multiple:
   635      :return:
   636      """
   637      log.info("Transfer accounts: {}".format(client.economic.create_staking_limit * multiple))
   638      account_balance = client.node.eth.getBalance(
   639          client.economic.account.account_with_money['address'])
   640      log.info("address: {} accounts: {}".format(client.economic.account.account_with_money['address'],
   641                                                 account_balance))
   642      # create account
   643      address, _ = client.economic.account.generate_account(client.node.web3,
   644                                                            client.economic.create_staking_limit * multiple)
   645  
   646      log.info("address: {} ,amount: {}".format(address, client.node.eth.getBalance(address)))
   647      benifit_address, _ = client.economic.account.generate_account(client.node.web3, 0)
   648      log.info(
   649          "address: {} ,amount: {}".format(benifit_address, client.node.eth.getBalance(benifit_address)))
   650      # create staking
   651      staking_amount = von_amount(client.economic.create_staking_limit, base)
   652      result = client.staking.create_staking(0, benifit_address, address, amount=staking_amount)
   653      assert_code(result, 0)
   654      log.info("Pledge node information: {}".format(
   655          client.ppos.getCandidateInfo(client.node.node_id)))
   656      return address, benifit_address
   657  
   658  
   659  @pytest.mark.P1
   660  def test_AL_NBI_001_to_003(client_new_node):
   661      """
   662      AL_NBI_001:非内置验证人Staking奖励(犹豫期)
   663      AL_NBI_002:非内置验证人出块奖励(犹豫期)
   664      AL_NBI_003:非内置验证人区块手续费奖励(犹豫期)
   665      :param client_new_node:
   666      :return:
   667      """
   668      # create pledge node
   669      address, benifit_address = create_pledge_node(client_new_node, 1)
   670      # view account amount
   671      benifit_balance = client_new_node.node.eth.getBalance(benifit_address)
   672      log.info("benifit_balance: {}".format(benifit_balance))
   673      # wait consensus block
   674      client_new_node.economic.wait_consensus_blocknum(client_new_node.node)
   675      # view account amount again
   676      benifit_balance1 = client_new_node.node.eth.getBalance(benifit_address)
   677      log.info("benifit_balance: {}".format(benifit_balance1))
   678      assert benifit_balance1 == benifit_balance, "ErrMsg:benifit_balance: {}".format(
   679          benifit_balance1)
   680  
   681  
   682  @pytest.mark.P1
   683  def test_AL_NBI_004_to_006(new_genesis_env, client_new_node, reset_environment):
   684      """
   685      AL_NBI_004:非内置验证人Staking奖励(候选人)
   686      AL_NBI_005:非内置验证人出块奖励(候选人)
   687      AL_NBI_006:非内置验证人手续费奖励(候选人)
   688      :param new_genesis_env:
   689      :return:
   690      """
   691      # Change configuration parameters
   692      genesis = from_dict(data_class=Genesis, data=new_genesis_env.genesis_config)
   693      genesis.economicModel.staking.maxValidators = 4
   694      new_file = new_genesis_env.cfg.env_tmp + "/genesis.json"
   695      genesis.to_file(new_file)
   696      new_genesis_env.deploy_all(new_file)
   697      # create pledge node
   698      address, benifit_address = create_pledge_node(client_new_node, 1)
   699      # view account amount
   700      benifit_balance = client_new_node.node.eth.getBalance(benifit_address)
   701      log.info("benifit_balance: {}".format(benifit_balance))
   702      # wait settlement block
   703      client_new_node.economic.wait_settlement_blocknum(client_new_node.node, 1)
   704      # view account amount again
   705      benifit_balance1 = client_new_node.node.eth.getBalance(benifit_address)
   706      log.info("benifit_balance: {}".format(benifit_balance1))
   707      assert benifit_balance1 == benifit_balance, "ErrMsg: benifit_balance: {}".format(
   708          benifit_balance1)
   709  
   710  
   711  def view_benifit_reward(client, address):
   712      """
   713      withdrew pledge return benifit balance and Number of blocks
   714      :param client:
   715      :param address:
   716      :return:
   717      """
   718      # withdrew of pledge
   719      result = client.staking.withdrew_staking(address)
   720      assert_code(result, 0)
   721      # wait settlement block
   722      client.economic.wait_settlement_blocknum(client.node)
   723      # wait consensus block
   724      client.economic.wait_consensus_blocknum(client.node)
   725      # count the number of blocks
   726      blocknumber = client.economic.get_block_count_number(client.node, 10)
   727      log.info("blocknumber: {}".format(blocknumber))
   728      return blocknumber
   729  
   730  
   731  @pytest.mark.P1
   732  @pytest.mark.compatibility
   733  def test_AL_NBI_007_to_009(client_new_node):
   734      """
   735      AL_NBI_007:非内置验证人Staking奖励(验证人)
   736      AL_NBI_008:非内置验证人出块奖励(验证人)
   737      AL_NBI_009:非内置验证人手续费奖励(验证人)
   738      :param client_new_node:
   739      :return:
   740      """
   741      # create pledge node
   742      address, benifit_address = create_pledge_node(client_new_node, 1.1)
   743      # view account amount
   744      benifit_balance = client_new_node.node.eth.getBalance(benifit_address)
   745      log.info("benifit_balance: {}".format(benifit_balance))
   746      # wait settlement block
   747      client_new_node.economic.wait_settlement_blocknum(client_new_node.node)
   748      # view block_reward
   749      block_reward, staking_reward = client_new_node.economic.get_current_year_reward(
   750          client_new_node.node)
   751      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   752      for i in range(4):
   753          result = check_node_in_list(client_new_node.node.node_id, client_new_node.ppos.getValidatorList)
   754          log.info("Current node in consensus list status:{}".format(result))
   755          if not result:
   756              # view benifit reward
   757              blocknumber = view_benifit_reward(client_new_node, address)
   758              # view account amount again
   759              benifit_balance1 = client_new_node.node.eth.getBalance(benifit_address)
   760              log.info("benifit_balance: {}".format(benifit_balance1))
   761              assert benifit_balance + staking_reward + blocknumber * Decimal(
   762                  str(block_reward)) - benifit_balance1 < client_new_node.node.web3.toWei(1,
   763                                                                                          'ether'), "ErrMsg:benifit_balance: {}".format(
   764                  benifit_balance1)
   765              break
   766          else:
   767              # wait consensus block
   768              client_new_node.economic.wait_consensus_blocknum(client_new_node.node)
   769  
   770  
   771  def assert_benifit_reward(client, benifit_address, address):
   772      """
   773      assert Amount of income address
   774      :param client:
   775      :param benifit_address:
   776      :param address:
   777      :return:
   778      """
   779      # view account amount
   780      benifit_balance = client.node.eth.getBalance(benifit_address)
   781      log.info("benifit_balance: {}".format(benifit_balance))
   782      # wait settlement block
   783      client.economic.wait_settlement_blocknum(client.node)
   784      # view block_reward
   785      block_reward, staking_reward = client.economic.get_current_year_reward(
   786          client.node)
   787      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   788      for i in range(4):
   789          result = check_node_in_list(client.node.node_id, client.ppos.getValidatorList)
   790          log.info("Current node in consensus list status:{}".format(result))
   791          if result:
   792              # view benifit reward
   793              blocknumber = view_benifit_reward(client, address)
   794              # view account amount again
   795              benifit_balance1 = client.node.eth.getBalance(benifit_address)
   796              log.info("benifit_balance: {}".format(benifit_balance1))
   797              assert benifit_balance + staking_reward + blocknumber * Decimal(
   798                  str(block_reward)) - benifit_balance1 < client.node.web3.toWei(1,
   799                                                                                 'ether'), "ErrMsg:benifit_balance: {}".format(
   800                  benifit_balance1)
   801              break
   802          else:
   803              # wait consensus block
   804              client.economic.wait_consensus_blocknum(client.node)
   805  
   806  
   807  @pytest.mark.P1
   808  def test_AL_NBI_010_to_012(client_new_node):
   809      """
   810      AL_NBI_010:非内置验证人Staking奖励(共识验证人)
   811      AL_NBI_011:非内置验证人出块奖励(共识验证人)
   812      AL_NBI_012:非内置验证人手续费出块奖励(共识验证人)
   813      :param client_new_node:
   814      :return:
   815      """
   816      # create pledge node
   817      address, benifit_address = create_pledge_node(client_new_node, 1.2)
   818      # assert benifit reward
   819      assert_benifit_reward(client_new_node, benifit_address, address)
   820  
   821  
   822  @pytest.mark.P1
   823  def test_AL_NBI_013(client_new_node):
   824      """
   825      修改节点质押收益地址查看收益变更
   826      :param client_new_node:
   827      :return:
   828      """
   829      # create pledge node
   830      address, benifit_address = create_pledge_node(client_new_node, 1.3)
   831      # create account
   832      benifit_address1, _ = client_new_node.economic.account.generate_account(client_new_node.node.web3, 0)
   833      # change benifit address
   834      result = client_new_node.staking.edit_candidate(address, benifit_address1)
   835      assert_code(result, 0)
   836      # assert benifit reward
   837      assert_benifit_reward(client_new_node, benifit_address1, address)
   838  
   839  
   840  def query_ccount_amount(client, address):
   841      balance = client.node.eth.getBalance(address)
   842      log.info("balance: {}".format(balance))
   843      return balance
   844  
   845  
   846  @pytest.mark.P1
   847  def test_AL_NBI_014(client_new_node):
   848      """
   849      修改节点质押收益地址查看收益变更(正在出块中)
   850      :param client_new_node:
   851      :return:
   852      """
   853      # create pledge node
   854      address, benifit_address = create_pledge_node(client_new_node, 1.4)
   855      # wait settlement block
   856      client_new_node.economic.wait_settlement_blocknum(client_new_node.node)
   857      # view block_reward
   858      block_reward, staking_reward = client_new_node.economic.get_current_year_reward(
   859          client_new_node.node)
   860      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   861      # view benifit_address amount again
   862      benifit_balance = query_ccount_amount(client_new_node, benifit_address)
   863      # change benifit address
   864      for i in range(4):
   865          result = check_node_in_list(client_new_node.node.node_id, client_new_node.ppos.getValidatorList)
   866          log.info("Current node in consensus list status:{}".format(result))
   867          if result:
   868              current_block = client_new_node.node.eth.blockNumber
   869              log.info("Current block:{}".format(current_block))
   870              for i in range(40):
   871                  nodeid = get_pub_key(client_new_node.node.url, current_block)
   872                  current_block = client_new_node.node.eth.blockNumber
   873                  log.info("当前块高:{}".format(current_block))
   874                  time.sleep(3)
   875                  if nodeid == client_new_node.node.node_id:
   876                      break
   877              # create account
   878              benifit_address1, _ = client_new_node.economic.account.generate_account(client_new_node.node.web3, 0)
   879              # change benifit address
   880              result = client_new_node.staking.edit_candidate(address, benifit_address1)
   881              assert_code(result, 0)
   882              # view benifit reward
   883              blocknumber = view_benifit_reward(client_new_node, address)
   884  
   885              # view benifit_address1 amount
   886              benifit_balance1 = query_ccount_amount(client_new_node, benifit_address1)
   887              assert benifit_balance + benifit_balance1 == int(Decimal(str(
   888                  block_reward)) * blocknumber) + staking_reward, "ErrMsg:benifit_balance + benifit_balance1: {}".format(
   889                  benifit_balance + benifit_balance1)
   890  
   891  
   892  @pytest.mark.P1
   893  def test_AL_NBI_015(client_new_node):
   894      """
   895      退回质押金并处于锁定期
   896      :param client_new_node:
   897      :return:
   898      """
   899      # create pledge node
   900      address, benifit_address = create_pledge_node(client_new_node, 1.5)
   901      # wait settlement block
   902      client_new_node.economic.wait_settlement_blocknum(client_new_node.node)
   903      # view account amount
   904      benifit_balance = query_ccount_amount(client_new_node, benifit_address)
   905      for i in range(4):
   906          result = check_node_in_list(client_new_node.node.node_id, client_new_node.ppos.getValidatorList)
   907          log.info("Current node in consensus list status:{}".format(result))
   908          if result:
   909              # withdrew of pledge
   910              result = client_new_node.staking.withdrew_staking(address)
   911              assert_code(result, 0)
   912              log.info("Current settlement cycle verifier list:{}".format(client_new_node.ppos.getVerifierList()))
   913              for i in range(40):
   914                  client_new_node.economic.account.sendTransaction(client_new_node.node.web3, '',
   915                                                                   client_new_node.economic.account.account_with_money[
   916                                                                       'address'], address,
   917                                                                   client_new_node.node.web3.platon.gasPrice,
   918                                                                   21000, 100)
   919                  time.sleep(1)
   920              # view account amount again
   921              benifit_balance1 = query_ccount_amount(client_new_node, benifit_address)
   922              assert benifit_balance1 > benifit_balance, "ErrMsg: {} > {}".format(benifit_balance1, benifit_balance)
   923              break
   924          else:
   925              # wait consensus block
   926              client_new_node.economic.wait_consensus_blocknum(client_new_node.node)
   927  
   928  
   929  @pytest.mark.P2
   930  @pytest.mark.compatibility
   931  def test_AL_NBI_016(client_new_node, reset_environment):
   932      """
   933      被双签处罚槛剔除验证人列表
   934      :param client_new_node:
   935      :return:
   936      """
   937      client = client_new_node
   938      economic = client.economic
   939      node = client.node
   940      client.economic.env.deploy_all()
   941      # create account
   942      address1, _ = economic.account.generate_account(node.web3, von_amount(economic.create_staking_limit, 2))
   943      address2, _ = economic.account.generate_account(node.web3, 0)
   944      report_address, _ = economic.account.generate_account(node.web3, node.web3.toWei(1000, 'ether'))
   945      # create staking
   946      staking_amount = von_amount(economic.create_staking_limit, 1.6)
   947      result = client_new_node.staking.create_staking(0, address2, address1, amount=staking_amount)
   948      assert_code(result, 0)
   949      # wait settlement block
   950      economic.wait_settlement_blocknum(node)
   951      # Check account balance
   952      balance = node.eth.getBalance(address2)
   953      log.info("Account Balance:{}".format(balance))
   954      # view block_reward
   955      block_reward, staking_reward = economic.get_current_year_reward(node)
   956      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
   957      for i in range(4):
   958          result = check_node_in_list(client_new_node.node.node_id, client_new_node.ppos.getValidatorList)
   959          log.info("Current node in consensus list status:{}".format(result))
   960          if result:
   961              # view Current block
   962              current_block = client_new_node.node.eth.blockNumber
   963              log.info("Current block: {}".format(current_block))
   964              # Report prepareblock signature
   965              report_information = mock_duplicate_sign(1, client_new_node.node.nodekey, client_new_node.node.blsprikey,
   966                                                       current_block)
   967              log.info("Report information: {}".format(report_information))
   968              result = client_new_node.duplicatesign.reportDuplicateSign(1, report_information, report_address)
   969              assert_code(result, 0)
   970              # wait settlement block
   971              economic.wait_settlement_blocknum(node)
   972              # Check account balance again
   973              balance1 = node.eth.getBalance(address2)
   974              log.info("Account Balance:{}".format(balance1))
   975              # count the number of blocks
   976              blocknumber = client_new_node.economic.get_block_count_number(node, 10)
   977              log.info("blocknumber: {}".format(blocknumber))
   978              total_block_reward = int(Decimal(str(block_reward)) * Decimal(str(blocknumber)))
   979              log.info("total_block_reward: {}".format(total_block_reward))
   980              assert balance1 == balance + total_block_reward, "ErrMsg:benifit_balance1:{}".format(balance1)
   981              break
   982          else:
   983              # wait consensus block
   984              economic.wait_consensus_blocknum(node)
   985  
   986  
   987  @pytest.mark.P2
   988  @pytest.mark.compatibility
   989  def test_AL_NBI_017(clients_new_node):
   990      """
   991      0出块率剔除验证人列表
   992      :param clients_new_node:
   993      :return:
   994      """
   995      clients_new_node[0].economic.env.deploy_all()
   996      # create pledge node
   997      address, benifit_address = create_pledge_node(clients_new_node[0], 1.6)
   998      # wait settlement block
   999      clients_new_node[0].economic.wait_settlement_blocknum(clients_new_node[0].node)
  1000      log.info("Current settlement cycle verifier list:{}".format(clients_new_node[0].ppos.getVerifierList()))
  1001      # view block_reward
  1002      block_reward, staking_reward = clients_new_node[0].economic.get_current_year_reward(
  1003          clients_new_node[0].node)
  1004      log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
  1005      # view account amount
  1006      benifit_balance = query_ccount_amount(clients_new_node[0], benifit_address)
  1007      for i in range(4):
  1008          result = check_node_in_list(clients_new_node[0].node.node_id, clients_new_node[0].ppos.getValidatorList)
  1009          log.info("Current node in consensus list status:{}".format(result))
  1010          if result:
  1011              # stop node
  1012              clients_new_node[0].node.stop()
  1013              log.info("Current settlement cycle verifier list:{}".format(clients_new_node[1].ppos.getVerifierList()))
  1014              # wait settlement block
  1015              clients_new_node[1].economic.wait_settlement_blocknum(clients_new_node[1].node)
  1016              # view account amount again
  1017              benifit_balance1 = query_ccount_amount(clients_new_node[1], benifit_address)
  1018              # count the number of blocks
  1019              blocknumber = clients_new_node[1].economic.get_block_count_number(clients_new_node[1].node, 5)
  1020              log.info("blocknumber: {}".format(blocknumber))
  1021              assert benifit_balance1 == benifit_balance + int(
  1022                  Decimal(str(block_reward)) * blocknumber), "ErrMsg:benifit_balance1:{}".format(benifit_balance1)
  1023              break
  1024          else:
  1025              # wait consensus block
  1026              clients_new_node[0].economic.wait_consensus_blocknum(clients_new_node[0].node)
  1027  
  1028  
  1029  @pytest.mark.P1
  1030  def test_AL_NBI_018(new_genesis_env, client_new_node):
  1031      """
  1032      调整质押和出块奖励比例
  1033      :param client_new_node:
  1034      :return:
  1035      """
  1036      # Change configuration parameters
  1037      genesis = from_dict(data_class=Genesis, data=new_genesis_env.genesis_config)
  1038      genesis.economicModel.reward.newBlockRate = 60
  1039      new_file = new_genesis_env.cfg.env_tmp + "/genesis.json"
  1040      genesis.to_file(new_file)
  1041      new_genesis_env.deploy_all(new_file)
  1042  
  1043      client = client_new_node
  1044      economic = client.economic
  1045      node = client.node
  1046      # create account
  1047      address1, _ = economic.account.generate_account(node.web3, von_amount(economic.create_staking_limit, 2))
  1048      address2, _ = economic.account.generate_account(node.web3, 0)
  1049      # create pledge
  1050      result = client.staking.create_staking(0, address1, address1)
  1051      assert_code(result, 0)
  1052      # Waiting for the end of the settlement
  1053      economic.wait_settlement_blocknum(node)
  1054      # Check account balance
  1055      balance = node.eth.getBalance(address1)
  1056      log.info("Account Balance: {}".format(balance))
  1057      block_reward, staking_reward = economic.get_current_year_reward(node)
  1058      # # Get the number of certifiers in the billing cycle list
  1059      # verifier_list = get_pledge_list(node.ppos.getVerifierList)
  1060      # verifier_num = len(verifier_list)
  1061      # # Get block_reward And pledge rewards
  1062      # amount = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, 0)
  1063      # block_proportion = str(60 / 100)
  1064      # staking_proportion = str(1 - 60 / 100)
  1065      # block_reward = int(Decimal(str(amount)) * Decimal(str(block_proportion)) / Decimal(str(1600)))
  1066      # staking_reward = int(Decimal(str(amount)) * Decimal(str(staking_proportion)) / Decimal(str(10)) / Decimal(
  1067      #     str(verifier_num)))
  1068      # log.info("block_reward: {} staking_reward: {}".format(block_reward, staking_reward))
  1069      # withdrew of pledge
  1070      result = client.staking.withdrew_staking(address1)
  1071      assert_code(result, 0)
  1072      # wait settlement block
  1073      client.economic.wait_settlement_blocknum(client.node)
  1074      # wait consensus block
  1075      client.economic.wait_consensus_blocknum(client.node)
  1076      # count the number of blocks
  1077      blocknumber = client.economic.get_block_count_number(client.node, 10)
  1078      log.info("blocknumber: {}".format(blocknumber))
  1079      # Check account balance again
  1080      balance1 = node.eth.getBalance(address1)
  1081      log.info("Account Balance: {}".format(balance1))
  1082      # Pledged income account to get the bonus amount
  1083      total_reward = int(Decimal(str(block_reward)) * blocknumber) + staking_reward
  1084      log.info("total:{}".format(balance + total_reward))
  1085      assert balance + total_reward - balance1 < node.web3.toWei(1, 'ether'), "ErrMsg:benifit_balance: {}".format(
  1086          balance1)
  1087  
  1088      # # create pledge node
  1089      # address, benifit_address = create_pledge_node(client_new_node, 1.2)
  1090      # # assert benifit reward
  1091      # assert_benifit_reward(client_new_node, benifit_address, address)
  1092  
  1093  
  1094  def calculate_block_rewards_and_pledge_rewards(client, incentive_pool_amount, annualcycle):
  1095      new_block_rate = client.economic.genesis.economicModel.reward.newBlockRate
  1096      block_proportion = str(new_block_rate / 100)
  1097      log.info("Get incentive pool to allocate block reward ratio: {}".format(block_proportion))
  1098      verifier_list = get_pledge_list(client.node.ppos.getVerifierList)
  1099      verifier_num = len(verifier_list)
  1100      log.info("Number of verification nodes in the current settlement cycle: {}".format(verifier_num))
  1101      amount_per_settlement = int(Decimal(str(incentive_pool_amount)) / Decimal(str(annualcycle)))
  1102      total_block_rewards = int(Decimal(str(amount_per_settlement)) * Decimal(str(block_proportion)))
  1103      per_block_reward = int(Decimal(str(total_block_rewards)) / Decimal(str(client.economic.settlement_size)))
  1104      log.info("Total block rewards: {} Each block reward: {}".format(total_block_rewards, per_block_reward))
  1105      staking_reward_total = amount_per_settlement - total_block_rewards
  1106      staking_reward = int(Decimal(str(staking_reward_total)) / Decimal(str(verifier_num)))
  1107      log.info("Total pledged rewards: {} Amount of pledged rewards in current settlement cycle: {}".format(
  1108          staking_reward_total, staking_reward))
  1109      return per_block_reward, staking_reward
  1110  
  1111  
  1112  def test_AL_NBI_019(client_consensus):
  1113      """
  1114      验证第一个结算周期区块奖励和质押奖励
  1115      :param client_consensus:
  1116      :return:
  1117      """
  1118      client = client_consensus
  1119      economic = client.economic
  1120      node = client.node
  1121      log.info("Start resetting the chain")
  1122      economic.env.deploy_all()
  1123      time.sleep(5)
  1124      incentive_pool_balance = 262215742000000000000000000
  1125      log.info("Get the initial value of the incentive pool:{}".format(incentive_pool_balance))
  1126      annualcycle = math.ceil((economic.additional_cycle_time * 60) / economic.settlement_size)
  1127      log.info("Number of current additional settlement cycles:{}".format(annualcycle))
  1128      annual_size = annualcycle * economic.settlement_size
  1129      log.info("Block height of current issuance cycle: {}".format(annual_size))
  1130      per_block_reward, staking_reward = calculate_block_rewards_and_pledge_rewards(client, incentive_pool_balance, annualcycle)
  1131      chain_block_reward, chain_staking_reward = economic.get_current_year_reward(node)
  1132      log.info("Block rewards on the chain: {}".format(chain_block_reward))
  1133      log.info("Pledge rewards on the chain:{}".format(chain_staking_reward))
  1134      assert per_block_reward == chain_block_reward, "ErrMsg:Block reward for the first settlement cycle {}".format(per_block_reward)
  1135      assert staking_reward == chain_staking_reward, "ErrMsg:Pledge rewards for the first settlement cycle {}".format(staking_reward)
  1136  
  1137  
  1138  def test_AL_NBI_020(client_consensus):
  1139      """
  1140      调整出块间隔,查看第二个结算周期出块奖励和质押奖励
  1141      :param client_consensus:
  1142      :return:
  1143      """
  1144      client = client_consensus
  1145      economic = client.economic
  1146      node = client.node
  1147      economic.env.deploy_all()
  1148      economic.wait_consensus_blocknum(node)
  1149      log.info("Start adjusting the block interval")
  1150      for i in range(3):
  1151          economic.env.stop_all()
  1152          time.sleep(2)
  1153          economic.env.start_nodes(economic.env.get_all_nodes(), False)
  1154          time.sleep(5)
  1155      current_settlement_block_height = economic.get_settlement_switchpoint(node)
  1156      log.info("Block height of current settlement cycle: {}".format(current_settlement_block_height))
  1157      wait_block_number(node, current_settlement_block_height)
  1158      annualcycle = math.ceil((economic.additional_cycle_time * 60) / economic.settlement_size)
  1159      log.info("Number of current additional settlement cycles:{}".format(annualcycle))
  1160      incentive_pool_balance = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, 0)
  1161      remaining_incentive_pool_amount = incentive_pool_balance - int(Decimal(str(incentive_pool_balance)) / Decimal(str(annualcycle)))
  1162      log.info("Settlement block high incentive pool balance: {}".format(remaining_incentive_pool_amount))
  1163      block_info = node.eth.getBlock(1)
  1164      first_timestamp = block_info['timestamp']
  1165      log.info("First block timestamp: {}".format(first_timestamp))
  1166      settlement_block_info = node.eth.getBlock(current_settlement_block_height)
  1167      settlement_timestamp = settlement_block_info['timestamp']
  1168      log.info("High block timestamp at the end of the current settlement cycle: {}".format(settlement_timestamp))
  1169      issuing_cycle_timestamp = first_timestamp + (economic.additional_cycle_time * 60000)
  1170      log.info("End time stamp of current issue cycle: {}".format(issuing_cycle_timestamp))
  1171      remaining_additional_time = issuing_cycle_timestamp - settlement_timestamp
  1172      log.info("Remaining time of current issuance cycle: {}".format(remaining_additional_time))
  1173      average_interval = (settlement_timestamp - first_timestamp) // (economic.settlement_size - 1)
  1174      log.info("Block interval in the last settlement cycle: {}".format(average_interval))
  1175      number_of_remaining_blocks = math.ceil(remaining_additional_time / average_interval)
  1176      log.info("Remaining block height of current issuance cycle: {}".format(number_of_remaining_blocks))
  1177      remaining_settlement_cycle = math.ceil(number_of_remaining_blocks / economic.settlement_size)
  1178      log.info("remaining settlement cycles in the current issuance cycle: {}".format(remaining_settlement_cycle))
  1179      per_block_reward, staking_reward = calculate_block_rewards_and_pledge_rewards(client, remaining_incentive_pool_amount, remaining_settlement_cycle)
  1180      chain_block_reward, chain_staking_reward = economic.get_current_year_reward(node)
  1181      log.info("Block rewards on the chain: {}".format(chain_block_reward))
  1182      log.info("Pledge rewards on the chain:{}".format(chain_staking_reward))
  1183      result = client.ppos.getAvgPackTime()
  1184      chain_time_interval = result['Ret']
  1185      log.info("Block interval on the chain:{}".format(chain_time_interval))
  1186      assert per_block_reward == chain_block_reward, "ErrMsg:Block rewards for the current settlement cycle {}".format(
  1187          per_block_reward)
  1188      assert staking_reward == chain_staking_reward, "ErrMsg:Pledge rewards for the current settlement cycle {}".format(
  1189          staking_reward)
  1190      assert average_interval == chain_time_interval, "ErrMsg:Block interval in the last settlement cycle {}".format(average_interval)
  1191  
  1192  
  1193  def AL_FI_006(client_consensus):
  1194      """
  1195      增发周期动态调整
  1196      :param client_consensus:
  1197      :return:
  1198      """
  1199      client = client_consensus
  1200      economic = client.economic
  1201      node = client.node
  1202      economic.env.deploy_all()
  1203      log.info("Chain reset completed")
  1204      economic.wait_consensus_blocknum(node)
  1205      log.info("Start adjusting the block interval")
  1206      for i in range(3):
  1207          economic.env.stop_all()
  1208          time.sleep(2)
  1209          economic.env.start_nodes(economic.env.get_all_nodes(), False)
  1210          time.sleep(5)
  1211      remaining_settlement_cycle = (economic.additional_cycle_time * 60) // economic.settlement_size
  1212      annual_size = remaining_settlement_cycle * economic.settlement_size
  1213      log.info("Additional issue settlement period:{} Block height of current issuance cycle: {}".format(remaining_settlement_cycle, annual_size))
  1214      economic.wait_settlement_blocknum(node)
  1215      while remaining_settlement_cycle != 1:
  1216          block_info = node.eth.getBlock(1)
  1217          first_timestamp = block_info['timestamp']
  1218          log.info("First block timestamp: {}".format(first_timestamp))
  1219          tmp_current_block = node.eth.blockNumber
  1220          if tmp_current_block % economic.settlement_size == 0:
  1221              time.sleep(1)
  1222          last_settlement_block = (math.ceil(tmp_current_block / economic.settlement_size) - 1) * economic.settlement_size
  1223          log.info("The last block height of the previous settlement period: {}".format(last_settlement_block))
  1224          settlement_block_info = node.eth.getBlock(last_settlement_block)
  1225          settlement_timestamp = settlement_block_info['timestamp']
  1226          log.info("High block timestamp at the end of the current settlement cycle: {}".format(settlement_timestamp))
  1227          issuing_cycle_timestamp = first_timestamp + (economic.additional_cycle_time * 60) * 1000
  1228          log.info("End time stamp of current issue cycle: {}".format(issuing_cycle_timestamp))
  1229          remaining_additional_time = issuing_cycle_timestamp - settlement_timestamp
  1230          log.info("Remaining time of current issuance cycle: {}".format(remaining_additional_time))
  1231          average_interval = (settlement_timestamp - first_timestamp) // (last_settlement_block - 1)
  1232          log.info("Block interval in the last settlement cycle: {}".format(average_interval))
  1233          number_of_remaining_blocks = math.ceil(remaining_additional_time / average_interval)
  1234          log.info("Remaining block height of current issuance cycle: {}".format(number_of_remaining_blocks))
  1235          remaining_settlement_cycle = math.ceil(number_of_remaining_blocks / economic.settlement_size)
  1236          log.info("remaining settlement cycles in the current issuance cycle: {}".format(remaining_settlement_cycle))
  1237          consensus_verification_list = node.ppos.getVerifierList()
  1238          log.info("List of consensus validators in the current settlement cycle: {}".format(consensus_verification_list))
  1239          economic.wait_settlement_blocknum(node)
  1240      plan_incentive_pool_amount = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, annual_size)
  1241      annual_last_block = (math.ceil(node.eth.blockNumber / economic.settlement_size) - 1) * economic.settlement_size
  1242      # current_increase_last_block = economic.get_settlement_switchpoint(node)
  1243      # log.info("The current issue cycle is high: {}".format(current_increase_last_block))
  1244      actual_incentive_pool_amount = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, annual_last_block)
  1245      log.info("Incentive pool actual amount: {}".format(actual_incentive_pool_amount))
  1246      assert actual_incentive_pool_amount > plan_incentive_pool_amount, "ErrMsg:Incentive pool balance {}".format(actual_incentive_pool_amount)
  1247  
  1248  
  1249  def AL_FI_007(client_consensus):
  1250      """
  1251      验证增发第一年出块奖励和质押奖励
  1252      :param client_consensus:
  1253      :return:
  1254      """
  1255      client = client_consensus
  1256      economic = client.economic
  1257      node = client.node
  1258      economic.env.deploy_all()
  1259      log.info("Chain reset completed")
  1260      economic.wait_consensus_blocknum(node)
  1261      log.info("Start adjusting the block interval")
  1262      for i in range(3):
  1263          economic.env.stop_all()
  1264          time.sleep(2)
  1265          economic.env.start_nodes(economic.env.get_all_nodes(), False)
  1266          time.sleep(5)
  1267      remaining_settlement_cycle = (economic.additional_cycle_time * 60) // economic.settlement_size
  1268      annual_size = remaining_settlement_cycle * economic.settlement_size
  1269      log.info("Additional issue settlement period:{} Block height of current issuance cycle: {}".format(
  1270          remaining_settlement_cycle, annual_size))
  1271      economic.wait_settlement_blocknum(node)
  1272      block_info = node.eth.getBlock(1)
  1273      first_timestamp = block_info['timestamp']
  1274      log.info("First block timestamp: {}".format(first_timestamp))
  1275      issuing_cycle_timestamp = None
  1276      while remaining_settlement_cycle != 1:
  1277          tmp_current_block = node.eth.blockNumber
  1278          if tmp_current_block % economic.settlement_size == 0:
  1279              time.sleep(1)
  1280          last_settlement_block = (math.ceil(tmp_current_block / economic.settlement_size) - 1) * economic.settlement_size
  1281          log.info("The last block height of the previous settlement period: {}".format(last_settlement_block))
  1282          settlement_block_info = node.eth.getBlock(last_settlement_block)
  1283          settlement_timestamp = settlement_block_info['timestamp']
  1284          log.info("High block timestamp at the end of the current settlement cycle: {}".format(settlement_timestamp))
  1285          issuing_cycle_timestamp = first_timestamp + (economic.additional_cycle_time * 60000)
  1286          log.info("End time stamp of current issue cycle: {}".format(issuing_cycle_timestamp))
  1287          remaining_additional_time = issuing_cycle_timestamp - settlement_timestamp
  1288          log.info("Remaining time of current issuance cycle: {}".format(remaining_additional_time))
  1289          average_interval = (settlement_timestamp - first_timestamp) // (last_settlement_block - 1)
  1290          log.info("Block interval in the last settlement cycle: {}".format(average_interval))
  1291          number_of_remaining_blocks = math.ceil(remaining_additional_time / average_interval)
  1292          log.info("Remaining block height of current issuance cycle: {}".format(number_of_remaining_blocks))
  1293          remaining_settlement_cycle = math.ceil(number_of_remaining_blocks / economic.settlement_size)
  1294          log.info("remaining settlement cycles in the current issuance cycle: {}".format(remaining_settlement_cycle))
  1295          consensus_verification_list = node.ppos.getVerifierList()
  1296          log.info("List of consensus validators in the current settlement cycle: {}".format(consensus_verification_list))
  1297          economic.wait_settlement_blocknum(node)
  1298      annual_last_block = (math.ceil(node.eth.blockNumber / economic.settlement_size) - 1) * economic.settlement_size
  1299      log.info("Last block height of last year:{}".format(annual_last_block))
  1300      settlement_block_info = node.eth.getBlock(annual_last_block)
  1301      settlement_timestamp = settlement_block_info['timestamp']
  1302      log.info("Second High block timestamp at the end of the current settlement cycle:{}".format(settlement_timestamp))
  1303      second_issuing_cycle_timestamp = issuing_cycle_timestamp + (economic.additional_cycle_time * 60000)
  1304      log.info("Second end time stamp of current issue cycle: {}".format(second_issuing_cycle_timestamp))
  1305      remaining_additional_time = second_issuing_cycle_timestamp - settlement_timestamp
  1306      log.info("Second Remaining time of current issuance cycle: {}".format(remaining_additional_time))
  1307      average_interval = (settlement_timestamp - first_timestamp) // (annual_last_block - 1)
  1308      log.info("Second Block interval in the last settlement cycle: {}".format(average_interval))
  1309      number_of_remaining_blocks = math.ceil(remaining_additional_time / average_interval)
  1310      log.info("Second Remaining block height of current issuance cycle: {}".format(number_of_remaining_blocks))
  1311      remaining_settlement_cycle = math.ceil(number_of_remaining_blocks / economic.settlement_size)
  1312      log.info("The additional settlement cycle in the second year: {}".format(number_of_remaining_blocks))
  1313      actual_incentive_pool_amount = node.eth.getBalance(EconomicConfig.INCENTIVEPOOL_ADDRESS, annual_last_block)
  1314      log.info("Incentive pool actual amount: {}".format(actual_incentive_pool_amount))
  1315      per_block_reward, staking_reward = calculate_block_rewards_and_pledge_rewards(client, actual_incentive_pool_amount, remaining_settlement_cycle)
  1316      chain_block_reward, chain_staking_reward = economic.get_current_year_reward(node)
  1317      log.info("Block rewards on the chain: {}".format(chain_block_reward))
  1318      log.info("Pledge rewards on the chain:{}".format(chain_staking_reward))
  1319      result = client.ppos.getAvgPackTime()
  1320      chain_time_interval = result['Ret']
  1321      log.info("Block interval on the chain:{}".format(chain_time_interval))
  1322      assert per_block_reward == chain_block_reward, "ErrMsg:Block rewards for the current settlement cycle {}".format(
  1323          per_block_reward)
  1324      assert staking_reward == chain_staking_reward, "ErrMsg:Pledge rewards for the current settlement cycle {}".format(
  1325          staking_reward)
  1326      assert average_interval == chain_time_interval, "ErrMsg:Block interval in the last settlement cycle {}".format(average_interval)
  1327      economic.wait_settlement_blocknum(node)
  1328  
  1329      amount_per_settlement = int(Decimal(str(actual_incentive_pool_amount)) / Decimal(str(remaining_settlement_cycle)))
  1330      remaining_incentive_pool_balance = actual_incentive_pool_amount - amount_per_settlement
  1331      log.info("Amount of remaining incentive pool: {}".format(remaining_incentive_pool_balance))
  1332      first_settlement_cycle = annual_last_block / economic.settlement_size
  1333      log.info("Number of first additional issue settlement cycles: {}".format(first_settlement_cycle))
  1334      current_last_block = (math.ceil(node.eth.blockNumber / economic.settlement_size) - 1) * economic.settlement_size
  1335      log.info("Current settlement block height: {}".format(current_last_block))
  1336      current_settlement_cycle = current_last_block / economic.settlement_size
  1337      log.info("Current latest settlement cycles: {}".format(current_settlement_cycle))
  1338      number = current_settlement_cycle - first_settlement_cycle
  1339      log.info("Phase difference period: {}".format(number))
  1340      second_start_info = node.eth.getBlock(int((first_settlement_cycle - (first_settlement_cycle - number)) * economic.settlement_size))
  1341      second_start_timestamp = second_start_info['timestamp']
  1342      log.info("second start timestamp : {}".format(second_start_timestamp))
  1343      second_end_info = node.eth.getBlock(current_last_block)
  1344      second_end_timestamp = second_end_info['timestamp']
  1345      log.info("second end timestamp : {}".format(second_end_timestamp))
  1346      average_interval = (second_end_timestamp - second_start_timestamp) // (annual_last_block - 1)
  1347      log.info("Second Block interval in the last settlement cycle: {}".format(average_interval))
  1348  
  1349      remaining_additional_time = second_issuing_cycle_timestamp - second_end_timestamp
  1350      log.info("Second Remaining time of current issuance cycle: {}".format(remaining_additional_time))
  1351      number_of_remaining_blocks = math.ceil(remaining_additional_time / average_interval)
  1352      log.info("Second Remaining block height of current issuance cycle: {}".format(number_of_remaining_blocks))
  1353      remaining_settlement_cycle = math.ceil(number_of_remaining_blocks / economic.settlement_size)
  1354      log.info("The additional settlement cycle in the second year: {}".format(number_of_remaining_blocks))
  1355      per_block_reward, staking_reward = calculate_block_rewards_and_pledge_rewards(client, remaining_incentive_pool_balance,
  1356                                                                                    remaining_settlement_cycle)
  1357      chain_block_reward, chain_staking_reward = economic.get_current_year_reward(node)
  1358      log.info("Block rewards on the chain: {}".format(chain_block_reward))
  1359      log.info("Pledge rewards on the chain:{}".format(chain_staking_reward))
  1360      result = client.ppos.getAvgPackTime()
  1361      chain_time_interval = result['Ret']
  1362      log.info("Block interval on the chain:{}".format(chain_time_interval))
  1363      assert per_block_reward == chain_block_reward, "ErrMsg:Block rewards for the current settlement cycle {}".format(
  1364          per_block_reward)
  1365      assert staking_reward == chain_staking_reward, "ErrMsg:Pledge rewards for the current settlement cycle {}".format(
  1366          staking_reward)
  1367      assert average_interval == chain_time_interval, "ErrMsg:Block interval in the last settlement cycle {}".format(
  1368          average_interval)