-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cache registry reverses, completion in address selector #4066
Changes from all commits
ac2c678
b38aa8d
6f492a4
71e80f1
2aa76e5
ad3b89d
c4f7b8f
ba4263e
92742e6
38734c9
f05ffd2
8c2aa4d
61a42cc
cca7627
67c2981
f12937a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,9 @@ const STORE = { | |
}, | ||
settings: { | ||
backgroundSeed: '' | ||
}, | ||
registry: { | ||
reverse: {} | ||
} | ||
}; | ||
} | ||
|
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' | ||
}); |
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); | ||
} | ||
}; | ||
}; |
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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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); | ||
|
||
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; | ||
|
@@ -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; | ||
|
@@ -194,6 +236,8 @@ export default class AddressSelectStore { | |
.filter((result) => result && !ZERO.test(result.address)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might actually want to get rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍