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

refactor(crypto): make all Slots.* methods static #2473

Merged
merged 4 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/integration/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe("Blockchain", () => {
const lastBlock = blockchain.state.getLastBlock();
const roundInfo = roundCalculator.calculateRound(lastBlock.data.height);
const activeDelegates = await blockchain.database.getActiveDelegates(roundInfo);
const nextSlot = Crypto.slots.getSlotNumber(lastBlock.data.timestamp) + 1;
const nextSlot = Crypto.Slots.getSlotNumber(lastBlock.data.timestamp) + 1;
return activeDelegates[nextSlot % activeDelegates.length];
};

Expand All @@ -156,7 +156,7 @@ describe("Blockchain", () => {

const lastBlock = blockchain.state.getLastBlock();
const data = {
timestamp: Crypto.slots.getSlotTime(Crypto.slots.getSlotNumber(lastBlock.data.timestamp) + 1),
timestamp: Crypto.Slots.getSlotTime(Crypto.Slots.getSlotNumber(lastBlock.data.timestamp) + 1),
version: 0,
previousBlock: lastBlock.data.id,
previousBlockHex: lastBlock.data.idHex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,14 +870,14 @@ describe("Transaction Guard", () => {
// const now = 47157042; // seconds since genesis block
// const transactionExists = processor.pool.transactionExists;
// processor.pool.transactionExists = jest.fn(() => false);
// const getTime = Crypto.slots.getTime;
// Crypto.slots.getTime = jest.fn(() => now);
// const getTime = Crypto.Slots.getTime;
// Crypto.Slots.getTime = jest.fn(() => now);

// const secondsInFuture = 3601;
// const tx = {
// id: "1",
// senderPublicKey: "affe",
// timestamp: Crypto.slots.getTime() + secondsInFuture,
// timestamp: Crypto.Slots.getTime() + secondsInFuture,
// };
// processor.__filterAndTransformTransactions([tx]);

Expand All @@ -888,7 +888,7 @@ describe("Transaction Guard", () => {
// },
// ]);

// Crypto.slots.getTime = getTime;
// Crypto.Slots.getTime = getTime;
// processor.pool.transactionExists = transactionExists;
// });

Expand Down
14 changes: 7 additions & 7 deletions __tests__/unit/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ describe("Blockchain", () => {
expect(blockchain.getLastBlock()).toEqual(lastBlock);
});

it("should broadcast a block if (Crypto.slots.getSlotNumber() * blocktime <= block.data.timestamp)", async () => {
it("should broadcast a block if (Crypto.Slots.getSlotNumber() * blocktime <= block.data.timestamp)", async () => {
blockchain.state.started = true;

const mockCallback = jest.fn(() => true);
const lastBlock = blockchain.getLastBlock();
lastBlock.data.timestamp = Crypto.slots.getSlotNumber() * 8000;
lastBlock.data.timestamp = Crypto.Slots.getSlotNumber() * 8000;

const broadcastBlock = jest.spyOn(getMonitor, "broadcastBlock");

Expand Down Expand Up @@ -167,7 +167,7 @@ describe("Blockchain", () => {

const block = {
height: 100,
timestamp: Crypto.slots.getEpochTime(),
timestamp: Crypto.Slots.getTime(),
};

// @ts-ignore
Expand All @@ -189,7 +189,7 @@ describe("Blockchain", () => {

const block = {
height: 100,
timestamp: Crypto.slots.getSlotTime(Crypto.slots.getNextSlot()),
timestamp: Crypto.Slots.getSlotTime(Crypto.Slots.getNextSlot()),
};

// @ts-ignore
Expand All @@ -207,7 +207,7 @@ describe("Blockchain", () => {
const loggerInfo = jest.spyOn(logger, "info");

const mockGetSlotNumber = jest
.spyOn(Crypto.slots, "getSlotNumber")
.spyOn(Crypto.Slots, "getSlotNumber")
.mockReturnValueOnce(1)
.mockReturnValueOnce(1);

Expand Down Expand Up @@ -242,7 +242,7 @@ describe("Blockchain", () => {
expect(
blockchain.isSynced({
data: {
timestamp: Crypto.slots.getTime(),
timestamp: Crypto.Slots.getTime(),
height: genesisBlock.height,
},
} as Interfaces.IBlock),
Expand All @@ -256,7 +256,7 @@ describe("Blockchain", () => {
const getLastBlock = jest.spyOn(blockchain, "getLastBlock").mockReturnValueOnce({
// @ts-ignore
data: {
timestamp: Crypto.slots.getTime(),
timestamp: Crypto.Slots.getTime(),
height: genesisBlock.height,
},
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-blockchain/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe("State Machine", () => {
// @ts-ignore
data: {
height: 2,
timestamp: Crypto.slots.getTime(),
timestamp: Crypto.Slots.getTime(),
},
});
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { delegates } from "../../../../../../utils/fixtures/testnet/delegates";
import { Crypto } from "@arkecosystem/crypto";

const timestampSlots = 104;
Crypto.slots.getTime = jest.fn().mockReturnValue(timestampSlots);
Crypto.Slots.getTime = jest.fn().mockReturnValue(timestampSlots);

describe("Internal handlers - rounds", () => {
describe("getCurrentRound", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe("Peers handler", () => {

describe("getStatus", () => {
it("should return status", async () => {
Crypto.slots.isForgingAllowed = jest.fn().mockReturnValue(true);
Crypto.slots.getSlotNumber = jest.fn().mockReturnValue(3);
Crypto.Slots.isForgingAllowed = jest.fn().mockReturnValue(true);
Crypto.Slots.getSlotNumber = jest.fn().mockReturnValue(3);

const result = await getStatus();

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-transaction-pool/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { database as databaseService } from "./mocks/database";

const { BlockFactory } = Blocks;
const { SATOSHI } = Constants;
const { slots } = Crypto;
const { Slots } = Crypto;
const { TransactionTypes } = Enums;

const delegatesSecrets = delegates.map(d => d.secret);
Expand Down Expand Up @@ -218,7 +218,7 @@ describe("Connection", () => {
expect(connection.getPoolSize()).toBe(0);

const expireAfterSeconds = 3;
const expiration = slots.getTime() + expireAfterSeconds;
const expiration = Slots.getTime() + expireAfterSeconds;

const transactions: Interfaces.ITransaction[] = [];

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-transactions/handler-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { testnet } from "../../../packages/crypto/src/networks";

const { transactionBaseSchema, extend } = Transactions.schemas;
const { TransactionTypes } = Enums;
const { slots } = Crypto;
const { Slots } = Crypto;

const TEST_TRANSACTION_TYPE = 100;

Expand Down Expand Up @@ -117,7 +117,7 @@ describe("TransactionHandlerRegistry", () => {
const keys = Identities.Keys.fromPassphrase("secret");
const data: Interfaces.ITransactionData = {
type: TEST_TRANSACTION_TYPE,
timestamp: slots.getTime(),
timestamp: Slots.getTime(),
senderPublicKey: keys.publicKey,
fee: Utils.BigNumber.make("10000000"),
amount: Utils.BigNumber.make("200000000"),
Expand Down
20 changes: 10 additions & 10 deletions __tests__/unit/core-utils/is-blocked-chained.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe("isChained", () => {
it("should be ok", () => {
const previousBlock = {
id: "1",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 1,
previousBlock: null,
} as Interfaces.IBlockData;

const nextBlock = {
id: "2",
timestamp: Crypto.slots.getSlotTime(1),
timestamp: Crypto.Slots.getSlotTime(1),
height: 2,
previousBlock: "1",
} as Interfaces.IBlockData;
Expand All @@ -25,14 +25,14 @@ describe("isChained", () => {
it("should not chain when previous block does not match", () => {
const previousBlock = {
id: "2",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 2,
previousBlock: null,
} as Interfaces.IBlockData;

const nextBlock = {
id: "1",
timestamp: Crypto.slots.getSlotTime(1),
timestamp: Crypto.Slots.getSlotTime(1),
height: 3,
previousBlock: "1",
} as Interfaces.IBlockData;
Expand All @@ -43,14 +43,14 @@ describe("isChained", () => {
it("should not chain when next height is not plus 1", () => {
const previousBlock = {
id: "1",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 1,
previousBlock: null,
} as Interfaces.IBlockData;

const nextBlock = {
id: "2",
timestamp: Crypto.slots.getSlotTime(1),
timestamp: Crypto.Slots.getSlotTime(1),
height: 3,
previousBlock: "1",
} as Interfaces.IBlockData;
Expand All @@ -61,14 +61,14 @@ describe("isChained", () => {
it("should not chain when same slot", () => {
const previousBlock = {
id: "1",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 1,
previousBlock: null,
} as Interfaces.IBlockData;

const nextBlock = {
id: "2",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 3,
previousBlock: "1",
} as Interfaces.IBlockData;
Expand All @@ -79,14 +79,14 @@ describe("isChained", () => {
it("should not chain when lower slot", () => {
const previousBlock = {
id: "1",
timestamp: Crypto.slots.getSlotTime(1),
timestamp: Crypto.Slots.getSlotTime(1),
height: 1,
previousBlock: null,
} as Interfaces.IBlockData;

const nextBlock = {
id: "2",
timestamp: Crypto.slots.getSlotTime(0),
timestamp: Crypto.Slots.getSlotTime(0),
height: 3,
previousBlock: "1",
} as Interfaces.IBlockData;
Expand Down
6 changes: 3 additions & 3 deletions __tests__/unit/crypto/blocks/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Interfaces, Utils } from "@arkecosystem/crypto";
import ByteBuffer from "bytebuffer";
import { Delegate } from "../../../../packages/core-forger/src/delegate";
import { Block, BlockFactory } from "../../../../packages/crypto/src/blocks";
import { slots } from "../../../../packages/crypto/src/crypto";
import { Slots } from "../../../../packages/crypto/src/crypto";
import { IBlock } from "../../../../packages/crypto/src/interfaces";
import { configManager } from "../../../../packages/crypto/src/managers";
import * as networks from "../../../../packages/crypto/src/networks";
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("Block", () => {
});

it("should fail to verify a block with incorrect timestamp", () => {
jest.spyOn(slots, "getSlotNumber").mockImplementation(timestamp => (timestamp ? 2 : 0));
jest.spyOn(Slots, "getSlotNumber").mockImplementation(timestamp => (timestamp ? 2 : 0));
const block = BlockFactory.fromData(dummyBlock);

expect(block.verification.verified).toBeFalse();
Expand Down Expand Up @@ -148,7 +148,7 @@ describe("Block", () => {

it("should fail to verify a block if error is thrown", () => {
const errorMessage = "Very very, very bad error";
jest.spyOn(slots, "getSlotNumber").mockImplementation(height => {
jest.spyOn(Slots, "getSlotNumber").mockImplementation(height => {
throw errorMessage;
});
const block = BlockFactory.fromData(dummyBlock);
Expand Down
46 changes: 9 additions & 37 deletions __tests__/unit/crypto/crypto/slots.test.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,51 @@
import "jest-extended";

import { slots } from "../../../../packages/crypto/src/crypto/slots";
import { Slots } from "../../../../packages/crypto/src/crypto/slots";
import { configManager } from "../../../../packages/crypto/src/managers/config";
import { devnet } from "../../../../packages/crypto/src/networks";

beforeEach(() => configManager.setConfig(devnet));

describe("Slots", () => {
describe("getEpochTime", () => {
it("return epoch datetime", () => {
expect(slots.getEpochTime()).toBeNumber();
});
});

describe("beginEpochTime", () => {
it("return epoch datetime", () => {
expect(slots.beginEpochTime().toISO()).toBe("2017-03-21T13:00:00.000Z");
});

it("return epoch unix", () => {
expect(slots.beginEpochTime().toUnix()).toBe(1490101200);
});
});

describe("getTime", () => {
it("return epoch time as number", () => {
const result = slots.getTime(1490101210000);
const result = Slots.getTime(1490101210000);

expect(result).toBeNumber();
expect(result).toEqual(10);
});
});

describe("getRealTime", () => {
it("return real time", () => {
expect(slots.getRealTime(10)).toBe(1490101210000);
});

it("should call this.getTime when called without time", () => {
const getTime = jest.spyOn(slots, "getTime");
slots.getRealTime(undefined);
expect(getTime).toHaveBeenCalledTimes(1);
});
});

describe("getSlotNumber", () => {
it("return slot number", () => {
expect(slots.getSlotNumber(10)).toBe(1);
expect(Slots.getSlotNumber(10)).toBe(1);
});
});

describe("getSlotTime", () => {
it("returns slot time", () => {
expect(slots.getSlotTime(19614)).toBe(156912);
expect(Slots.getSlotTime(19614)).toBe(156912);
});
});

describe("getNextSlot", () => {
it("returns next slot", () => {
expect(slots.getNextSlot()).toBeNumber();
expect(Slots.getNextSlot()).toBeNumber();
});
});

describe("isForgingAllowed", () => {
it("returns boolean", () => {
expect(slots.isForgingAllowed()).toBeDefined();
expect(Slots.isForgingAllowed()).toBeDefined();
});
});

describe("getTimeInMsUntilNextSlot", () => {
it("should be ok", () => {
const nextSlotTime = slots.getSlotTime(slots.getNextSlot());
const now = slots.getTime();
const nextSlotTime = Slots.getSlotTime(Slots.getNextSlot());
const now = Slots.getTime();

expect(slots.getTimeInMsUntilNextSlot()).toEqual((nextSlotTime - now) * 1000);
expect(Slots.getTimeInMsUntilNextSlot()).toEqual((nextSlotTime - now) * 1000);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { slots } from "../../../../../../../packages/crypto/src/crypto";
import { Slots } from "../../../../../../../packages/crypto/src/crypto";
import { Keys } from "../../../../../../../packages/crypto/src/identities";
import { Transaction } from "../../../../../../../packages/crypto/src/transactions";
import { TransactionBuilder } from "../../../../../../../packages/crypto/src/transactions/builders/transactions/transaction";
Expand All @@ -24,7 +24,7 @@ export const transactionBuilder = <T extends TransactionBuilder<T>>(provider: ()
let data;

beforeEach(() => {
timestamp = slots.getTime();
timestamp = Slots.getTime();

data = {
id: "02d0d835266297f15c192be2636eb3fbc30b39b87fc583ff112062ef8dae1a1f",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/versions/1/delegates/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class DelegatesController extends Controller {
const limit = request.query.limit || 10;

const delegatesCount = this.config.getMilestone(lastBlock).activeDelegates;
const currentSlot = Crypto.slots.getSlotNumber(lastBlock.data.timestamp);
const currentSlot = Crypto.Slots.getSlotNumber(lastBlock.data.timestamp);

const roundInfo = roundCalculator.calculateRound(lastBlock.data.height);
const activeDelegates = await this.databaseService.getActiveDelegates(roundInfo);
Expand Down
Loading