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

View File

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

View File

@ -13,4 +13,13 @@ export default class BallotsStorage {
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)
}
async getValidatorFullName({ votingKey, miningKey }) {
miningKey = miningKey || (await this.getMiningByVoting(votingKey))
async getValidatorFullName(miningKey) {
let validator
if (this.metadataInstance.methods.getValidatorName) {
validator = await this.metadataInstance.methods.getValidatorName(miningKey).call()
@ -29,8 +28,4 @@ export default class ValidatorMetadata {
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.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_KEYS_ADDRESS
this.instance = this.votingToChangeKeysInstance
}
//setters
@ -44,6 +45,9 @@ export default class VotingToChangeKeys {
//getters
areBallotParamsValid({ ballotType, affectedKey, affectedKeyType, miningKey }) {
if (!this.doesMethodExist('areBallotParamsValid')) {
return null
}
return this.votingToChangeKeysInstance.methods
.areBallotParamsValid(ballotType, affectedKey, affectedKeyType, miningKey)
.call()
@ -83,22 +87,8 @@ export default class VotingToChangeKeys {
return null
}
getMiningByVotingKey(_votingKey) {
return this.votingToChangeKeysInstance.methods.getMiningByVotingKey(_votingKey).call()
}
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))
async getBallotLimit(_miningKey, _limitPerValidator) {
const _activeBallots = await this.votingToChangeKeysInstance.methods.validatorActiveBallots(_miningKey).call()
return _limitPerValidator - _activeBallots
}
}

View File

@ -18,6 +18,7 @@ export default class VotingToChangeMinThreshold {
)
this.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS
this.instance = this.votingToChangeMinThresholdInstance
}
//setters
@ -77,22 +78,10 @@ export default class VotingToChangeMinThreshold {
return null
}
getMiningByVotingKey(_votingKey) {
return this.votingToChangeMinThresholdInstance.methods.getMiningByVotingKey(_votingKey).call()
}
async getValidatorActiveBallots(_votingKey) {
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))
async getBallotLimit(_miningKey, _limitPerValidator) {
const _activeBallots = await this.votingToChangeMinThresholdInstance.methods
.validatorActiveBallots(_miningKey)
.call()
return _limitPerValidator - _activeBallots
}
}

View File

@ -15,6 +15,7 @@ export default class VotingToChangeProxy {
this.votingToChangeProxyInstance = new web3_10.eth.Contract(votingToChangeProxyABI, VOTING_TO_CHANGE_PROXY_ADDRESS)
this.gasPrice = web3_10.utils.toWei('1', 'gwei')
this.address = VOTING_TO_CHANGE_PROXY_ADDRESS
this.instance = this.votingToChangeProxyInstance
}
//setters
@ -74,22 +75,8 @@ export default class VotingToChangeProxy {
return null
}
getMiningByVotingKey(_votingKey) {
return this.votingToChangeProxyInstance.methods.getMiningByVotingKey(_votingKey).call()
}
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))
async getBallotLimit(_miningKey, _limitPerValidator) {
const _activeBallots = await this.votingToChangeProxyInstance.methods.validatorActiveBallots(_miningKey).call()
return _limitPerValidator - _activeBallots
}
}

View File

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

View File

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