UInt extends BN (#396)
* Fix Uint and Moment and Compact * Fix address and account index * Remove .toBN * Add comment * Fix lint * _raw to raw * bitLength as method in U8aFixed
This commit is contained in:
@@ -25,9 +25,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
.nextAccountEnumSet()
|
||||
.pipe(
|
||||
switchMap((nextEnum: AccountIndex | undefined) => {
|
||||
const lastIndex = nextEnum
|
||||
? nextEnum.toBn()
|
||||
: new BN(0);
|
||||
const lastIndex = nextEnum || new BN(0);
|
||||
const setSize = ENUMSET_SIZE.toNumber();
|
||||
const enumRange = [...Array(lastIndex.div(ENUMSET_SIZE).toNumber() + 1).keys()];
|
||||
|
||||
@@ -61,7 +59,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
.getAccountEnumSet(accountIndex)
|
||||
.pipe(
|
||||
map((accounts: Array<AccountId> = []): AccountId | undefined =>
|
||||
accounts[accountIndex.toBn().mod(ENUMSET_SIZE).toNumber()]
|
||||
accounts[accountIndex.mod(ENUMSET_SIZE).toNumber()]
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -103,9 +101,9 @@ export default class ApiCombined extends ApiCalls {
|
||||
.accountIdFromIndex(accountIndex)
|
||||
.pipe(
|
||||
map((accountId?: AccountId): [AccountId | undefined, AccountIndex] =>
|
||||
[accountId, accountIndex]
|
||||
)
|
||||
);
|
||||
[accountId, accountIndex]
|
||||
)
|
||||
);
|
||||
} catch (error) {
|
||||
// swallow
|
||||
}
|
||||
@@ -161,10 +159,10 @@ export default class ApiCombined extends ApiCalls {
|
||||
).pipe(
|
||||
// @ts-ignore After upgrade to 6.3.2
|
||||
switchMap(([referendumCount, nextTally]: [ReferendumIndex | undefined, ReferendumIndex | undefined]): Observable<Array<RxReferendum>> =>
|
||||
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gt(0)
|
||||
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
|
||||
? this.referendumsInfo(
|
||||
[...Array(referendumCount.toBn().sub(nextTally.toBn()).toNumber())].map((_, i) =>
|
||||
nextTally.add(i).toNumber()
|
||||
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i) =>
|
||||
nextTally.addn(i).toNumber()
|
||||
)
|
||||
)
|
||||
: EMPTY
|
||||
@@ -238,9 +236,9 @@ export default class ApiCombined extends ApiCalls {
|
||||
sessionCurrentIndex
|
||||
// last era can be null (i.e. new chain, valid key, no value set yet)
|
||||
.sub(eraLastLengthChange || new BlockNumber(0))
|
||||
.mod(sessionsPerEra.toBn())
|
||||
.mul(sessionLength.toBn())
|
||||
.add(sessionBlockProgress.toBn())
|
||||
.mod(sessionsPerEra)
|
||||
.mul(sessionLength)
|
||||
.add(sessionBlockProgress)
|
||||
)
|
||||
: undefined
|
||||
);
|
||||
@@ -274,8 +272,8 @@ export default class ApiCombined extends ApiCalls {
|
||||
bestNumber
|
||||
// last change can be null (i.e. new chain, valid key, no value set yet)
|
||||
.sub(lastSessionLengthChange || new BlockNumber(0))
|
||||
.add(sessionLength.toBn())
|
||||
.mod(sessionLength.toBn())
|
||||
.add(sessionLength)
|
||||
.mod(sessionLength)
|
||||
)
|
||||
: undefined
|
||||
);
|
||||
@@ -324,7 +322,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
([sessionLength, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
|
||||
sessionLength && blockPeriod
|
||||
? new Moment(
|
||||
sessionLength.mul(blockPeriod.toBn()).muln(1000)
|
||||
sessionLength.muln(blockPeriod.toNumber()).muln(1000)
|
||||
)
|
||||
: undefined
|
||||
);
|
||||
@@ -339,7 +337,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
([sessionBlockRemaining, blockPeriod]: [BlockNumber | undefined, Moment | undefined]): Moment | undefined =>
|
||||
blockPeriod && sessionBlockRemaining
|
||||
? new Moment(
|
||||
sessionBlockRemaining.mul(blockPeriod.toBn()).muln(1000)
|
||||
sessionBlockRemaining.muln(blockPeriod.toNumber()).muln(1000)
|
||||
)
|
||||
: undefined
|
||||
);
|
||||
@@ -353,7 +351,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
],
|
||||
([balance, nominators = []]: [RxBalance, Array<RxBalance>]): RxBalance => {
|
||||
const nominatedBalance = nominators.reduce((total, nominator: RxBalance) => {
|
||||
return total.add(nominator.votingBalance.toBn());
|
||||
return total.add(nominator.votingBalance);
|
||||
}, new BN(0));
|
||||
|
||||
const result = {
|
||||
@@ -361,7 +359,7 @@ export default class ApiCombined extends ApiCalls {
|
||||
nominators,
|
||||
nominatedBalance: new Balance(nominatedBalance),
|
||||
stakingBalance: new Balance(
|
||||
nominatedBalance.add(balance.votingBalance.toBn())
|
||||
nominatedBalance.add(balance.votingBalance)
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ describe.skip('e2e state', () => {
|
||||
.then((balance) => {
|
||||
console.error(balance);
|
||||
|
||||
expect(balance.toBn().isZero()).not.toEqual(true);
|
||||
expect(balance.isZero()).not.toEqual(true);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
|
||||
@@ -135,8 +135,8 @@ export default class Mock implements ProviderInterface {
|
||||
|
||||
// increment the balances and nonce for each account
|
||||
keyring.getPairs().forEach(({ publicKey }, index) => {
|
||||
this.setStateBn(storage.balances.freeBalance(publicKey()), newHead.blockNumber.mul(3).iaddn(index));
|
||||
this.setStateBn(storage.system.accountNonce(publicKey()), newHead.blockNumber.add(index));
|
||||
this.setStateBn(storage.balances.freeBalance(publicKey()), newHead.blockNumber.muln(3).iaddn(index));
|
||||
this.setStateBn(storage.system.accountNonce(publicKey()), newHead.blockNumber.addn(index));
|
||||
});
|
||||
|
||||
// set the timestamp for the current block
|
||||
|
||||
@@ -8,14 +8,14 @@ import AccountIndex from './AccountIndex';
|
||||
describe('AccountIndex', () => {
|
||||
it('creates a BN representation', () => {
|
||||
expect(
|
||||
new AccountIndex(new Uint8Array([17, 18, 19, 20])).toBn().toString()
|
||||
).toEqual('336794129');
|
||||
new AccountIndex(new Uint8Array([17, 18, 19, 20])).toNumber()
|
||||
).toEqual(336794129);
|
||||
});
|
||||
|
||||
it('creates a BN representation (from ss-58)', () => {
|
||||
expect(
|
||||
new AccountIndex('Mwz15xN8').toBn().toString()
|
||||
).toEqual('336794129');
|
||||
new AccountIndex('Mwz15xN8').toNumber()
|
||||
).toEqual(336794129);
|
||||
});
|
||||
|
||||
it('constructs 2-byte from number', () => {
|
||||
|
||||
@@ -31,9 +31,11 @@ export default class AccountIndex extends U32 {
|
||||
}
|
||||
|
||||
static decodeAccountIndex (value: AnyNumber): BN | Uint8Array | number | string {
|
||||
if (value instanceof UInt) {
|
||||
return value.raw;
|
||||
} else if (isBn(value) || isNumber(value) || isU8a(value)) {
|
||||
if (value instanceof AccountIndex) {
|
||||
// `value.toBn()` on AccountIndex returns a pure BN (i.e. not an
|
||||
// AccountIndex), which has the initial `toString()` implementation.
|
||||
return value.toBn();
|
||||
} else if (value instanceof UInt || isBn(value) || isNumber(value) || isU8a(value)) {
|
||||
return value;
|
||||
} else if (isHex(value)) {
|
||||
// Here we convert via hexToU8a since we expect the LE encoded value representation. This
|
||||
@@ -81,6 +83,10 @@ export default class AccountIndex extends U32 {
|
||||
}
|
||||
}
|
||||
|
||||
toBn (): BN {
|
||||
return new BN(this);
|
||||
}
|
||||
|
||||
// Like in our decoding function, we explicitly override this to allow us to output
|
||||
// LE-hex encoded numbers (generally UInt in JSON are expected as BE, these LE)
|
||||
toHex (): string {
|
||||
@@ -92,7 +98,7 @@ export default class AccountIndex extends U32 {
|
||||
}
|
||||
|
||||
toString (): string {
|
||||
const length = AccountIndex.calcLength(this.raw);
|
||||
const length = AccountIndex.calcLength(this);
|
||||
|
||||
return encodeAddress(this.toU8a().subarray(0, length));
|
||||
}
|
||||
@@ -103,7 +109,7 @@ export default class AccountIndex extends U32 {
|
||||
// construct a query). This is needed to get enumSet(AccountIndex) queries to
|
||||
// work in the way it was intended
|
||||
return isBare
|
||||
? bnToU8a(this.toBn().div(ENUMSET_SIZE), 32, true)
|
||||
? bnToU8a(this.div(ENUMSET_SIZE), 32, true)
|
||||
: super.toU8a();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,12 +27,12 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
}
|
||||
|
||||
static decodeAddress (value: AnyAddress): AccountId | AccountIndex {
|
||||
if (isBn(value) || isNumber(value)) {
|
||||
if (value instanceof AccountId || value instanceof AccountIndex) {
|
||||
return value;
|
||||
} else if (isBn(value) || isNumber(value)) {
|
||||
return new AccountIndex(value);
|
||||
} else if (value instanceof Address) {
|
||||
return value.raw;
|
||||
} else if (value instanceof AccountId || value instanceof AccountIndex) {
|
||||
return value;
|
||||
} else if (Array.isArray(value)) {
|
||||
return Address.decodeAddress(u8aToU8a(value));
|
||||
} else if (isU8a(value)) {
|
||||
@@ -60,7 +60,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
|
||||
get rawLength (): number {
|
||||
return this.raw instanceof AccountIndex
|
||||
? AccountIndex.calcLength(this.raw.toBn())
|
||||
? AccountIndex.calcLength(this.raw)
|
||||
: this.raw.encodedLength;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ describe('Header', () => {
|
||||
header.hash.toHex()
|
||||
).toEqual('0x63ccfdc044d3ff4c915ad01c0d57d2ff807f4eb7d60cd41584917363bc83a99f');
|
||||
expect(
|
||||
header.blockNumber.toBn()
|
||||
).toEqual(new BN(2918));
|
||||
header.blockNumber.eq(new BN(2918))
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import BN from 'bn.js';
|
||||
import { bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import Base from './codec/Base';
|
||||
import UInt, { UIntBitLength } from './codec/UInt';
|
||||
import { UIntBitLength } from './codec/UInt';
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
const BITLENGTH: UIntBitLength = 64;
|
||||
@@ -27,8 +27,6 @@ export default class Moment extends Base<Date> {
|
||||
return value.raw;
|
||||
} else if (value instanceof Date) {
|
||||
return new Date(Math.ceil(value.getTime() / 1000) * 1000);
|
||||
} else if (value instanceof UInt) {
|
||||
value = value.toBn();
|
||||
} else if (isU8a(value)) {
|
||||
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
|
||||
} else if (isString(value)) {
|
||||
@@ -40,7 +38,7 @@ export default class Moment extends Base<Date> {
|
||||
);
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
bitLength (): UIntBitLength {
|
||||
return BITLENGTH;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ describe('Compact', () => {
|
||||
describe('constructor', () => {
|
||||
it('has the correct bitLength for constructor values (BlockNumber)', () => {
|
||||
expect(
|
||||
new Compact(BlockNumber, 0xfffffff9).bitLength
|
||||
new Compact(BlockNumber, 0xfffffff9).bitLength()
|
||||
).toEqual(64);
|
||||
});
|
||||
|
||||
@@ -114,8 +114,8 @@ describe('Compact', () => {
|
||||
|
||||
it('constructs properly via U8a as U32', () => {
|
||||
expect(
|
||||
new Compact(U32, new Uint8Array([254, 255, 3, 0])).toBn()
|
||||
).toEqual(new BN(0xffff));
|
||||
new Compact(U32, new Uint8Array([254, 255, 3, 0])).toNumber()
|
||||
).toEqual(new BN(0xffff).toNumber());
|
||||
});
|
||||
|
||||
it('constructs properly via number as Moment', () => {
|
||||
|
||||
@@ -72,7 +72,7 @@ export default class Compact extends Base<UInt | Moment> {
|
||||
} else if (isNumber(value) || isBn(value)) {
|
||||
return new Type(bnToBn(value));
|
||||
} else if (isU8a(value)) {
|
||||
const [, _value] = Compact.decodeU8a(value, new Type(0).bitLength);
|
||||
const [, _value] = Compact.decodeU8a(value, new Type(0).bitLength());
|
||||
|
||||
return new Type(_value);
|
||||
}
|
||||
@@ -118,8 +118,8 @@ export default class Compact extends Base<UInt | Moment> {
|
||||
);
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
return this.raw.bitLength;
|
||||
bitLength (): UIntBitLength {
|
||||
return this.raw.bitLength();
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
@@ -147,6 +147,6 @@ export default class Compact extends Base<UInt | Moment> {
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return Compact.encodeU8a(this.raw.toBn(), this.bitLength);
|
||||
return Compact.encodeU8a(this.raw.toBn(), this.bitLength());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ describe('U8aFixed', () => {
|
||||
});
|
||||
|
||||
it('exposes the correct bitLength', () => {
|
||||
expect(u8a.bitLength).toEqual(32);
|
||||
expect(u8a.bitLength()).toEqual(32);
|
||||
});
|
||||
|
||||
it('allows wrapping of a pre-existing instance', () => {
|
||||
|
||||
@@ -29,7 +29,7 @@ export default class U8aFixed extends U8a {
|
||||
return value;
|
||||
}
|
||||
|
||||
get bitLength () {
|
||||
bitLength () {
|
||||
return this.length * 8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from '../types';
|
||||
import { AnyNumber, Codec } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { bnToBn, bnToHex, bnToU8a, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import Base from './Base';
|
||||
|
||||
export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
|
||||
|
||||
export const DEFAULT_UINT_BITS = 64;
|
||||
@@ -19,9 +17,10 @@ export const DEFAULT_UINT_BITS = 64;
|
||||
//
|
||||
// TODO:
|
||||
// - Apart from encoding/decoding we don't actuall keep check on the sizes, is this good enough?
|
||||
export default class UInt extends Base<BN> {
|
||||
export default class UInt extends BN implements Codec {
|
||||
protected _bitLength: UIntBitLength;
|
||||
private _isHexJson: boolean;
|
||||
public raw: BN; // FIXME Remove this once we convert all types out of Base
|
||||
|
||||
constructor (value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isHexJson: boolean = true) {
|
||||
super(
|
||||
@@ -30,24 +29,26 @@ export default class UInt extends Base<BN> {
|
||||
|
||||
this._bitLength = bitLength;
|
||||
this._isHexJson = isHexJson;
|
||||
this.raw = this;
|
||||
}
|
||||
|
||||
static decodeUInt (value: AnyNumber, bitLength: UIntBitLength): BN {
|
||||
if (value instanceof UInt) {
|
||||
return value.raw;
|
||||
} else if (isHex(value)) {
|
||||
return hexToBn(value);
|
||||
static decodeUInt (value: AnyNumber, bitLength: UIntBitLength): string {
|
||||
// This function returns a string, which will be passed in the BN
|
||||
// constructor. It would be ideal to actually return a BN, but there's a
|
||||
// bug: https://github.com/indutny/bn.js/issues/206.
|
||||
if (isHex(value)) {
|
||||
return hexToBn(value).toString();
|
||||
} else if (isU8a(value)) {
|
||||
// NOTE When passing u8a in (typically from decoded data), it is always LE
|
||||
return u8aToBn(value.subarray(0, bitLength / 8), true);
|
||||
return u8aToBn(value.subarray(0, bitLength / 8), true).toString();
|
||||
} else if (isString(value)) {
|
||||
return new BN(value, 10);
|
||||
return new BN(value, 10).toString();
|
||||
}
|
||||
|
||||
return bnToBn(value);
|
||||
return bnToBn(value).toString();
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
bitLength (): UIntBitLength {
|
||||
return this._bitLength;
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ export default class UInt extends Base<BN> {
|
||||
}
|
||||
|
||||
toHex (): string {
|
||||
return bnToHex(this.raw, this._bitLength);
|
||||
return bnToHex(this, this._bitLength);
|
||||
}
|
||||
|
||||
toJSON (): any {
|
||||
@@ -66,84 +67,10 @@ export default class UInt extends Base<BN> {
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return bnToU8a(this.raw, this._bitLength, true);
|
||||
}
|
||||
|
||||
toString (): string {
|
||||
return this.raw.toString();
|
||||
return bnToU8a(this, this._bitLength, true);
|
||||
}
|
||||
|
||||
toBn (): BN {
|
||||
return this.raw;
|
||||
}
|
||||
|
||||
toNumber (): number {
|
||||
return this.raw.toNumber();
|
||||
}
|
||||
|
||||
// helpers from BN, this would be great as a "don't do this", i.e. extending properly
|
||||
// from BN. Underlying these will always return BN (unless it is compare checks)
|
||||
add (other: UInt | BN | number): BN {
|
||||
return other instanceof UInt
|
||||
? this.raw.add(other.raw)
|
||||
: this.raw.add(bnToBn(other));
|
||||
}
|
||||
|
||||
cmp (other: UInt | BN | number): number {
|
||||
return other instanceof UInt
|
||||
? this.raw.cmp(other.raw)
|
||||
: this.raw.cmp(bnToBn(other));
|
||||
}
|
||||
|
||||
div (other: UInt | BN | number): BN {
|
||||
return other instanceof UInt
|
||||
? this.raw.div(other.raw)
|
||||
: this.raw.div(bnToBn(other));
|
||||
}
|
||||
|
||||
eq (other: UInt | BN | number): boolean {
|
||||
return other instanceof UInt
|
||||
? this.raw.eq(other.raw)
|
||||
: this.raw.eq(bnToBn(other));
|
||||
}
|
||||
|
||||
isZero (): boolean {
|
||||
return this.raw.isZero();
|
||||
}
|
||||
|
||||
lt (test: UInt | BN | number): boolean {
|
||||
return test instanceof UInt
|
||||
? this.raw.lt(test.raw)
|
||||
: this.raw.lt(bnToBn(test));
|
||||
}
|
||||
|
||||
lte (test: UInt | BN | number): boolean {
|
||||
return test instanceof UInt
|
||||
? this.raw.lte(test.raw)
|
||||
: this.raw.lte(bnToBn(test));
|
||||
}
|
||||
|
||||
gt (test: UInt | BN | number): boolean {
|
||||
return test instanceof UInt
|
||||
? this.raw.gt(test.raw)
|
||||
: this.raw.gt(bnToBn(test));
|
||||
}
|
||||
|
||||
gte (test: UInt | BN | number): boolean {
|
||||
return test instanceof UInt
|
||||
? this.raw.gte(test.raw)
|
||||
: this.raw.gte(bnToBn(test));
|
||||
}
|
||||
|
||||
mul (other: UInt | BN | number): BN {
|
||||
return other instanceof UInt
|
||||
? this.raw.mul(other.raw)
|
||||
: this.raw.mul(bnToBn(other));
|
||||
}
|
||||
|
||||
sub (other: UInt | BN | number): BN {
|
||||
return other instanceof UInt
|
||||
? this.raw.sub(other.raw)
|
||||
: this.raw.sub(bnToBn(other));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user