github.com/klaytn/klaytn@v1.12.1/blockchain/system/storage_test.go (about) 1 // Copyright 2023 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 system 18 19 import ( 20 "testing" 21 22 "github.com/klaytn/klaytn/common" 23 "github.com/klaytn/klaytn/common/hexutil" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestStorageCalc(t *testing.T) { 28 assert.Equal(t, "0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f3", 29 calcMappingSlot(0, common.HexToAddress("0xaaaa"), 0).Hex()) 30 assert.Equal(t, "0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f4", 31 calcMappingSlot(0, common.HexToAddress("0xaaaa"), 1).Hex()) 32 33 assert.Equal(t, "0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6", 34 calcArraySlot(1, 1, 0, 0).Hex()) 35 assert.Equal(t, "0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf7", 36 calcArraySlot(1, 1, 1, 0).Hex()) 37 38 assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000001234", 39 lpad32(0x1234).Hex()) 40 } 41 42 func TestStorageDynamicData(t *testing.T) { 43 testcases := []struct { 44 baseSlot interface{} 45 data []byte 46 expected map[string]string 47 }{ 48 { // short data 49 baseSlot: 1, 50 data: []byte("AcmeContract"), 51 expected: map[string]string{ 52 "0x0000000000000000000000000000000000000000000000000000000000000001": "0x41636d65436f6e74726163740000000000000000000000000000000000000018", 53 }, 54 }, 55 { // long 48-byte data (e.g. BLS public key) 56 baseSlot: common.HexToHash("0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f3"), 57 data: hexutil.MustDecode("0x91edb62902264ada822e48f0beedf582fa8c5d1518c0b536b1347983f2fe9c80f283c1e5dfc7a37c3437ceaaa9a1f867"), 58 expected: map[string]string{ 59 "0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f3": "0x0000000000000000000000000000000000000000000000000000000000000061", 60 "0x89005672ae92e5773a2b83cb802d9bc9be4cf262b7ddcca0635550f0ea96e12d": "0x91edb62902264ada822e48f0beedf582fa8c5d1518c0b536b1347983f2fe9c80", 61 "0x89005672ae92e5773a2b83cb802d9bc9be4cf262b7ddcca0635550f0ea96e12e": "0xf283c1e5dfc7a37c3437ceaaa9a1f86700000000000000000000000000000000", 62 }, 63 }, 64 { // long 95-byte data (e.g. BLS PoP) 65 baseSlot: common.HexToHash("0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f4"), 66 data: hexutil.MustDecode("0xb72d443815683d633b5933366ac8aa6e2891aaff8ab95e69a7531aa2afe10f0740368128992a2a54cb24f0c93dad5c220692915ddb5013fb475726baf711590f7ea1eafaf396264cc2940c744ee6914f3734a4d404dffa91d15e3d138ee81464"), 67 expected: map[string]string{ 68 "0x839613f731613c3a2f728362760f939c8004b5d9066154aab51d6dadf74733f4": "0x00000000000000000000000000000000000000000000000000000000000000c1", 69 "0xccf7c844150261de21551c00e90c49fd101dcf60b58bfeb3f7c1ce662560a383": "0xb72d443815683d633b5933366ac8aa6e2891aaff8ab95e69a7531aa2afe10f07", 70 "0xccf7c844150261de21551c00e90c49fd101dcf60b58bfeb3f7c1ce662560a384": "0x40368128992a2a54cb24f0c93dad5c220692915ddb5013fb475726baf711590f", 71 "0xccf7c844150261de21551c00e90c49fd101dcf60b58bfeb3f7c1ce662560a385": "0x7ea1eafaf396264cc2940c744ee6914f3734a4d404dffa91d15e3d138ee81464", 72 }, 73 }, 74 } 75 76 for _, tc := range testcases { 77 alloc := allocDynamicData(tc.baseSlot, tc.data) 78 79 strAlloc := make(map[string]string) 80 for k, v := range alloc { 81 strAlloc[k.Hex()] = v.Hex() 82 } 83 assert.Equal(t, tc.expected, strAlloc) 84 } 85 }