Skip to content

Commit e77ac51

Browse files
authored
all: replace Div/Mul with Rsh/Lsh if possible ethereum#29911 (#1966)
1 parent d12f980 commit e77ac51

19 files changed

Lines changed: 46 additions & 49 deletions

File tree

XDCx/order_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (XDCx *XDCX) getTradeQuantity(quotePrice *big.Int, coinbase common.Address,
379379
quotePrice = quoteTokenDecimal
380380
}
381381
if takerOrder.ExchangeAddress == makerOrder.ExchangeAddress {
382-
if err := tradingstate.CheckRelayerFee(takerOrder.ExchangeAddress, new(big.Int).Mul(common.RelayerFee, big.NewInt(2)), statedb); err != nil {
382+
if err := tradingstate.CheckRelayerFee(takerOrder.ExchangeAddress, new(big.Int).Lsh(common.RelayerFee, 1), statedb); err != nil {
383383
log.Debug("Reject order Taker Exchnage = Maker Exchange , relayer not enough fee ", "err", err)
384384
return tradingstate.Zero, false, nil, nil
385385
}

XDCx/tradingstate/settle_balance_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestGetSettleBalance(t *testing.T) {
7878
makerFeeRate: big.NewInt(10), // feeRate 0.1%
7979
baseTokenDecimal: common.BasePrice,
8080
quoteTokenDecimal: common.BasePrice,
81-
quantityToTrade: new(big.Int).Mul(big.NewInt(2), common.BasePrice),
81+
quantityToTrade: new(big.Int).Lsh(common.BasePrice, 1),
8282
},
8383
nil,
8484
true,
@@ -112,7 +112,7 @@ func TestGetSettleBalance(t *testing.T) {
112112
makerFeeRate: big.NewInt(10), // feeRate 0.1%
113113
baseTokenDecimal: common.BasePrice,
114114
quoteTokenDecimal: common.BasePrice,
115-
quantityToTrade: new(big.Int).Mul(big.NewInt(2), common.BasePrice),
115+
quantityToTrade: new(big.Int).Lsh(common.BasePrice, 1),
116116
},
117117
nil,
118118
true,
@@ -185,7 +185,7 @@ func TestGetSettleBalance(t *testing.T) {
185185
makerFeeRate: big.NewInt(10), // feeRate 0.1%
186186
baseTokenDecimal: common.BasePrice,
187187
quoteTokenDecimal: common.BasePrice,
188-
quantityToTrade: new(big.Int).Mul(big.NewInt(2), common.BasePrice),
188+
quantityToTrade: new(big.Int).Lsh(common.BasePrice, 1),
189189
},
190190
nil,
191191
true,
@@ -219,7 +219,7 @@ func TestGetSettleBalance(t *testing.T) {
219219
makerFeeRate: big.NewInt(10), // feeRate 0.1%
220220
baseTokenDecimal: common.BasePrice,
221221
quoteTokenDecimal: common.BasePrice,
222-
quantityToTrade: new(big.Int).Mul(big.NewInt(2), common.BasePrice),
222+
quantityToTrade: new(big.Int).Lsh(common.BasePrice, 1),
223223
},
224224
nil,
225225
true,

XDCxlending/lendingstate/settle_balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func CalculateInterestRate(finalizeTime, liquidationTime, term uint64, apr uint6
235235
// the time interval which borrower have to pay interest
236236
// (T + T1) / 2
237237
timeToPayInterest := new(big.Int).Add(new(big.Int).SetUint64(term), new(big.Int).SetUint64(borrowingTime))
238-
timeToPayInterest = new(big.Int).Div(timeToPayInterest, new(big.Int).SetUint64(2))
238+
timeToPayInterest = new(big.Int).Rsh(timeToPayInterest, 1)
239239

240240
interestRate := new(big.Int).SetUint64(apr)
241241
interestRate = new(big.Int).Mul(interestRate, timeToPayInterest)

XDCxlending/order_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (l *Lending) getLendQuantity(
450450
return lendingstate.Zero, lendingstate.Zero, false, nil, fmt.Errorf("fail to get tokenDecimal: Token: %v . Err: %v", collateralToken, err)
451451
}
452452
if takerOrder.Relayer == makerOrder.Relayer {
453-
if err := lendingstate.CheckRelayerFee(takerOrder.Relayer, new(big.Int).Mul(common.RelayerLendingFee, big.NewInt(2)), statedb); err != nil {
453+
if err := lendingstate.CheckRelayerFee(takerOrder.Relayer, new(big.Int).Lsh(common.RelayerLendingFee, 1), statedb); err != nil {
454454
log.Debug("Reject order Taker Exchnage = Maker Exchange , relayer not enough fee ", "err", err)
455455
return lendingstate.Zero, lendingstate.Zero, false, nil, nil
456456
}

XDCxlending/order_processor_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ func TestGetLendQuantity(t *testing.T) {
400400
common.BasePrice,
401401
depositRate,
402402
common.BasePrice,
403-
new(big.Int).Div(collateralLocked, big.NewInt(2)), // 1/2
403+
new(big.Int).Rsh(collateralLocked, 1), // 1/2
404404
lendQuantity,
405405
lendQuantity,
406406
},
407-
new(big.Int).Div(lendQuantity, big.NewInt(2)),
407+
new(big.Int).Rsh(lendQuantity, 1),
408408
false,
409409
},
410410
{
@@ -414,7 +414,7 @@ func TestGetLendQuantity(t *testing.T) {
414414
common.BasePrice,
415415
depositRate,
416416
common.BasePrice,
417-
new(big.Int).Div(collateralLocked, big.NewInt(2)),
417+
new(big.Int).Rsh(collateralLocked, 1),
418418
common.Big0,
419419
lendQuantity,
420420
},
@@ -429,10 +429,10 @@ func TestGetLendQuantity(t *testing.T) {
429429
depositRate,
430430
common.BasePrice,
431431
collateralLocked,
432-
new(big.Int).Div(lendQuantity, big.NewInt(2)),
432+
new(big.Int).Rsh(lendQuantity, 1),
433433
lendQuantity,
434434
},
435-
new(big.Int).Div(lendQuantity, big.NewInt(2)),
435+
new(big.Int).Rsh(lendQuantity, 1),
436436
true,
437437
},
438438
{
@@ -457,7 +457,7 @@ func TestGetLendQuantity(t *testing.T) {
457457
common.BasePrice,
458458
depositRate,
459459
common.BasePrice,
460-
new(big.Int).Div(collateralLocked, big.NewInt(2)),
460+
new(big.Int).Rsh(collateralLocked, 1),
461461
common.Big0,
462462
lendQuantity,
463463
},
@@ -471,11 +471,11 @@ func TestGetLendQuantity(t *testing.T) {
471471
common.BasePrice,
472472
depositRate,
473473
common.BasePrice,
474-
new(big.Int).Div(lendQuantity, big.NewInt(2)), // 1/2
474+
new(big.Int).Rsh(lendQuantity, 1), // 1/2
475475
collateralLocked,
476476
lendQuantity,
477477
},
478-
new(big.Int).Div(lendQuantity, big.NewInt(2)),
478+
new(big.Int).Rsh(lendQuantity, 1),
479479
false,
480480
},
481481
{
@@ -486,7 +486,7 @@ func TestGetLendQuantity(t *testing.T) {
486486
depositRate,
487487
common.BasePrice,
488488
common.Big0,
489-
new(big.Int).Div(collateralLocked, big.NewInt(2)),
489+
new(big.Int).Rsh(collateralLocked, 1),
490490
lendQuantity,
491491
},
492492
common.Big0,
@@ -514,10 +514,10 @@ func TestGetLendQuantity(t *testing.T) {
514514
depositRate,
515515
common.BasePrice,
516516
collateralLocked,
517-
new(big.Int).Div(collateralLocked, big.NewInt(2)),
517+
new(big.Int).Rsh(collateralLocked, 1),
518518
lendQuantity,
519519
},
520-
new(big.Int).Div(lendQuantity, big.NewInt(2)),
520+
new(big.Int).Rsh(lendQuantity, 1),
521521
true,
522522
},
523523
{

consensus/ethash/consensus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,10 @@ func accumulateRewards(config *params.ChainConfig, stateDB vm.StateDB, header *t
465465
r.Add(uncle.Number, big8)
466466
r.Sub(r, header.Number)
467467
r.Mul(r, blockReward)
468-
r.Div(r, big8)
468+
r.Rsh(r, 3)
469469
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)
470470

471-
r.Div(blockReward, big32)
471+
r.Rsh(blockReward, 5)
472472
reward.Add(reward, r)
473473
}
474474
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)

contracts/trc21issuer/simulation/test/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func airDropTokenToAccountNoXDC() {
3030
mainAccount.Nonce = big.NewInt(int64(nonce))
3131
mainAccount.Value = big.NewInt(0) // in wei
3232
mainAccount.GasLimit = uint64(4000000) // in units
33-
mainAccount.GasPrice = big.NewInt(0).Mul(common.TRC21GasPrice, big.NewInt(2))
33+
mainAccount.GasPrice = big.NewInt(0).Lsh(common.TRC21GasPrice, 1)
3434
trc21Instance, _ := trc21issuer.NewTRC21(mainAccount, trc21TokenAddr, client)
3535
trc21IssuerInstance, _ := trc21issuer.NewTRC21Issuer(mainAccount, common.TRC21IssuerSMC, client)
3636
// air drop token
@@ -76,7 +76,7 @@ func testTransferTRC21TokenWithAccountNoXDC() {
7676
airDropAccount.Nonce = big.NewInt(int64(nonce))
7777
airDropAccount.Value = big.NewInt(0) // in wei
7878
airDropAccount.GasLimit = uint64(4000000) // in units
79-
airDropAccount.GasPrice = big.NewInt(0).Mul(common.TRC21GasPrice, big.NewInt(2))
79+
airDropAccount.GasPrice = big.NewInt(0).Lsh(common.TRC21GasPrice, 1)
8080
trc21Instance, _ := trc21issuer.NewTRC21(airDropAccount, trc21TokenAddr, client)
8181
trc21IssuerInstance, _ := trc21issuer.NewTRC21Issuer(airDropAccount, common.TRC21IssuerSMC, client)
8282

@@ -139,7 +139,7 @@ func testTransferTrc21Fail() {
139139
airDropAccount.Nonce = big.NewInt(int64(nonce))
140140
airDropAccount.Value = big.NewInt(0) // in wei
141141
airDropAccount.GasLimit = uint64(4000000) // in units
142-
airDropAccount.GasPrice = big.NewInt(0).Mul(common.TRC21GasPrice, big.NewInt(2))
142+
airDropAccount.GasPrice = big.NewInt(0).Lsh(common.TRC21GasPrice, 1)
143143
trc21Instance, _ := trc21issuer.NewTRC21(airDropAccount, trc21TokenAddr, client)
144144
trc21IssuerInstance, _ := trc21issuer.NewTRC21Issuer(airDropAccount, common.TRC21IssuerSMC, client)
145145
balanceIssuerFee, _ := trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr)

core/txpool/legacypool/legacypool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,7 @@ func TestSetCodeTransactions(t *testing.T) {
26342634

26352635
minGasPrice := new(big.Int).Set(common.MinGasPrice)
26362636
minGasFee := uint256.MustFromBig(minGasPrice)
2637-
doubleGasFee := new(uint256.Int).Mul(new(uint256.Int).Set(minGasFee), uint256.NewInt(2))
2637+
doubleGasFee := new(uint256.Int).Lsh(new(uint256.Int).Set(minGasFee), 1)
26382638
tripleGasFee := new(uint256.Int).Mul(new(uint256.Int).Set(minGasFee), uint256.NewInt(3))
26392639
legacyReplacePrice := new(big.Int).Mul(minGasPrice, big.NewInt(10))
26402640

core/types/transaction_signing.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func NewEIP155Signer(chainId *big.Int) EIP155Signer {
412412
}
413413
return EIP155Signer{
414414
chainId: chainId,
415-
chainIdMul: new(big.Int).Mul(chainId, big.NewInt(2)),
415+
chainIdMul: new(big.Int).Lsh(chainId, 1),
416416
}
417417
}
418418

@@ -589,8 +589,8 @@ func deriveChainId(v *big.Int) *big.Int {
589589
}
590590
return new(big.Int).SetUint64((v - 35) / 2)
591591
}
592-
v = new(big.Int).Sub(v, big.NewInt(35))
593-
return v.Div(v, big.NewInt(2))
592+
v.Sub(v, big.NewInt(35))
593+
return v.Rsh(v, 1)
594594
}
595595

596596
func CacheSigner(signer Signer, tx *Transaction) {

core/vm/contracts.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ var (
296296
big0 = big.NewInt(0)
297297
big1 = big.NewInt(1)
298298
big3 = big.NewInt(3)
299-
big4 = big.NewInt(4)
300299
big7 = big.NewInt(7)
301-
big8 = big.NewInt(8)
302-
big16 = big.NewInt(16)
303300
big20 = big.NewInt(20)
304301
big32 = big.NewInt(32)
305302
big64 = big.NewInt(64)
@@ -326,13 +323,13 @@ func modexpMultComplexity(x *big.Int) *big.Int {
326323
case x.Cmp(big1024) <= 0:
327324
// (x ** 2 // 4 ) + ( 96 * x - 3072)
328325
x = new(big.Int).Add(
329-
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
326+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
330327
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
331328
)
332329
default:
333330
// (x ** 2 // 16) + (480 * x - 199680)
334331
x = new(big.Int).Add(
335-
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
332+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
336333
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
337334
)
338335
}
@@ -370,7 +367,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
370367
adjExpLen := new(big.Int)
371368
if expLen.Cmp(big32) > 0 {
372369
adjExpLen.Sub(expLen, big32)
373-
adjExpLen.Mul(big8, adjExpLen)
370+
adjExpLen.Lsh(adjExpLen, 3)
374371
}
375372
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
376373
// Calculate the gas cost of the operation
@@ -389,8 +386,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
389386
// ceiling(x/8)^2
390387
//
391388
// where is x is max(length_of_MODULUS, length_of_BASE)
392-
gas = gas.Add(gas, big7)
393-
gas = gas.Div(gas, big8)
389+
gas.Add(gas, big7)
390+
gas.Rsh(gas, 3)
394391
gas.Mul(gas, gas)
395392

396393
if adjExpLen.Cmp(big1) > 0 {

0 commit comments

Comments
 (0)