add trigger for disable XDCx precompiles (#509)

This commit is contained in:
Wanwiset Peerapatanapokin 2024-04-22 11:43:22 +04:00 committed by GitHub
parent ef4b8ef91b
commit fb91c1e802
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 7 deletions

View file

@ -87,6 +87,18 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{42}): &XDCxEpochPrice{},
}
var PrecompiledContractsXDCv2 = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
}
// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
gas := p.RequiredGas(input)

View file

@ -357,9 +357,15 @@ var bn256PairingTests = []precompiledTest{
}
var XDCxLastPriceTests = []precompiledTest{
// {
// input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
// expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTLastPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
// name: "BTCUSDT",
// },
// since we diable XDCx precompiles, the test now returns 0
{
input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTLastPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
expected: common.Bytes2Hex(common.LeftPadBytes(common.Big0.Bytes(), XDCXPriceNumberOfBytesReturn)),
name: "BTCUSDT",
},
{
@ -375,9 +381,15 @@ var XDCxLastPriceTests = []precompiledTest{
}
var XDCxEpochPriceTests = []precompiledTest{
// {
// input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
// expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTEpochPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
// name: "BTCUSDT",
// },
// since we diable XDCx precompiles, the test now returns 0
{
input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTEpochPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
expected: common.Bytes2Hex(common.LeftPadBytes(common.Big0.Bytes(), XDCXPriceNumberOfBytesReturn)),
name: "BTCUSDT",
},
{

View file

@ -48,6 +48,8 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
if contract.CodeAddr != nil {
var precompiles map[common.Address]PrecompiledContract
switch {
case evm.chainRules.IsXDCxDisable:
precompiles = PrecompiledContractsXDCv2
case evm.chainRules.IsIstanbul:
precompiles = PrecompiledContractsIstanbul
case evm.chainRules.IsByzantium:
@ -56,11 +58,13 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
precompiles = PrecompiledContractsHomestead
}
if p := precompiles[*contract.CodeAddr]; p != nil {
switch p.(type) {
case *XDCxEpochPrice:
p.(*XDCxEpochPrice).SetTradingState(evm.tradingStateDB)
case *XDCxLastPrice:
p.(*XDCxLastPrice).SetTradingState(evm.tradingStateDB)
if evm.chainConfig.IsTIPXDCXReceiver(evm.BlockNumber) {
switch p := p.(type) {
case *XDCxEpochPrice:
p.SetTradingState(evm.tradingStateDB)
case *XDCxLastPrice:
p.SetTradingState(evm.tradingStateDB)
}
}
return RunPrecompiledContract(p, input, contract)
}

View file

@ -753,6 +753,7 @@ type Rules struct {
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
IsBerlin, IsLondon bool
IsMerge, IsShanghai bool
IsXDCxDisable bool
}
func (c *ChainConfig) Rules(num *big.Int) Rules {
@ -774,5 +775,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
IsLondon: c.IsLondon(num),
IsMerge: c.IsMerge(num),
IsShanghai: c.IsShanghai(num),
IsXDCxDisable: c.IsTIPXDCXReceiver(num),
}
}