From 33272c156fced057ba623d6aa76e81434bf36ac7 Mon Sep 17 00:00:00 2001 From: blxdyx <125243069+blxdyx@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:35:07 +0800 Subject: [PATCH] optimize hot fix (#564) --- core/state/state_fix.go | 146 ++++++++++++++++++++++++++++++++ core/state/state_object.go | 169 +------------------------------------ 2 files changed, 148 insertions(+), 167 deletions(-) create mode 100644 core/state/state_fix.go diff --git a/core/state/state_fix.go b/core/state/state_fix.go new file mode 100644 index 00000000000..b6d2ee40644 --- /dev/null +++ b/core/state/state_fix.go @@ -0,0 +1,146 @@ +package state + +import ( + libcommon "github.com/erigontech/erigon-lib/common" + "github.com/erigontech/erigon-lib/common/hexutil" + "github.com/holiman/uint256" +) + +const ( + BscBlockNum = 33851236 + Bsc1Index = 89 + Bsc2Index = 90 + Chapel1BlockNum = 35547779 + Chapel1TxIndex = 196 + Chapel2BlockNum = 35548081 + Chapel2TxIndex = 486 +) + +type HotFixPattern struct { + blockNumber uint64 + txIndex int + txHash libcommon.Hash + addr libcommon.Address + kvList Storage +} + +var patches = []HotFixPattern{ + { + blockNumber: BscBlockNum, + txIndex: Bsc1Index, + txHash: libcommon.HexToHash("0x7eba4edc7c1806d6ee1691d43513838931de5c94f9da56ec865721b402f775b0"), + addr: libcommon.HexToAddress("0x00000000001f8b68515EfB546542397d3293CCfd"), + kvList: createBsc1KVs(), + }, + { + blockNumber: BscBlockNum, + txIndex: Bsc2Index, + txHash: libcommon.HexToHash("0x5217324f0711af744fe8e12d73f13fdb11805c8e29c0c095ac747b7e4563e935"), + addr: libcommon.HexToAddress("0x00000000001f8b68515EfB546542397d3293CCfd"), + kvList: createBsc2KVs(), + }, + { + blockNumber: Chapel1BlockNum, + txIndex: Chapel1TxIndex, + txHash: libcommon.HexToHash("0x7ce9a3cf77108fcc85c1e84e88e363e3335eca515dfcf2feb2011729878b13a7"), + addr: libcommon.HexToAddress("0x89791428868131eb109e42340ad01eb8987526b2"), + kvList: createChapel1KVs(), + }, + { + blockNumber: Chapel2BlockNum, + txIndex: Chapel2TxIndex, + txHash: libcommon.HexToHash("0xe3895eb95605d6b43ceec7876e6ff5d1c903e572bf83a08675cb684c047a695c"), + addr: libcommon.HexToAddress("0x89791428868131eb109e42340ad01eb8987526b2"), + kvList: createChapel2KVs(), + }, +} + +func createBsc1KVs() Storage { + return createKVs(map[string]string{ + "0x0000000000000000000000000000000000000000000000000000000000000001": "0x00000000000000000000000052db206170b430da8223651d28830e56ba3cdc04", + "0x0000000000000000000000000000000000000000000000000000000000000002": "0x000000000000000000000000bb45f138499734bf5c0948d490c65903676ea1de", + "0x65c95177950b486c2071bf2304da1427b9136564150fb97266ffb318b03a71cc": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x245e58a02bec784ccbdb9e022a84af83227a4125a22a5e68fcc596c7e436434e": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x1c4534c86090a60a9120f34c7b15254913c00bda3d4b276d6edb65c9f48a913f": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x0000000000000000000000000000000000000000000000000000000000000004": "0x0000000000000000000000000000000000000000000000000000000000000019", + "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b4": "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b5": "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b6": "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000005": "0x00000000000000000000000000000000000000000000000000000000000fc248", + "0x0000000000000000000000000000000000000000000000000000000000000006": "0x00000000000000000000000000000000000000000000000000000000000fc132", + }) +} + +func createBsc2KVs() Storage { + return createKVs(map[string]string{ + "0xbcfc62ca570bdb58cf9828ac51ae8d7e063a1cc0fa1aee57691220a7cd78b1c8": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x30dce49ce1a4014301bf21aad0ee16893e4dcc4a4e4be8aa10e442dd13259837": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xc0582628d787ee16fe03c8e5b5f5644d3b81989686f8312280b7a1f733145525": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xfca5cf22ff2e8d58aece8e4370cce33cd0144d48d00f40a5841df4a42527694b": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xb189302b37865d2ae522a492ff1f61a5addc1db44acbdcc4b6814c312c815f46": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xfe1f1986775fc2ac905aeaecc7b1aa8b0d6722b852c90e26edacd2dac7382489": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x36052a8ddb27fecd20e2e09da15494a0f2186bf8db36deebbbe701993f8c4aae": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x4959a566d8396b889ff4bc20e18d2497602e01e5c6013af5af7a7c4657ece3e2": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xe0b5aeb100569add952966f803cb67aca86dc6ec8b638f5a49f9e0760efa9a7a": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x632467ad388b91583f956f76488afc42846e283c962cbb215d288033ffc4fb71": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x9ad4e69f52519f7b7b8ee5ae3326d57061b429428ea0c056dd32e7a7102e79a7": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x35e130c7071699eae5288b12374ef157a15e4294e2b3a352160b7c1cd4641d82": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xa0d8279f845f63979dc292228adfa0bda117de27e44d90ac2adcd44465b225e7": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x9a100b70ffda9ed9769becdadca2b2936b217e3da4c9b9817bad30d85eab25ff": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x28d67156746295d901005e2d95ce589e7093decb638f8c132d9971fd0a37e176": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x297c4e115b5df76bcd5a1654b8032661680a1803e30a0774cb42bb01891e6d97": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x5f71b88f1032d27d8866948fc9c49525f3e584bdd52a66de6060a7b1f767326f": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xe6d8ddf6a0bbeb4840f48f0c4ffda9affa4675354bdb7d721235297f5a094f54": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x30ba10aef6238bf19667aaa988b18b72adb4724c016e19eb64bbb52808d1a842": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x9c6806a4d6a99e4869b9a4aaf80b0a3bf5f5240a1d6032ed82edf0e86f2a2467": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xe8480d613bbf3b979aee2de4487496167735bb73df024d988e1795b3c7fa559a": "0x0000000000000000000000000000000000000000000000000000000000000001", + "0xebfaec01f898f7f0e2abdb4b0aee3dfbf5ec2b287b1e92f9b62940f85d5f5bac": "0x0000000000000000000000000000000000000000000000000000000000000001", + }) +} + +func createChapel1KVs() Storage { + return createKVs(map[string]string{ + "0xf1e9242398de526b8dd9c25d38e65fbb01926b8940377762d7884b8b0dcdc3b0": "0x0000000000000000000000000000000000000000000000f6a7831804efd2cd0a", + }) +} + +func createChapel2KVs() Storage { + return createKVs(map[string]string{ + "0xf1e9242398de526b8dd9c25d38e65fbb01926b8940377762d7884b8b0dcdc3b0": "0x0000000000000000000000000000000000000000000000114be8ecea72b64003", + }) +} + +func createKVs(data map[string]string) Storage { + kvList := make(Storage) + for k, v := range data { + kvList[libcommon.HexToHash(k)] = *new(uint256.Int).SetBytes(hexutil.MustDecode(v)) + } + return kvList +} + +func (so *stateObject) applyPatches() { + for _, patch := range patches { + if so.db.blockNum == patch.blockNumber && so.db.txIndex == patch.txIndex && so.address == patch.addr { + for k, v := range patch.kvList { + so.originStorage[k] = v + } + } + } +} + +func (so *stateObject) patchGethHotFix(blockNum uint64, txIndex int) { + if so.db.blockNum != blockNum || so.db.txIndex != txIndex { + return + } + so.applyPatches() +} + +func (so *stateObject) patchGethHotFixMainnet() { + so.patchGethHotFix(BscBlockNum, Bsc1Index) + so.patchGethHotFix(BscBlockNum, Bsc2Index) +} + +func (so *stateObject) patchGethHotFixChapel() { + so.patchGethHotFix(Chapel1BlockNum, Chapel1TxIndex) + so.patchGethHotFix(Chapel2BlockNum, Chapel2TxIndex) +} diff --git a/core/state/state_object.go b/core/state/state_object.go index 364ae388817..d6b280a6fe8 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -21,7 +21,6 @@ package state import ( "fmt" - "github.com/erigontech/erigon-lib/common/hexutil" "io" "maps" "math/big" @@ -165,169 +164,6 @@ func (so *stateObject) GetState(key *libcommon.Hash, out *uint256.Int) { so.GetCommittedState(key, out) } -type HotFixPattern struct { - blockNumber uint64 - txIndex int - txHash libcommon.Hash - addr libcommon.Address - kvList Storage -} - -func (so *stateObject) patchGethHotFixMainnet1() { - totalPatches := []HotFixPattern{} - // patch 1: BlockNum 33851236, txIndex 89 - patch1 := HotFixPattern{ - blockNumber: 33851236, - txIndex: 89, - txHash: libcommon.HexToHash("0x7eba4edc7c1806d6ee1691d43513838931de5c94f9da56ec865721b402f775b0"), - addr: libcommon.HexToAddress("0x00000000001f8b68515EfB546542397d3293CCfd"), - kvList: make(Storage), - } - // patch 2: BlockNum 33851236, txIndex 90 - patch2 := HotFixPattern{ - blockNumber: 33851236, - txIndex: 90, - txHash: libcommon.HexToHash("0x5217324f0711af744fe8e12d73f13fdb11805c8e29c0c095ac747b7e4563e935"), - addr: libcommon.HexToAddress("0x00000000001f8b68515EfB546542397d3293CCfd"), - kvList: make(Storage), - } - if so.db.blockNum != patch1.blockNumber { - return - } - - patch1KVs := map[string]string{ - "0x0000000000000000000000000000000000000000000000000000000000000001": "0x00000000000000000000000052db206170b430da8223651d28830e56ba3cdc04", - "0x0000000000000000000000000000000000000000000000000000000000000002": "0x000000000000000000000000bb45f138499734bf5c0948d490c65903676ea1de", - "0x65c95177950b486c2071bf2304da1427b9136564150fb97266ffb318b03a71cc": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x245e58a02bec784ccbdb9e022a84af83227a4125a22a5e68fcc596c7e436434e": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x1c4534c86090a60a9120f34c7b15254913c00bda3d4b276d6edb65c9f48a913f": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x0000000000000000000000000000000000000000000000000000000000000004": "0x0000000000000000000000000000000000000000000000000000000000000019", - "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b4": "0x0000000000000000000000000000000000000000000000000000000000000000", - "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b5": "0x0000000000000000000000000000000000000000000000000000000000000000", - "0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd1b6": "0x0000000000000000000000000000000000000000000000000000000000000000", - "0x0000000000000000000000000000000000000000000000000000000000000005": "0x00000000000000000000000000000000000000000000000000000000000fc248", - "0x0000000000000000000000000000000000000000000000000000000000000006": "0x00000000000000000000000000000000000000000000000000000000000fc132", - } - for k, v := range patch1KVs { - patch1.kvList[libcommon.HexToHash(k)] = *new(uint256.Int).SetBytes(hexutil.MustDecode(v)) - } - totalPatches = append(totalPatches, patch1) - - patch2KVs := map[string]string{ - "0xbcfc62ca570bdb58cf9828ac51ae8d7e063a1cc0fa1aee57691220a7cd78b1c8": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x30dce49ce1a4014301bf21aad0ee16893e4dcc4a4e4be8aa10e442dd13259837": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xc0582628d787ee16fe03c8e5b5f5644d3b81989686f8312280b7a1f733145525": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xfca5cf22ff2e8d58aece8e4370cce33cd0144d48d00f40a5841df4a42527694b": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xb189302b37865d2ae522a492ff1f61a5addc1db44acbdcc4b6814c312c815f46": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xfe1f1986775fc2ac905aeaecc7b1aa8b0d6722b852c90e26edacd2dac7382489": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x36052a8ddb27fecd20e2e09da15494a0f2186bf8db36deebbbe701993f8c4aae": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x4959a566d8396b889ff4bc20e18d2497602e01e5c6013af5af7a7c4657ece3e2": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xe0b5aeb100569add952966f803cb67aca86dc6ec8b638f5a49f9e0760efa9a7a": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x632467ad388b91583f956f76488afc42846e283c962cbb215d288033ffc4fb71": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x9ad4e69f52519f7b7b8ee5ae3326d57061b429428ea0c056dd32e7a7102e79a7": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x35e130c7071699eae5288b12374ef157a15e4294e2b3a352160b7c1cd4641d82": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xa0d8279f845f63979dc292228adfa0bda117de27e44d90ac2adcd44465b225e7": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x9a100b70ffda9ed9769becdadca2b2936b217e3da4c9b9817bad30d85eab25ff": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x28d67156746295d901005e2d95ce589e7093decb638f8c132d9971fd0a37e176": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x297c4e115b5df76bcd5a1654b8032661680a1803e30a0774cb42bb01891e6d97": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x5f71b88f1032d27d8866948fc9c49525f3e584bdd52a66de6060a7b1f767326f": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xe6d8ddf6a0bbeb4840f48f0c4ffda9affa4675354bdb7d721235297f5a094f54": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x30ba10aef6238bf19667aaa988b18b72adb4724c016e19eb64bbb52808d1a842": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0x9c6806a4d6a99e4869b9a4aaf80b0a3bf5f5240a1d6032ed82edf0e86f2a2467": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xe8480d613bbf3b979aee2de4487496167735bb73df024d988e1795b3c7fa559a": "0x0000000000000000000000000000000000000000000000000000000000000001", - "0xebfaec01f898f7f0e2abdb4b0aee3dfbf5ec2b287b1e92f9b62940f85d5f5bac": "0x0000000000000000000000000000000000000000000000000000000000000001", - } - for k, v := range patch2KVs { - patch2.kvList[libcommon.HexToHash(k)] = *new(uint256.Int).SetBytes(hexutil.MustDecode(v)) - } - totalPatches = append(totalPatches, patch2) - - // apply the patches - for _, patch := range totalPatches { - if so.db.txIndex != patch.txIndex { - continue - } - if so.address != patch.addr { - continue - } - for k, v := range patch.kvList { - so.originStorage[k] = v - } - } -} - -func (so *stateObject) patchGethHotFixChapel1() { - totalPatches := []HotFixPattern{} - // patch 1: BlockNum 35547779, txIndex 196 - patch1 := HotFixPattern{ - blockNumber: 35547779, - txIndex: 196, - txHash: libcommon.HexToHash("0x7ce9a3cf77108fcc85c1e84e88e363e3335eca515dfcf2feb2011729878b13a7"), - addr: libcommon.HexToAddress("0x89791428868131eb109e42340ad01eb8987526b2"), - kvList: make(Storage), - } - if so.db.blockNum != patch1.blockNumber && so.db.txIndex != patch1.txIndex { - return - } - patch1KVs := map[string]string{ - "0xf1e9242398de526b8dd9c25d38e65fbb01926b8940377762d7884b8b0dcdc3b0": "0x0000000000000000000000000000000000000000000000f6a7831804efd2cd0a", - } - for k, v := range patch1KVs { - patch1.kvList[libcommon.HexToHash(k)] = *new(uint256.Int).SetBytes(hexutil.MustDecode(v)) - } - totalPatches = append(totalPatches, patch1) - - // apply the patches - for _, patch := range totalPatches { - if so.db.txIndex != patch.txIndex { - continue - } - if so.address != patch.addr { - continue - } - for k, v := range patch.kvList { - so.originStorage[k] = v - } - } -} - -func (so *stateObject) patchGethHotFixChapel2() { - totalPatches := []HotFixPattern{} - // patch 1: BlockNum 35548081, txIndex 486 - patch1 := HotFixPattern{ - blockNumber: 35548081, - txIndex: 486, - txHash: libcommon.HexToHash("0xe3895eb95605d6b43ceec7876e6ff5d1c903e572bf83a08675cb684c047a695c"), - addr: libcommon.HexToAddress("0x89791428868131eb109e42340ad01eb8987526b2"), - kvList: make(Storage), - } - - if so.db.blockNum != patch1.blockNumber && so.db.txIndex != patch1.txIndex { - return - } - - patch1KVs := map[string]string{ - "0xf1e9242398de526b8dd9c25d38e65fbb01926b8940377762d7884b8b0dcdc3b0": "0x0000000000000000000000000000000000000000000000114be8ecea72b64003", - } - for k, v := range patch1KVs { - patch1.kvList[libcommon.HexToHash(k)] = *new(uint256.Int).SetBytes(hexutil.MustDecode(v)) - } - totalPatches = append(totalPatches, patch1) - - // apply the patches - for _, patch := range totalPatches { - if so.db.blockNum != patch.blockNumber { - continue - } - if so.address != patch.addr { - continue - } - for k, v := range patch.kvList { - so.originStorage[k] = v - } - } -} - // GetCommittedState retrieves a value from the committed account storage trie. func (so *stateObject) GetCommittedState(key *libcommon.Hash, out *uint256.Int) { // If the fake storage is set, only lookup the state here(in the debugging mode) @@ -335,9 +171,8 @@ func (so *stateObject) GetCommittedState(key *libcommon.Hash, out *uint256.Int) *out = so.fakeStorage[*key] return } - so.patchGethHotFixMainnet1() - so.patchGethHotFixChapel1() - so.patchGethHotFixChapel2() + so.patchGethHotFixMainnet() + so.patchGethHotFixChapel() // If we have the original value cached, return that { value, cached := so.originStorage[*key]