Skip to content

Instantly share code, notes, and snippets.

@CJ42
Created April 12, 2022 22:18
Show Gist options
  • Save CJ42/bd20f58afb81dc1ce257d8f414e98a1d to your computer and use it in GitHub Desktop.
Save CJ42/bd20f58afb81dc1ce257d8f414e98a1d to your computer and use it in GitHub Desktop.
Bad example of loops in storage
pragma solidity ^0.8.0;
/// THIS IS STILL A NEGATIVE EXAMPLE! DO NOT USE!
contract Voting {
mapping(address => uint) voteWeight;
address[] yesVotes;
uint requiredWeight;
address beneficiary;
uint amount;
function voteYes() {
yesVotes.push(msg.sender);
}
function tallyVotes() {
uint yesVotes;
for (uint i = 0; i < yesVotes.length; ++i)
yesVotes += voteWeight[yesVotes[i]];
if (yesVotes > requiredWeight)
beneficiary.send(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment