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: reject V1 transactions after milestone #2879

Merged
merged 18 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const setUp = async (): Promise<void> => {
],
});

Managers.configManager.getMilestone().aip11 = true;

const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
await databaseService.reset();
await databaseService.buildWallets();
Expand Down
19 changes: 19 additions & 0 deletions __tests__/helpers/block-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Delegate } from '@arkecosystem/core-forger';
import { Interfaces, Networks, Utils } from '@arkecosystem/crypto';
import { delegates, genesisBlock } from '../utils/fixtures/unitnet';

export class BlockFactory {

public static createDummy(transactions: Interfaces.ITransactionData[] = []): Interfaces.IBlock {
const delegate = new Delegate(delegates[0].passphrase, Networks.testnet.network);
spkjp marked this conversation as resolved.
Show resolved Hide resolved
return delegate.forge(transactions, {
timestamp: 12345689,
previousBlock: {
id: genesisBlock.id,
height: 1,
},
reward: Utils.BigNumber.ZERO,
});
}

}
1 change: 1 addition & 0 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./rest-client";
export * from "./transaction-factory";
export * from "./block-factory";
4 changes: 3 additions & 1 deletion __tests__/helpers/transaction-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ export class TransactionFactory {
}

private sign<T>(quantity: number, method: string): T[] {
Managers.configManager.setFromPreset(this.network);
if (Managers.configManager.get("network.name") !== this.network) {
Managers.configManager.setFromPreset(this.network);
}

if (!this.senderPublicKey) {
this.senderPublicKey = Identities.PublicKey.fromPassphrase(this.passphrase);
Expand Down
11 changes: 7 additions & 4 deletions __tests__/integration/core-api/handlers/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "../../../utils";
import { setUp, tearDown } from "../__support__/setup";
import { utils } from "../utils";

import { Address } from "../../../../packages/crypto/src/identities";
import { Identities, Managers } from "@arkecosystem/crypto";
import { TransactionFactory } from "../../../helpers/transaction-factory";
import { genesisBlock } from "../../../utils/config/testnet/genesisBlock";
import { delegates } from "../../../utils/fixtures/testnet/delegates";
Expand Down Expand Up @@ -44,7 +44,7 @@ beforeAll(async () => {
wrongType = 3;
version = 1;
senderPublicKey = genesisTransaction.senderPublicKey;
senderAddress = Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
senderAddress = Identities.Address.fromPublicKey(genesisTransaction.senderPublicKey, 23);
recipientAddress = genesisTransaction.recipientId;
timestamp = genesisTransaction.timestamp;
timestampFrom = timestamp;
Expand Down Expand Up @@ -527,18 +527,19 @@ describe("API 2.0 - Transactions", () => {
});

it.each([3, 5, 8])("should accept and broadcast %i transactions emptying a wallet", async txNumber => {
delete process.env.CORE_TEST_DELAY_AIP11;
Managers.configManager.getMilestone().aip11 = true;

const sender = delegates[txNumber]; // use txNumber so that we use a different delegate for each test case
const receivers = generateWallets("testnet", 2);
const amountPlusFee = Math.floor(+sender.balance / txNumber);
const lastAmountPlusFee = +sender.balance - (txNumber - 1) * amountPlusFee;

const transactions = TransactionFactory.transfer(receivers[0].address, amountPlusFee - transferFee)
.withNetwork("testnet")
.withPassphrase(sender.secret)
.create(txNumber - 1);

const lastTransaction = TransactionFactory.transfer(receivers[0].address, lastAmountPlusFee - transferFee)
.withNetwork("testnet")
.withNonce(transactions[transactions.length - 1].nonce)
.withPassphrase(sender.secret)
.create();
Expand All @@ -556,6 +557,8 @@ describe("API 2.0 - Transactions", () => {
allTransactions.map(transaction => transaction.id).sort(),
);
expect(response.data.data.invalid).toHaveLength(0);

process.env.CORE_TEST_DELAY_AIP11 = "true";
});

it.each([3, 5, 8])(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import transactionRaw from "./transaction-raw.json";
import transactionTransformed from "./transaction-transformed.json";

Managers.configManager.setFromPreset("testnet");

Managers.configManager.getMilestone().aip11 = false;
const genesisTransaction = Transactions.TransactionFactory.fromData(genesisBlock.transactions[0]);
delete genesisBlock.transactions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from "@arkecosystem/core-container";
import { Managers } from "@arkecosystem/crypto";
import { plugin } from "../../../../packages/core-database-postgres/src/plugin";
import { plugin as pluginDatabase } from "../../../../packages/core-database/src/plugin";
import { registerWithContainer, setUpContainer } from "../../../utils/helpers/container";
Expand All @@ -13,6 +14,8 @@ export const setUp = async () => {

process.env.CORE_RESET_DATABASE = "1";

Managers.configManager.getMilestone().aip11 = false;

await registerWithContainer(pluginDatabase);

await registerWithContainer(plugin, {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/integration/core-p2p/socket-server/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import "jest-extended";
import "../mocks/core-container";
import { defaults } from "../mocks/p2p-options";

import { Blocks, Managers } from "@arkecosystem/crypto/src";
import { Managers } from "@arkecosystem/crypto/src";
import delay from "delay";
import SocketCluster from "socketcluster";
import socketCluster from "socketcluster-client";
import { startSocketServer } from "../../../../packages/core-p2p/src/socket-server";
import { BlockFactory } from "../../../helpers";
import { createPeerService } from "../../../helpers/peers";
import { TransactionFactory } from "../../../helpers/transaction-factory";
import { genesisBlock } from "../../../utils/config/unitnet/genesisBlock";
import { wallets } from "../../../utils/fixtures/unitnet/wallets";

Managers.configManager.setFromPreset("unitnet");
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("Peer socket endpoint", () => {
it("should postBlock successfully", async () => {
await delay(1000);
const { data } = await emit("p2p.peer.postBlock", {
data: { block: Blocks.BlockFactory.fromData(genesisBlock).toJson() },
data: { block: BlockFactory.createDummy().toJson() },
headers,
});

Expand Down Expand Up @@ -164,7 +164,7 @@ describe("Peer socket endpoint", () => {
it("should cancel the request when exceeding rate limit on a certain endpoint", async () => {
await delay(1000);

const block = Blocks.BlockFactory.fromData(genesisBlock).toJson();
const block = BlockFactory.createDummy().toJson();

await emit("p2p.peer.postBlock", {
headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ describe("Transaction Guard", () => {
});

it("should not accept transaction in excess", async () => {
Managers.configManager.getMilestone().aip11 = true;

const delegate3 = delegates[3];
const newWalletPassphrase = generateMnemonic();
const { publicKey } = Identities.Keys.fromPassphrase(newWalletPassphrase);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, Database } from "@arkecosystem/core-interfaces";
import { Networks, Utils } from "@arkecosystem/crypto";
import { Managers, Networks, Utils } from "@arkecosystem/crypto";
import { StateBuilder } from "../../../../packages/core-database-postgres/src";
import { Delegate } from "../../../../packages/core-forger/src/delegate";
import { WalletManager } from "../../../../packages/core-state/src/wallets";
Expand All @@ -26,6 +26,8 @@ const genesisWalletBalance = wallet =>
beforeAll(async () => {
container = await setUp();

Managers.configManager.getMilestone().aip11 = true;

walletManager = new WalletManager();
database = container.resolvePlugin<Database.IDatabaseService>("database");
stateBuilder = new StateBuilder(database.connection, walletManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container, Database } from "@arkecosystem/core-interfaces";
import { Identities, Networks, Utils } from "@arkecosystem/crypto";
import { Identities, Managers, Networks, Utils } from "@arkecosystem/crypto";
import { StateBuilder } from "../../../../packages/core-database-postgres/src";
import { Delegate } from "../../../../packages/core-forger/src/delegate";
import { WalletManager } from "../../../../packages/core-state/src/wallets";
Expand All @@ -16,6 +16,8 @@ let stateBuilder: StateBuilder;
beforeAll(async () => {
container = await setUp();

Managers.configManager.getMilestone().aip11 = true;

walletManager = new WalletManager();
database = container.resolvePlugin<Database.IDatabaseService>("database");
stateBuilder = new StateBuilder(database.connection, walletManager);
Expand Down
5 changes: 3 additions & 2 deletions __tests__/unit/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./mocks/";
import { container } from "./mocks/container";

import * as Utils from "@arkecosystem/core-utils";
import { Blocks, Crypto, Interfaces } from "@arkecosystem/crypto";
import { Blocks, Crypto, Interfaces, Managers } from "@arkecosystem/crypto";
import delay from "delay";
import { Blockchain } from "../../../packages/core-blockchain/src/blockchain";
import { stateMachine } from "../../../packages/core-blockchain/src/state-machine";
Expand All @@ -27,8 +27,9 @@ describe("Blockchain", () => {
beforeAll(async () => {
// Create the genesis block after the setup has finished or else it uses a potentially
// wrong network config.
Managers.configManager.getMilestone().aip11 = false;
genesisBlock = BlockFactory.fromData(GB);

Managers.configManager.getMilestone().aip11 = true;
// Workaround: Add genesis transactions to the exceptions list, because they have a fee of 0
// and otherwise don't pass validation.
config["exceptions.transactions"] = genesisBlock.transactions.map(tx => tx.id);
Expand Down
6 changes: 5 additions & 1 deletion __tests__/unit/core-blockchain/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { logger } from "./mocks/logger";
import { getMonitor } from "./mocks/p2p/network-monitor";
import { stateStorageStub as stateStorage } from "./stubs/state-storage";

import { Blocks, Crypto } from "@arkecosystem/crypto";
import { Blocks, Crypto, Managers } from "@arkecosystem/crypto";
import { defaults } from "../../../packages/core-blockchain/src/defaults";
import { genesisBlock } from "../../utils/config/testnet/genesisBlock";

Expand Down Expand Up @@ -165,6 +165,8 @@ describe("State Machine", () => {
let loggerError;

beforeEach(() => {
Managers.configManager.getMilestone().aip11 = false;

const config = container.app.getConfig();
jest.spyOn(config, "get").mockImplementation(key => (key === "genesisBlock" ? genesisBlock : ""));

Expand All @@ -191,6 +193,8 @@ describe("State Machine", () => {

afterEach(() => jest.restoreAllMocks());
afterAll(() => {
Managers.configManager.getMilestone().aip11 = true;

jest.restoreAllMocks();

process.env.NODE_ENV = "TEST";
Expand Down
4 changes: 3 additions & 1 deletion __tests__/unit/core-database/database-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { app } from "@arkecosystem/core-container";
import { ApplicationEvents } from "@arkecosystem/core-event-emitter";
import { Database, EventEmitter, State } from "@arkecosystem/core-interfaces";
import { Handlers } from "@arkecosystem/core-transactions";
import { Blocks, Constants, Enums, Identities, Transactions, Utils } from "@arkecosystem/crypto";
import { Blocks, Constants, Enums, Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
import { DatabaseService } from "../../../packages/core-database/src/database-service";
import { Wallet, WalletManager } from "../../../packages/core-state/src/wallets";
import { roundCalculator } from "../../../packages/core-utils/dist";
Expand Down Expand Up @@ -191,6 +191,7 @@ describe("Database Service", () => {
const initialHeight = 52;

// Create delegates
Managers.configManager.getMilestone().aip11 = false;
for (const transaction of genesisBlock.transactions) {
if (transaction.type === TransactionType.DelegateRegistration) {
const wallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
Expand All @@ -203,6 +204,7 @@ describe("Database Service", () => {
walletManager.reindex(wallet);
}
}
Managers.configManager.getMilestone().aip11 = true;

const keys = {
passphrase: "this is a secret passphrase",
Expand Down
15 changes: 0 additions & 15 deletions __tests__/unit/core-forger/__fixtures__/transaction.ts

This file was deleted.

Loading