This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
828 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { BN } = require("ethereumjs-util"); | ||
const hexToBn = (val = 0) => new BN(parseInt("0x" + val.toString("hex"), 16)); | ||
const MULTIPLE = 64 / 63; | ||
|
||
module.exports = async function binSearch(generateVM, runArgs, result, callback) { | ||
const MAX = hexToBn(runArgs.block.header.gasLimit); | ||
const gasRefund = result.execResult.gasRefund; | ||
const startingGas = gasRefund ? result.gasEstimate.add(gasRefund) : result.gasEstimate; | ||
const range = { lo: startingGas, hi: startingGas }; | ||
const isEnoughGas = async(gas) => { | ||
const vm = generateVM(); // Generate fresh VM | ||
runArgs.tx.gasLimit = gas.toBuffer(); | ||
const result = await vm.runTx(runArgs).catch((vmerr) => ({ vmerr })); | ||
return !result.vmerr && !result.execResult.exceptionError; | ||
}; | ||
|
||
if (!(await isEnoughGas(range.hi))) { | ||
do { | ||
range.hi = range.hi.muln(MULTIPLE); | ||
} while (!(await isEnoughGas(range.hi))); | ||
while (range.lo.addn(1).lt(range.hi)) { | ||
const mid = range.lo.add(range.hi).divn(2); | ||
if (await isEnoughGas(mid)) { | ||
range.hi = mid; | ||
} else { | ||
range.lo = mid; | ||
} | ||
} | ||
if (range.hi.gte(MAX)) { | ||
if (!(await isEnoughGas(range.hi))) { | ||
return callback(new Error("gas required exceeds allowance or always failing transaction")); | ||
} | ||
} | ||
} | ||
|
||
result.gasEstimate = range.hi; | ||
callback(null, result); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const estimateGas = require("./guestimation"); | ||
const binSearch = require("./binSearch"); | ||
|
||
module.exports = async(generateVM, runArgs, callback) => { | ||
const vm = generateVM(); | ||
estimateGas(vm, runArgs, async(err, result) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
await binSearch(generateVM, runArgs, result, callback); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.