github.com/klaytn/klaytn@v1.12.1/node/sc/bridge_account_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package sc
    18  
    19  import (
    20  	"math"
    21  	"math/big"
    22  	"os"
    23  	"path"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/klaytn/klaytn/accounts/keystore"
    28  	"github.com/klaytn/klaytn/storage/database"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  // TestBridgeAccountLockUnlock checks the lock/unlock functionality.
    33  func TestBridgeAccountLockUnlock(t *testing.T) {
    34  	tempDir, err := os.MkdirTemp(os.TempDir(), "sc")
    35  	assert.NoError(t, err)
    36  	defer func() {
    37  		if err := os.RemoveAll(tempDir); err != nil {
    38  			t.Fatalf("fail to delete file %v", err)
    39  		}
    40  	}()
    41  
    42  	// Config Bridge Account Manager
    43  	config := &SCConfig{}
    44  	config.DataDir = tempDir
    45  	bAcc, err := NewBridgeAccounts(nil, config.DataDir, database.NewDBManager(&database.DBConfig{DBType: database.MemoryDB}), DefaultBridgeTxGasLimit, DefaultBridgeTxGasLimit)
    46  	assert.NoError(t, err)
    47  	assert.Equal(t, true, bAcc.cAccount.IsUnlockedAccount())
    48  	assert.Equal(t, true, bAcc.pAccount.IsUnlockedAccount())
    49  
    50  	pPwdFilePath := path.Join(tempDir, "parent_bridge_account", bAcc.pAccount.address.String())
    51  	pPwd, err := os.ReadFile(pPwdFilePath)
    52  	pPwdStr := string(pPwd)
    53  	assert.NoError(t, err)
    54  
    55  	cPwdFilePath := path.Join(tempDir, "child_bridge_account", bAcc.cAccount.address.String())
    56  	cPwd, err := os.ReadFile(cPwdFilePath)
    57  	cPwdStr := string(cPwd)
    58  	assert.NoError(t, err)
    59  
    60  	lockAccountsWithCheck := func(t *testing.T, bAcc *BridgeAccounts) {
    61  		{
    62  			err := bAcc.cAccount.LockAccount()
    63  			assert.NoError(t, err)
    64  			assert.Equal(t, false, bAcc.cAccount.IsUnlockedAccount())
    65  		}
    66  		{
    67  			err := bAcc.pAccount.LockAccount()
    68  			assert.NoError(t, err)
    69  			assert.Equal(t, false, bAcc.pAccount.IsUnlockedAccount())
    70  		}
    71  	}
    72  
    73  	unlockParentAccountWithCheck := func(t *testing.T, bAcc *BridgeAccounts, passwd string, duration *uint64, expectedErr error) {
    74  		expectedIsUnLock := false
    75  		if expectedErr == nil {
    76  			expectedIsUnLock = true
    77  		}
    78  
    79  		err := bAcc.pAccount.UnLockAccount(passwd, duration)
    80  		assert.Equal(t, expectedErr, err)
    81  
    82  		time.Sleep(time.Second)
    83  
    84  		if duration == nil || *duration == 0 {
    85  			assert.Equal(t, expectedIsUnLock, bAcc.pAccount.IsUnlockedAccount())
    86  			return
    87  		}
    88  
    89  		time.Sleep(time.Duration(*duration) * time.Second)
    90  		assert.Equal(t, false, bAcc.pAccount.IsUnlockedAccount())
    91  	}
    92  
    93  	unlockChildAccountWithCheck := func(t *testing.T, bAcc *BridgeAccounts, passwd string, duration *uint64, expectedErr error) {
    94  		expectedIsUnLock := false
    95  		if expectedErr == nil {
    96  			expectedIsUnLock = true
    97  		}
    98  
    99  		err := bAcc.cAccount.UnLockAccount(passwd, duration)
   100  		assert.Equal(t, expectedErr, err)
   101  
   102  		time.Sleep(time.Second)
   103  
   104  		if duration == nil || *duration == 0 {
   105  			assert.Equal(t, expectedIsUnLock, bAcc.cAccount.IsUnlockedAccount())
   106  			return
   107  		}
   108  
   109  		time.Sleep(time.Duration(*duration) * time.Second)
   110  		assert.Equal(t, false, bAcc.cAccount.IsUnlockedAccount())
   111  	}
   112  
   113  	// Double time Lock Account
   114  	lockAccountsWithCheck(t, bAcc)
   115  	lockAccountsWithCheck(t, bAcc)
   116  
   117  	// Fail to UnLock Account with invalid timeout
   118  	lockAccountsWithCheck(t, bAcc)
   119  	duration := uint64(time.Duration(math.MaxInt64)/time.Second) + 1
   120  	unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, errUnlockDurationTooLarge)
   121  	unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, errUnlockDurationTooLarge)
   122  
   123  	// Fail to UnLock Account with wrong password
   124  	lockAccountsWithCheck(t, bAcc)
   125  	duration = uint64(0)
   126  	unlockParentAccountWithCheck(t, bAcc, pPwdStr[:3], &duration, keystore.ErrDecrypt)
   127  	unlockChildAccountWithCheck(t, bAcc, cPwdStr[:3], &duration, keystore.ErrDecrypt)
   128  
   129  	// Succeed to UnLock Account
   130  	lockAccountsWithCheck(t, bAcc)
   131  	duration = uint64(0)
   132  	unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, nil)
   133  	unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, nil)
   134  
   135  	// Succeed to UnLock Account with timeout
   136  	lockAccountsWithCheck(t, bAcc)
   137  	duration = uint64(5)
   138  	unlockParentAccountWithCheck(t, bAcc, pPwdStr, &duration, nil)
   139  	unlockChildAccountWithCheck(t, bAcc, cPwdStr, &duration, nil)
   140  
   141  	// Fail to UnLock Account with wrong password
   142  	lockAccountsWithCheck(t, bAcc)
   143  	duration = uint64(5)
   144  	unlockParentAccountWithCheck(t, bAcc, pPwdStr[:3], &duration, keystore.ErrDecrypt)
   145  	unlockChildAccountWithCheck(t, bAcc, cPwdStr[:3], &duration, keystore.ErrDecrypt)
   146  
   147  	// Succeed to UnLock Account with nil timeout
   148  	lockAccountsWithCheck(t, bAcc)
   149  	unlockParentAccountWithCheck(t, bAcc, pPwdStr, nil, nil)
   150  	unlockChildAccountWithCheck(t, bAcc, cPwdStr, nil, nil)
   151  }
   152  
   153  // TestBridgeAccountInformation checks if the information result is right or not.
   154  func TestBridgeAccountInformation(t *testing.T) {
   155  	tempDir, err := os.MkdirTemp(os.TempDir(), "sc")
   156  	assert.NoError(t, err)
   157  	defer func() {
   158  		if err := os.RemoveAll(tempDir); err != nil {
   159  			t.Fatalf("fail to delete file %v", err)
   160  		}
   161  	}()
   162  
   163  	// Config Bridge Account Manager
   164  	config := &SCConfig{}
   165  	config.DataDir = tempDir
   166  	bAcc, err := NewBridgeAccounts(nil, config.DataDir, database.NewDBManager(&database.DBConfig{DBType: database.MemoryDB}), DefaultBridgeTxGasLimit, DefaultBridgeTxGasLimit)
   167  	assert.NoError(t, err)
   168  	assert.Equal(t, true, bAcc.cAccount.IsUnlockedAccount())
   169  	assert.Equal(t, true, bAcc.pAccount.IsUnlockedAccount())
   170  
   171  	bAcc.pAccount.gasPrice = big.NewInt(100)
   172  	bAcc.pAccount.nonce = 10
   173  	bAcc.pAccount.chainID = big.NewInt(200)
   174  	bAcc.pAccount.isNonceSynced = true
   175  	err = bAcc.pAccount.LockAccount()
   176  	assert.NoError(t, err)
   177  
   178  	res := bAcc.GetBridgeOperators()
   179  	assert.Equal(t, 2, len(res))
   180  
   181  	pRes := res["parentOperator"].(map[string]interface{})
   182  	cRes := res["childOperator"].(map[string]interface{})
   183  	assert.Equal(t, 6, len(pRes))
   184  	assert.Equal(t, 6, len(cRes))
   185  
   186  	assert.Equal(t, pRes["address"], bAcc.pAccount.address)
   187  	assert.Equal(t, pRes["nonce"], bAcc.pAccount.nonce)
   188  	assert.Equal(t, pRes["chainID"].(*big.Int).String(), bAcc.pAccount.chainID.String())
   189  	assert.Equal(t, pRes["gasPrice"].(*big.Int).String(), bAcc.pAccount.gasPrice.String())
   190  	assert.Equal(t, pRes["isNonceSynced"], bAcc.pAccount.isNonceSynced)
   191  	assert.Equal(t, pRes["isUnlocked"], bAcc.pAccount.IsUnlockedAccount())
   192  
   193  	assert.Equal(t, cRes["address"], bAcc.cAccount.address)
   194  	assert.Equal(t, cRes["nonce"], bAcc.cAccount.nonce)
   195  	assert.Equal(t, cRes["chainID"].(*big.Int).String(), bAcc.cAccount.chainID.String())
   196  	assert.Equal(t, cRes["gasPrice"].(*big.Int).String(), bAcc.cAccount.gasPrice.String())
   197  	assert.Equal(t, cRes["isNonceSynced"], bAcc.cAccount.isNonceSynced)
   198  	assert.Equal(t, cRes["isUnlocked"], bAcc.cAccount.IsUnlockedAccount())
   199  }