github.com/klaytn/klaytn@v1.10.2/blockchain/state_migration_test.go (about)

     1  // Copyright 2022 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 blockchain
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/klaytn/klaytn/blockchain/vm"
    27  	"github.com/klaytn/klaytn/common"
    28  	"github.com/klaytn/klaytn/consensus/gxhash"
    29  	"github.com/klaytn/klaytn/log"
    30  	"github.com/klaytn/klaytn/params"
    31  	"github.com/klaytn/klaytn/storage/database"
    32  )
    33  
    34  func createLocalTestDB(t *testing.T) (string, database.DBManager) {
    35  	dir, err := ioutil.TempDir("", "klaytn-test-migration")
    36  	if err != nil {
    37  		t.Fatalf("failed to create a database: %v", err)
    38  	}
    39  	dbc := &database.DBConfig{Dir: dir, DBType: database.LevelDB, LevelDBCacheSize: 128, OpenFilesLimit: 128}
    40  	db := database.NewDBManager(dbc)
    41  	return dir, db
    42  }
    43  
    44  func TestBlockChain_migrateState(t *testing.T) {
    45  	log.EnableLogForTest(log.LvlCrit, log.LvlTrace)
    46  
    47  	dir, testdb := createLocalTestDB(t)
    48  	defer os.RemoveAll(dir)
    49  
    50  	var (
    51  		gspec = &Genesis{Config: params.TestChainConfig}
    52  		_     = gspec.MustCommit(testdb)
    53  	)
    54  
    55  	chain, err := NewBlockChain(testdb, nil, gspec.Config, gxhash.NewFaker(), vm.Config{})
    56  	if err != nil {
    57  		t.Fatalf("Failed to create local chain, %v", err)
    58  	}
    59  	defer chain.Stop()
    60  
    61  	chain.testMigrationHook = func() {
    62  		// sleep 2 seconds to write byte codes while migrating statedb
    63  		t.Log("taking 2 seconds for testing purpose...")
    64  		time.Sleep(2 * time.Second)
    65  	}
    66  
    67  	if err := chain.PrepareStateMigration(); err != nil {
    68  		t.Fatalf("failed to prepare state migration: %v", err)
    69  	}
    70  
    71  	b := chain.CurrentBlock()
    72  	if err := chain.StartStateMigration(b.NumberU64(), b.Root()); err != nil {
    73  		t.Fatalf("failed to start state migration: %v", err)
    74  	}
    75  
    76  	// write code while statedb migration
    77  	expectedCode := []byte{0x60, 0x80, 0x60, 0x40}
    78  	codeHash := common.BytesToHash(expectedCode)
    79  	chain.stateCache.TrieDB().DiskDB().WriteCode(codeHash, expectedCode)
    80  
    81  	for chain.db.InMigration() {
    82  		t.Log("It is in migration, sleeping 1 second")
    83  		time.Sleep(1 * time.Second)
    84  	}
    85  
    86  	// read code after deleting the original db
    87  	actualCode := chain.db.ReadCode(codeHash)
    88  	if actualCode == nil || bytes.Compare(expectedCode, actualCode) != 0 {
    89  		t.Fatalf("mismatch bytecodes: (expected: %v, actual: %v)", common.Bytes2Hex(expectedCode), common.Bytes2Hex(actualCode))
    90  	}
    91  }