Skip to content

Commit

Permalink
fix: treat and return BigInt values as strings (#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Jun 27, 2019
1 parent 4dfe51a commit 16ace9d
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 115 deletions.
2 changes: 1 addition & 1 deletion __tests__/integration/core-api/handlers/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("API 2.0 - Blockchain", () => {

expect(response.data.data.block.height).toBeNumber();
expect(response.data.data.block.id).toBeString();
expect(response.data.data.supply).toBeNumber();
expect(response.data.data.supply).toBeString();
});
});
});
36 changes: 18 additions & 18 deletions __tests__/integration/core-api/handlers/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("API 2.0 - Delegates", () => {
expect(response.data.data).toBeArray();

expect(response.data.data[0].username).toBe(wallet.username);
expect(response.data.data[0].votes).toBe(+wallet.voteBalance.toFixed());
expect(response.data.data[0].votes).toBe(wallet.voteBalance.toFixed());
});

it("should GET all the delegates sorted by votes,desc", async () => {
Expand All @@ -82,7 +82,7 @@ describe("API 2.0 - Delegates", () => {
expect(response.data.data).toBeArray();

expect(response.data.data[0].username).toBe(wallet.username);
expect(response.data.data[0].votes).toBe(+wallet.voteBalance.toFixed());
expect(response.data.data[0].votes).toBe(wallet.voteBalance.toFixed());
});

it("should GET all the delegates ordered by descending rank", async () => {
Expand Down Expand Up @@ -240,8 +240,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.production.approval).toBeGreaterThanOrEqual(1);
expect(elem.production.approval).toBeLessThanOrEqual(100);
expect(+elem.production.approval).toBeGreaterThanOrEqual(1);
expect(+elem.production.approval).toBeLessThanOrEqual(100);
}
});

Expand All @@ -259,7 +259,7 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.fees).toEqual(delegate.forgedFees);
expect(+elem.forged.fees).toEqual(delegate.forgedFees);
}
});

Expand All @@ -277,8 +277,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.fees).toBeGreaterThanOrEqual(0);
expect(elem.forged.fees).toBeLessThanOrEqual(delegate.forgedFees);
expect(+elem.forged.fees).toBeGreaterThanOrEqual(0);
expect(+elem.forged.fees).toBeLessThanOrEqual(delegate.forgedFees);
}
});

Expand All @@ -296,7 +296,7 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.rewards).toEqual(delegate.forgedRewards);
expect(+elem.forged.rewards).toEqual(delegate.forgedRewards);
}
});

Expand All @@ -314,8 +314,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.rewards).toBeGreaterThanOrEqual(0);
expect(elem.forged.rewards).toBeLessThanOrEqual(delegate.forgedRewards);
expect(+elem.forged.rewards).toBeGreaterThanOrEqual(0);
expect(+elem.forged.rewards).toBeLessThanOrEqual(delegate.forgedRewards);
}
});

Expand All @@ -333,7 +333,7 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.total).toEqual(delegate.forgedTotal);
expect(+elem.forged.total).toEqual(delegate.forgedTotal);
}
});

Expand All @@ -351,8 +351,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.forged.total).toBeGreaterThanOrEqual(0);
expect(elem.forged.total).toBeLessThanOrEqual(delegate.forgedTotal);
expect(+elem.forged.total).toBeGreaterThanOrEqual(0);
expect(+elem.forged.total).toBeLessThanOrEqual(delegate.forgedTotal);
}
});

Expand Down Expand Up @@ -388,8 +388,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.blocks.produced).toBeGreaterThanOrEqual(0);
expect(elem.blocks.produced).toBeLessThanOrEqual(delegate.producedBlocks);
expect(+elem.blocks.produced).toBeGreaterThanOrEqual(0);
expect(+elem.blocks.produced).toBeLessThanOrEqual(delegate.producedBlocks);
}
});

Expand All @@ -407,7 +407,7 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.votes).toEqual(delegate.voteBalance);
expect(+elem.votes).toEqual(delegate.voteBalance);
}
});

Expand All @@ -425,8 +425,8 @@ describe("API 2.0 - Delegates", () => {

for (const elem of response.data.data) {
utils.expectDelegate(elem);
expect(elem.votes).toBeGreaterThanOrEqual(0);
expect(elem.votes).toBeLessThanOrEqual(delegate.voteBalance);
expect(+elem.votes).toBeGreaterThanOrEqual(0);
expect(+elem.votes).toBeLessThanOrEqual(delegate.voteBalance);
}
});
});
Expand Down
12 changes: 6 additions & 6 deletions __tests__/integration/core-api/handlers/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe("API 2.0 - Transactions", () => {

for (const transaction of response.data.data) {
utils.expectTransaction(transaction);
expect(transaction.amount).toBe(amount);
expect(+transaction.amount).toBe(amount);
}
});

Expand All @@ -351,8 +351,8 @@ describe("API 2.0 - Transactions", () => {

for (const transaction of response.data.data) {
utils.expectTransaction(transaction);
expect(transaction.amount).toBeGreaterThanOrEqual(amountFrom);
expect(transaction.amount).toBeLessThanOrEqual(amountTo);
expect(+transaction.amount).toBeGreaterThanOrEqual(amountFrom);
expect(+transaction.amount).toBeLessThanOrEqual(amountTo);
}
});

Expand All @@ -370,7 +370,7 @@ describe("API 2.0 - Transactions", () => {

for (const transaction of response.data.data) {
utils.expectTransaction(transaction);
expect(transaction.fee).toBe(fee);
expect(+transaction.fee).toBe(fee);
}
});

Expand All @@ -388,8 +388,8 @@ describe("API 2.0 - Transactions", () => {

for (const transaction of response.data.data) {
utils.expectTransaction(transaction);
expect(transaction.fee).toBeGreaterThanOrEqual(feeFrom);
expect(transaction.fee).toBeLessThanOrEqual(feeTo);
expect(+transaction.fee).toBeGreaterThanOrEqual(feeFrom);
expect(+transaction.fee).toBeLessThanOrEqual(feeTo);
}
});

Expand Down
8 changes: 4 additions & 4 deletions __tests__/integration/core-api/handlers/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("API 2.0 - Wallets", () => {
expect(response.data.data).toBeArray();

expect(response.data.data[0].address).toBe("APnhwwyTbMiykJwYbGhYjNgtHiVJDSEhSn");
expect(response.data.data[0].balance).toBe(-12500000000000000);
expect(response.data.data[0].balance).toBe("-12500000000000000");
});

it("should GET all the wallets sorted by balance,desc", async () => {
Expand All @@ -37,7 +37,7 @@ describe("API 2.0 - Wallets", () => {
expect(response.data.data).toBeArray();

expect(response.data.data[0].address).toBe("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo");
expect(response.data.data[0].balance).toBe(245100000000000);
expect(response.data.data[0].balance).toBe("245100000000000");
});
});

Expand Down Expand Up @@ -236,7 +236,7 @@ describe("API 2.0 - Wallets", () => {
const wallet = response.data.data[0];
utils.expectWallet(wallet);
expect(wallet.address).toBe(address);
expect(wallet.balance).toBe(balance);
expect(+wallet.balance).toBe(balance);
});

it("should POST a search for wallets with the specified balance range", async () => {
Expand All @@ -255,7 +255,7 @@ describe("API 2.0 - Wallets", () => {
const wallet = response.data.data[0];
utils.expectWallet(wallet);
expect(wallet.address).toBe(address);
expect(wallet.balance).toBe(balance);
expect(+wallet.balance).toBe(balance);
});

it("should POST a search for wallets with the exact specified voteBalance", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": 0,
"height": 1,
"previous": null,
"forged": { "reward": 0, "fee": 0, "total": 0, "amount": 12500000000000000 },
"forged": { "reward": "0", "fee": "0", "total": "0", "amount": "12500000000000000" },
"payload": { "hash": "d9acd04bde4234a81addb8482333b4ac906bed7be5a9970ce8ada428bd083192", "length": 35960 },
"generator": {
"address": "AP6kAVdX1zQ3S8mfDnnHx9GaAohEqQUins",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"id": "db1aa687737858cc9199bfa336f9b1c035915c30aaee60b1e0f8afadfdb946bd",
"version": 1,
"type": 0,
"amount": 245098000000000,
"fee": 0,
"amount": "245098000000000",
"fee": "0",
"sender": "APnhwwyTbMiykJwYbGhYjNgtHiVJDSEhSn",
"senderPublicKey": "035b63b4668ee261c16ca91443f3371e2fe349e131cb7bf5f8a3e93a3ddfdfc788",
"recipient": "AHXtmB84sTZ9Zd35h9Y1vfFvPE2Xzqj8ri",
Expand Down
22 changes: 20 additions & 2 deletions __tests__/integration/core-api/services/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ Managers.configManager.setFromPreset("testnet");
const genesisTransaction = Transactions.TransactionFactory.fromData(genesisBlock.transactions[0]);
delete genesisBlock.transactions;

const filterUndefined = values => {
if (Array.isArray(values)) {
for (const value of values) {
filterUndefined(value);
}
}

for (const key of Object.keys(values)) {
if (values[key] === undefined) {
delete values[key];
}
}

return values;
};

beforeAll(async () => setUp());
afterAll(async () => tearDown());

Expand All @@ -26,7 +42,9 @@ describe("Transformer", () => {
});

it("should transform a transaction", () => {
expect(transformerService.toResource(genesisTransaction, "transaction")).toEqual(transactionTransformed);
expect(filterUndefined(transformerService.toResource(genesisTransaction, "transaction"))).toEqual(
transactionTransformed,
);
});

it("should not transform a transaction", () => {
Expand All @@ -44,7 +62,7 @@ describe("Transformer", () => {
});

it("should transform a transaction", () => {
expect(transformerService.toCollection([genesisTransaction], "transaction")).toEqual([
expect(filterUndefined(transformerService.toCollection([genesisTransaction], "transaction"))).toEqual([
transactionTransformed,
]);
});
Expand Down
24 changes: 12 additions & 12 deletions __tests__/integration/core-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ class Helpers {
expect(block.height).toBeNumber();
expect(block).toHaveProperty("previous"); // `undefined` or String
expect(block).toHaveProperty("forged");
expect(block.forged.reward).toBeNumber();
expect(block.forged.fee).toBeNumber();
expect(block.forged.total).toBeNumber();
expect(block.forged.amount).toBeNumber();
expect(block.forged.reward).toBeString();
expect(block.forged.fee).toBeString();
expect(block.forged.total).toBeString();
expect(block.forged.amount).toBeString();
expect(block).toHaveProperty("payload");
expect(block.payload.length).toBeNumber();
expect(block.payload.hash).toBeString();
Expand All @@ -95,29 +95,29 @@ class Helpers {
expect(block.signature).toBeString();
expect(block.transactions).toBeNumber();

Object.keys(expected || {}).forEach(attr => {
for (const attr of Object.keys(expected || {})) {
expect(block[attr]).toEqual(expected[attr]);
});
}
}

public expectDelegate(delegate, expected: any = {}) {
expect(delegate).toBeObject();
expect(delegate.username).toBeString();
expect(delegate.address).toBeString();
expect(delegate.publicKey).toBeString();
expect(delegate.votes).toBeNumber();
expect(delegate.votes).toBeString();
expect(delegate.rank).toBeNumber();
expect(delegate.blocks).toBeObject();
expect(delegate.blocks.produced).toBeNumber();
expect(delegate.production).toBeObject();
expect(delegate.production.approval).toBeNumber();
expect(delegate.forged.fees).toBeNumber();
expect(delegate.forged.rewards).toBeNumber();
expect(delegate.forged.total).toBeNumber();
expect(delegate.forged.fees).toBeString();
expect(delegate.forged.rewards).toBeString();
expect(delegate.forged.total).toBeString();

Object.keys(expected || {}).forEach(attr => {
for (const attr of Object.keys(expected || {})) {
expect(delegate[attr]).toBe(expected[attr]);
});
}
}

public expectWallet(wallet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("Delegate Repository", () => {

for (const delegate of rows) {
expect(delegate.hasOwnProperty("forgedTotal"));
expect(+delegate.forgedTotal.toFixed()).toBe(delegateCalculator.calculateForgedTotal(delegate));
expect(delegate.forgedTotal).toBe(delegateCalculator.calculateForgedTotal(delegate));
}
});

Expand Down Expand Up @@ -230,9 +230,9 @@ describe("Delegate Repository", () => {
expect(count).toBe(3);
expect(rows).toHaveLength(3);

rows.forEach(row => {
for (const row of rows) {
expect(usernames.includes(row.username)).toBeTrue();
});
}
});

describe('when a username is "undefined"', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-utils/delegate-calculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Delegate Calculator", () => {
delegate.forgedFees = Utils.BigNumber.make(10);
delegate.forgedRewards = Utils.BigNumber.make(100);

expect(calculateForgedTotal(delegate)).toBe(110);
expect(calculateForgedTotal(delegate)).toBe("110");
});
});
});
11 changes: 7 additions & 4 deletions __tests__/unit/core-utils/supply-calculator.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { Utils } from "@arkecosystem/crypto";
import "jest-extended";
import { calculate } from "../../../packages/core-utils/src/supply-calculator";

let config;

const toString = value => Utils.BigNumber.make(value).toFixed();

const mockConfig = {
genesisBlock: { totalAmount: 1000 },
milestones: [{ height: 1, reward: 2 }],
Expand Down Expand Up @@ -41,14 +44,14 @@ beforeAll(() => {
describe("Supply calculator", () => {
it("should calculate supply with milestone at height 2", () => {
mockConfig.milestones[0].height = 2;
expect(calculate(1)).toBe(mockConfig.genesisBlock.totalAmount);
expect(calculate(1)).toBe(toString(mockConfig.genesisBlock.totalAmount));
mockConfig.milestones[0].height = 1;
});

describe.each([0, 5, 100, 2000, 4000, 8000])("at height %s", height => {
it("should calculate the genesis supply without milestone", () => {
const genesisSupply = config.genesisBlock.totalAmount;
expect(calculate(height)).toBe(genesisSupply + height * config.milestones[0].reward);
expect(calculate(height)).toBe(toString(genesisSupply + height * config.milestones[0].reward));
});
});

Expand All @@ -65,7 +68,7 @@ describe("Supply calculator", () => {
};

const genesisSupply = config.genesisBlock.totalAmount;
expect(calculate(height)).toBe(genesisSupply + reward(height));
expect(calculate(height)).toBe(toString(genesisSupply + reward(height)));

mockConfig.milestones = [{ height: 1, reward: 2 }];
});
Expand Down Expand Up @@ -99,7 +102,7 @@ describe("Supply calculator", () => {
};

const genesisSupply = config.genesisBlock.totalAmount;
expect(calculate(height)).toBe(genesisSupply + reward(height));
expect(calculate(height)).toBe(toString(genesisSupply + reward(height)));

mockConfig.milestones = [{ height: 1, reward: 2 }];
});
Expand Down
Loading

0 comments on commit 16ace9d

Please sign in to comment.