From c55aeb59e1133aa810cd71d065000037b3202e16 Mon Sep 17 00:00:00 2001 From: Mike Seese Date: Fri, 29 Jun 2018 16:20:34 -0400 Subject: [PATCH] convert bignumbers properly for forked fallback balance lookup also update the test to make sure the balance isn't too large (which it was in this case) for #128 --- lib/utils/forkedblockchain.js | 3 ++- test/forking.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/utils/forkedblockchain.js b/lib/utils/forkedblockchain.js index c48c991b81..abaac880a6 100644 --- a/lib/utils/forkedblockchain.js +++ b/lib/utils/forkedblockchain.js @@ -10,6 +10,7 @@ var Web3 = require("web3"); var to = require("./to.js"); var async = require("async"); var txhelper = require("./txhelper.js") +var BN = require("bn.js"); var inherits = require("util").inherits; @@ -467,7 +468,7 @@ ForkedBlockchain.prototype.fetchBalanceFromFallback = function(address, block_nu self.web3.eth.getBalance(address, safe_block_number, function(err, balance) { if (err) return callback(err); - balance = "0x" + balance.toString(16); // BigNumber + balance = "0x" + new BN(balance).toString(16); callback(null, balance); }); }); diff --git a/test/forking.js b/test/forking.js index 2e8c23a5e2..8b1ffb7dda 100644 --- a/test/forking.js +++ b/test/forking.js @@ -509,11 +509,21 @@ describe("Forking", function() { // are hard to assert in this case. assert(balanceBeforeFork.gt(balanceAfterFork)); + // Make sure it's not substantially larger. it should only be larger by a small + // amount (<2%). This assertion was added since forked balances were previously + // incorrectly being converted between decimal and hex + assert(balanceBeforeFork.muln(0.95).lt(balanceAfterFork)); + // Since the forked provider had once extra transaction for this account, // it should have a lower balance, and the main provider shouldn't acknowledge // that transaction. assert(balanceLatestMain.gt(balanceLatestFallback)); + // Make sure it's not substantially larger. it should only be larger by a small + // amount (<2%). This assertion was added since forked balances were previously + // incorrectly being converted between decimal and hex + assert(balanceLatestMain.muln(0.95).lt(balanceLatestFallback)); + done(); }); });