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