CellFrame Voting Service
Voting is a service in the CellFrame Node that allows participants to conduct polls on the blockchain, ensuring that each vote is recorded transparently and securely. Voting in CellFrame is based on standard transactions with specific features to ensure the integrity of the voting process.
Example Code
This example demonstrates how to retrieve and display voting data using the CellFrame Voting module in Python.
from pycfhelpers.node.srv.vote import CFVotingOption, CFVoting
from pycfhelpers.logger import log
from pycfhelpers.node.net import CFNet
def init():
# Define the name of the network you want to retrieve voting data from.
net_name = "riemann"
RIEMANN_NET = CFNet(net_name)
# Retrieve the list of votings for the specified network.
votings = CFVoting.list(RIEMANN_NET)
# Log the number of votings found.
log.notice(f"Network {net_name} has {len(votings)} votings.")
# Iterate through each voting to display its details.
for voting in votings:
log.notice(f"Voting Hash: {voting.hash}")
log.notice(f"Question: {voting.question}")
log.notice(f"Expiration Time: {voting.expire}")
log.notice(f"Delegate Key Required: {voting.is_delegate_key_required}")
log.notice(f"Vote Changing Allowed: {voting.is_vote_changing_allowed}")
log.notice("Options:")
# Iterate through each voting option to display its details.
for option in voting.options:
log.notice(f" - Description: {option.description}")
log.notice(f" Votes: {option.votes}")
log.notice(f" Weights: {option.weights}")
log.notice(f" Transaction Hashes: {', '.join(option.hash_txs)}")
log.notice("\n")
return 0
Retrieving Voting Data
The CFVoting.list
method is called to retrieve a list of votings for the specified network. This method returns a list of CFVoting
objects, each representing a voting instance.
Logging Voting Details
The script logs the number of votings found in the network. It then iterates through each voting instance to log its details:
- Voting Hash: A unique identifier for the voting.
- Question: The question posed in the voting.
- Expiration Time: When the voting will expire.
- Delegate Key Required: Whether a delegate key is required to vote.
- Vote Changing Allowed: Whether changing votes is allowed.
For each voting, the script also logs the details of each voting option:
- Description: The description of the option.
- Votes: The number of votes received.
- Weights: The weight of the votes.
- Transaction Hashes: The transaction hashes associated with the votes.