7. Blockchain Permission Governance¶
Tags: “Contract Permissions” “Deployment Permissions” “Permission Control” “Permission Usage”
FISCO BCOS 3.x introduces the authority governance system of contract granularity。The governance committee can manage the deployment of the contract and the interface call permission of the contract by voting。
For detailed design, please refer to the link: Permission Management System Design
1. Enable permission governance mode¶
Before the blockchain is initialized and started, you must enable and set the permission governance configuration in the configuration to correctly start the permission governance mode。Reconfiguration after blockchain startup will not work。
To enable the permission governance mode, set the ‘is _ auth _ check’ option to ‘true’ and set the ‘auth _ admin _ account’ initial committee account address to the correct address。
Updated: After version 3.3, it will be supported to dynamically open the permission mode after the chain is started and deploy the governance committee contract。Please pay attention to the section of dynamic opening permission mode in this chapter。
Different node deployment modes of FISCO BCOS have slightly different ways to enable permission governance。This section will discuss separately how to turn on permission governance in different node deployment modes。
1.1 FISCO BCOS Air Edition Opens Permission Governance¶
FISCO BCOS Air version of the chain deployment tool details, please refer to: Air deployment tool。Take building four nodes as an example to enable permission governance settings。
The deployment tool has two modes, ‘-A’ and ‘-a’, to enable the permission mode:
Note: After version 3.3, the ‘build _ chain.sh’ script will turn on the permission mode by default and no longer have the ‘-A’ option. If you do not specify an account address, an account public-private key pair will be generated by default and placed in the ‘ca’ directory of the chain. For the creation and use of accounts, please refer to: Creating and Using Accounts。
-A: The permission setting will be enabled, and an account address will be randomly generated by using the ‘get _ account.sh’ and ‘get _ gm _ account.sh’ scripts, and the public-private key pair of the generated account will be placed in the ‘ca’ directory of the chain. For details about creating and using an account, see Creating and Using an Account‘-a’: will turn on permission settings and specify an account address as the only account to initialize the governance committeeWhen specifying, you must confirm that the account exists and that the account address is correct, otherwise permission governance will be unavailable because there is no governance committee authority。
1.1.1 Examples of enabling permission governance¶
Use the ‘-A’ option to enable permission mode. You can see that ‘Auth Mode’ is enabled and the initial account of ‘Auth init account’ is’ 0x976fe0c250181c7ef68a17d3bc34916978da103a ‘。
Note: After version 3.3, the ‘build _ chain.sh’ script will enable the permission mode by default. If the account address is not specified, an account public-private key pair will be generated by default and placed in the ‘ca’ directory of the chain. For the creation and use of accounts, please refer to: Creating and Using Accounts。
## If the -A option is used, the permission setting is turned on and an account address is randomly generated as the only admin account to initialize the governance committee
bash build_chain.sh -l 127.0.0.1:4 -o nodes -A
[INFO] Downloading fisco-bcos binary from https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/releases/v3.0.0/fisco-bcos-linux-x86_64.tar.gz ...
######################################################################## 100.0%
[INFO] Generate ca cert successfully!
Processing IP:127.0.0.1 Total:4
[INFO] Generate nodes/127.0.0.1/sdk cert successful!
[INFO] Generate nodes/127.0.0.1/node0/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node1/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node2/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node3/conf cert successful!
[INFO] Downloading get_account.sh from https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/tools/get_account.sh...
######################################################################## 100.0%
[INFO] Generate uuid success: 4cf39d8a-9f9f-43d0-baa4-3b89d6c9e013
[INFO] Generate uuid success: 16d14ff1-b3f0-4104-8b3e-1bb5faf0ee12
[INFO] Generate uuid success: cc90fc61-bd2f-4029-adaa-135543ec887e
[INFO] Generate uuid success: 12799030-e698-4edd-8719-b5aaa6f8f4c7
==============================================================
[INFO] GroupID : group0
[INFO] ChainID : chain0
[INFO] fisco-bcos path : bin/fisco-bcos
[INFO] Auth mode : true
[INFO] Auth account : 0x976fe0c250181c7ef68a17d3bc34916978da103a
[INFO] Start port : 30300 20200 3901
[INFO] Server IP : 127.0.0.1:4
[INFO] SM model : false
[INFO] Output dir : nodes
[INFO] All completed. Files in nodes
# Randomly generated accounts will be placed in the{Node Name}/ ca / accounts directory
ls nodes/ca/accounts
0x976fe0c250181c7ef68a17d3bc34916978da103a.pem 0x976fe0c250181c7ef68a17d3bc34916978da103a.public.pem
Use the ‘-a’ option to enable permission mode, specify the account address as the initial governance member, you can see that ‘Auth Mode’ is enabled, and the initial account of ‘Auth init account’ is’ 0x976fe0c250181c7ef68a17d3bc34916978da103a ‘
## If you use the -a option, turn on permission settings and specify the account address as the only admin account to initialize the governance committee
bash build_chain.sh -l 127.0.0.1:4 -o nodes -a 0x976fe0c250181c7ef68a17d3bc34916978da103a
[INFO] Generate ca cert successfully!
Processing IP:127.0.0.1 Total:4
[INFO] Generate nodes/127.0.0.1/sdk cert successful!
[INFO] Generate nodes/127.0.0.1/node0/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node1/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node2/conf cert successful!
[INFO] Generate nodes/127.0.0.1/node3/conf cert successful!
[INFO] Generate uuid success: 1b1a3ea8-cacf-498d-9609-6185500b724f
[INFO] Generate uuid success: e0099fb5-0a54-4073-9f67-077772187df6
[INFO] Generate uuid success: 5a8c6f09-1984-4bbc-9c9b-627d6a125985
[INFO] Generate uuid success: 688d7047-644b-48e3-b61b-6b4360b5ac2c
==============================================================
[INFO] GroupID : group0
[INFO] ChainID : chain0
[INFO] fisco-bcos path : bin/fisco-bcos
[INFO] Auth mode : true
[INFO] Auth account : 0x976fe0c250181c7ef68a17d3bc34916978da103a
[INFO] Start port : 30300 20200 3901
[INFO] Server IP : 127.0.0.1:4
[INFO] SM model : false
[INFO] Output dir : nodes
[INFO] All completed. Files in nodes
1.1.2 View node permission configuration¶
Whether permission governance is enabled with the ‘-A’ or ‘-a’ option, it is reflected in the configuration of each node。When the node starts initialization, it will read the configuration and initialize the permission contract。
Let’s take ‘nodes / 127.0.0.1 / node0 / config.genesis’ as an example:
...
[executor]
; use the wasm virtual machine or not
is_wasm=false
is_auth_check=true
auth_admin_account=0x976fe0c250181c7ef68a17d3bc34916978da103a
is_serial_execute=false
...
1.2 FISCO BCOS Pro / Max Edition Opens Permission Governance¶
FISCO BCOS Pro version of the build chain deployment tool details please refer to: build Pro version of the blockchain network。Take BcosBuilder as an example to enable permission governance settings。
Before enabling the Pro / Max blockchain network permission mode, ensure that [Deploy Pro Blockchain Node] has been completed(../tutorial/pro/installation.html#id4)All previous steps。
When copying a configuration file, you need to manually configure permissions to initialize the configuration。To copy a configuration file, refer to: Deploying RPC Services
# Enter config.toml
vim config.toml
Here we choose to enable the ‘auth _ check’ configuration item, and use the ‘0x976fe0c250181c7ef68a17d3bc34916978da103a’ account address as the initialization governance member, please refer to the actual situation。
How to create and use an on-chain account, please refer to the link: Create and use an account。
For more information, see tars service configuration
...
[group]
group_id="group0"
vm_type="evm"
sm_crypto=false
auth_check=true
init_auth_address="0x976fe0c250181c7ef68a17d3bc34916978da103a"
...
After completing the configuration items, you can continue to deploy RPC services, GateWay services, and node services。Continue process reference: Deploy RPC Service
1.3 Dynamic opening permissions¶
Note: This section applies only to binary and chain versions 3.3.0 and later。
After the blockchain is started, if the previous data of the blockchain has not been configured with the functions of the permission part, you can use the ‘initAuth’ command in the console to initialize the chain’s governance committee, and then use the ‘setSystemConfigByKey’ to enable permission checking。Specific steps are as follows:
When appointing the first governance member, you must confirm that the account exists and that the account address is correct, otherwise it will result in the inability to govern the authority because there is no governance member authority。
# First, you must upgrade the data version number of the chain to 3.3.0 and above. If it is already 3.3.0 or above, skip this step
[group0]: /apps> setSystemConfigByKey compatibility_version 3.3.0
{
"code":0,
"msg":"Success"
}
Switched to group group0.
# Initialize the governance committee using initAuth
[group0]: /apps> initAuth 0xf4a3d4177ce2bad732a63702a751a8e41ea6bce4
{
"code":0,
"msg":"Success"
}
# After restarting the console, view the governance board information
[group0]: /apps> getCommitteeInfo
---------------------------------------------------------------------------------------------
Committee address : 0xd04cfe0e4a8c9cafe504bbd68d665471d1ce5010
ProposalMgr address : 0x0fb8268e0f749097844639dff0fc938d44fa55ba
---------------------------------------------------------------------------------------------
ParticipatesRate: 0% , WinRate: 0%
---------------------------------------------------------------------------------------------
Governor Address | Weight
index0 : 0xf4a3d4177ce2bad732a63702a751a8e41ea6bce4 | 1
# At this point, the permission management committee has been initialized and permission checking has not been enabled. Use the setSystemConfigByKey command to enable the permission check
[group0]: /apps> setSystemConfigByKey auth_check_status 1
{
"code":0,
"msg":"Success"
}
1.4 Dynamic Close Permission Check¶
Note: This section applies only to binary and chain versions 3.3.0 and later。 This section describes the check that closes the authority, the governance committee is still available。
# Use the setSystemConfigByKey command to set the auth _ check _ status configuration to 0
[group0]: /apps> setSystemConfigByKey auth_check_status 0
{
"code":0,
"msg":"Success"
}
2. Console use¶
The console has commands dedicated to permission governance and commands to switch console accounts。You can use the console to manage permissions. For more information, see Permission command。The permission governance command will only appear if the console is connected to the node with permission governance enabled。
Console operation commands include the following three types. For details, see Permission Operation Commands:
Query status command, which has no permission control and is accessible to all accounts。
Governance Committee-specific orders, which can only be used if the account of the Governance Committee is held。
Contract administrator-specific commands, which can only be accessed by an administrator account with administrative privileges on a contract。
3. Use examples¶
First, use the build _ chain.sh script to build a blockchain with permission mode:
Reference here Creating and Using an Accountlink to create a new account, specifying that the account address of the initialization governance member is 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
You can use the ‘-A’ option to automatically generate an account。Accounts are distinguished between state and non-state secrets and are automatically generated based on the type of chain。
bash build_chain.sh -l 127.0.0.1:4 -o nodes4 -a 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
3.1 Use of governance members¶
Use the ‘getCommitteeInfo’ command to see that there is only one governance committee at initialization, with a weight of 1
And the account used in the current console is the member
[group0]: /> getCommitteeInfo
---------------------------------------------------------------------------------------------
Committee address : 0xcbc22a496c810dde3fa53c72f575ed024789b2cc
ProposalMgr address : 0xa0974646d4462913a36c986ea260567cf471db1f
---------------------------------------------------------------------------------------------
ParticipatesRate: 0% , WinRate: 0%
---------------------------------------------------------------------------------------------
Governor Address | Weight
index0 : 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642 | 1
[group0]: /> getCurrentAccount
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Initiate a proposal to update the governance committee with the current account:
As you can see, a proposal was launched, proposal number 1。
Because the current governance committee has only one member and both the participation threshold and the weight threshold are zero, the proposal initiated is certain to succeed。
Use the ‘getCommitteeInfo’ command to see that the weight of the governance committee has indeed been updated
[group0]: /> updateGovernorProposal 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642 2
Update governor proposal created, ID is: 1
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setWeight
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
You can also add new governance members using ‘updateGovernorProposal’:
Only length and character checks will be done here, not correctness checks。You can see the successful addition of a governance committee with a weight of 1
[group0]: /> updateGovernorProposal 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e 1
Update governor proposal created, ID is: 2
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setWeight
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
You can also use ‘updateGovernorProposal’ to delete governance members:
If the account weight is set to 0, the governance member is deleted
[group0]: /> updateGovernorProposal 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e 0
Update governor proposal created, ID is: 3
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setWeight
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> getCommitteeInfo
---------------------------------------------------------------------------------------------
Committee address : 0xcbc22a496c810dde3fa53c72f575ed024789b2cc
ProposalMgr address : 0xa0974646d4462913a36c986ea260567cf471db1f
---------------------------------------------------------------------------------------------
ParticipatesRate: 0% , WinRate: 0%
---------------------------------------------------------------------------------------------
Governor Address | Weight
index0 : 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642 | 2
Use the ‘setRateProposal’ command to change the voting threshold for governance members
[group0]: /> setRateProposal 51 51
Set rate proposal created, ID is: 4
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setRate
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> getCommitteeInfo
---------------------------------------------------------------------------------------------
Committee address : 0xcbc22a496c810dde3fa53c72f575ed024789b2cc
ProposalMgr address : 0xa0974646d4462913a36c986ea260567cf471db1f
---------------------------------------------------------------------------------------------
ParticipatesRate: 51% , WinRate: 51%
---------------------------------------------------------------------------------------------
Governor Address | Weight
index0 : 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642 | 2
At this point, the Commission’s participation rate must be greater than 51, the weighting threshold must also be greater than 51, and the Commission has two accounts
Use the current account to initiate the ‘setDeployAuthTypeProposal’ proposal, change the global deployment permission policy, and use the whitelist mode。
At this point, you can see that the type of the sixth proposal is’ setDeployAuthType ‘and the status is’ notEnoughVotes’. The proposal cannot be passed yet, and the current deployment permission policy is still in the no-policy state。
[group0]: /> setDeployAuthTypeProposal white_list
Set deploy auth type proposal created, ID is: 6
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setDeployAuthType
Proposal Status : notEnoughVotes
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> getDeployAuth
There is no deploy strategy, everyone can deploy contracts.
Switch to another committee account and vote on proposal 6, you can see that the vote was successful and the proposal status changed to end。Deployment policy also becomes whitelist mode。
[group0]: /> loadAccount 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
Load account 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e success!
[group0]: /> voteProposal 6
Vote proposal success.
---------------------------------------------------------------------------------------------
Proposer: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Proposal Type : setDeployAuthType
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> getDeployAuth
Deploy strategy is White List Access.
3.2 Deployment Permissions¶
Continue. The deployment permission of the current chain is in whitelist mode。
The governance committee does not have the permission to deploy, but the governance committee can initiate the deployment permission to open an account。
You can also initiate a proposal to turn off deployment permissions through the command ‘closeDeployAuthProposal’
[group0]: /> deploy HelloWorld
deploy contract for HelloWorld failed!
return message: Permission denied
return code:18
Return values:null
[group0]: /> openDeployAuthProposal 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Open deploy auth proposal created, ID is: 7
---------------------------------------------------------------------------------------------
Proposer: 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
Proposal Type : modifyDeployAuth
Proposal Status : notEnoughVotes
---------------------------------------------------------------------------------------------
Agree Voters:
0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> loadAccount 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Load account 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642 success!
[group0]: /> voteProposal 7
Vote proposal success.
---------------------------------------------------------------------------------------------
Proposer: 0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
Proposal Type : modifyDeployAuth
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0xba0cd3e729cfe3ebdf1f74a10ec237bfd3954e1e
0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /> checkDeployAuth
Deploy : ACCESS
Account: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
[group0]: /> deploy HelloWorld
transaction hash: 0xe38ca3d69efee66aaf7de5600d3bdada8fd7c658d52bc0401ce65dd7e6437d97
contract address: 0x33E56a083e135936C1144960a708c43A661706C0
currentAccount: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
[group0]: /> getContractAdmin 0x33E56a083e135936C1144960a708c43A661706C0
Admin for contract 0x33E56a083e135936C1144960a708c43A661706C0 is: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
As you can see, by voting, the ‘0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642’ account already has permission to deploy。
At this point, the HelloWorld contract administrator for address 0x33E56a083e135936C1144960a708c43A661706C0 is the ‘0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642’ account
3.3 Contract Administrator Use¶
The contract administrator of the current HelloWorld contract 0x33E56a083e135936C1144960a708c43A661706C0 is the ‘0xab835e87a86f94af10c81278bb9a82ea13d82d39’ account
The contract administrator can set the interface policy for the current contract:
The contract administrator’s “set” to the HelloWorld contract(string)”The contract sets the whitelist mode, and after the setting is successful, the administrator does not have permission to call set(string)Interface
[group0]: /> getContractAdmin 0x33E56a083e135936C1144960a708c43A661706C0
Admin for contract 0x33E56a083e135936C1144960a708c43A661706C0 is: 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
[group0]: /> setMethodAuth 0x33E56a083e135936C1144960a708c43A661706C0 "set(string)" white_list
{
"code":0,
"msg":"Success"
}
[group0]: /> checkMethodAuth 0x33E56a083e135936C1144960a708c43A661706C0 "set(string)"
Method : PERMISSION DENIED
Account : 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
Interface: set(string)
Contract : 0x33E56a083e135936C1144960a708c43A661706C0
[group0]: /> call HelloWorld 0x33E56a083e135936C1144960a708c43A661706C0 set oops
transaction hash: 0xf9fcac9a0d4503e366c582c72fccf4e571b081ba44d46f58ff0b17fb7ab4361b
---------------------------------------------------------------------------------------------
transaction status: 18
---------------------------------------------------------------------------------------------
Receipt message: Permission denied
Return message: Permission denied
---------------------------------------------------------------------------------------------
# You can still use the set interface
[group0]: /> call HelloWorld 0x33E56a083e135936C1144960a708c43A661706C0 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return value size:1
Return types: (string)
Return values:(Hello, World!)
---------------------------------------------------------------------------------------------
Administrators can use the command to open an account to set(string)permission to use the interface
[group0]: /> openMethodAuth 0x33E56a083e135936C1144960a708c43A661706C0 "set(string)" 0x1cc06388cd8a12dcf7fb8967378c0aea4e6cf642
{
"code":0,
"msg":"Success"
}
[group0]: /> call HelloWorld 0x33E56a083e135936C1144960a708c43A661706C0 set "May the flame guide thee."
transaction hash: 0x3986a27e3075d703bc6828984bbfa8115fad612eaecaf7749e835edb495f38d6
---------------------------------------------------------------------------------------------
transaction status: 0
description: transaction executed successfully
---------------------------------------------------------------------------------------------
Receipt message: Success
Return message: Success
Return value size:0
Return types: ()
Return values:()
---------------------------------------------------------------------------------------------
Event logs
Event: {}
[group0]: /> call HelloWorld 0x33E56a083e135936C1144960a708c43A661706C0 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return value size:1
Return types: (string)
Return values:(May the flame guide thee.)
---------------------------------------------------------------------------------------------
3.4 Upgrade Permission Voting Rights Recalculation Logic¶
Initiate a proposal to upgrade the logic of voting calculations。The upgrade proposal vote calculation logic is divided into the following steps:
Write contracts based on interfaces;
Deploy the written contract on the chain and get the address of the contract;
Initiate a proposal to upgrade the voting calculation logic, enter the address of the contract as a parameter, and vote on it in the governance committee;
After the vote is passed (the voting calculation logic is still the original logic at this time), the voting calculation logic is upgraded;Otherwise do not upgrade。
The voting calculation logic contract can only be used according to a certain interface implementation。For contract implementation, see the following interface contract ‘VoteComputerTemplate.sol’:
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
import "./Committee.sol";
import "./BasicAuth.sol";
abstract contract VoteComputerTemplate is BasicAuth {
// Governors and threshold
Committee public _committee;
constructor(address committeeMgrAddress, address committeeAddress) {
setOwner(committeeMgrAddress);
_committee = Committee(committeeAddress);
// first, test committee exist; second, test committee is helthy
require(
_committee.getWeights() >= 1,
"committee is error, please check address!"
);
}
/ / This is the only entry to the voting rights recalculation logic. You must implement this interface and specify:
/ / Not enough votes, return 1;Vote passed, return 2;Vote not passed, return 3;
function determineVoteResult(
address[] memory agreeVoters,
address[] memory againstVoters
) public view virtual returns (uint8);
/ / This is a verification interface for computational logic for other governance members to verify the validity of the contract
function voteResultCalc(
uint32 agreeVotes,
uint32 doneVotes,
uint32 allVotes,
uint8 participatesRate,
uint8 winRate
) public pure virtual returns (uint8);
}
The existing contracts based on the above ‘VoteComputerTemplate.sol’ interface implementation are as follows:
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.6.10 <0.8.20;
import "./Committee.sol";
import "./VoteComputerTemplate.sol";
contract VoteComputer is VoteComputerTemplate {
constructor(address committeeMgrAddress, address committeeAddress)
public
VoteComputerTemplate(committeeMgrAddress, committeeAddress)
{}
/ / Voting Rights Recalculation Logic Implementation
function determineVoteResult(
address[] memory agreeVoters,
address[] memory againstVoters
) public view override returns (uint8) {
uint32 agreeVotes = _committee.getWeights(agreeVoters);
uint32 doneVotes = agreeVotes + _committee.getWeights(againstVoters);
uint32 allVotes = _committee.getWeights();
return
voteResultCalc(
agreeVotes,
doneVotes,
allVotes,
_committee._participatesRate(),
_committee._winRate()
);
}
/ / Implementation of verification interface for calculation logic
function voteResultCalc(
uint32 agreeVotes,
uint32 doneVotes,
uint32 allVotes,
uint8 participatesRate,
uint8 winRate
) public pure override returns (uint8) {
//1. Checks enough voters: totalVotes/totalVotesPower >= p_rate/100
if (doneVotes * 100 < allVotes * participatesRate) {
//not enough voters, need more votes
return 1;
}
//2. Checks whether for votes wins: agreeVotes/totalVotes >= win_rate/100
if (agreeVotes * 100 >= winRate * doneVotes) {
return 2;
} else {
return 3;
}
}
}
Once the contract is written, it can be deployed on-chain and updated to the governance committee:
# First, confirm that the address of the Committee contract is 0xa0974646d4462913a36c986ea260567cf471db1f through the getCommitteeInfo command
[group0]: /apps> getCommitteeInfo
---------------------------------------------------------------------------------------------
Committee address : 0xa0974646d4462913a36c986ea260567cf471db1f
ProposalMgr address : 0x2568bd207f50455f1b933220d0aef11be8d096b2
---------------------------------------------------------------------------------------------
ParticipatesRate: 0% , WinRate: 0%
---------------------------------------------------------------------------------------------
Governor Address | Weight
index0 : 0x4a37eba43c66df4b8394abdf8b239e3381ea4221 | 2
# Deploy the VoteComputer contract. The first parameter 0x10001 is a fixed address, and the second parameter is the address of the current governance committee member Committee
[group0]: /apps> deploy VoteComputer 0x10001 0xa0974646d4462913a36c986ea260567cf471db1f
transaction hash: 0x429a7ceccefb3a4a1649599f18b60cac1af040cd86bb8283b9aab68f0ab35ae4
contract address: 0x6EA6907F036Ff456d2F0f0A858Afa9807Ff4b788
currentAccount: 0x4a37eba43c66df4b8394abdf8b239e3381ea4221
# After successful deployment, update via upgradeVoteProposal
[group0]: /apps> upgradeVoteProposal 0x6EA6907F036Ff456d2F0f0A858Afa9807Ff4b788
Upgrade vote computer proposal created, ID is: 10
---------------------------------------------------------------------------------------------
Proposer: 0x4a37eba43c66df4b8394abdf8b239e3381ea4221
Proposal Type : upgradeVoteCalc
Proposal Status : finished
---------------------------------------------------------------------------------------------
Agree Voters:
0x4a37eba43c66df4b8394abdf8b239e3381ea4221
---------------------------------------------------------------------------------------------
Against Voters:
[group0]: /apps>