github.com/turingchain2020/turingchain@v1.1.21/account/execaccount_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package account
     6  
     7  import (
     8  	"testing"
     9  	//"fmt"
    10  
    11  	"github.com/turingchain2020/turingchain/common/address"
    12  	//"github.com/turingchain2020/turingchain/queue"
    13  	"github.com/turingchain2020/turingchain/types"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func (acc *DB) GenerExecAccData(execaddr string) {
    18  	// 加入账户
    19  	account := &types.Account{
    20  		Balance: 1000 * 1e8,
    21  		Frozen:  20 * 1e8,
    22  		Addr:    addr1,
    23  	}
    24  	acc.SaveExecAccount(execaddr, account)
    25  	acc.SaveAccount(account)
    26  
    27  	account.Balance = 900 * 1e8
    28  	account.Frozen = 20 * 1e8
    29  	account.Addr = addr2
    30  	acc.SaveExecAccount(execaddr, account)
    31  	acc.SaveAccount(account)
    32  }
    33  
    34  func TestLoadExecAccountQueue(t *testing.T) {
    35  
    36  	q := initEnv()
    37  	qAPI, _ := initQueAPI(q)
    38  	blockchainProcess(q)
    39  	storeProcess(q)
    40  
    41  	execaddress := address.ExecAddress("ticket")
    42  	accCoin, _ := GenerAccDb()
    43  	acc, err := accCoin.LoadExecAccountQueue(qAPI, addr1, execaddress)
    44  	require.NoError(t, err)
    45  	t.Logf("LoadExecAccountQueue is %v", acc)
    46  }
    47  
    48  func TestTransferToExec(t *testing.T) {
    49  	q := initEnv()
    50  	blockchainProcess(q)
    51  	storeProcess(q)
    52  
    53  	accCoin, _ := GenerAccDb()
    54  	accCoin.GenerAccData()
    55  	_, err := accCoin.TransferToExec(addr1, addr2, 10*1e8)
    56  	require.NoError(t, err)
    57  	t.Logf("TransferToExec from addr balance [%d] to addr balance [%d]",
    58  		accCoin.LoadAccount(addr1).Balance,
    59  		accCoin.LoadAccount(addr2).Balance)
    60  	require.Equal(t, int64(1000*1e8-10*1e8), accCoin.LoadAccount(addr1).Balance)
    61  	require.Equal(t, int64(900*1e8+10*1e8), accCoin.LoadAccount(addr2).Balance)
    62  }
    63  
    64  func TestTransferWithdraw(t *testing.T) {
    65  	q := initEnv()
    66  	blockchainProcess(q)
    67  	storeProcess(q)
    68  
    69  	accCoin, _ := GenerAccDb()
    70  	execaddr := address.ExecAddress("coins")
    71  
    72  	account := &types.Account{
    73  		Balance: 1000 * 1e8,
    74  		Frozen:  0,
    75  		Addr:    execaddr,
    76  	}
    77  	//往合约地址中打钱
    78  	accCoin.SaveAccount(account)
    79  
    80  	//往执行账户中打钱
    81  	account.Addr = addr1
    82  	accCoin.SaveExecAccount(execaddr, account)
    83  
    84  	_, err := accCoin.TransferWithdraw(addr1, execaddr, 10*1e8)
    85  	require.NoError(t, err)
    86  	t.Logf("TransferWithdraw [%d]___[%d]",
    87  		accCoin.LoadAccount(execaddr).Balance,
    88  		accCoin.LoadExecAccount(addr1, execaddr).Balance)
    89  }
    90  
    91  func TestExecFrozen(t *testing.T) {
    92  	q := initEnv()
    93  	blockchainProcess(q)
    94  	storeProcess(q)
    95  
    96  	execaddress := address.ExecAddress("coins")
    97  	accCoin, _ := GenerAccDb()
    98  	accCoin.GenerExecAccData(execaddress)
    99  	_, err := accCoin.ExecFrozen(addr1, execaddress, 10*1e8)
   100  	require.NoError(t, err)
   101  
   102  	t.Logf("ExecFrozen [%d]___[%d]",
   103  		accCoin.LoadExecAccount(addr1, execaddress).Balance,
   104  		accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   105  
   106  	require.Equal(t, int64(1000*1e8-10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Balance)
   107  	require.Equal(t, int64(20*1e8+10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   108  }
   109  
   110  func TestExecActive(t *testing.T) {
   111  	q := initEnv()
   112  	blockchainProcess(q)
   113  	storeProcess(q)
   114  
   115  	execaddress := address.ExecAddress("coins")
   116  	accCoin, _ := GenerAccDb()
   117  	accCoin.GenerExecAccData(execaddress)
   118  	_, err := accCoin.ExecActive(addr1, execaddress, 10*1e8)
   119  	require.NoError(t, err)
   120  
   121  	t.Logf("ExecActive [%d]___[%d]",
   122  		accCoin.LoadExecAccount(addr1, execaddress).Balance,
   123  		accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   124  
   125  	require.Equal(t, int64(1000*1e8+10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Balance)
   126  	require.Equal(t, int64(20*1e8-10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   127  }
   128  
   129  func TestExecTransfer(t *testing.T) {
   130  	q := initEnv()
   131  	blockchainProcess(q)
   132  	storeProcess(q)
   133  
   134  	execaddress := address.ExecAddress("coins")
   135  	accCoin, _ := GenerAccDb()
   136  	accCoin.GenerExecAccData(execaddress)
   137  	_, err := accCoin.ExecTransfer(addr1, addr2, execaddress, 10*1e8)
   138  	require.NoError(t, err)
   139  	t.Logf("ExecActive [%d]___[%d]",
   140  		accCoin.LoadExecAccount(addr1, execaddress).Balance,
   141  		accCoin.LoadExecAccount(addr2, execaddress).Balance)
   142  	require.Equal(t, int64(1000*1e8-10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Balance)
   143  	require.Equal(t, int64(900*1e8+10*1e8), accCoin.LoadExecAccount(addr2, execaddress).Balance)
   144  }
   145  
   146  func TestExecTransferFrozen(t *testing.T) {
   147  	q := initEnv()
   148  	blockchainProcess(q)
   149  	storeProcess(q)
   150  
   151  	execaddress := address.ExecAddress("coins")
   152  	accCoin, _ := GenerAccDb()
   153  	accCoin.GenerExecAccData(execaddress)
   154  	_, err := accCoin.ExecTransferFrozen(addr1, addr2, execaddress, 10*1e8)
   155  	require.NoError(t, err)
   156  	t.Logf("TransferFrozen [%d]___[%d]",
   157  		accCoin.LoadExecAccount(addr1, execaddress).Frozen,
   158  		accCoin.LoadExecAccount(addr2, execaddress).Balance)
   159  	require.Equal(t, int64(20*1e8-10*1e8), accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   160  	require.Equal(t, int64(900*1e8+10*1e8), accCoin.LoadExecAccount(addr2, execaddress).Balance)
   161  }
   162  
   163  func TestExecDepositFrozen(t *testing.T) {
   164  	q := initEnv()
   165  	blockchainProcess(q)
   166  	storeProcess(q)
   167  
   168  	execaddress := address.ExecAddress("ticket")
   169  	accCoin, _ := GenerAccDb()
   170  	accCoin.GenerExecAccData(execaddress)
   171  	_, err := accCoin.ExecDepositFrozen(addr1, execaddress, 25*1e8)
   172  	require.NoError(t, err)
   173  	t.Logf("ExecDepositFrozen [%d]",
   174  		accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   175  	require.Equal(t, int64(20*1e8+25*1e8), accCoin.LoadExecAccount(addr1, execaddress).Frozen)
   176  }
   177  
   178  func TestExecIssueCoins(t *testing.T) {
   179  
   180  	q := initEnv()
   181  	blockchainProcess(q)
   182  	storeProcess(q)
   183  	accCoin, _ := GenerAccDb()
   184  	execaddress := address.ExecAddress("token")
   185  	_, err := accCoin.ExecIssueCoins(execaddress, 25*1e8)
   186  	require.Equal(t, types.ErrNotAllowDeposit, err)
   187  	require.Zero(t, accCoin.LoadAccount(execaddress).Balance)
   188  	execaddress = address.ExecAddress("user.p.para.ticket")
   189  	_, err = accCoin.ExecIssueCoins(execaddress, 25*1e8)
   190  	require.Equal(t, types.ErrNotAllowDeposit, err)
   191  	execaddress = address.ExecAddress("ticket")
   192  	_, err = accCoin.ExecIssueCoins(execaddress, 25*1e8)
   193  	require.NoError(t, err)
   194  	t.Logf("ExecIssueCoins [%d]",
   195  		accCoin.LoadAccount(execaddress).Balance)
   196  	require.Equal(t, int64(25*1e8), accCoin.LoadAccount(execaddress).Balance)
   197  }