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