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

Add function compare blocks #3682

Merged
merged 13 commits into from
Sep 1, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Released with 1.0.0-beta.37 code base.
### Added

- Support for typescript files (.ts) to be writtern alongside regular .js files (#3652)
- Add compareBlock function that allows for complex block comparisons (#3682)

### Changed

Expand Down
62 changes: 60 additions & 2 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var ethjsUnit = require('ethjs-unit');
var utils = require('./utils.js');
var soliditySha3 = require('./soliditySha3.js');
var randombytes = require('randombytes');

var BN = require('bn.js');


/**
Expand Down Expand Up @@ -316,6 +316,62 @@ var toChecksumAddress = function (address) {
return checksumAddress;
};

/**
* Returns -1 if a<b, 1 if a>b; 0 if a == b.
* For more details on this type of function, see
* developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
*
* @method compareBlockNumbers
*
* @param {String|Number|BN} a
*
* @param {String|Number|BN} b
*
* @returns {Number} -1, 0, or 1
*/
var compareBlockNumbers = function(a, b) {
if (a == b) {
return 0;
} else if (("genesis" == a || "earliest" == a || 0 == a) && ("genesis" == b || "earliest" == b || 0 == b)) {
return 0;
} else if ("genesis" == a || "earliest" == a) {
// b !== a, thus a < b
return -1;
} else if ("genesis" == b || "earliest" == b) {
// b !== a, thus a > b
return 1;
} else if (a == "latest") {
if (b == "pending") {
return -1;
} else {
// b !== ("pending" OR "latest"), thus a > b
return 1;
}
} else if (b === "latest") {
if (a == "pending") {
return 1;
} else {
// b !== ("pending" OR "latest"), thus a > b
return -1
}
} else if (a == "pending") {
// b (== OR <) "latest", thus a > b
return 1;
} else if (b == "pending") {
return -1;
} else {
let bnA = new BN(a);
let bnB = new BN(b);
if(bnA.lt(bnB)) {
return -1;
} else if(bnA.eq(bnB)) {
return 0;
} else {
return 1;
}
}
};

module.exports = {
_fireError: _fireError,
_jsonInterfaceMethodToString: _jsonInterfaceMethodToString,
Expand Down Expand Up @@ -379,5 +435,7 @@ module.exports = {
isContractAddressInBloom: utils.isContractAddressInBloom,
isTopic: utils.isTopic,
isTopicInBloom: utils.isTopicInBloom,
isInBloom: utils.isInBloom
isInBloom: utils.isInBloom,

compareBlockNumbers: compareBlockNumbers
};
58 changes: 58 additions & 0 deletions test/utils.compareBlockNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const chai = require('chai');
const assert = chai.assert;
const BN = require('bn.js');
const formatters = require('../packages/web3-utils/src/index.js');

const pending = "pending";
const latest = "latest";
const genesis = "genesis";
const earliest = "earliest";

const tests = [
// Base cases for numbers
{ input: {a: 1, b: 1}, result: 0 },
{ input: {a: 1, b: 2}, result: -1 },
{ input: {a: 2, b: 1}, result: 1 },
// Base cases for BN
{ input: {a: new BN(1), b: new BN(1)}, result: 0 },
{ input: {a: new BN(1), b: new BN(2)}, result: -1 },
{ input: {a: new BN(2), b: new BN(1)}, result: 1 },
// Base cases for numbers vs BN
{ input: {a: new BN(1), b: 1}, result: 0 },
{ input: {a: new BN(1), b: 2}, result: -1 },
{ input: {a: new BN(2), b: 1}, result: 1 },
// Base cases for strings (sanity)
{ input: {a: genesis, b: earliest}, result: 0 },
{ input: {a: genesis, b: 0}, result: 0 },
{ input: {a: earliest, b: 0}, result: 0 },
{ input: {a: latest, b: latest}, result: 0 },
{ input: {a: pending, b: pending}, result: 0 },
// Complex Strings
// Genesis
{ input: {a: earliest, b: 2}, result: -1 },
{ input: {a: earliest, b: new BN(2)}, result: -1 },
{ input: {a: earliest, b: latest}, result: -1 },
{ input: {a: earliest, b: pending}, result: -1 },
{ input: {a: genesis, b: 2}, result: -1 },
{ input: {a: genesis, b: new BN(2)}, result: -1 },
{ input: {a: genesis, b: latest}, result: -1 },
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
{ input: {a: genesis, b: pending}, result: -1 },
// latest
{ input: {a: latest, b: 0}, result: 1 },
{ input: {a: latest, b: new BN(1)}, result: 1 },
{ input: {a: latest, b: pending}, result: -1 },
// pending
{ input: {a: pending, b: 0}, result: 1 },
{ input: {a: pending, b: new BN(1)}, result: 1 },
];

describe('formatters', function () {
describe('compare blocknumbers', function () {
tests.forEach(function(test){
it('should return the correct value', function () {
assert.deepEqual(formatters.compareBlockNumbers(test.input.a, test.input.b), test.result);
});
});
});

});