Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Store the pending requests per network version #5405

Merged
merged 4 commits into from
Apr 19, 2017
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
11 changes: 9 additions & 2 deletions js/src/redux/providers/requestsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import BigNumber from 'bignumber.js';

import { outTransaction } from '~/api/format/output';
import { trackRequest as trackRequestUtil, parseTransactionReceipt } from '~/util/tx';
import SavedRequests from '~/views/Application/Requests/savedRequests';
Expand All @@ -29,7 +31,7 @@ export const init = (api) => (dispatch) => {
dispatch(watchRequest(request));
});

api.on('connected', () => {
api.once('connected', () => {
savedRequests.load(api).then((requests) => {
requests.forEach((request) => dispatch(watchRequest(request)));
});
Expand All @@ -48,7 +50,9 @@ export const watchRequest = (request) => (dispatch, getState) => {
export const trackRequest = (requestId, { transactionHash = null } = {}) => (dispatch, getState) => {
const { api } = getState();

trackRequestUtil(api, { requestId, transactionHash }, (error, data) => {
trackRequestUtil(api, { requestId, transactionHash }, (error, _data = {}) => {
const data = { ..._data };

if (error) {
console.error(error);
return dispatch(setRequest(requestId, { error }));
Expand All @@ -61,6 +65,9 @@ export const trackRequest = (requestId, { transactionHash = null } = {}) => (dis
const requestData = requests[requestId];
let blockSubscriptionId = -1;

// Set the block height to 0 at the beggining
data.blockHeight = new BigNumber(0);

// If the request was a contract deployment,
// then add the contract with the saved metadata to the account
if (requestData.metadata && requestData.metadata.deployment) {
Expand Down
2 changes: 1 addition & 1 deletion js/src/views/Application/Requests/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Requests extends Component {
/>
<div className={ styles.fill }>
<ScrollableText
text={ error.text || error.message }
text={ error.text || error.message || error.toString() }
/>
</div>
</div>
Expand Down
35 changes: 32 additions & 3 deletions js/src/views/Application/Requests/savedRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,28 @@ 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) {
const requests = this._get();
return api.net.version()
.then((network) => {
this.network = network;
return this.loadRequests(api);
})
.catch((error) => {
console.error(error);
return [];
});
}

/**
* Load the requests of the current network
*/
loadRequests (api) {
const requests = this._get();
const promises = Object.values(requests).map((request) => {
const { requestId, transactionHash } = request;

Expand Down Expand Up @@ -67,11 +86,21 @@ export default class SavedRequests {
}

_get () {
return store.get(LS_REQUESTS_KEY) || {};
const allRequests = store.get(LS_REQUESTS_KEY) || {};

return allRequests[this.network] || {};
}

_set (requests = {}) {
return store.set(LS_REQUESTS_KEY, 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, allRequests);
}

_requestExists (api, requestId) {
Expand Down
25 changes: 21 additions & 4 deletions js/src/views/Application/Requests/savedRequests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,37 @@ import store from 'store';

import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests';

const NETWORK_ID = 42;
const DEFAULT_REQUEST = {
requestId: '0x1',
transaction: {}
};

const api = createApi();
const api = createApi(NETWORK_ID);
const api2 = createApi(1);
const savedRequests = new SavedRequests();

function createApi () {
function createApi (networkVersion) {
return {
parity: {
checkRequest: sinon.stub().resolves()
},
net: {
version: sinon.stub().resolves(networkVersion)
}
};
}

describe('views/Application/Requests/savedRequests', () => {
beforeEach(() => {
beforeEach((done) => {
store.set(LS_REQUESTS_KEY, {
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST
[NETWORK_ID]: {
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST
}
});

savedRequests.load(api)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just return the Promise here instead of calling done. (Mocha is promise-aware)

.then(() => done());
});

afterEach(() => {
Expand Down Expand Up @@ -85,4 +95,11 @@ describe('views/Application/Requests/savedRequests', () => {
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([]);
});
});
});