Retry PoPA address fetch using validator mining key, when possible.

This commit is contained in:
Jorge Luis Shirai 2018-10-02 12:08:02 -03:00
parent 5e7e20f144
commit 413692df7e
2 changed files with 52 additions and 33 deletions

View File

@ -48,10 +48,11 @@ export default class AllValidators extends Component {
throw new Error(`ProofOfPhysicalAddress not deployed in the current network`)
}
// Get each validator voting key, default to its mining key if no voting key (Master of Ceremony case)
let validatorsVotingOrMiningKeys = await Promise.all(
validators.map((validator, index) =>
this.getKeysManagerContract()
// Get each validator's voting & mining key array (if voting key is present set it as the 1st element)
let validatorsVotingAndMiningKeys = await Promise.all(
validators.map((validator, index) => {
const miningKey = validators[index].address
return this.getKeysManagerContract()
.getVotingByMining(validator.address)
.then(votingKey => {
const isNotVotingKey =
@ -59,14 +60,25 @@ export default class AllValidators extends Component {
votingKey === '0x00' ||
votingKey === '0x0' ||
votingKey === '0x'
return isNotVotingKey ? validators[index].address : votingKey
return isNotVotingKey ? [miningKey] : [votingKey, miningKey]
})
)
})
)
const validatorsPhysicalAddresses = await popa.getPhysicalAddressesOfWalletAddressArray(
validatorsVotingOrMiningKeys
)
// Get PoPA physical address of validator using voting & mining array
const addressRegisteredEvents = await popa.getAllAddressRegisteredEvents()
const getValidatorsPhysicalAddressesPromises = validatorsVotingAndMiningKeys.map(validatorKeys => {
return popa
.getPhysicalAddressesOfWalletAddress(validatorKeys[0], addressRegisteredEvents)
.then(getPhysicalAddressesResult => {
// If addresses not found and the keys array has an extra element, retry the fetch and return its result
return getPhysicalAddressesResult === null && validatorKeys.length > 1
? popa.getPhysicalAddressesOfWalletAddress(validatorKeys[1], addressRegisteredEvents)
: getPhysicalAddressesResult
})
})
const validatorsPhysicalAddresses = await Promise.all(getValidatorsPhysicalAddressesPromises)
augmentedValidators = validatorsPhysicalAddresses.map((physicalAddresses, index) => {
const validator = validators[index]
let validatorPhysicalAddresses

View File

@ -15,42 +15,41 @@ export default class ProofOfPhysicalAddress {
let proofOfPhysicalAddressAbi = await helpers.getABI(branch, 'ProofOfPhysicalAddress')
this.instance = new web3_10.eth.Contract(proofOfPhysicalAddressAbi, PROOF_OF_PHYSICAL_ADDRESS)
this.getPhysicalAddressesOfWalletAddressArray = this.getPhysicalAddressesOfWalletAddressArray.bind(this)
this.getPhysicalAddressesOfWalletAddress = this.getPhysicalAddressesOfWalletAddress.bind(this)
this.getAllEvents = this.getAllEvents.bind(this)
this.getAllAddressRegisteredEvents = this.getAllAddressRegisteredEvents.bind(this)
this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray = this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray.bind(
this
)
}
/**
* Given a wallet address array, return a promise that resolves to an array of physical addresses from
* PoPA contract.
* @param {String[]} walletAddressArray
* Given a wallet address, return a promise that resolves to an array of physical addresses from PoPA contract or null.
* If a cached registeredAddressEvents is provided, it will be used to get the corresponding keccakIdentifiers from it.
* @param {String} walletAddress
* @param {Object[]} registeredAddressEvents (optional)
* @return {Promise}
*/
async getPhysicalAddressesOfWalletAddressArray(walletAddressArray) {
let result = []
async getPhysicalAddressesOfWalletAddress(walletAddress, registeredAddressEvents) {
let result = null
try {
const registeredAddressEvents = await this.getAllEvents(REGISTER_ADDRESS_EVENT_NAME)
const allPhysicalAddressesPromises = walletAddressArray.map(walletAddress => {
let physicalAddresses = null
let keccakIdentifiers = registeredAddressEvents
.filter(event => event.returnValues.wallet === walletAddress)
.map(event => event.returnValues.keccakIdentifier)
if (!registeredAddressEvents) {
registeredAddressEvents = await this.getAllEvents(REGISTER_ADDRESS_EVENT_NAME)
}
if (keccakIdentifiers.length > 0) {
physicalAddresses = this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray(
walletAddress,
keccakIdentifiers
).then(physicalAddresses => {
return physicalAddresses.length > 0 ? physicalAddresses : null
})
}
return Promise.resolve(physicalAddresses)
})
result = await Promise.all(allPhysicalAddressesPromises)
let keccakIdentifiers = registeredAddressEvents
.filter(event => event.returnValues.wallet === walletAddress)
.map(event => event.returnValues.keccakIdentifier)
if (keccakIdentifiers.length > 0) {
let physicalAddresses = await this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray(
walletAddress,
keccakIdentifiers
)
result = physicalAddresses.length > 0 ? physicalAddresses : result
}
} catch (e) {
console.error(`Error in getPhysicalAddressesOfWalletAddressArray`, e)
console.error(`Error in getPhysicalAddressesOfWalletAddress`, e)
}
return result
}
@ -104,4 +103,12 @@ export default class ProofOfPhysicalAddress {
}
return result
}
/**
* Get all addressRegistered event objects.
* @return {Promise}
*/
async getAllAddressRegisteredEvents() {
return this.getAllEvents(REGISTER_ADDRESS_EVENT_NAME)
}
}