github.com/klaytn/klaytn@v1.12.1/blockchain/system/registry_test.go (about) 1 package system 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/klaytn/klaytn/accounts/abi/bind" 8 "github.com/klaytn/klaytn/accounts/abi/bind/backends" 9 "github.com/klaytn/klaytn/blockchain" 10 "github.com/klaytn/klaytn/common" 11 contracts "github.com/klaytn/klaytn/contracts/system_contracts" 12 "github.com/klaytn/klaytn/crypto" 13 "github.com/klaytn/klaytn/log" 14 "github.com/klaytn/klaytn/params" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestReadRegistry(t *testing.T) { 19 log.EnableLogForTest(log.LvlCrit, log.LvlWarn) 20 var ( 21 senderKey, _ = crypto.GenerateKey() 22 sender = bind.NewKeyedTransactor(senderKey) 23 24 alloc = blockchain.GenesisAlloc{ 25 sender.From: { 26 Balance: big.NewInt(params.KLAY), 27 }, 28 RegistryAddr: { 29 Code: RegistryMockCode, 30 Balance: common.Big0, 31 }, 32 } 33 backend = backends.NewSimulatedBackend(alloc) 34 35 recordName = "AcmeContract" 36 recordAddr = common.HexToAddress("0xaaaa") 37 ) 38 39 // Without a record 40 addr, err := ReadActiveAddressFromRegistry(backend, recordName, common.Big0) 41 assert.Nil(t, err) 42 assert.Equal(t, common.Address{}, addr) 43 44 // Register a record 45 contract, err := contracts.NewRegistryMockTransactor(RegistryAddr, backend) 46 _, err = contract.Register(sender, recordName, recordAddr, common.Big1) 47 assert.Nil(t, err) 48 backend.Commit() 49 50 // With the record 51 addr, err = ReadActiveAddressFromRegistry(backend, recordName, common.Big1) 52 assert.Nil(t, err) 53 assert.Equal(t, recordAddr, addr) 54 } 55 56 // Test that AllocRegistry correctly reproduces the storage state 57 // identical to the state after a series of register() call. 58 func TestAllocRegistry(t *testing.T) { 59 log.EnableLogForTest(log.LvlCrit, log.LvlWarn) 60 61 // 1. Create storage with AllocRegistry 62 allocStorage := AllocRegistry(¶ms.RegistryConfig{ 63 Records: map[string]common.Address{ 64 "AcmeContract": common.HexToAddress("0xaaaa"), 65 "TestContract": common.HexToAddress("0xcccc"), 66 }, 67 Owner: common.HexToAddress("0xffff"), 68 }) 69 70 // 2. Create storage by calling register() 71 var ( 72 senderKey, _ = crypto.GenerateKey() 73 sender = bind.NewKeyedTransactor(senderKey) 74 75 alloc = blockchain.GenesisAlloc{ 76 sender.From: { 77 Balance: big.NewInt(params.KLAY), 78 }, 79 RegistryAddr: { 80 Code: RegistryMockCode, 81 Balance: common.Big0, 82 }, 83 } 84 backend = backends.NewSimulatedBackend(alloc) 85 contract, _ = contracts.NewRegistryMockTransactor(RegistryAddr, backend) 86 ) 87 88 contract.Register(sender, "AcmeContract", common.HexToAddress("0xaaaa"), common.Big0) 89 contract.Register(sender, "TestContract", common.HexToAddress("0xcccc"), common.Big0) 90 contract.TransferOwnership(sender, common.HexToAddress("0xffff")) 91 backend.Commit() 92 93 execStorage := make(map[common.Hash]common.Hash) 94 stateDB, _ := backend.BlockChain().State() 95 stateDB.ForEachStorage(RegistryAddr, func(key common.Hash, value common.Hash) bool { 96 execStorage[key] = value 97 return true 98 }) 99 100 // 3. Compare the two states 101 for k, v := range allocStorage { 102 assert.Equal(t, v.Hex(), execStorage[k].Hex(), k.Hex()) 103 t.Logf("%x %x\n", k, v) 104 } 105 for k, v := range execStorage { 106 assert.Equal(t, v.Hex(), allocStorage[k].Hex(), k.Hex()) 107 } 108 }