Skip to content

Commit fb91c1e

Browse files
authored
add trigger for disable XDCx precompiles (#509)
1 parent ef4b8ef commit fb91c1e

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

core/vm/contracts.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
8787
common.BytesToAddress([]byte{42}): &XDCxEpochPrice{},
8888
}
8989

90+
var PrecompiledContractsXDCv2 = map[common.Address]PrecompiledContract{
91+
common.BytesToAddress([]byte{1}): &ecrecover{},
92+
common.BytesToAddress([]byte{2}): &sha256hash{},
93+
common.BytesToAddress([]byte{3}): &ripemd160hash{},
94+
common.BytesToAddress([]byte{4}): &dataCopy{},
95+
common.BytesToAddress([]byte{5}): &bigModExp{},
96+
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
97+
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
98+
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
99+
common.BytesToAddress([]byte{9}): &blake2F{},
100+
}
101+
90102
// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
91103
func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
92104
gas := p.RequiredGas(input)

core/vm/contracts_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,15 @@ var bn256PairingTests = []precompiledTest{
357357
}
358358

359359
var XDCxLastPriceTests = []precompiledTest{
360+
// {
361+
// input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
362+
// expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTLastPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
363+
// name: "BTCUSDT",
364+
// },
365+
// since we diable XDCx precompiles, the test now returns 0
360366
{
361367
input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
362-
expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTLastPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
368+
expected: common.Bytes2Hex(common.LeftPadBytes(common.Big0.Bytes(), XDCXPriceNumberOfBytesReturn)),
363369
name: "BTCUSDT",
364370
},
365371
{
@@ -375,9 +381,15 @@ var XDCxLastPriceTests = []precompiledTest{
375381
}
376382

377383
var XDCxEpochPriceTests = []precompiledTest{
384+
// {
385+
// input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
386+
// expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTEpochPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
387+
// name: "BTCUSDT",
388+
// },
389+
// since we diable XDCx precompiles, the test now returns 0
378390
{
379391
input: common.Bytes2Hex(append(common.Hex2BytesFixed(BTCAddress, 32), common.Hex2BytesFixed(USDTAddress, 32)...)),
380-
expected: common.Bytes2Hex(common.LeftPadBytes(BTCUSDTEpochPrice.Bytes(), XDCXPriceNumberOfBytesReturn)),
392+
expected: common.Bytes2Hex(common.LeftPadBytes(common.Big0.Bytes(), XDCXPriceNumberOfBytesReturn)),
381393
name: "BTCUSDT",
382394
},
383395
{

core/vm/evm.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
4848
if contract.CodeAddr != nil {
4949
var precompiles map[common.Address]PrecompiledContract
5050
switch {
51+
case evm.chainRules.IsXDCxDisable:
52+
precompiles = PrecompiledContractsXDCv2
5153
case evm.chainRules.IsIstanbul:
5254
precompiles = PrecompiledContractsIstanbul
5355
case evm.chainRules.IsByzantium:
@@ -56,11 +58,13 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
5658
precompiles = PrecompiledContractsHomestead
5759
}
5860
if p := precompiles[*contract.CodeAddr]; p != nil {
59-
switch p.(type) {
60-
case *XDCxEpochPrice:
61-
p.(*XDCxEpochPrice).SetTradingState(evm.tradingStateDB)
62-
case *XDCxLastPrice:
63-
p.(*XDCxLastPrice).SetTradingState(evm.tradingStateDB)
61+
if evm.chainConfig.IsTIPXDCXReceiver(evm.BlockNumber) {
62+
switch p := p.(type) {
63+
case *XDCxEpochPrice:
64+
p.SetTradingState(evm.tradingStateDB)
65+
case *XDCxLastPrice:
66+
p.SetTradingState(evm.tradingStateDB)
67+
}
6468
}
6569
return RunPrecompiledContract(p, input, contract)
6670
}

params/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ type Rules struct {
753753
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
754754
IsBerlin, IsLondon bool
755755
IsMerge, IsShanghai bool
756+
IsXDCxDisable bool
756757
}
757758

758759
func (c *ChainConfig) Rules(num *big.Int) Rules {
@@ -774,5 +775,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
774775
IsLondon: c.IsLondon(num),
775776
IsMerge: c.IsMerge(num),
776777
IsShanghai: c.IsShanghai(num),
778+
IsXDCxDisable: c.IsTIPXDCXReceiver(num),
777779
}
778780
}

0 commit comments

Comments
 (0)