Skip to content

Commit

Permalink
Merge branch 'feature/tx_queue' into feature/integration_of_tx_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Lenz committed Feb 12, 2018
2 parents bab1b44 + de61aa2 commit dd55563
Show file tree
Hide file tree
Showing 9 changed files with 983 additions and 281 deletions.
55 changes: 20 additions & 35 deletions src/database/schemata.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,46 +120,28 @@ export const MessageJobSchema = {
/**
* @typedef TransactionJobType
* @property {number} id
* @property {number} timeout
* @property {string} processor
* @property {object} data
* @property {string} status
* @property {number} version
* @property {string} successHeading
* @property {string} successBody
* @property {string} failHeading
* @property {string} failBody
* @property {string} txHash
* @property {number} status
* @property {string} type Can be something like NATION_JOIN, NATION_LEAVE, NATION_CREATE etc. Used to know what this transaction is about.
*/
export type TransactionJobType = {
id: number,
timeout: number,
processor: string,
data: ?string,
successHeading: string,
successBody: string,
failHeading: string,
failBody: string,
status: 'WAITING' | 'DONE' | 'PROCESSING' | 'FAILED',
version: number
txHash: string,
status: number,
type: string,
nation: NationType | null
}

export const TransactionJobSchema = {
name: 'TransactionJob',
primaryKey: 'id',
properties: {
id: 'int',
timeout: 'int',
processor: 'string',
data: {
type: 'string',
optional: true,
txHash: 'string',
status: 'int',
type: 'string',
nation: {
type: 'linkingObjects',
objectType: 'Nation',
property: 'tx',
},
successHeading: 'string',
successBody: 'string',
failHeading: 'string',
failBody: 'string',
status: 'string',
version: 'int',
},
};

Expand All @@ -179,6 +161,9 @@ export const TransactionJobSchema = {
* @property {boolean} diplomaticRecognition
* @property {string} decisionMakingProcess
* @property {string} governanceService
* @property {number} citizens
* @property {boolean} joined
* @property {TransactionJobType | null} tx
*/
export type NationType = {
id: number,
Expand All @@ -197,7 +182,7 @@ export type NationType = {
governanceService: string,
citizens: number,
joined: boolean,
txHash: string
tx: TransactionJobType | null
}

export const NationSchema = {
Expand All @@ -209,8 +194,8 @@ export const NationSchema = {
default: -1,
type: 'int',
},
txHash: {
type: 'string',
tx: {
type: 'TransactionJob',
optional: true,
},
created: 'bool',
Expand Down
12 changes: 8 additions & 4 deletions src/ethereum/nation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @flow

import type {NationType} from '../database/schemata';
import type {NationType, TransactionJobType} from '../database/schemata';
import type {DBInterface} from '../database/db';
import type {TransactionQueueInterface} from '../queues/transaction';
import {TX_JOB_TYPE_NATION_CREATE} from '../queues/transaction';
const Web3 = require('web3');
const EventEmitter = require('eventemitter3');
const eachSeries = require('async/eachSeries');
Expand Down Expand Up @@ -308,9 +309,12 @@ export default function(db: DBInterface, txQueue: TransactionQueueInterface, web
});
}

// Attach transaction hash to nation
db
.write((_) => nation.txHash = txHash)
// Attach transaction to nation
txQueue
.jobFactory(txHash, TX_JOB_TYPE_NATION_CREATE)
.then((txJob: TransactionJobType) => db.write((realm) => {
nation.tx = txJob;
}))
.then((_) => res({
transKey: 'nation.submit_success',
nation: nation,
Expand Down
59 changes: 33 additions & 26 deletions src/ethereum/nation.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nationsFactory from './nation';
import dbFactory from '../database/db';
import TxQueue, {TX_JOB_TYPE_NATION_CREATE} from '../queues/transaction';
import {NATION_CREATE} from '../events';
import type {NationType} from '../database/schemata';
const EventEmitter = require('eventemitter3');
Expand Down Expand Up @@ -342,7 +343,7 @@ describe('nation', () => {
id: 1,
idInSmartContract: -1,
joined: false,
txHash: null,
tx: null,
});

nationData.nationName = 'updated nation name';
Expand Down Expand Up @@ -421,18 +422,21 @@ describe('nation', () => {
createNation: jest.fn(function(data, cb) {
expect(JSON.parse(data)).toEqual(nationData);

cb(null, 'i_am_a_tx_hash');
cb(null, '0xbe26a83c5248034f6c37eefb175c88e2815f5920354e37798a657387fa3b4736');
}),
};

const nationService = nationsFactory(db, null, null, null, nationContractMock);
const nationService = nationsFactory(db, new TxQueue(db), null, null, nationContractMock);

nationService
.saveDraft(nationData)
.then((response: {transKey: string, nation:NationType}) => nationService.submitDraft(response.nation.id))
.then((result) => {
expect(result.transKey).toBe('nation.submit_success');
expect(result.nation.txHash).toBe('i_am_a_tx_hash');
expect(result.nation.tx.txHash).toBe('0xbe26a83c5248034f6c37eefb175c88e2815f5920354e37798a657387fa3b4736');
expect(result.nation.tx.status).toBe(200);
expect(result.nation.tx.type).toBe(TX_JOB_TYPE_NATION_CREATE);

expect(nationContractMock.createNation).toHaveBeenCalledTimes(1);

done();
Expand All @@ -446,11 +450,11 @@ describe('nation', () => {

const nationContractMock = {
createNation: jest.fn((data, cb) => {
cb(null, 'i_am_the_tx_hash');
cb(null, '0xbe26a83c5248034f6c37eefb175c88e2815f5920354e37798a657387fa3b4736');
}),
};

const nations = nationsFactory(db, null, null, null, nationContractMock);
const nations = nationsFactory(db, new TxQueue(db, new EventEmitter()), null, null, nationContractMock);

const nationData = {
nationName: 'Bitnation',
Expand All @@ -472,29 +476,32 @@ describe('nation', () => {
.then((response: {transKey: string, nation:NationType}) => {
expect(nationContractMock.createNation).toHaveBeenCalledTimes(1);
expect(response.transKey).toBe('nation.submit_success');
expect(JSON.parse(JSON.stringify(response.nation))).toEqual({
nationName: 'Bitnation',
nationDescription: 'We <3 cryptography',
exists: true,
virtualNation: false,
nationCode: 'Code civil',
lawEnforcementMechanism: 'xyz',
profit: true,
nonCitizenUse: false,
diplomaticRecognition: false,
decisionMakingProcess: 'dictatorship',
governanceService: 'Security',
created: false,
citizens: 0,
id: 1,
idInSmartContract: -1,
joined: false,
txHash: 'i_am_the_tx_hash',
});

const n = response.nation;
expect(n.nationName).toBe('Bitnation');
expect(n.nationDescription).toBe('We <3 cryptography');
expect(n.exists).toBe(true);
expect(n.virtualNation).toBe(false);
expect(n.nationCode).toBe('Code civil');
expect(n.lawEnforcementMechanism).toBe('xyz');
expect(n.profit).toBe(true);
expect(n.nonCitizenUse).toBe(false);
expect(n.diplomaticRecognition).toBe(false);
expect(n.decisionMakingProcess).toBe('dictatorship');
expect(n.governanceService).toBe('Security');
expect(n.created).toBe(false);
expect(n.citizens).toBe(0);
expect(n.id).toBe(1);
expect(n.idInSmartContract).toBe(-1);
expect(n.joined).toBe(false);

expect(n.tx.txHash).toBe('0xbe26a83c5248034f6c37eefb175c88e2815f5920354e37798a657387fa3b4736');
expect(n.tx.type).toBe('NATION_CREATE');
expect(n.tx.status).toBe(200);

done();
})
.catch(done.fail);
.catch(console.log);
});
});
describe('deleteDraft', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ export const AMOUNT_OF_ADDRESSES_CHANGED = 'amount_of_addresses_changed';
*/
export const MESSAGING_QUEUE_JOB_ADDED = 'messaging_queue:job:added';

/**
* @desc Should be emitted after an transaction job was processed AND it's state changed
* @type {string}
*/
export const MESSAGING_QUEUE_JOB_PROCESSED = 'messaging_queue:job:processed';

/**
* @desc Is emitted when an job as added to the transaction queue
* @type {string}
*/
export const TRANSACTION_QUEUE_JOB_ADDED = 'transaction_queue:job:added';

/**
* @desc Is emitted when the transaction queue finishes an processing cycle
* @type {string}
*/
export const TRANSACTION_QUEUE_FINISHED_CYCLE = 'transaction_queue.finished_cycle';

/**
* @desc Emitted when there is an transaction to sign
* @type {string}
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import walletFactory from './ethereum/wallet';
import type {OsDependenciesInterface} from './specification/osDependencies';
import nationFactory from './ethereum/nation';
import {APP_OFFLINE, AMOUNT_OF_ADDRESSES_CHANGED, APP_ONLINE} from './events';
import txQueueFactory from './queues/transaction';
import TxQueue from './queues/transaction';
import messagingFactory from './queues/messaging';
import {NATION_CONTRACT_ABI, NATION_CONTRACT_ADDRESS_DEV, NATION_CONTRACT_ADDRESS_PROD} from './constants';
const EventEmitter = require('eventemitter3');
Expand All @@ -30,7 +30,6 @@ export default function pangeaLibsFactory(ss: SecureStorageInterface, dbPath: st
const ethUtils = utilsFactory(ss, ee, osDeps);
const profile = profileFactory(db, ethUtils);
const msgQueue = messagingFactory(ee, db);
const txQueue = txQueueFactory(db, ee);

const nationContractAddress = (production ? NATION_CONTRACT_ADDRESS_PROD : NATION_CONTRACT_ADDRESS_DEV);

Expand All @@ -40,7 +39,6 @@ export default function pangeaLibsFactory(ss: SecureStorageInterface, dbPath: st
utils: ethUtils,
},
queue: {
txQueue: txQueue,
msgQueue: msgQueue,
},
profile: {
Expand All @@ -57,6 +55,8 @@ export default function pangeaLibsFactory(ss: SecureStorageInterface, dbPath: st
.then((web3) => {
// $FlowFixMe
pangeaLibs.eth.wallet = walletFactory(ethUtils, web3, db);
// $FlowFixMe
pangeaLibs.queue.txQueue = new TxQueue(db, ee, web3, msgQueue);

const nationContract = web3.eth.contract(NATION_CONTRACT_ABI).at(nationContractAddress);

Expand Down Expand Up @@ -93,6 +93,8 @@ export default function pangeaLibsFactory(ss: SecureStorageInterface, dbPath: st
.then((web3) => {
// $FlowFixMe
pangeaLibs.eth.wallet = walletFactory(ethUtils, web3, db);
// $FlowFixMe
pangeaLibs.queue.txQueue = new TxQueue(db, ee, web3, msgQueue);

const nationContract = web3.eth.contract(NATION_CONTRACT_ABI).at(nationContractAddress);

Expand Down
2 changes: 1 addition & 1 deletion src/queues/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Msg {
* @param {string} params
* @param {string} interpret
*/
constructor(msg: string, params: {...mixed}, interpret: boolean) {
constructor(msg: string, params: ?{...mixed}, interpret: ?boolean) {
this._msg = msg;
this._params = params || {};
this._interpret = true;
Expand Down
Loading

0 comments on commit dd55563

Please sign in to comment.