Skip to content

Commit

Permalink
Add function compare blocks (#3682)
Browse files Browse the repository at this point in the history
* Add function compareBlockNumbers(a, b)

No tests yet; BigNumber values not presently included

* Fix format of fn declaration & add to exports

* Finish fixing format of fn declaration

* refactor exisiting pr

* update changelog

* more tests

* more tests

* move to web3-utils

* fix if clause

Co-authored-by: wbt <wbt@users.noreply.github.com>
Co-authored-by: Frankie <frankie.diamond@gmail.com>
  • Loading branch information
3 people committed Sep 1, 2020
1 parent 9edd908 commit f33e5d5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
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 },
{ 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);
});
});
});

});

0 comments on commit f33e5d5

Please sign in to comment.