XinFinOrg / XDPoSChain

Customer centric, Hybrid & Interoperable XinFin Network
https://www.xinfin.org
GNU Lesser General Public License v3.0
51 stars 63 forks source link

core/vm: replace function `precompile2` with `precompile` #641

Closed gzliudan closed 1 week ago

gzliudan commented 2 weeks ago

Proposed changes

This PR replaces function precompile2 with precompile:

func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
    var precompiles map[common.Address]PrecompiledContract
    switch {
    case evm.chainRules.IsXDCxDisable:
        precompiles = PrecompiledContractsXDCv2
    case evm.chainRules.IsIstanbul:
        precompiles = PrecompiledContractsIstanbul
    case evm.chainRules.IsByzantium:
        precompiles = PrecompiledContractsByzantium
    default:
        precompiles = PrecompiledContractsHomestead
    }
    p, ok := precompiles[addr]
    return p, ok
}

func (evm *EVM) precompile2(addr common.Address) (PrecompiledContract, bool) {
    var precompiles map[common.Address]PrecompiledContract
    switch {
    case evm.chainRules.IsXDCxDisable:
        precompiles = PrecompiledContractsXDCv2
    case evm.chainRules.IsIstanbul && evm.ChainConfig().IsTIPXDCXCancellationFee(evm.BlockNumber):
        precompiles = PrecompiledContractsIstanbul
    case evm.chainRules.IsByzantium:
        precompiles = PrecompiledContractsByzantium
    default:
        precompiles = PrecompiledContractsHomestead
    }
    p, ok := precompiles[addr]
    return p, ok
}

The conditions evm.chainRules.IsIstanbul and evm.ChainConfig().IsTIPXDCXCancellationFee(evm.BlockNumber) are same because:

func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
    return isForked(common.TIPXDCXCancellationFee, num) || isForked(c.IstanbulBlock, num)
}

func (c *ChainConfig) IsTIPXDCXCancellationFee(num *big.Int) bool {
    return isForked(common.TIPXDCXCancellationFee, num)
}

Types of changes

What types of changes does your code introduce to XDC network? Put an in the boxes that apply

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

wanwiset25 commented 2 weeks ago

It doesn't matter now because the fork block have passed, but minor nitpick: the condition is not exactly the same because in precompile2 it is && condition while in IsIstanbul it is || condition.

gzliudan commented 2 weeks ago
func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
  return isForked(common.TIPXDCXCancellationFee, num) || isForked(c.IstanbulBlock, num)
}

The old version is:

func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
    return isForked(common.TIPXDCXCancellationFee, num)
}

I change it to:

return isForked(common.TIPXDCXCancellationFee, num) || isForked(c.IstanbulBlock, num)

for other purpose in a PR ago.