Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genesis change fix #252

Merged
merged 16 commits into from
Dec 3, 2021
25 changes: 25 additions & 0 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,27 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,
// Set state sync data to blockchain
bc := chain.(*core.BlockChain)
bc.SetStateSync(stateSyncData)

c.changeContractCodeIfNeeded(headerNumber, state)

}

func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.StateDB) {
flag := false
for blockNumber, genesisAlloc := range c.config.BlockAlloc {
if blockNumber == strconv.FormatUint(headerNumber+1, 10) {
var tempBytesConverted core.GenesisAlloc
b, _ := json.Marshal(genesisAlloc)
json.Unmarshal(b, &tempBytesConverted)
for addr, account := range tempBytesConverted {
state.SetCode(addr, account.Code)
}
flag = true
}
}
if flag {
state.Commit(false)
}
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
Expand All @@ -690,6 +711,7 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ
stateSyncData := []*types.StateSyncData{}

headerNumber := header.Number.Uint64()

if headerNumber%c.config.Sprint == 0 {
cx := chainContext{Chain: chain, Bor: c}

Expand Down Expand Up @@ -722,6 +744,9 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ
bc.SetStateSync(stateSyncData)

// return the final block for sealing

c.changeContractCodeIfNeeded(headerNumber, state)

return block, nil
}

Expand Down
3 changes: 3 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
if err := newcfg.CheckConfigForkOrder(); err != nil {
return newcfg, common.Hash{}, err
}

storedcfg := rawdb.ReadChainConfig(db, stored)
if storedcfg == nil {
log.Warn("Found genesis block without chain config")
Expand All @@ -236,6 +237,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
if compatErr != nil && *height != 0 && compatErr.RewindTo != 0 {
return newcfg, stored, compatErr
}

rawdb.WriteChainConfig(db, stored, newcfg)
return newcfg, stored, nil
}
Expand Down Expand Up @@ -279,6 +281,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
statedb.SetState(addr, key, value)
}
}

root := statedb.IntermediateRoot(false)
head := &types.Header{
Number: new(big.Int).SetUint64(g.Number),
Expand Down
3 changes: 2 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ type BorConfig struct {
ValidatorContract string `json:"validatorContract"` // Validator set contract
StateReceiverContract string `json:"stateReceiverContract"` // State receiver contract

OverrideStateSyncRecords map[string]int `json:"overrideStateSyncRecords"` // override state records count
OverrideStateSyncRecords map[string]int `json:"overrideStateSyncRecords"` // override state records count
BlockAlloc map[string]interface{} `json:"block_alloc"`
ferranbt marked this conversation as resolved.
Show resolved Hide resolved
}

// String implements the stringer interface, returning the consensus engine details.
Expand Down