github.com/klaytn/klaytn@v1.12.1/storage/database/child_chain_data_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 database
    18  
    19  import (
    20  	"math/big"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/klaytn/klaytn/blockchain/types"
    25  	"github.com/klaytn/klaytn/common"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestChildChainData_ReadAndWrite_ChildChainTxHash(t *testing.T) {
    30  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
    31  	if err != nil {
    32  		t.Fatalf("cannot create temporary directory: %v", err)
    33  	}
    34  	defer os.RemoveAll(dir)
    35  
    36  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
    37  	dbm := NewDBManager(dbc)
    38  	defer dbm.Close()
    39  
    40  	ccBlockHash := common.HexToHash("0x0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e")
    41  	ccTxHash := common.HexToHash("0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f")
    42  
    43  	// Before writing the data into DB, nil should be returned.
    44  	ccTxHashFromDB := dbm.ConvertChildChainBlockHashToParentChainTxHash(ccBlockHash)
    45  	assert.Equal(t, common.Hash{}, ccTxHashFromDB)
    46  
    47  	// After writing the data into DB, data should be returned.
    48  	dbm.WriteChildChainTxHash(ccBlockHash, ccTxHash)
    49  	ccTxHashFromDB = dbm.ConvertChildChainBlockHashToParentChainTxHash(ccBlockHash)
    50  	assert.NotNil(t, ccTxHashFromDB)
    51  	assert.Equal(t, ccTxHash, ccTxHashFromDB)
    52  
    53  	ccBlockHashFake := common.HexToHash("0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a")
    54  	// Invalid information should not return the data.
    55  	ccTxHashFromDB = dbm.ConvertChildChainBlockHashToParentChainTxHash(ccBlockHashFake)
    56  	assert.Equal(t, common.Hash{}, ccTxHashFromDB)
    57  }
    58  
    59  func TestLastIndexedBlockData_ReadAndWrite_AnchoredBlockNumber(t *testing.T) {
    60  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
    61  	if err != nil {
    62  		t.Fatalf("cannot create temporary directory: %v", err)
    63  	}
    64  	defer os.RemoveAll(dir)
    65  
    66  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
    67  	dbm := NewDBManager(dbc)
    68  	defer dbm.Close()
    69  
    70  	blockNum := uint64(123)
    71  
    72  	blockNumFromDB := dbm.GetLastIndexedBlockNumber()
    73  	assert.Equal(t, uint64(0), blockNumFromDB)
    74  
    75  	dbm.WriteLastIndexedBlockNumber(blockNum)
    76  	blockNumFromDB = dbm.GetLastIndexedBlockNumber()
    77  	assert.Equal(t, blockNum, blockNumFromDB)
    78  
    79  	newBlockNum := uint64(321)
    80  	dbm.WriteLastIndexedBlockNumber(newBlockNum)
    81  	blockNumFromDB = dbm.GetLastIndexedBlockNumber()
    82  	assert.Equal(t, newBlockNum, blockNumFromDB)
    83  }
    84  
    85  func TestChildChainData_ReadAndWrite_AnchoredBlockNumber(t *testing.T) {
    86  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
    87  	if err != nil {
    88  		t.Fatalf("cannot create temporary directory: %v", err)
    89  	}
    90  	defer os.RemoveAll(dir)
    91  
    92  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
    93  	dbm := NewDBManager(dbc)
    94  	defer dbm.Close()
    95  
    96  	blockNum := uint64(123)
    97  
    98  	blockNumFromDB := dbm.ReadAnchoredBlockNumber()
    99  	assert.Equal(t, uint64(0), blockNumFromDB)
   100  
   101  	dbm.WriteAnchoredBlockNumber(blockNum)
   102  	blockNumFromDB = dbm.ReadAnchoredBlockNumber()
   103  	assert.Equal(t, blockNum, blockNumFromDB)
   104  
   105  	newBlockNum := uint64(321)
   106  	dbm.WriteAnchoredBlockNumber(newBlockNum)
   107  	blockNumFromDB = dbm.ReadAnchoredBlockNumber()
   108  	assert.Equal(t, newBlockNum, blockNumFromDB)
   109  }
   110  
   111  func TestChildChainData_ReadAndWrite_ReceiptFromParentChain(t *testing.T) {
   112  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
   113  	if err != nil {
   114  		t.Fatalf("cannot create temporary directory: %v", err)
   115  	}
   116  	defer os.RemoveAll(dir)
   117  
   118  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
   119  	dbm := NewDBManager(dbc)
   120  	defer dbm.Close()
   121  
   122  	blockHash := common.HexToHash("0x0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e")
   123  	rct := &types.Receipt{}
   124  	rct.TxHash = common.BigToHash(big.NewInt(12345))
   125  	rct.GasUsed = uint64(12345)
   126  	rct.Status = types.ReceiptStatusSuccessful
   127  
   128  	rctFromDB := dbm.ReadReceiptFromParentChain(blockHash)
   129  	assert.Nil(t, rctFromDB)
   130  
   131  	dbm.WriteReceiptFromParentChain(blockHash, rct)
   132  	rctFromDB = dbm.ReadReceiptFromParentChain(blockHash)
   133  
   134  	assert.Equal(t, rct.Status, rctFromDB.Status)
   135  	assert.Equal(t, rct.GasUsed, rctFromDB.GasUsed)
   136  	assert.Equal(t, rct.TxHash, rctFromDB.TxHash)
   137  
   138  	newBlockHash := common.HexToHash("0x0f0f0e0e0e0e0e0e0e0e0e0e0e0e0f0f")
   139  	rctFromDB = dbm.ReadReceiptFromParentChain(newBlockHash)
   140  	assert.Nil(t, rctFromDB)
   141  }
   142  
   143  func TestChildChainData_ReadAndWrite_ValueTransferTxHash(t *testing.T) {
   144  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
   145  	if err != nil {
   146  		t.Fatalf("cannot create temporary directory: %v", err)
   147  	}
   148  	defer os.RemoveAll(dir)
   149  
   150  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
   151  	dbm := NewDBManager(dbc)
   152  	defer dbm.Close()
   153  
   154  	rTxHash := common.HexToHash("0x0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e")
   155  	hTxHash := common.HexToHash("0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f")
   156  
   157  	// Before writing the data into DB, nil should be returned.
   158  	hTxHashFromDB := dbm.ReadHandleTxHashFromRequestTxHash(rTxHash)
   159  	assert.Equal(t, common.Hash{}, hTxHashFromDB)
   160  
   161  	// After writing the data into DB, data should be returned.
   162  	dbm.WriteHandleTxHashFromRequestTxHash(rTxHash, hTxHash)
   163  	hTxHashFromDB = dbm.ReadHandleTxHashFromRequestTxHash(rTxHash)
   164  	assert.NotNil(t, hTxHashFromDB)
   165  	assert.Equal(t, hTxHash, hTxHashFromDB)
   166  
   167  	ccBlockHashFake := common.HexToHash("0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a")
   168  	// Invalid information should not return the data.
   169  	hTxHashFromDB = dbm.ReadHandleTxHashFromRequestTxHash(ccBlockHashFake)
   170  	assert.Equal(t, common.Hash{}, hTxHashFromDB)
   171  }
   172  
   173  func TestChildChainData_ReadAndWrite_OperatorFeePayer(t *testing.T) {
   174  	dir, err := os.MkdirTemp("", "klaytn-test-child-chain-data")
   175  	if err != nil {
   176  		t.Fatalf("cannot create temporary directory: %v", err)
   177  	}
   178  	defer os.RemoveAll(dir)
   179  
   180  	firstAddr := common.HexToAddress("0x1")
   181  	secondAddr := common.HexToAddress("0x2")
   182  
   183  	dbc := &DBConfig{Dir: dir, DBType: LevelDB, LevelDBCacheSize: 32, OpenFilesLimit: 32}
   184  	dbm := NewDBManager(dbc)
   185  	defer dbm.Close()
   186  
   187  	// check initial value
   188  	{
   189  		feePayer := dbm.ReadParentOperatorFeePayer()
   190  		assert.Equal(t, common.Address{}, feePayer)
   191  	}
   192  	{
   193  		feePayer := dbm.ReadChildOperatorFeePayer()
   194  		assert.Equal(t, common.Address{}, feePayer)
   195  	}
   196  
   197  	// check write/read
   198  	{
   199  		dbm.WriteParentOperatorFeePayer(firstAddr)
   200  		feePayer := dbm.ReadParentOperatorFeePayer()
   201  		assert.Equal(t, firstAddr, feePayer)
   202  	}
   203  	{
   204  		dbm.WriteChildOperatorFeePayer(secondAddr)
   205  		feePayer := dbm.ReadChildOperatorFeePayer()
   206  		assert.Equal(t, secondAddr, feePayer)
   207  	}
   208  
   209  	// check write zero address and read
   210  	{
   211  		dbm.WriteParentOperatorFeePayer(common.Address{})
   212  		feePayer := dbm.ReadParentOperatorFeePayer()
   213  		assert.Equal(t, common.Address{}, feePayer)
   214  	}
   215  	{
   216  		dbm.WriteChildOperatorFeePayer(common.Address{})
   217  		feePayer := dbm.ReadChildOperatorFeePayer()
   218  		assert.Equal(t, common.Address{}, feePayer)
   219  	}
   220  }