Skip to content

Commit

Permalink
closes #74
Browse files Browse the repository at this point in the history
Added some test utils, stubs, and tx helpers.
  • Loading branch information
vekexasia committed Feb 4, 2018
1 parent 245e493 commit cc8e5ca
Show file tree
Hide file tree
Showing 21 changed files with 698 additions and 165 deletions.
25 changes: 19 additions & 6 deletions src/modules/blocks/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { IDatabase, ITask } from 'pg-promise';
import { Bus, catchToLoggerAndRemapError, ILogger, Inserts, Sequence, TransactionType } from '../../helpers/';
import { IBlockLogic, ITransactionLogic } from '../../ioc/interfaces/logic';
import {
IAccountsModule, IBlocksModule, IBlocksModuleChain, IBlocksModuleUtils, IRoundsModule,
IAccountsModule,
IBlocksModule,
IBlocksModuleChain,
IBlocksModuleUtils,
IRoundsModule,
ITransactionsModule
} from '../../ioc/interfaces/modules';
import { Symbols } from '../../ioc/symbols';
Expand Down Expand Up @@ -34,10 +38,10 @@ export class BlocksModuleChain implements IBlocksModuleChain {
private genesisBlock: SignedAndChainedBlockType;

// Helpers
@inject(Symbols.helpers.logger)
private logger: ILogger;
@inject(Symbols.helpers.bus)
private bus: Bus;
@inject(Symbols.helpers.logger)
private logger: ILogger;
@inject(Symbols.helpers.sequence)
@tagged(Symbols.helpers.sequence, Symbols.tags.helpers.balancesSequence)
private balancesSequence: Sequence;
Expand Down Expand Up @@ -73,7 +77,7 @@ export class BlocksModuleChain implements IBlocksModuleChain {
if (lastBlock.height === 1) {
throw new Error('Cannot delete genesis block');
}
const newLastBlock = await this.popLastBlock(lastBlock);
const newLastBlock = await this.popLastBlock(lastBlock);
// Set new "new" last block.
this.blocksModule.lastBlock = newLastBlock;
return newLastBlock;
Expand Down Expand Up @@ -122,8 +126,17 @@ export class BlocksModuleChain implements IBlocksModuleChain {
* @returns {Promise<void>}
*/
public async applyGenesisBlock(block: SignedAndChainedBlockType) {
// This is a shitty sort. Does not take into account b and it's not ok
block.transactions.sort((a) => a.type === TransactionType.VOTE ? 1 : 0);
// Order vote transactions to be at the end of processing.
block.transactions.sort((a, b) => {
if (a.type !== b.type) {
if (a.type === TransactionType.VOTE) {
return 1;
} else if (b.type === TransactionType.VOTE) {
return -1;
}
}
return 0;
});

const tracker = this.blocksModuleUtils.getBlockProgressLogger(
block.transactions.length,
Expand Down
20 changes: 6 additions & 14 deletions tests/integration/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import initializer from './init';
import { ISystemModule, ITransactionsModule } from '../../../src/ioc/interfaces/modules';
import { Symbols } from '../../../src/ioc/symbols';
import { LiskWallet } from 'dpos-offline/dist/es5/liskWallet';
import { IBaseTransaction, VoteAsset } from '../../../src/logic/transactions';
import * as txCrafter from '../../utils/txCrafter';

const delegates = require('../genesisDelegates.json');

Expand Down Expand Up @@ -47,14 +47,11 @@ export const createRandomWallet = (): LiskWallet => {

export const createVoteTransaction = async (confirmations: number, from: LiskWallet, to: publicKey, add: boolean): Promise<ITransaction> => {
const systemModule = initializer.appManager.container.get<ISystemModule>(Symbols.modules.system);
const t = new dposOffline.transactions.VoteTx({
votes: [`${add ? '+' : '-'}${to}`],
const tx = txCrafter.createVoteTransaction(from, systemModule.getFees().fees.vote, {
asset: {
votes: [`${add ? '+' : '-'}${to}`],
},
});
t.set('amount', 0);
t.set('fee', systemModule.getFees().fees.vote);
t.set('timestamp', 0);
t.set('recipientId', from.address);
const tx = t.sign(from);
if (confirmations > 0) {
await confirmTransactions([tx], confirmations);
}
Expand All @@ -63,12 +60,7 @@ export const createVoteTransaction = async (confirmations: number, from: LiskWal

export const createSendTransaction = async (confirmations: number, amount: number, from: LiskWallet, dest: string): Promise<ITransaction> => {
const systemModule = initializer.appManager.container.get<ISystemModule>(Symbols.modules.system);
const t = new dposOffline.transactions.SendTx();
t.set('amount', amount);
t.set('fee', systemModule.getFees().fees.send);
t.set('timestamp', 0);
t.set('recipientId', dest);
const tx = t.sign(from);
const tx = txCrafter.createSendTransaction(from, dest, systemModule.getFees().fees.send);
if (confirmations > 0) {
await confirmTransactions([tx], confirmations);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/helpers/BusStub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { injectable } from 'inversify';
import { BaseStubClass } from '../BaseStubClass';
import { stubMethod } from '../stubDecorator';

@injectable()
export class BusStub extends BaseStubClass {
@stubMethod()
public message(event: string, ...rest: any[]) {
Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/helpers/EdStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { stubMethod } from '../stubDecorator';
// tslint:disable no-empty

@injectable()
export default class EdStub extends BaseStubClass {
export default class EdStub extends BaseStubClass {

@stubMethod()
public sign() {}
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/helpers/ExceptionsManagerStub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { injectable } from 'inversify';
import { BaseStubClass } from '../BaseStubClass';
import { spyMethod, stubMethod } from '../stubDecorator';

@injectable()
export class ExceptionsManagerStub extends BaseStubClass {
@spyMethod
public registerExceptionHandler() {}
Expand Down
10 changes: 7 additions & 3 deletions tests/stubs/helpers/SequenceStub.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { injectable } from 'inversify';
import { Sequence } from '../../../src/helpers';
import { BaseStubClass } from '../BaseStubClass';
import { spyMethod } from '../stubDecorator';
import { spyMethod, stubMethod } from '../stubDecorator';

@injectable()
export class SequenceStub extends BaseStubClass {
private realImplementation: Sequence = new Sequence({});

@spyMethod
public addAndPromise() {
public addAndPromise(w) {
return this.realImplementation.addAndPromise(w);
}

@spyMethod
public count() {

return this.realImplementation.count();
}

@stubMethod()
public ____noiop() {
}
}
2 changes: 2 additions & 0 deletions tests/stubs/helpers/ZSchemaStub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { injectable } from 'inversify';
import * as sinon from 'sinon';

@injectable()
export default class ZSchemaStub {
public stubConfig: {
validate: {return: any}
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/logic/BlockRewardLogicStub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { injectable } from 'inversify';
import * as sinon from 'sinon';

@injectable()
export default class BlockRewardLogicStub {
public stubConfig: {
initRewards: {
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/logic/PeerLogicStub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { injectable } from 'inversify';
import * as sinon from 'sinon';

@injectable()
export default class PeerLogicStub {
public stubConfig: {
accept: {
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/logic/PeersLogicStub.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { injectable } from 'inversify';
import { IPeerLogic, IPeersLogic } from '../../../src/ioc/interfaces/logic';
import { BasePeerType, PeerType } from '../../../src/logic';
import { BaseStubClass } from '../BaseStubClass';
import { stubMethod } from '../stubDecorator';

@injectable()
export class PeersLogicStub extends BaseStubClass implements IPeersLogic {
@stubMethod()
public create(peer: BasePeerType): IPeerLogic {
Expand Down
26 changes: 14 additions & 12 deletions tests/stubs/logic/RoundLogicStub.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,67 @@
import { injectable } from 'inversify';
import { IRoundLogic } from '../../../src/ioc/interfaces/logic';
import { BaseStubClass } from '../BaseStubClass';
import { stubMethod } from '../stubDecorator';

@injectable()
export class RoundLogicStub extends BaseStubClass implements IRoundLogic {

@stubMethod
@stubMethod()
public mergeBlockGenerator(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public updateMissedBlocks(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public getVotes(): Promise<Array<{ delegate: string; amount: number }>> {
return undefined;
}

@stubMethod
@stubMethod()
public updateVotes(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public markBlockId(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public flushRound(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public truncateBlocks(): Promise<null> {
return undefined;
}

@stubMethod
@stubMethod()
public restoreRoundSnapshot(): Promise<null> {
return undefined;
}

@stubMethod
@stubMethod()
public restoreVotesSnapshot(): Promise<null> {
return undefined;
}

@stubMethod
@stubMethod()
public applyRound(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public land(): Promise<void> {
return undefined;
}

@stubMethod
@stubMethod()
public backwardLand(): Promise<void> {
return undefined;
}
Expand Down
Loading

0 comments on commit cc8e5ca

Please sign in to comment.