From fb91c1e802a1fd4f7a6377410063cc1ac3f5c90f Mon Sep 17 00:00:00 2001 From: Wanwiset Peerapatanapokin Date: Mon, 22 Apr 2024 11:43:22 +0400 Subject: [PATCH] add trigger for disable XDCx precompiles (#509) --- core/vm/contracts.go | 12 ++++++++++++ core/vm/contracts_test.go | 16 ++++++++++++++-- core/vm/evm.go | 14 +++++++++----- params/config.go | 2 ++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 067bc7018a..a9e76a83e7 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -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) diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index 497943d174..4d8a4c762f 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -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", }, { diff --git a/core/vm/evm.go b/core/vm/evm.go index 227d38412c..9fc32ab0b4 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -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) } diff --git a/params/config.go b/params/config.go index 92210a4065..36b336a3f2 100644 --- a/params/config.go +++ b/params/config.go @@ -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), } }