The substrate-node-template-Voting is a blockchain node built on the Substrate framework, leveraging the Substrate Node Template. It has been enhanced with a custom voting pallet to enable decentralized voting functionality. This introduction will cover the core components and capabilities of this project, focusing on how the voting system has been integrated and how it operates within the Substrate ecosystem.
Substrate is a modular framework for building blockchain networks. It provides a highly customizable environment that allows developers to tailor their blockchain’s consensus, networking, and state transition functions. The Substrate Node Template is a pre-configured, ready-to-use Substrate node that serves as a starting point for building custom blockchain solutions.
As this project is built on the substrate-node-template, so all the build commands and how to run is same. Please see below link for basic info such as build, run ,etc . [https://github.com/substrate-developer-hub/substrate-node-template/blob/main/README.md]
-
Modular Design: Inherits the modular design of Substrate, allowing easy integration and customization of pallets (modules) to extend functionality.
-
Voting Pallet Integration: Includes a custom-built voting pallet that facilitates proposal creation, voting, and result finalization.
-
Decentralized Voting: Enables users to create proposals, cast votes, and view results in a decentralized manner, leveraging blockchain transparency and immutability.
-
Account Management: Uses Substrate’s robust account and balance management system to handle voter accounts and ensure secure transactions.
-
Event-Driven Architecture: Utilizes Substrate’s event system to log and handle important actions such as proposal creation and vote casting.
The custom voting pallet added to the substrate-node-template-Voting project introduces several key functionalities:
-
Proposal Creation: Users can create new proposals by submitting a description and specifying the voting duration. Each proposal is stored on-chain with a unique identifier.
-
Voting: Users can cast votes on active proposals. Each user is limited to one vote per proposal, ensuring fair voting practices.
-
Result Finalization: After the voting period ends, the results can be finalized. A proposal is concluded as approved if it receives a majority of "Yes" votes; otherwise, it is rejected.
-
Event Emission: The pallet emits events for proposal creation, voting actions, and result finalization, allowing easy tracking and auditing of the voting process.
- Ensure you have a local Substrate node running with your custom pallet integrated. Refer to [https://github.com/substrate-developer-hub/substrate-node-template/blob/main/README.md]
- Open the Polkadot.js app and connect it to your local node.
To enable accounts other than Alice, Alice-stash, Bob, Bob-stash to participate in voting, you need to transfer some balance to them.
-
Select Alice or Bob who have default balances.
-
Click on the Transfer button next to the account name.
-
Enter the account name you want to transfer to (e.g., charlie, dave).
-
Enter the amount of balance to transfer (e.g., 1000 units).
-
Click Make Transfer and confirm the transaction.
-
Go to Developer -> Extrinsics.
-
Select Alice (or any funded account) from the "using the selected account" dropdown.
-
In the "submit the following extrinsic" section:
-
Select "VotingModule" pallet .
-
Select the create_proposal function.
-
Enter the description (e.g., "Proposal 1") and duration (e.g., 10 blocks).
-
Click Submit Transaction and confirm.
-
Go to Developer -> Network -> Explorer -> Recent events section.
-
Select the event which corresponds to "votingModule.ProposalCreated"
-
Confirm the block number where this transaction is stored.
-
Notedown the corresponding H256. This is the uniqueId for proposal
-
The above unique id will be used for Vote and getPropsalResults
Example: 0x57697c5970983b431d34f39ce6ce5b9d2561391852eba4c2ca1cf3bc0e1226d3
-
Go to Developer -> Extrinsics.
-
Select an account from the "using the selected account" dropdown (e.g., Alice, Bob).
-
In the "submit the following extrinsic" section:
-
Select "VotingModule" pallet.
-
Select the vote function.
-
Enter the proposal_hash (you can find this hash in the events or storage) and vote (true for "Yes", false for "No").
-
Click Submit signed Transaction and confirm.
-
Repeat the above steps for other accounts like Bob, Charlie, Dave, etc who has balance.
After the voting period has ended (e.g., 10 blocks):
-
Go to Developer -> Extrinsics.
-
Select an account from the "using the selected account" dropdown (e.g., Alice).
-
In the "submit the following extrinsic" section:
-
Select your custom pallet (e.g., palletVoting).
-
Select the
getPropsalResults
function. -
Enter the proposal_hash of the proposal you want to get the results for.
-
Click Submit Transaction and confirm.
-
Select Alice.
-
Call create_proposal with description "Proposal 1" and duration 10.
-
Select Bob.
-
Call vote with the proposal_hash obtained from the proposal creation event and vote true.
-
Select Charlie.
-
Call vote with the same proposal_hash and vote false.
-
Wait for the voting period to end (e.g., 10 blocks).
-
Select Alice.
-
Call get_proposal_results with the proposal_hash.
-
To test each account votes only once per proposal, Try voting with same account on the same proposal 2 times
-
Cant vote after the duration: Try to vote on proposal whos duration is already completed.
-
Cant get the proposal results before the voting period ends : Try to call the get_proposal_result before the duration has ended.