Skip to content

Conversation

@nicholas-source
Copy link
Owner

Description:

This pull request introduces a comprehensive initial test suite for the EchoVote smart contract. The test suite covers key functionalities, including proposal creation, voting, ending proposals, and read-only functions. Additionally, beforeEach hooks are implemented to ensure a consistent blockchain state across tests, making them reliable and repeatable.

Details:

  1. Test Cases for create-proposal Function:

    • Ensures the contract owner can create proposals.
    • Validates that non-owners are restricted from creating proposals.
    • Includes validation of proposal parameters like title, description, and start block.
  2. Test Cases for vote Function:

    • Allows users to vote on active proposals.
    • Includes validation to prevent voting before the start block and after the end block.
    • Prevents double voting and invalid vote options.
  3. Test Cases for end-proposal Function:

    • Ensures only the contract owner can end proposals after the end block.
    • Prevents non-owners from ending proposals.
    • Validates that proposals cannot be ended before the designated end block.
  4. Test Cases for Read-Only Functions:

    • get-proposal: Validates correct proposal details are returned.
    • get-vote: Ensures the correct vote is returned for a user.
    • get-vote-count: Verifies the vote count for a specific option.
    • get-total-votes: Confirms the total votes for a given proposal.
  5. Additional Setup:

    • beforeEach hooks are added to reset the blockchain state before each test to ensure consistent results.
    • Preloads blockchain state with a proposal and votes for testing read-only functions.

Test Coverage:

  • Total Tests: 15
  • Passed: 11
  • Failed: 4

Summary of Failed Tests:

  1. vote: Prevents voting before the start block
  2. vote: Prevents voting after the end block
  3. end-proposal: Allows the contract owner to end a proposal after the end block
  4. get-proposal: Returns correct proposal details

These failing tests are related to boundary conditions and handling of proposal states. A deeper look into these test cases and the corresponding smart contract logic will be required to resolve the issues.

Next Steps:

  • Investigate and fix failing test cases.
  • Ensure proper state transitions and error handling for proposals.
  • Further enhance the test coverage for edge cases and error conditions.

File Changes:

  • tests/echovote.test.ts:
    • Added 15 test cases covering create-proposal, vote, end-proposal, and read-only functions.

This PR marks the initial step in ensuring robust and reliable functionality for the EchoVote smart contract. With thorough testing and future improvements, the system will offer a dependable and secure voting experience for users.

- Added contract-owner constant
- Added error constants
- Added proposal-count data variable
- Added proposals map
- Added votes map
- Added vote-counts map
- Added create-proposal function to create new proposals
- Includes validation for contract owner
- Updates proposal-count
- Added vote function to allow voting on proposals
- Includes validation for proposal existence, active status, and vote option range
- Updates votes and vote-counts maps
- Added get-proposal function to retrieve proposal details
- Added get-vote function to retrieve a specific vote
- Added get-vote-count function to retrieve vote count for an option
- Added get-total-votes function to retrieve total votes for a proposal
Refactor increment-total-votes function to use match expression
Refactor get-total-votes function to use match expression
This commit adds a new configuration file, `index.ts`, which exports an object containing various configuration options for the application. It also adds a new API endpoint in the `index.ts` file, which responds with a simple message to indicate that the EchoVote API is running.
- Added express import
- Added proposalService import
- Initialized express router
- Added POST route for creating proposals
- Handles request body and calls createProposal service
- Returns 201 status on success
- Returns 500 status on error
- Added createProposal function in proposalService
- Added GET route for fetching proposals by ID
- Handles request params and calls getProposal service
- Returns proposal data on success
- Returns 404 status if proposal not found
- Returns 500 status on error
- Added getProposal function in proposalService
- Improved variable naming
- Added comments for better understanding
- Structured code for better readability
- Fixed bug in proposal ID parsing
- Fixed bug in error handling
- Added validation for request body
- Ensured only authorized users can create proposals
- Reverted previous commit due to identified issues
…g, and counting votes

- Implemented POST route to cast a vote for a proposal, specifying proposalId and voteOption
- Added GET route to fetch a specific voter's vote on a given proposal
- Implemented additional routes to retrieve vote count by option and total votes for a proposal
- These routes integrate with Stacks smart contract functions via the service layer
- Added requestLogger middleware that logs the HTTP method, URL, and timestamp for each incoming request
- Helps with debugging and monitoring API traffic, providing visibility into system activity
- Added errorHandler middleware to catch and handle errors globally within the Express app
- In production mode, the stack trace is hidden to prevent leakage of sensitive information, while in development mode, full details are provided for easier debugging
- Added placeholder routes for proposal-related functionality, to be implemented in future iterations
- Allows the structure for proposals to be set up while focusing on vote functionality for now
…unction

- Refactored the logic that determines the network (mainnet/testnet) into a reusable utility function
- Reduces duplication and ensures consistency across different parts of the codebase that require network information
…outes

- Introduced input validation using `express-validator` to ensure that proposal IDs and vote options are valid integers and that voter addresses are well-formed
- Helps prevent malicious input and strengthens security by ensuring data integrity
…mode

- Modified the global error handler to only expose error messages in production mode, while stack traces are available in development mode
- Protects against potential security vulnerabilities by avoiding stack trace exposure in production environments
…emented

- Removed placeholder proposal route as it was not yet fully functional and created potential confusion in the codebase
- This will be reintroduced once the full proposal functionality has been developed
- Added test cases for create-proposal function.
- Added test cases for vote function.
- Added test cases for end-proposal function.
- Added test cases for read-only functions.
- Added beforeEach hook to reset blockchain state before each test.
- Ensured proposals are created before tests in vote and end-proposal describe blocks.
- Added test cases to validate proposal parameters.
- Ensured proper error handling for invalid title, description, and start block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants