itns-sidechain/test/utils-test.js

248 lines
7.3 KiB
JavaScript
Raw Normal View History

2017-07-30 16:49:24 -07:00
/* eslint-env mocha */
2017-07-31 00:34:42 -07:00
/* eslint prefer-arrow-callback: "off" */
2017-07-30 16:49:24 -07:00
2016-06-13 01:06:01 -07:00
'use strict';
2017-10-30 21:22:31 -07:00
const {U64, I64} = require('n64');
2017-11-16 10:41:19 -08:00
const Validator = require('bval');
const {base58} = require('bstring');
2017-11-16 11:43:24 -08:00
const {encoding} = require('bufio');
2017-11-16 10:41:19 -08:00
const assert = require('./util/assert');
2017-06-29 20:54:07 -07:00
const Amount = require('../lib/btc/amount');
2017-10-26 04:07:36 -07:00
const fixed = require('../lib/utils/fixed');
2014-04-28 17:12:26 +04:00
const base58Tests = [
['', ''],
['61', '2g'],
['626262', 'a3gV'],
['636363', 'aPEr'],
[
'73696d706c792061206c6f6e6720737472696e67',
'2cFupjhnEsSn59qHXstmK2ffpLv2'
],
[
'00eb15231dfceb60925886b67d065299925915aeb172c06647',
'1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L'
],
['516b6fcd0f', 'ABnLTmg'],
['bf4f89001e670274dd', '3SEo3LWLoPntC'],
['572e4794', '3EFU7m'],
['ecac89cad93923c02321', 'EJDM8drfXA6uyA'],
['10c8511e', 'Rt5zm'],
['00000000000000000000', '1111111111']
];
2016-06-03 11:38:18 -07:00
2017-08-11 18:25:54 -07:00
const unsigned = [
new U64('ffeeffee', 16),
new U64('001fffeeffeeffee', 16),
new U64('eeffeeff', 16),
new U64('001feeffeeffeeff', 16),
new U64(0),
new U64(1)
2017-08-11 18:25:54 -07:00
];
const signed = [
new I64('ffeeffee', 16),
new I64('001fffeeffeeffee', 16),
new I64('eeffeeff', 16),
new I64('001feeffeeffeeff', 16),
new I64(0),
new I64(1),
new I64('ffeeffee', 16).ineg(),
new I64('001fffeeffeeffee', 16).ineg(),
new I64('eeffeeff', 16).ineg(),
new I64('001feeffeeffeeff', 16).ineg(),
new I64(0).ineg(),
new I64(1).ineg()
2017-08-11 18:25:54 -07:00
];
describe('Utils', function() {
2017-06-29 20:54:07 -07:00
it('should encode/decode base58', () => {
2017-07-27 09:27:00 -07:00
const buf = Buffer.from('000000deadbeef', 'hex');
const str = base58.encode(buf);
2016-12-10 14:25:02 -08:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(str, '1116h8cQN');
2017-08-10 11:17:10 -07:00
assert.bufferEqual(base58.decode(str), buf);
2016-12-10 14:25:02 -08:00
for (const [hex, b58] of base58Tests) {
const data = Buffer.from(hex, 'hex');
2017-08-09 15:28:03 -07:00
assert.strictEqual(base58.encode(data), b58);
2017-08-10 11:17:10 -07:00
assert.bufferEqual(base58.decode(b58), data);
2016-06-03 11:38:18 -07:00
}
2014-04-28 17:12:26 +04:00
});
2014-05-03 16:51:16 +04:00
2017-06-29 20:54:07 -07:00
it('should convert satoshi to btc', () => {
2018-01-05 04:07:16 -08:00
assert.strictEqual(Amount.coin(5460), '0.0000546');
assert.strictEqual(Amount.coin(54678 * 1000000), '546.78');
assert.strictEqual(Amount.coin(5460 * 10000000), '546.0');
});
2017-06-29 20:54:07 -07:00
it('should convert btc to satoshi', () => {
2017-08-13 13:15:56 -07:00
assert.strictEqual(Amount.value('0.0000546'), 5460);
assert.strictEqual(Amount.value('546.78'), 54678 * 1000000);
assert.strictEqual(Amount.value('546'), 5460 * 10000000);
assert.strictEqual(Amount.value('546.0'), 5460 * 10000000);
assert.strictEqual(Amount.value('546.0000'), 5460 * 10000000);
2017-06-29 20:54:07 -07:00
assert.doesNotThrow(() => {
Amount.value('546.00000000000000000');
2016-05-15 17:41:45 -07:00
});
2017-06-29 20:54:07 -07:00
assert.throws(() => {
Amount.value('546.00000000000000001');
2016-05-15 17:41:45 -07:00
});
2017-06-29 20:54:07 -07:00
assert.doesNotThrow(() => {
Amount.value('90071992.54740991');
2016-05-15 17:41:45 -07:00
});
2017-06-29 20:54:07 -07:00
assert.doesNotThrow(() => {
Amount.value('090071992.547409910');
2016-05-15 17:41:45 -07:00
});
2017-06-29 20:54:07 -07:00
assert.throws(() => {
Amount.value('90071992.54740992');
2016-05-15 17:41:45 -07:00
});
2017-06-29 20:54:07 -07:00
assert.throws(() => {
Amount.value('190071992.54740991');
2016-05-15 17:41:45 -07:00
});
2017-08-13 13:15:56 -07:00
assert.strictEqual(0.15645647 * 1e8, 15645646.999999998);
assert.strictEqual(parseFloat('0.15645647') * 1e8, 15645646.999999998);
2017-08-13 13:15:56 -07:00
assert.strictEqual(15645647 / 1e8, 0.15645647);
2017-10-26 04:07:36 -07:00
assert.strictEqual(fixed.decode('0.15645647', 8), 15645647);
assert.strictEqual(fixed.encode(15645647, 8), '0.15645647');
assert.strictEqual(fixed.fromFloat(0.15645647, 8), 15645647);
assert.strictEqual(fixed.toFloat(15645647, 8), 0.15645647);
2014-05-15 02:59:07 +04:00
});
2016-05-15 03:33:58 -07:00
2017-06-29 20:54:07 -07:00
it('should write/read new varints', () => {
2016-05-22 06:49:27 -07:00
/*
* 0: [0x00] 256: [0x81 0x00]
* 1: [0x01] 16383: [0xFE 0x7F]
* 127: [0x7F] 16384: [0xFF 0x00]
* 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
* 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
* 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
*/
let b = Buffer.alloc(1, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 0, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 0);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [0]);
b = Buffer.alloc(1, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 1, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 1);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [1]);
b = Buffer.alloc(1, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 127, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 127);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [0x7f]);
b = Buffer.alloc(2, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 128, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 128);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [0x80, 0x00]);
b = Buffer.alloc(2, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 255, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 255);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [0x80, 0x7f]);
b = Buffer.alloc(2, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 16383, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 16383);
assert.deepEqual(b, [0xfe, 0x7f]);
b = Buffer.alloc(2, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 16384, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 16384);
assert.deepEqual(b, [0xff, 0x00]);
b = Buffer.alloc(3, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 16511, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 16511);
assert.deepEqual(b.slice(0, 2), [0xff, 0x7f]);
// assert.deepEqual(b, [0x80, 0xff, 0x7f]);
2016-05-22 06:49:27 -07:00
b = Buffer.alloc(3, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, 65535, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, 65535);
2016-05-22 06:49:27 -07:00
assert.deepEqual(b, [0x82, 0xfe, 0x7f]);
// assert.deepEqual(b, [0x82, 0xfd, 0x7f]);
b = Buffer.alloc(5, 0xff);
2016-11-19 05:29:16 -08:00
encoding.writeVarint2(b, Math.pow(2, 32), 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(encoding.readVarint2(b, 0).value, Math.pow(2, 32));
assert.deepEqual(b, [0x8e, 0xfe, 0xfe, 0xff, 0x00]);
2016-05-22 06:49:27 -07:00
});
for (const num of unsigned) {
2017-07-27 09:27:00 -07:00
const bits = num.bitLength();
2016-12-10 14:25:02 -08:00
it(`should write+read a ${bits} bit unsigned int`, () => {
const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8);
2017-08-10 11:17:10 -07:00
encoding.writeU64N(buf1, num, 0);
2016-12-02 04:23:41 -08:00
encoding.writeU64(buf2, num.toNumber(), 0);
2017-08-10 11:17:10 -07:00
assert.bufferEqual(buf1, buf2);
2016-12-10 14:25:02 -08:00
const n1 = encoding.readU64N(buf1, 0);
const n2 = encoding.readU64(buf2, 0);
2017-08-10 11:17:10 -07:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(n1.toNumber(), n2);
2016-05-15 03:33:58 -07:00
});
}
2016-05-15 03:33:58 -07:00
for (const num of signed) {
2017-07-27 09:27:00 -07:00
const bits = num.bitLength();
const sign = num.isNeg() ? 'negative' : 'positive';
2016-12-10 14:25:02 -08:00
it(`should write+read a ${bits} bit ${sign} int`, () => {
const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8);
2017-08-10 11:17:10 -07:00
encoding.writeI64N(buf1, num, 0);
encoding.writeI64(buf2, num.toNumber(), 0);
2017-08-10 11:17:10 -07:00
assert.bufferEqual(buf1, buf2);
2016-12-10 14:25:02 -08:00
const n1 = encoding.readI64N(buf1, 0);
const n2 = encoding.readI64(buf2, 0);
2017-08-10 11:17:10 -07:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(n1.toNumber(), n2);
2016-05-15 03:33:58 -07:00
});
2016-12-10 14:25:02 -08:00
it(`should write+read a ${bits} bit ${sign} int as unsigned`, () => {
const buf1 = Buffer.allocUnsafe(8);
const buf2 = Buffer.allocUnsafe(8);
2017-08-10 11:17:10 -07:00
encoding.writeU64N(buf1, num.toU64(), 0);
2016-12-02 04:23:41 -08:00
encoding.writeU64(buf2, num.toNumber(), 0);
2017-08-10 11:17:10 -07:00
assert.bufferEqual(buf1, buf2);
2016-12-10 14:25:02 -08:00
const n1 = encoding.readU64N(buf1, 0);
2017-08-10 11:17:10 -07:00
2016-05-15 03:33:58 -07:00
if (num.isNeg()) {
assert.throws(() => encoding.readU64(buf2, 0));
2016-05-15 03:33:58 -07:00
} else {
const n2 = encoding.readU64(buf2, 0);
2017-08-09 15:28:03 -07:00
assert.strictEqual(n1.toNumber(), n2);
2016-05-15 03:33:58 -07:00
}
});
}
2017-06-29 20:54:07 -07:00
it('should validate integers 0 and 1 as booleans', () => {
2017-08-10 11:17:10 -07:00
const validator = new Validator({
shouldBeTrue: 1,
shouldBeFalse: 0
});
2017-08-09 15:38:33 -07:00
assert.strictEqual(validator.bool('shouldBeTrue'), true);
assert.strictEqual(validator.bool('shouldBeFalse'), false);
});
2014-04-28 17:12:26 +04:00
});