(Update) Add calling ValidatorMetadata.isValidatorAlreadyVoted

This commit is contained in:
Vadim Arasev 2018-08-07 11:53:06 +03:00
parent da23b16622
commit 422995b51f
4 changed files with 28 additions and 29 deletions

View File

@ -43,14 +43,10 @@ class App extends Component {
this.setIsValidVotingKey.call(this) this.setIsValidVotingKey.call(this)
} }
async setMetadata() { async setMetadata() {
const currentData = await this.getMetadataContract().getValidatorData({ const currentData = await this.getMetadataContract().getValidatorData(this.getMiningKey())
votingKey: this.getVotingKey()
})
const hasData = currentData.postal_code ? true : false const hasData = currentData.postal_code ? true : false
this.defaultValues = currentData this.defaultValues = currentData
const pendingChange = await this.getMetadataContract().getPendingChange({ const pendingChange = await this.getMetadataContract().getPendingChange(this.getMiningKey())
votingKey: this.getVotingKey()
})
if (Number(pendingChange.minThreshold) > 0) { if (Number(pendingChange.minThreshold) > 0) {
var msg = ` var msg = `
First Name: <b>${pendingChange.firstName}</b> <br/> First Name: <b>${pendingChange.firstName}</b> <br/>
@ -91,6 +87,9 @@ class App extends Component {
getVotingKey() { getVotingKey() {
return this.props.web3Config.votingKey return this.props.web3Config.votingKey
} }
getMiningKey() {
return this.props.web3Config.miningKey
}
checkValidation() { checkValidation() {
const isAfter = moment(this.state.form.expirationDate).isAfter(moment()) const isAfter = moment(this.state.form.expirationDate).isAfter(moment())
let keys = Object.keys(this.state.form) let keys = Object.keys(this.state.form)

View File

@ -15,4 +15,7 @@ export default class KeysManager {
async isVotingActive(votingKey) { async isVotingActive(votingKey) {
return await this.keysInstance.methods.isVotingActive(votingKey).call() return await this.keysInstance.methods.isVotingActive(votingKey).call()
} }
async miningKeyByVoting(votingKey) {
return await this.keysInstance.methods.miningKeyByVoting(votingKey).call()
}
} }

View File

@ -77,8 +77,7 @@ export default class Metadata {
} }
} }
async getValidatorData({ votingKey, miningKey }) { async getValidatorData(miningKey) {
miningKey = miningKey || (await this.getMiningByVoting(votingKey))
if (!miningKey) { if (!miningKey) {
helpersGlobal.generateAlert('warning', 'Warning!', messages.invalidaVotingKey) helpersGlobal.generateAlert('warning', 'Warning!', messages.invalidaVotingKey)
return {} return {}
@ -102,16 +101,6 @@ export default class Metadata {
} }
} }
async getMiningByVoting(votingKey) {
let miningKey
try {
miningKey = await this.metadataInstance.methods.getMiningByVotingKey(votingKey).call()
} catch (e) {
console.log(e.message)
}
return miningKey
}
async getAllValidatorsData(netId) { async getAllValidatorsData(netId) {
let all = [] let all = []
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
@ -125,7 +114,7 @@ export default class Metadata {
} }
data = this.getMocData() data = this.getMocData()
} else { } else {
data = await this.getValidatorData({ miningKey: key }) data = await this.getValidatorData(key)
} }
data.address = key data.address = key
all.push(data) all.push(data)
@ -134,8 +123,7 @@ export default class Metadata {
}) })
} }
async getPendingChange({ votingKey, miningKey }) { async getPendingChange(miningKey) {
miningKey = miningKey || (await this.getMiningByVoting(votingKey))
if (!miningKey) { if (!miningKey) {
helpersGlobal.generateAlert('warning', 'Warning!', messages.invalidaVotingKey) helpersGlobal.generateAlert('warning', 'Warning!', messages.invalidaVotingKey)
return {} return {}
@ -163,7 +151,7 @@ export default class Metadata {
async getAllPendingChanges() { async getAllPendingChanges() {
let pendingChanges = [] let pendingChanges = []
for (let key of this.miningKeys) { for (let key of this.miningKeys) {
let pendingChange = await this.getPendingChange({ miningKey: key }) let pendingChange = await this.getPendingChange(key)
pendingChange.address = key pendingChange.address = key
if (pendingChange.postal_code) { if (pendingChange.postal_code) {
pendingChanges.push(pendingChange) pendingChanges.push(pendingChange)
@ -172,18 +160,24 @@ export default class Metadata {
return pendingChanges return pendingChanges
} }
async confirmPendingChange({ miningKeyToConfirm, senderVotingKey }) { async confirmPendingChange({ miningKeyToConfirm, senderVotingKey, senderMiningKey }) {
let alreadyConfirmed = await this.metadataInstance.methods let alreadyConfirmed
.isAddressAlreadyVoted(miningKeyToConfirm, senderVotingKey) if (this.metadataInstance.methods.isValidatorAlreadyVoted) {
.call() alreadyConfirmed = await this.metadataInstance.methods
.isValidatorAlreadyVoted(miningKeyToConfirm, senderMiningKey)
.call()
} else {
alreadyConfirmed = await this.metadataInstance.methods
.isAddressAlreadyVoted(miningKeyToConfirm, senderVotingKey)
.call()
}
console.log(alreadyConfirmed) console.log(alreadyConfirmed)
if (alreadyConfirmed) { if (alreadyConfirmed) {
throw { throw {
message: `You already confirmed this change.` message: `You already confirmed this change.`
} }
} }
const miningKeySender = await this.getMiningByVoting(senderVotingKey) if (senderMiningKey === miningKeyToConfirm) {
if (miningKeySender === miningKeyToConfirm) {
throw { throw {
message: `You cannot confirm your own changes.\n message: `You cannot confirm your own changes.\n
Please ask other validators to verify your new information.` Please ask other validators to verify your new information.`

View File

@ -58,6 +58,7 @@ class AppMainRouter extends Component {
metadataContract: null, metadataContract: null,
poaConsensus: null, poaConsensus: null,
votingKey: null, votingKey: null,
miningKey: null,
loading: true, loading: true,
searchTerm: '', searchTerm: '',
injectedWeb3: true, injectedWeb3: true,
@ -86,6 +87,7 @@ class AppMainRouter extends Component {
}) })
this.setState({ this.setState({
votingKey: web3Config.defaultAccount, votingKey: web3Config.defaultAccount,
miningKey: await keysManager.miningKeyByVoting(web3Config.defaultAccount),
keysManager, keysManager,
metadataContract, metadataContract,
loading: false, loading: false,
@ -139,7 +141,8 @@ class AppMainRouter extends Component {
try { try {
let result = await this.state.metadataContract[methodToCall]({ let result = await this.state.metadataContract[methodToCall]({
miningKeyToConfirm: miningKey, miningKeyToConfirm: miningKey,
senderVotingKey: this.state.votingKey senderVotingKey: this.state.votingKey,
senderMiningKey: this.state.miningKey
}) })
console.log(result) console.log(result)
this.setState({ loading: false }) this.setState({ loading: false })