(Update) Finalization w/o reverts, some optimizations and refactoring

This commit is contained in:
Vadim Arasev 2018-08-04 13:41:34 +03:00
parent 84b3a6f29b
commit efd89dbd24
10 changed files with 97 additions and 85 deletions

View File

@ -262,6 +262,11 @@ export class BallotCard extends React.Component {
contract.address, contract.address,
contract.finalize(id), contract.finalize(id),
async tx => { async tx => {
const events = await contract.instance.getPastEvents('BallotFinalized', {
fromBlock: tx.blockNumber,
toBlock: tx.blockNumber
})
if (events.length > 0) {
this.isFinalized = true this.isFinalized = true
ballotsStore.ballotCards[pos].props.votingState.isFinalized = this.isFinalized ballotsStore.ballotCards[pos].props.votingState.isFinalized = this.isFinalized
if (this.canBeFinalized !== null) { if (this.canBeFinalized !== null) {
@ -271,6 +276,9 @@ export class BallotCard extends React.Component {
swal('Congratulations!', messages.FINALIZED_SUCCESS_MSG, 'success').then(result => { swal('Congratulations!', messages.FINALIZED_SUCCESS_MSG, 'success').then(result => {
push(`${commonStore.rootPath}`) push(`${commonStore.rootPath}`)
}) })
} else {
swal('Warning!', messages.INVALID_FINALIZE_MSG, 'warning')
}
}, },
messages.FINALIZE_FAILED_TX messages.FINALIZE_FAILED_TX
) )

View File

@ -218,9 +218,13 @@ export class NewBallot extends React.Component {
miningKey: ballotStore.ballotKeys.miningKey.value, miningKey: ballotStore.ballotKeys.miningKey.value,
ballotType: ballotStore.ballotKeys.keysBallotType ballotType: ballotStore.ballotKeys.keysBallotType
} }
let areBallotParamsValid = await contractsStore.votingToChangeKeys.areBallotParamsValid( let areBallotParamsValid
areBallotParamsValid = await contractsStore.ballotsStorage.areKeysBallotParamsValid(inputToAreBallotParamsValid)
if (areBallotParamsValid === null) {
areBallotParamsValid = await contractsStore.votingToChangeKeys.areBallotParamsValid(
inputToAreBallotParamsValid inputToAreBallotParamsValid
) )
}
if (ballotStore.ballotKeys.keysBallotType === ballotStore.KeysBallotType.add) { if (ballotStore.ballotKeys.keysBallotType === ballotStore.KeysBallotType.add) {
if (ballotStore.ballotKeys.keyType !== ballotStore.KeyType.mining) { if (ballotStore.ballotKeys.keyType !== ballotStore.KeyType.mining) {
if (!ballotStore.ballotKeys.miningKey.value) { if (!ballotStore.ballotKeys.miningKey.value) {
@ -239,7 +243,6 @@ export class NewBallot extends React.Component {
let methodToCreateBallot let methodToCreateBallot
let contractType let contractType
let contractInstance let contractInstance
//let web3 = new Web3(contractsStore.web3Instance.currentProvider)
switch (ballotStore.ballotType) { switch (ballotStore.ballotType) {
case ballotStore.BallotType.keys: case ballotStore.BallotType.keys:
methodToCreateBallot = this.createBallotForKeys methodToCreateBallot = this.createBallotForKeys

View File

@ -13,4 +13,13 @@ export default class BallotsStorage {
this.ballotsStorageInstance = new web3_10.eth.Contract(ballotsStorageAbi, BALLOTS_STORAGE_ADDRESS) this.ballotsStorageInstance = new web3_10.eth.Contract(ballotsStorageAbi, BALLOTS_STORAGE_ADDRESS)
} }
areKeysBallotParamsValid({ ballotType, affectedKey, affectedKeyType, miningKey }) {
if (!this.ballotsStorageInstance.methods.areKeysBallotParamsValid) {
return null
}
return this.ballotsStorageInstance.methods
.areKeysBallotParamsValid(ballotType, affectedKey, affectedKeyType, miningKey)
.call()
}
} }

View File

@ -0,0 +1,18 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
export default class KeysManager {
async init({ web3, netId }) {
const { KEYS_MANAGER_ADDRESS } = networkAddresses(netId)
console.log('KeysManager address', KEYS_MANAGER_ADDRESS)
let web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
let keysManagerABI = await helpers.getABI(branch, 'KeysManager')
this.instance = new web3_10.eth.Contract(keysManagerABI, KEYS_MANAGER_ADDRESS)
this.address = KEYS_MANAGER_ADDRESS
}
}

View File

@ -16,8 +16,7 @@ export default class ValidatorMetadata {
this.metadataInstance = new web3_10.eth.Contract(MetadataAbi, METADATA_ADDRESS) this.metadataInstance = new web3_10.eth.Contract(MetadataAbi, METADATA_ADDRESS)
} }
async getValidatorFullName({ votingKey, miningKey }) { async getValidatorFullName(miningKey) {
miningKey = miningKey || (await this.getMiningByVoting(votingKey))
let validator let validator
if (this.metadataInstance.methods.getValidatorName) { if (this.metadataInstance.methods.getValidatorName) {
validator = await this.metadataInstance.methods.getValidatorName(miningKey).call() validator = await this.metadataInstance.methods.getValidatorName(miningKey).call()
@ -29,8 +28,4 @@ export default class ValidatorMetadata {
lastName: toAscii(validator.lastName) lastName: toAscii(validator.lastName)
} }
} }
async getMiningByVoting(votingKey) {
return await this.metadataInstance.methods.getMiningByVotingKey(votingKey).call()
}
} }

View File

@ -15,6 +15,7 @@ export default class VotingToChangeKeys {
this.votingToChangeKeysInstance = new web3_10.eth.Contract(votingToChangeKeysABI, VOTING_TO_CHANGE_KEYS_ADDRESS) this.votingToChangeKeysInstance = new web3_10.eth.Contract(votingToChangeKeysABI, VOTING_TO_CHANGE_KEYS_ADDRESS)
this.gasPrice = web3_10.utils.toWei('1', 'gwei') this.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_KEYS_ADDRESS this.address = VOTING_TO_CHANGE_KEYS_ADDRESS
this.instance = this.votingToChangeKeysInstance
} }
//setters //setters
@ -44,6 +45,9 @@ export default class VotingToChangeKeys {
//getters //getters
areBallotParamsValid({ ballotType, affectedKey, affectedKeyType, miningKey }) { areBallotParamsValid({ ballotType, affectedKey, affectedKeyType, miningKey }) {
if (!this.doesMethodExist('areBallotParamsValid')) {
return null
}
return this.votingToChangeKeysInstance.methods return this.votingToChangeKeysInstance.methods
.areBallotParamsValid(ballotType, affectedKey, affectedKeyType, miningKey) .areBallotParamsValid(ballotType, affectedKey, affectedKeyType, miningKey)
.call() .call()
@ -83,22 +87,8 @@ export default class VotingToChangeKeys {
return null return null
} }
getMiningByVotingKey(_votingKey) { async getBallotLimit(_miningKey, _limitPerValidator) {
return this.votingToChangeKeysInstance.methods.getMiningByVotingKey(_votingKey).call() const _activeBallots = await this.votingToChangeKeysInstance.methods.validatorActiveBallots(_miningKey).call()
} return _limitPerValidator - _activeBallots
async getValidatorActiveBallots(_votingKey) {
let miningKey
try {
miningKey = await this.getMiningByVotingKey(_votingKey)
} catch (e) {
miningKey = '0x0000000000000000000000000000000000000000'
}
return await this.votingToChangeKeysInstance.methods.validatorActiveBallots(miningKey).call()
}
async getBallotLimit(_votingKey) {
const currentLimit = await this.votingToChangeKeysInstance.methods.getBallotLimitPerValidator().call()
return currentLimit - (await this.getValidatorActiveBallots(_votingKey))
} }
} }

View File

@ -18,6 +18,7 @@ export default class VotingToChangeMinThreshold {
) )
this.gasPrice = web3_10.utils.toWei('1', 'gwei') this.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS this.address = VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS
this.instance = this.votingToChangeMinThresholdInstance
} }
//setters //setters
@ -77,22 +78,10 @@ export default class VotingToChangeMinThreshold {
return null return null
} }
getMiningByVotingKey(_votingKey) { async getBallotLimit(_miningKey, _limitPerValidator) {
return this.votingToChangeMinThresholdInstance.methods.getMiningByVotingKey(_votingKey).call() const _activeBallots = await this.votingToChangeMinThresholdInstance.methods
} .validatorActiveBallots(_miningKey)
.call()
async getValidatorActiveBallots(_votingKey) { return _limitPerValidator - _activeBallots
let miningKey
try {
miningKey = await this.getMiningByVotingKey(_votingKey)
} catch (e) {
miningKey = '0x0000000000000000000000000000000000000000'
}
return await this.votingToChangeMinThresholdInstance.methods.validatorActiveBallots(miningKey).call()
}
async getBallotLimit(_votingKey) {
const currentLimit = await this.votingToChangeMinThresholdInstance.methods.getBallotLimitPerValidator().call()
return currentLimit - (await this.getValidatorActiveBallots(_votingKey))
} }
} }

View File

@ -15,6 +15,7 @@ export default class VotingToChangeProxy {
this.votingToChangeProxyInstance = new web3_10.eth.Contract(votingToChangeProxyABI, VOTING_TO_CHANGE_PROXY_ADDRESS) this.votingToChangeProxyInstance = new web3_10.eth.Contract(votingToChangeProxyABI, VOTING_TO_CHANGE_PROXY_ADDRESS)
this.gasPrice = web3_10.utils.toWei('1', 'gwei') this.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_PROXY_ADDRESS this.address = VOTING_TO_CHANGE_PROXY_ADDRESS
this.instance = this.votingToChangeProxyInstance
} }
//setters //setters
@ -74,22 +75,8 @@ export default class VotingToChangeProxy {
return null return null
} }
getMiningByVotingKey(_votingKey) { async getBallotLimit(_miningKey, _limitPerValidator) {
return this.votingToChangeProxyInstance.methods.getMiningByVotingKey(_votingKey).call() const _activeBallots = await this.votingToChangeProxyInstance.methods.validatorActiveBallots(_miningKey).call()
} return _limitPerValidator - _activeBallots
async getValidatorActiveBallots(_votingKey) {
let miningKey
try {
miningKey = await this.getMiningByVotingKey(_votingKey)
} catch (e) {
miningKey = '0x0000000000000000000000000000000000000000'
}
return await this.votingToChangeProxyInstance.methods.validatorActiveBallots(miningKey).call()
}
async getBallotLimit(_votingKey) {
const currentLimit = await this.votingToChangeProxyInstance.methods.getBallotLimitPerValidator().call()
return currentLimit - (await this.getValidatorActiveBallots(_votingKey))
} }
} }

View File

@ -41,6 +41,7 @@ class AppMainRouter extends Component {
let setPoaConsensus = contractsStore.setPoaConsensus(web3Config) let setPoaConsensus = contractsStore.setPoaConsensus(web3Config)
let setBallotsStorage = contractsStore.setBallotsStorage(web3Config) let setBallotsStorage = contractsStore.setBallotsStorage(web3Config)
let setKeysManager = contractsStore.setKeysManager(web3Config)
let setProxyStorage = contractsStore.setProxyStorage(web3Config) let setProxyStorage = contractsStore.setProxyStorage(web3Config)
let setVotingToChangeKeys = contractsStore.setVotingToChangeKeys(web3Config) let setVotingToChangeKeys = contractsStore.setVotingToChangeKeys(web3Config)
let setVotingToChangeMinThreshold = contractsStore.setVotingToChangeMinThreshold(web3Config) let setVotingToChangeMinThreshold = contractsStore.setVotingToChangeMinThreshold(web3Config)
@ -50,6 +51,7 @@ class AppMainRouter extends Component {
await Promise.all([ await Promise.all([
setPoaConsensus, setPoaConsensus,
setBallotsStorage, setBallotsStorage,
setKeysManager,
setProxyStorage, setProxyStorage,
setVotingToChangeKeys, setVotingToChangeKeys,
setVotingToChangeMinThreshold, setVotingToChangeMinThreshold,

View File

@ -3,6 +3,7 @@ import React from 'react'
import PoaConsensus from '../contracts/PoaConsensus.contract' import PoaConsensus from '../contracts/PoaConsensus.contract'
import BallotsStorage from '../contracts/BallotsStorage.contract' import BallotsStorage from '../contracts/BallotsStorage.contract'
import KeysManager from '../contracts/KeysManager.contract'
import ProxyStorage from '../contracts/ProxyStorage.contract' import ProxyStorage from '../contracts/ProxyStorage.contract'
import VotingToChangeKeys from '../contracts/VotingToChangeKeys.contract' import VotingToChangeKeys from '../contracts/VotingToChangeKeys.contract'
import VotingToChangeMinThreshold from '../contracts/VotingToChangeMinThreshold.contract' import VotingToChangeMinThreshold from '../contracts/VotingToChangeMinThreshold.contract'
@ -21,6 +22,7 @@ import 'babel-polyfill'
class ContractsStore { class ContractsStore {
@observable poaConsensus @observable poaConsensus
@observable ballotsStorage @observable ballotsStorage
@observable keysManager
@observable proxyStorage @observable proxyStorage
@observable votingToChangeKeys @observable votingToChangeKeys
@observable votingToChangeMinThreshold @observable votingToChangeMinThreshold
@ -90,6 +92,15 @@ class ContractsStore {
}) })
} }
@action('Set KeysManager contract')
setKeysManager = async web3Config => {
this.keysManager = new KeysManager()
await this.keysManager.init({
web3: web3Config.web3Instance,
netId: web3Config.netId
})
}
@action('Set ProxyStorage contract') @action('Set ProxyStorage contract')
setProxyStorage = async web3Config => { setProxyStorage = async web3Config => {
this.proxyStorage = new ProxyStorage() this.proxyStorage = new ProxyStorage()
@ -148,9 +159,7 @@ class ContractsStore {
@action('Set mining key') @action('Set mining key')
setMiningKey = async web3Config => { setMiningKey = async web3Config => {
try { try {
this.miningKey = await this.votingToChangeKeys.votingToChangeKeysInstance.methods this.miningKey = await this.keysManager.instance.methods.miningKeyByVoting(web3Config.defaultAccount).call()
.getMiningByVotingKey(web3Config.defaultAccount)
.call()
} catch (e) { } catch (e) {
console.log(e) console.log(e)
this.miningKey = '0x0000000000000000000000000000000000000000' this.miningKey = '0x0000000000000000000000000000000000000000'
@ -329,20 +338,22 @@ class ContractsStore {
@action @action
async getBallotsLimits() { async getBallotsLimits() {
if (this.web3Instance && this.netId) { if (this.web3Instance && this.netId) {
let setVotingToChangeKeys = this.setVotingToChangeKeys({ web3Instance: this.web3Instance, netId: this.netId }) //let setVotingToChangeKeys = this.setVotingToChangeKeys({ web3Instance: this.web3Instance, netId: this.netId })
let setVotingToChangeMinThreshold = this.setVotingToChangeMinThreshold({ //let setVotingToChangeMinThreshold = this.setVotingToChangeMinThreshold({
web3Instance: this.web3Instance, // web3Instance: this.web3Instance,
netId: this.netId // netId: this.netId
}) //})
let setVotingToChangeProxy = this.setVotingToChangeProxy({ web3Instance: this.web3Instance, netId: this.netId }) //let setVotingToChangeProxy = this.setVotingToChangeProxy({ web3Instance: this.web3Instance, netId: this.netId })
await Promise.all([setVotingToChangeKeys, setVotingToChangeMinThreshold, setVotingToChangeProxy]) //await Promise.all([setVotingToChangeKeys, setVotingToChangeMinThreshold, setVotingToChangeProxy])
let getKeysLimit = await this.votingToChangeKeys.getBallotLimit(this.web3Instance.eth.defaultAccount) const limitPerValidator = await this.ballotsStorage.ballotsStorageInstance.methods
let getMinThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit( .getBallotLimitPerValidator()
this.web3Instance.eth.defaultAccount .call()
)
let getProxyLimit = await this.votingToChangeProxy.getBallotLimit(this.web3Instance.eth.defaultAccount) let getKeysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator)
let getMinThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit(this.miningKey, limitPerValidator)
let getProxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator)
await Promise.all([getKeysLimit, getMinThresholdLimit, getProxyLimit]).then( await Promise.all([getKeysLimit, getMinThresholdLimit, getProxyLimit]).then(
([keysLimit, minThresholdLimit, proxyLimit]) => { ([keysLimit, minThresholdLimit, proxyLimit]) => {
@ -360,7 +371,7 @@ class ContractsStore {
const keys = await this.poaConsensus.getValidators() const keys = await this.poaConsensus.getValidators()
this.validatorsLength = keys.length this.validatorsLength = keys.length
keys.forEach(async key => { keys.forEach(async key => {
const metadata = await this.validatorMetadata.getValidatorFullName({ miningKey: key }) const metadata = await this.validatorMetadata.getValidatorFullName(key)
this.validatorsMetadata[key.toLowerCase()] = { this.validatorsMetadata[key.toLowerCase()] = {
label: `${key} ${metadata.lastName}`, label: `${key} ${metadata.lastName}`,
lastNameAndKey: `${metadata.lastName} ${key}`, lastNameAndKey: `${metadata.lastName} ${key}`,