Store the pending requests per network version (#5405)

* Store the requests in LS per network version

* Fixing tests

* Add network switching test

* Fixes
This commit is contained in:
Nicolas Gotchac 2017-04-19 15:15:43 +02:00 committed by Jaco Greeff
parent b50fb71dd1
commit e83de5cde2
4 changed files with 64 additions and 11 deletions

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import { outTransaction } from '~/api/format/output'; import { outTransaction } from '~/api/format/output';
import { trackRequest as trackRequestUtil, parseTransactionReceipt } from '~/util/tx'; import { trackRequest as trackRequestUtil, parseTransactionReceipt } from '~/util/tx';
import SavedRequests from '~/views/Application/Requests/savedRequests'; import SavedRequests from '~/views/Application/Requests/savedRequests';
@ -29,7 +31,7 @@ export const init = (api) => (dispatch) => {
dispatch(watchRequest(request)); dispatch(watchRequest(request));
}); });
api.on('connected', () => { api.once('connected', () => {
savedRequests.load(api).then((requests) => { savedRequests.load(api).then((requests) => {
requests.forEach((request) => dispatch(watchRequest(request))); requests.forEach((request) => dispatch(watchRequest(request)));
}); });
@ -48,7 +50,9 @@ export const watchRequest = (request) => (dispatch, getState) => {
export const trackRequest = (requestId, { transactionHash = null } = {}) => (dispatch, getState) => { export const trackRequest = (requestId, { transactionHash = null } = {}) => (dispatch, getState) => {
const { api } = getState(); const { api } = getState();
trackRequestUtil(api, { requestId, transactionHash }, (error, data) => { trackRequestUtil(api, { requestId, transactionHash }, (error, _data = {}) => {
const data = { ..._data };
if (error) { if (error) {
console.error(error); console.error(error);
return dispatch(setRequest(requestId, { error })); return dispatch(setRequest(requestId, { error }));
@ -61,6 +65,9 @@ export const trackRequest = (requestId, { transactionHash = null } = {}) => (dis
const requestData = requests[requestId]; const requestData = requests[requestId];
let blockSubscriptionId = -1; let blockSubscriptionId = -1;
// Set the block height to 0 at the beggining
data.blockHeight = new BigNumber(0);
// If the request was a contract deployment, // If the request was a contract deployment,
// then add the contract with the saved metadata to the account // then add the contract with the saved metadata to the account
if (requestData.metadata && requestData.metadata.deployment) { if (requestData.metadata && requestData.metadata.deployment) {

View File

@ -145,7 +145,7 @@ class Requests extends Component {
/> />
<div className={ styles.fill }> <div className={ styles.fill }>
<ScrollableText <ScrollableText
text={ error.text || error.message } text={ error.text || error.message || error.toString() }
/> />
</div> </div>
</div> </div>

View File

@ -21,9 +21,28 @@ import { ERROR_CODES } from '~/api/transport/error';
export const LS_REQUESTS_KEY = '_parity::requests'; export const LS_REQUESTS_KEY = '_parity::requests';
export default class SavedRequests { export default class SavedRequests {
load (api) { network = null;
const requests = this._get();
/**
* Load the network version, and then the related requests
*/
load (api) {
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 promises = Object.values(requests).map((request) => {
const { requestId, transactionHash } = request; const { requestId, transactionHash } = request;
@ -67,11 +86,21 @@ export default class SavedRequests {
} }
_get () { _get () {
return store.get(LS_REQUESTS_KEY) || {}; const allRequests = store.get(LS_REQUESTS_KEY) || {};
return allRequests[this.network] || {};
} }
_set (requests = {}) { _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) { _requestExists (api, requestId) {

View File

@ -19,27 +19,37 @@ import store from 'store';
import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests'; import SavedRequests, { LS_REQUESTS_KEY } from './savedRequests';
const NETWORK_ID = 42;
const DEFAULT_REQUEST = { const DEFAULT_REQUEST = {
requestId: '0x1', requestId: '0x1',
transaction: {} transaction: {}
}; };
const api = createApi(); const api = createApi(NETWORK_ID);
const api2 = createApi(1);
const savedRequests = new SavedRequests(); const savedRequests = new SavedRequests();
function createApi () { function createApi (networkVersion) {
return { return {
parity: { parity: {
checkRequest: sinon.stub().resolves() checkRequest: sinon.stub().resolves()
},
net: {
version: sinon.stub().resolves(networkVersion)
} }
}; };
} }
describe('views/Application/Requests/savedRequests', () => { describe('views/Application/Requests/savedRequests', () => {
beforeEach(() => { beforeEach((done) => {
store.set(LS_REQUESTS_KEY, { store.set(LS_REQUESTS_KEY, {
[NETWORK_ID]: {
[DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST [DEFAULT_REQUEST.requestId]: DEFAULT_REQUEST
}
}); });
savedRequests.load(api)
.then(() => done());
}); });
afterEach(() => { afterEach(() => {
@ -85,4 +95,11 @@ describe('views/Application/Requests/savedRequests', () => {
expect(requests[0]).to.deep.equal(DEFAULT_REQUEST); 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([]);
});
});
}); });