Remove CalcuttaBlock in favour of BlockAlloc (#8371)

System contract upgrades for Polygon are already handled by the
`BlockAlloc` logic and there's no need to duplicate it with the
`CalcuttaBlock` logic (there's no Calcutta in
https://github.com/maticnetwork/bor).
This commit is contained in:
Andrew Ashikhmin 2023-10-05 15:39:57 +02:00 committed by GitHub
parent 417e316ed5
commit 0bd6d77acd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 211 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
@ -36,7 +35,6 @@ import (
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/crypto"
@ -415,7 +413,7 @@ func New(
// make sure we can decode all the GenesisAlloc in the BorConfig.
for key, genesisAlloc := range c.config.BlockAlloc {
if _, err := decodeGenesisAlloc(genesisAlloc); err != nil {
if _, err := types.DecodeGenesisAlloc(genesisAlloc); err != nil {
panic(fmt.Sprintf("BUG: Block alloc '%s' in genesis is not correct: %v", key, err))
}
}
@ -1041,25 +1039,10 @@ func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.
return nil, types.Receipts{}, nil
}
func decodeGenesisAlloc(i interface{}) (types.GenesisAlloc, error) {
var alloc types.GenesisAlloc
b, err := json.Marshal(i)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &alloc); err != nil {
return nil, err
}
return alloc, nil
}
func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.IntraBlockState) error {
for blockNumber, genesisAlloc := range c.config.BlockAlloc {
if blockNumber == strconv.FormatUint(headerNumber, 10) {
allocs, err := decodeGenesisAlloc(genesisAlloc)
allocs, err := types.DecodeGenesisAlloc(genesisAlloc)
if err != nil {
return fmt.Errorf("failed to decode genesis alloc: %v", err)
}
@ -1128,7 +1111,6 @@ func (c *Bor) GenerateSeal(chain consensus.ChainHeaderReader, currnt, parent *ty
func (c *Bor) Initialize(config *chain.Config, chain consensus.ChainHeaderReader, header *types.Header,
state *state.IntraBlockState, syscall consensus.SysCallCustom, logger log.Logger) {
systemcontracts.UpgradeBuildInSystemContract(config, header.Number, state, logger)
}
// Authorize injects a private key into the consensus engine to mint new blocks

View File

@ -1,49 +1,59 @@
package core
import (
"encoding/hex"
"fmt"
"strconv"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname"
)
func init() {
// Initialise systemContractCodeLookup
// Initialise SystemContractCodeLookup
for _, chainName := range []string{networkname.BorMainnetChainName, networkname.MumbaiChainName, networkname.BorDevnetChainName} {
byChain := map[libcommon.Address][]libcommon.CodeRecord{}
systemcontracts.SystemContractCodeLookup[chainName] = byChain
// Apply genesis with the block number 0
genesisBlock := GenesisBlockByChainName(chainName)
for addr, alloc := range genesisBlock.Alloc {
if len(alloc.Code) > 0 {
list := byChain[addr]
codeHash, err := common.HashData(alloc.Code)
if err != nil {
panic(fmt.Errorf("failed to hash system contract code: %s", err.Error()))
}
list = append(list, libcommon.CodeRecord{BlockNumber: 0, CodeHash: codeHash})
byChain[addr] = list
}
}
allocToCodeRecords(genesisBlock.Alloc, byChain, 0)
// Process upgrades
chainConfig := params.ChainConfigByChainName(chainName)
if chainConfig.Bor != nil && chainConfig.Bor.CalcuttaBlock != nil {
blockNum := chainConfig.Bor.CalcuttaBlock.Uint64()
if blockNum != 0 {
addCodeRecords(systemcontracts.CalcuttaUpgrade[chainName], blockNum, byChain)
for blockNumStr, genesisAlloc := range chainConfig.Bor.BlockAlloc {
blockNum, err := strconv.ParseUint(blockNumStr, 10, 64)
if err != nil {
panic(fmt.Errorf("failed to parse block number in BlockAlloc: %s", err.Error()))
}
alloc, err := types.DecodeGenesisAlloc(genesisAlloc)
if err != nil {
panic(fmt.Errorf("failed to decode block alloc: %v", err))
}
allocToCodeRecords(alloc, byChain, blockNum)
}
}
addGnosisSpecialCase()
}
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]
codeHash, err := common.HashData(account.Code)
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
}
}
}
// 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() {
@ -75,19 +85,3 @@ func addGnosisSpecialCase() {
byChain[address] = list
}
func addCodeRecords(upgrade *systemcontracts.Upgrade, blockNum uint64, byChain map[libcommon.Address][]libcommon.CodeRecord) {
for _, config := range upgrade.Configs {
list := byChain[config.ContractAddr]
code, err := hex.DecodeString(config.Code)
if err != nil {
panic(fmt.Errorf("failed to decode system contract code: %s", err.Error()))
}
codeHash, err := common.HashData(code)
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[config.ContractAddr] = list
}
}

View File

@ -1,21 +0,0 @@
package systemcontracts
import (
libcommon "github.com/ledgerwatch/erigon-lib/common"
)
var (
// genesis contracts
ValidatorContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001000")
SlashContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001001")
SystemRewardContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001002")
LightClientContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001003")
TokenHubContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001004")
RelayerIncentivizeContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001005")
RelayerHubContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001006")
GovHubContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001007")
TokenManagerContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001008")
MaticTokenContract = libcommon.HexToAddress("0x0000000000000000000000000000000000001010")
CrossChainContract = libcommon.HexToAddress("0x0000000000000000000000000000000000002000")
StakingContract = libcommon.HexToAddress("0x0000000000000000000000000000000000002001")
)

File diff suppressed because one or more lines are too long

View File

@ -70,13 +70,6 @@ type Genesis struct {
// GenesisAlloc specifies the initial state that is part of the genesis block.
type GenesisAlloc map[common.Address]GenesisAccount
type AuthorityRoundSeal struct {
/// Seal step.
Step uint64 `json:"step"`
/// Seal signature.
Signature common.Hash `json:"signature"`
}
func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
m := make(map[common2.UnprefixedAddress]GenesisAccount)
if err := json.Unmarshal(data, &m); err != nil {
@ -89,6 +82,21 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
return nil
}
func DecodeGenesisAlloc(i interface{}) (GenesisAlloc, error) {
var alloc GenesisAlloc
b, err := json.Marshal(i)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &alloc); err != nil {
return nil, err
}
return alloc, nil
}
// GenesisAccount is an account in the state of the genesis block.
// Either use "constructor" for deployment code or "code" directly for the final code.
type GenesisAccount struct {

View File

@ -410,11 +410,10 @@ type BorConfig struct {
OverrideStateSyncRecords map[string]int `json:"overrideStateSyncRecords"` // override state records count
BlockAlloc map[string]interface{} `json:"blockAlloc"`
CalcuttaBlock *big.Int `json:"calcuttaBlock"` // Calcutta switch block (nil = no fork, 0 = already on calcutta)
JaipurBlock *big.Int `json:"jaipurBlock"` // Jaipur switch block (nil = no fork, 0 = already on jaipur)
DelhiBlock *big.Int `json:"delhiBlock"` // Delhi switch block (nil = no fork, 0 = already on delhi)
IndoreBlock *big.Int `json:"indoreBlock"` // Indore switch block (nil = no fork, 0 = already on indore)
StateSyncConfirmationDelay map[string]uint64 `json:"stateSyncConfirmationDelay"` // StateSync Confirmation Delay, in seconds, to calculate `to`
sprints sprints
@ -504,14 +503,6 @@ func (c *BorConfig) IsDelhi(number uint64) bool {
return isForked(c.DelhiBlock, number)
}
func (c *BorConfig) IsCalcutta(number uint64) bool {
return isForked(c.CalcuttaBlock, number)
}
func (c *BorConfig) IsOnCalcutta(number *big.Int) bool {
return numEqual(c.CalcuttaBlock, number)
}
func (c *BorConfig) IsIndore(number uint64) bool {
return isForked(c.IndoreBlock, number)
}

View File

@ -22,7 +22,7 @@
"0": 4
},
"sprint": {
"0":16
"0": 16
},
"backupMultiplier": {
"0": 5
@ -41,8 +41,7 @@
}
},
"jaipurBlock": 0,
"delhiBlock":0,
"calcuttaBlock": 30,
"delhiBlock": 0,
"indoreBlock": 0
}
}

View File

@ -53,7 +53,6 @@
}
}
},
"calcuttaBlock": 22156660,
"jaipurBlock": 23850000,
"delhiBlock": 38189056,
"indoreBlock": 44934656

View File

@ -47,7 +47,6 @@
}
}
},
"calcuttaBlock": 22244000,
"jaipurBlock": 22770000,
"delhiBlock": 29638656,
"indoreBlock": 37075456