2022-12-10 22:41:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-05 13:39:57 +00:00
|
|
|
"strconv"
|
2022-12-10 22:41:04 +00:00
|
|
|
|
2023-10-18 06:37:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain/networkname"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-03-01 21:29:10 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/hexutility"
|
2024-01-09 18:20:42 +00:00
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"
|
2023-03-01 21:29:10 +00:00
|
|
|
|
2022-12-10 22:41:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/systemcontracts"
|
2023-10-05 13:39:57 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2022-12-10 22:41:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-10-05 13:39:57 +00:00
|
|
|
// Initialise SystemContractCodeLookup
|
2023-11-30 01:19:52 +00:00
|
|
|
for _, chainName := range []string{networkname.BorMainnetChainName, networkname.MumbaiChainName, networkname.AmoyChainName, networkname.BorDevnetChainName} {
|
2023-01-22 12:39:33 +00:00
|
|
|
byChain := map[libcommon.Address][]libcommon.CodeRecord{}
|
2022-12-10 22:41:04 +00:00
|
|
|
systemcontracts.SystemContractCodeLookup[chainName] = byChain
|
|
|
|
// Apply genesis with the block number 0
|
2023-03-20 15:44:22 +00:00
|
|
|
genesisBlock := GenesisBlockByChainName(chainName)
|
2023-10-05 13:39:57 +00:00
|
|
|
allocToCodeRecords(genesisBlock.Alloc, byChain, 0)
|
2022-12-10 22:41:04 +00:00
|
|
|
// Process upgrades
|
|
|
|
chainConfig := params.ChainConfigByChainName(chainName)
|
2024-01-04 09:44:57 +00:00
|
|
|
borConfig := chainConfig.Bor.(*borcfg.BorConfig)
|
|
|
|
for blockNumStr, genesisAlloc := range borConfig.BlockAlloc {
|
2023-10-05 13:39:57 +00:00
|
|
|
blockNum, err := strconv.ParseUint(blockNumStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to parse block number in BlockAlloc: %s", err.Error()))
|
2023-03-02 14:12:16 +00:00
|
|
|
}
|
2023-10-05 13:39:57 +00:00
|
|
|
alloc, err := types.DecodeGenesisAlloc(genesisAlloc)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to decode block alloc: %v", err))
|
|
|
|
}
|
|
|
|
allocToCodeRecords(alloc, byChain, blockNum)
|
2023-03-02 14:12:16 +00:00
|
|
|
}
|
2022-12-10 22:41:04 +00:00
|
|
|
}
|
2023-03-01 21:29:10 +00:00
|
|
|
|
|
|
|
addGnosisSpecialCase()
|
|
|
|
}
|
|
|
|
|
2023-10-05 13:39:57 +00:00
|
|
|
func allocToCodeRecords(alloc types.GenesisAlloc, byChain map[libcommon.Address][]libcommon.CodeRecord, blockNum uint64) {
|
|
|
|
for addr, account := range alloc {
|
|
|
|
if len(account.Code) > 0 {
|
|
|
|
list := byChain[addr]
|
2023-10-21 23:17:18 +00:00
|
|
|
codeHash, err := libcommon.HashData(account.Code)
|
2023-10-05 13:39:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to hash system contract code: %s", err.Error()))
|
|
|
|
}
|
|
|
|
list = append(list, libcommon.CodeRecord{BlockNumber: blockNum, CodeHash: codeHash})
|
|
|
|
byChain[addr] = list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:29:10 +00:00
|
|
|
// some hard coding for gnosis chain here to solve a historical problem with the token contract being re-written
|
|
|
|
// and losing the history for it in the DB. Temporary hack until erigon 3 arrives
|
|
|
|
func addGnosisSpecialCase() {
|
|
|
|
byChain := map[libcommon.Address][]libcommon.CodeRecord{}
|
|
|
|
systemcontracts.SystemContractCodeLookup[networkname.GnosisChainName] = byChain
|
|
|
|
address := libcommon.HexToAddress("0xf8d1677c8a0c961938bf2f9adc3f3cfda759a9d9")
|
|
|
|
list := byChain[address]
|
|
|
|
|
|
|
|
oldContractCode := hexutility.FromHex("0x6080604052600436106101b65763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166305d2035b81146101bb57806306fdde03146101e4578063095ea7b31461026e5780630b26cf661461029257806318160ddd146102b557806323b872dd146102dc57806330adf81f14610306578063313ce5671461031b5780633644e51514610346578063395093511461035b5780634000aea01461037f57806340c10f19146103b057806342966c68146103d457806354fd4d50146103ec578063661884631461040157806369ffa08a1461042557806370a082311461044c578063715018a61461046d578063726600ce146104825780637d64bcb4146104a35780637ecebe00146104b8578063859ba28c146104d95780638da5cb5b1461051a5780638fcbaf0c1461054b57806395d89b4114610589578063a457c2d71461059e578063a9059cbb146105c2578063b753a98c146105e6578063bb35783b1461060a578063cd59658314610634578063d73dd62314610649578063dd62ed3e1461066d578063f2d5d56b14610694578063f2fde38b146106b8578063ff9e884d146106d9575b600080fd5b3480156101c757600080fd5b506101d0610700565b604080519115158252519081900360200190f35b3480156101f057600080fd5b506101f9610721565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561023357818101518382015260200161021b565b50505050905090810190601f1680156102605780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561027a57600080fd5b506101d0600160a060020a03600435166024356107af565b34801561029e57600080fd5b506102b3600160a060020a0360043516610803565b005b3480156102c157600080fd5b506102ca61085d565b60408051918252519081900360200190f35b3480156102e857600080fd5b506101d0600160a060020a0360043581169060243516604435610863565b34801561031257600080fd5b506102ca610a32565b34801561032757600080fd5b50610330610a56565b6040805160ff9092168252519081900360200190f35b34801561035257600080fd5b506102ca610a5f565b34801561036757600080fd5b506101d0600160a060020a0360043516602435610a65565b34801561038b57600080fd5b506101d060048035600160a060020a0316906024803591604435918201910135610a78565b3480156103bc57600080fd5b506101d0600160a060020a0360043516602435610b89565b3480156103e057600080fd5b506102b3600435610c94565b3480156103f857600080fd5b506101f9610ca1565b34801561040d57600080fd5b506101d0600160a060020a0360043516602435610cd8565b34801561043157600080fd5b506102b3600160a060020a0360043581169060243516610db5565b34801561045857600080fd5b506102ca600160a060020a0360043516610df1565b34801561047957600080fd5b506102b3610e0c565b34801561048e57600080fd5b506101d0600160a060020a0360043516610e23565b3480156104af57600080fd5b506101d0610e37565b3480156104c457600080fd5b506102ca600160a060020a0360043516610e3e565b3480156104e557600080fd5b506104ee610e50565b6040805167ffffffffffffffff9485168152928416602084015292168183015290519081900360600190f35b34801561052657600080fd5b5061052f610e5b565b60408051600160a060020a039092168252519081900360200190f35b34801561055757600080fd5b506102b3600160a060020a0360043581169060243516604435606435608435151560ff60a4351660c43560e435610e6a565b34801561059557600080fd5b506101f9611171565b3480156105aa57600080fd5b506101d0600160a060020a03600435166024356111cb565b3480156105ce57600080fd5b506101d0600160a060020a03600435166024356111d7565b3480156105f257600080fd5b506102b3600160a060020a0360043516602435611202565b34801561061657600080fd5b506102b3600160a060020a036004358116906024351660443561120d565b34801561064057600080fd5b5061052f61121e565b34801561065557600080fd5b506101d0600160a060020a036004351660243561122d565b34801561067957600080fd5b506102ca600160a060020a03600435811690602435166112b4565b3480156106a057600080fd5b506102b3600160a060020a03600435166024356112df565b3480156106c457600080fd5b506102b3600160a060020a03600435166112ea565b3480156106e557600080fd5b506102ca600160a060020a036004358116906024351661130a565b60065474010000000000000000000000000000000000000000900460ff1681565b6000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156107a75780601f1061077c576101008083540402835291602001916107a7565b820191906000526020600020905b81548152906001019060200180831161078a57829003601f168201915b505050505081565b336000818152600560209081526040808320600160a060020a0387168085529083528184208690558151868152915193949093909260008051602
|
|
|
|
newContractCode := hexutility.FromHex("0x6080604052600436106101b35763ffffffff60e060020a60003504166305d2035b81146101b857806306fdde03146101e1578063095ea7b31461026b5780630b26cf661461028f57806318160ddd146102b257806323b872dd146102d957806330adf81f14610303578063313ce567146103185780633644e5151461034357806339509351146103585780634000aea01461037c57806340c10f19146103ad57806342966c68146103d157806354fd4d50146103e957806366188463146103fe57806369ffa08a1461042257806370a0823114610449578063715018a61461046a578063726600ce1461047f5780637d64bcb4146104a05780637ecebe00146104b5578063859ba28c146104d65780638da5cb5b146105175780638fcbaf0c1461054857806395d89b4114610586578063a457c2d71461059b578063a9059cbb146105bf578063b753a98c146105e3578063bb35783b14610607578063c6a1dedf14610631578063cd59658314610646578063d505accf1461065b578063d73dd62314610694578063dd62ed3e146106b8578063f2d5d56b146106df578063f2fde38b14610703578063ff9e884d14610724575b600080fd5b3480156101c457600080fd5b506101cd61074b565b604080519115158252519081900360200190f35b3480156101ed57600080fd5b506101f661076c565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610230578181015183820152602001610218565b50505050905090810190601f16801561025d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561027757600080fd5b506101cd600160a060020a03600435166024356107fa565b34801561029b57600080fd5b506102b0600160a060020a0360043516610810565b005b3480156102be57600080fd5b506102c761086a565b60408051918252519081900360200190f35b3480156102e557600080fd5b506101cd600160a060020a0360043581169060243516604435610870565b34801561030f57600080fd5b506102c7610a38565b34801561032457600080fd5b5061032d610a5c565b6040805160ff9092168252519081900360200190f35b34801561034f57600080fd5b506102c7610a65565b34801561036457600080fd5b506101cd600160a060020a0360043516602435610a6b565b34801561038857600080fd5b506101cd60048035600160a060020a0316906024803591604435918201910135610aac565b3480156103b957600080fd5b506101cd600160a060020a0360043516602435610bbd565b3480156103dd57600080fd5b506102b0600435610cc8565b3480156103f557600080fd5b506101f6610cd5565b34801561040a57600080fd5b506101cd600160a060020a0360043516602435610d0c565b34801561042e57600080fd5b506102b0600160a060020a0360043581169060243516610de9565b34801561045557600080fd5b506102c7600160a060020a0360043516610e0e565b34801561047657600080fd5b506102b0610e29565b34801561048b57600080fd5b506101cd600160a060020a0360043516610e40565b3480156104ac57600080fd5b506101cd610e54565b3480156104c157600080fd5b506102c7600160a060020a0360043516610e5b565b3480156104e257600080fd5b506104eb610e6d565b6040805167ffffffffffffffff9485168152928416602084015292168183015290519081900360600190f35b34801561052357600080fd5b5061052c610e78565b60408051600160a060020a039092168252519081900360200190f35b34801561055457600080fd5b506102b0600160a060020a0360043581169060243516604435606435608435151560ff60a4351660c43560e435610e87565b34801561059257600080fd5b506101f6610fc5565b3480156105a757600080fd5b506101cd600160a060020a036004351660243561101f565b3480156105cb57600080fd5b506101cd600160a060020a0360043516602435611032565b3480156105ef57600080fd5b506102b0600160a060020a0360043516602435611054565b34801561061357600080fd5b506102b0600160a060020a0360043581169060243516604435611064565b34801561063d57600080fd5b506102c7611075565b34801561065257600080fd5b5061052c611099565b34801561066757600080fd5b506102b0600160a060020a036004358116906024351660443560643560ff6084351660a43560c4356110a8565b3480156106a057600080fd5b506101cd600160a060020a0360043516602435611184565b3480156106c457600080fd5b506102c7600160a060020a036004358116906024351661120b565b3480156106eb57600080fd5b506102b0600160a060020a0360043516602435611236565b34801561070f57600080fd5b506102b0600160a060020a0360043516611241565b34801561073057600080fd5b506102c7600160a060020a0360043581169060243516611261565b60065474010000000000000000000000000000000000000000900460ff1681565b6000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156107f25780601f106107c7576101008083540402835291602001916107f2565b820191906000526020600020905b8154815290600101906020018083116107d557829
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
codeHash, err := libcommon.HashData(oldContractCode)
|
2023-03-01 21:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("could not get code hash from old gnosis token contract")
|
|
|
|
}
|
|
|
|
list = append(list, libcommon.CodeRecord{
|
|
|
|
BlockNumber: 0,
|
|
|
|
CodeHash: codeHash,
|
|
|
|
})
|
|
|
|
|
2023-10-21 23:17:18 +00:00
|
|
|
codeHash, err = libcommon.HashData(newContractCode)
|
2023-03-01 21:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("could not get code hash from new gnosis token contract")
|
|
|
|
}
|
|
|
|
list = append(list, libcommon.CodeRecord{
|
|
|
|
BlockNumber: 21735000,
|
|
|
|
CodeHash: codeHash,
|
|
|
|
})
|
|
|
|
|
|
|
|
byChain[address] = list
|
2022-12-10 22:41:04 +00:00
|
|
|
}
|