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

cache registry reverses, completion in address selector #4066

Merged
merged 16 commits into from
Jan 10, 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
4 changes: 2 additions & 2 deletions js/src/contracts/verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import subscribeToEvent from '../util/subscribe-to-event';
import subscribeToEvents from '../util/subscribe-to-events';

export const checkIfVerified = (contract, account) => {
return contract.instance.certified.call({}, [account]);
Expand Down Expand Up @@ -72,7 +72,7 @@ export const awaitPuzzle = (api, contract, account) => {
return blockNumber(api)
.then((block) => {
return new Promise((resolve, reject) => {
const subscription = subscribeToEvent(contract, 'Puzzled', {
const subscription = subscribeToEvents(contract, ['Puzzled'], {
from: block.toNumber(),
filter: (log) => log.params.who.value === account
});
Expand Down
3 changes: 3 additions & 0 deletions js/src/modals/ExecuteContract/executeContract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const STORE = {
},
settings: {
backgroundSeed: ''
},
registry: {
reverse: {}
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion js/src/redux/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import SignerMiddleware from './providers/signerMiddleware';
import statusMiddleware from '~/views/Status/middleware';
import CertificationsMiddleware from './providers/certifications/middleware';
import ChainMiddleware from './providers/chainMiddleware';
import RegistryMiddleware from './providers/registry/middleware';

export default function (api, browserHistory) {
const errors = new ErrorsMiddleware();
Expand All @@ -32,13 +33,15 @@ export default function (api, browserHistory) {
const certifications = new CertificationsMiddleware();
const routeMiddleware = routerMiddleware(browserHistory);
const chain = new ChainMiddleware();
const registry = new RegistryMiddleware(api);

const middleware = [
settings.toMiddleware(),
signer.toMiddleware(),
errors.toMiddleware(),
certifications.toMiddleware(),
chain.toMiddleware()
chain.toMiddleware(),
registry
];

return middleware.concat(status, routeMiddleware, thunk);
Expand Down
28 changes: 28 additions & 0 deletions js/src/redux/providers/registry/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

export const setReverse = (address, reverse) => ({
type: 'setReverse',
address, reverse
});

export const startCachingReverses = () => ({
type: 'startCachingReverses'
});

export const stopCachingReverses = () => ({
type: 'stopCachingReverses'
});
99 changes: 99 additions & 0 deletions js/src/redux/providers/registry/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import Contracts from '~/contracts';
import subscribeToEvents from '~/util/subscribe-to-events';

import registryABI from '~/contracts/abi/registry.json';

import { setReverse, startCachingReverses } from './actions';

export default (api) => (store) => {
let contract, subscription, timeout, interval;

let addressesToCheck = {};

const onLog = (log) => {
switch (log.event) {
case 'ReverseConfirmed':
addressesToCheck[log.params.reverse.value] = true;

break;
case 'ReverseRemoved':
delete addressesToCheck[log.params.reverse.value];

break;
}
};

const checkReverses = () => {
Object
.keys(addressesToCheck)
.forEach((address) => {
contract
.instance
.reverse
.call({}, [ address ])
.then((reverse) => store.dispatch(setReverse(address, reverse)));
});

addressesToCheck = {};
};

return (next) => (action) => {
switch (action.type) {
case 'initAll':
next(action);
store.dispatch(startCachingReverses());

break;
case 'startCachingReverses':
const { registry } = Contracts.get();

registry.getInstance()
.then((instance) => api.newContract(registryABI, instance.address))
.then((_contract) => {
contract = _contract;

subscription = subscribeToEvents(_contract, ['ReverseConfirmed', 'ReverseRemoved']);
subscription.on('log', onLog);

timeout = setTimeout(checkReverses, 5000);
interval = setInterval(checkReverses, 20000);
})
.catch((err) => {
console.error('Failed to start caching reverses:', err);
throw err;
});

break;
case 'stopCachingReverses':
if (subscription) {
subscription.unsubscribe();
}
if (interval) {
clearInterval(interval);
}
if (timeout) {
clearTimeout(timeout);
}

break;
default:
next(action);
}
};
};
33 changes: 33 additions & 0 deletions js/src/redux/providers/registry/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

const initialState = {
reverse: {} // cache for reverse lookup
};

export default (state = initialState, action) => {
if (action.type === 'setReverse') {
if (state.reverse[action.address] === action.reverse) {
return state;
}

return { ...state, reverse: {
...state.reverse, [ action.address ]: action.reverse
} };
}

return state;
};
2 changes: 2 additions & 0 deletions js/src/redux/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
snackbarReducer, walletReducer
} from './providers';
import certificationsReducer from './providers/certifications/reducer';
import registryReducer from './providers/registry/reducer';

import errorReducer from '~/ui/Errors/reducers';
import settingsReducer from '~/views/Settings/reducers';
Expand All @@ -43,6 +44,7 @@ export default function () {
images: imagesReducer,
nodeStatus: nodeStatusReducer,
personal: personalReducer,
registry: registryReducer,
signer: signerReducer,
snackbar: snackbarReducer,
wallet: walletReducer,
Expand Down
5 changes: 4 additions & 1 deletion js/src/ui/Form/AddressSelect/addressSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AddressSelect extends Component {
contacts: PropTypes.object,
contracts: PropTypes.object,
tokens: PropTypes.object,
reverse: PropTypes.object,

// Optional props
allowCopy: PropTypes.bool,
Expand Down Expand Up @@ -584,10 +585,12 @@ class AddressSelect extends Component {
function mapStateToProps (state) {
const { accountsInfo } = state.personal;
const { balances } = state.balances;
const { reverse } = state.registry;

return {
accountsInfo,
balances
balances,
reverse
};
}

Expand Down
50 changes: 47 additions & 3 deletions js/src/ui/Form/AddressSelect/addressSelectStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import React from 'react';
import { observable, action } from 'mobx';
import { flatMap } from 'lodash';
import { flatMap, uniqBy } from 'lodash';
import { FormattedMessage } from 'react-intl';

import Contracts from '~/contracts';
Expand All @@ -30,7 +30,48 @@ export default class AddressSelectStore {
@observable registryValues = [];

initValues = [];
regLookups = [];
regLookups = [
(query) => {
query = query.toLowerCase().trim();
if (query.length === 0 || query === '0x') {
return null;
}
const startsWithQuery = (s) => new RegExp('^' + query, 'i').test(s);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


let address;
let name = this.reverse[query];

if (!name) {
const addr = Object
.keys(this.reverse)
.find((addr) => {
const name = this.reverse[addr];
return startsWithQuery(addr) || (name && startsWithQuery(name));
});

if (addr) {
address = addr;
name = this.reverse[addr];
} else {
return null;
}
}

return {
address,
name,
description: (
<FormattedMessage
id='addressSelect.fromRegistry'
defaultMessage='{name} (from registry)'
values={ {
name
} }
/>
)
};
}
];

constructor (api) {
this.api = api;
Expand Down Expand Up @@ -114,7 +155,8 @@ export default class AddressSelectStore {
}

@action setValues (props) {
const { accounts = {}, contracts = {}, contacts = {} } = props;
const { accounts = {}, contracts = {}, contacts = {}, reverse = {} } = props;
this.reverse = reverse;

const accountsN = Object.keys(accounts).length;
const contractsN = Object.keys(contracts).length;
Expand Down Expand Up @@ -194,6 +236,8 @@ export default class AddressSelectStore {
.filter((result) => result && !ZERO.test(result.address));
Copy link
Contributor

Choose a reason for hiding this comment

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

Might actually want to get rid of this.registryValues = []; line 231, to prevent the flickering when typing an autocompleted name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is dangerous, especially when we have (or add in the future) long-running queries, as it will show incorrect values to the users for a while. I'd rather introduce a loading flag.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm yes that's true. The flickering is a bit odd, but no biggies

})
.then((results) => {
results = uniqBy(results, (result) => result.address);

this.registryValues = results
.map((result) => {
const lowercaseAddress = result.address.toLowerCase();
Expand Down
Loading