Check pending request on Node local transactions (#5564)

* Check pending request on Node

* Linting

* Liting

* Fix tests
This commit is contained in:
Nicolas Gotchac 2017-05-10 17:33:53 +02:00 committed by Jaco Greeff
parent 710339d0a8
commit 8146cbdae7
2 changed files with 47 additions and 64 deletions

View File

@ -16,22 +16,11 @@
import store from 'store';
import { ERROR_CODES } from '~/api/transport/error';
export const LS_REQUESTS_KEY = '_parity::requests';
export default class SavedRequests {
network = null;
/**
* Load the network version, and then the related requests
*/
load (api) {
return api.net.version()
.then((network) => {
this.network = network;
return this.loadRequests(api);
})
return this.loadRequests(api)
.catch((error) => {
console.error(error);
return [];
@ -43,28 +32,38 @@ export default class SavedRequests {
*/
loadRequests (api) {
const requests = this._get();
return api.parity.localTransactions()
.then((localTransactions) => {
const promises = Object.values(requests).map((request) => {
const { requestId, transactionHash } = request;
// The request hasn't been signed yet
if (transactionHash) {
return request;
}
return this._requestExists(api, requestId)
.then((exists) => {
if (!exists) {
// The transaction might be from an other
// chain
if (!localTransactions[transactionHash]) {
this.remove(requestId);
return null;
}
return request;
})
.catch(() => {
}
// The request hasn't been signed yet
return this._requestExists(api, requestId)
.then((exists) => {
if (!exists) {
this.remove(requestId);
return null;
}
return request;
});
});
return Promise.all(promises).then((requests) => requests.filter((request) => request));
return Promise.all(promises);
})
.then((requests) => requests.filter((request) => request));
}
save (requestId, requestData) {
@ -86,33 +85,23 @@ export default class SavedRequests {
}
_get () {
const allRequests = store.get(LS_REQUESTS_KEY) || {};
return allRequests[this.network] || {};
return store.get(LS_REQUESTS_KEY) || {};
}
_set (requests = {}) {
const allRequests = store.get(LS_REQUESTS_KEY) || {};
if (Object.keys(requests).length > 0) {
allRequests[this.network] = requests;
} else {
delete allRequests[this.network];
return store.set(LS_REQUESTS_KEY, requests);
}
return store.set(LS_REQUESTS_KEY, allRequests);
return store.remove(LS_REQUESTS_KEY);
}
_requestExists (api, requestId) {
return api.parity
.checkRequest(requestId)
.then(() => true)
.catch((error) => {
if (error.code === ERROR_CODES.REQUEST_NOT_FOUND) {
.catch(() => {
return false;
}
throw error;
});
}
}

View File

@ -19,23 +19,25 @@ import store from 'store';
import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests';
const NETWORK_ID = 42;
const DEFAULT_REQUEST = {
requestId: '0x1',
transaction: {}
};
const api = createApi(NETWORK_ID);
const api2 = createApi(1);
const SIGNED_REQUEST = {
requestId: '0x2',
transactionHash: '0xabcdef',
transaction: {}
};
const api = createApi();
const savedRequests = new SavedRequests();
function createApi (networkVersion) {
function createApi () {
return {
parity: {
checkRequest: sinon.stub().resolves()
},
net: {
version: sinon.stub().resolves(networkVersion)
checkRequest: sinon.stub().resolves(),
localTransactions: sinon.stub().resolves([])
}
};
}
@ -43,9 +45,8 @@ function createApi (networkVersion) {
describe('views/Application/Requests/savedRequests', () => {
beforeEach((done) => {
store.set(LS_REQUESTS_KEY, {
[NETWORK_ID]: {
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST
}
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST,
[SIGNED_REQUEST.requestId]: SIGNED_REQUEST
});
savedRequests.load(api)
@ -75,7 +76,7 @@ describe('views/Application/Requests/savedRequests', () => {
const requests = savedRequests._get();
expect(requests).to.deep.equal({});
expect(requests[DEFAULT_REQUEST.requestId]).to.be.undefined;
});
it('saves new requests', () => {
@ -92,14 +93,7 @@ describe('views/Application/Requests/savedRequests', () => {
it('loads requests', () => {
return savedRequests.load(api)
.then((requests) => {
expect(requests[0]).to.deep.equal(DEFAULT_REQUEST);
});
});
it('loads requests from the right network', () => {
return savedRequests.load(api2)
.then((requests) => {
expect(requests).to.deep.equal([]);
expect(requests).to.deep.equal([ DEFAULT_REQUEST ]);
});
});
});