Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create less instances of bn.js #256

Merged
merged 1 commit into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/opFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ module.exports = {
return new BN(runState.lastReturned.length)
},
RETURNDATACOPY: function (memOffset, returnDataOffset, length, runState) {
if ((returnDataOffset.add(length)).gt(new BN(runState.lastReturned.length))) {
if ((returnDataOffset.add(length)).gtn(runState.lastReturned.length)) {
trap(ERROR.OUT_OF_GAS)
}

Expand Down Expand Up @@ -346,7 +346,7 @@ module.exports = {
subGas(runState, new BN(fees.sstoreResetGas.v))
} else if (value.length === 0 && found.length) {
subGas(runState, new BN(fees.sstoreResetGas.v))
runState.gasRefund.iadd(new BN(fees.sstoreRefundGas.v))
runState.gasRefund.iaddn(fees.sstoreRefundGas.v)
} else if (value.length !== 0 && !found.length) {
subGas(runState, new BN(fees.sstoreSetGas.v))
} else if (value.length !== 0 && found.length) {
Expand Down Expand Up @@ -546,8 +546,8 @@ module.exports = {
}

if (!value.isZero()) {
runState.gasLeft.iadd(new BN(fees.callStipend.v))
options.gasLimit.iadd(new BN(fees.callStipend.v))
runState.gasLeft.iaddn(fees.callStipend.v)
options.gasLimit.iaddn(fees.callStipend.v)
}

makeCall(runState, options, localOpts, done)
Expand Down Expand Up @@ -583,8 +583,8 @@ module.exports = {
checkOutOfGas(runState, options)

if (!value.isZero()) {
runState.gasLeft.iadd(new BN(fees.callStipend.v))
options.gasLimit.iadd(new BN(fees.callStipend.v))
runState.gasLeft.iaddn(fees.callStipend.v)
options.gasLimit.iaddn(fees.callStipend.v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

}

// load the code
Expand Down Expand Up @@ -710,7 +710,6 @@ module.exports = {
var stateManager = runState.stateManager
var contract = runState.contract
var contractAddress = runState.address
var zeroBalance = new BN(0)
selfdestructToAddress = addressToBuffer(selfdestructToAddress)

stateManager.getAccount(selfdestructToAddress, function (err, toAccount) {
Expand All @@ -726,7 +725,7 @@ module.exports = {
return
}

if ((new BN(contract.balance)).gt(zeroBalance)) {
if ((new BN(contract.balance)).gtn(0)) {
if (!toAccount.exists || empty) {
try {
subGas(runState, new BN(fees.callNewAccountGas.v))
Expand All @@ -739,7 +738,7 @@ module.exports = {

// only add to refund if this is the first selfdestruct for the address
if (!runState.selfdestruct[contractAddress.toString('hex')]) {
runState.gasRefund = runState.gasRefund.add(new BN(fees.suicideRefundGas.v))
runState.gasRefund = runState.gasRefund.addn(fees.suicideRefundGas.v)
}
runState.selfdestruct[contractAddress.toString('hex')] = selfdestructToAddress
runState.stopped = true
Expand Down Expand Up @@ -863,7 +862,7 @@ function memStore (runState, offset, val, valOffset, length, skipSubMem) {

var safeLen = 0
if (valOffset.addn(length).gtn(val.length)) {
if (valOffset.gte(new BN(val.length))) {
if (valOffset.gten(val.length)) {
safeLen = 0
} else {
valOffset = valOffset.toNumber()
Expand Down Expand Up @@ -910,7 +909,7 @@ function checkCallMemCost (runState, callOptions, localOpts) {
}

function checkOutOfGas (runState, callOptions) {
const gasAllowed = runState.gasLeft.sub(runState.gasLeft.div(new BN(64)))
const gasAllowed = runState.gasLeft.sub(runState.gasLeft.divn(64))
if (callOptions.gasLimit.gt(gasAllowed)) {
callOptions.gasLimit = gasAllowed
}
Expand Down
8 changes: 4 additions & 4 deletions lib/runBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ module.exports = function (opts, cb) {
ommers.forEach(rewardOmmer)

// calculate nibling reward
var niblingReward = minerReward.div(new BN(32))
var totalNiblingReward = niblingReward.mul(new BN(ommers.length))
var niblingReward = minerReward.divn(32)
var totalNiblingReward = niblingReward.muln(ommers.length)
minerAccount = self.stateManager.cache.get(block.header.coinbase)
// give miner the block reward
minerAccount.balance = new BN(minerAccount.balance)
Expand All @@ -203,9 +203,9 @@ module.exports = function (opts, cb) {
function rewardOmmer (ommer) {
// calculate reward
var heightDiff = new BN(block.header.number).sub(new BN(ommer.number))
var reward = ((new BN(8)).sub(heightDiff)).mul(minerReward.div(new BN(8)))
var reward = ((new BN(8)).sub(heightDiff)).mul(minerReward.divn(8))

if (reward.lt(new BN(0))) {
if (reward.ltn(0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest looks good as well.

reward = new BN(0)
}

Expand Down