Skip to content

Instantly share code, notes, and snippets.

@CJ42
Last active July 21, 2022 07:19
Show Gist options
  • Select an option

  • Save CJ42/5a2fa2b00931b25e45e1d61238ff5c4b to your computer and use it in GitHub Desktop.

Select an option

Save CJ42/5a2fa2b00931b25e45e1d61238ff5c4b to your computer and use it in GitHub Desktop.
explaining that elementary type copy and storage references do not in Solidity
pragma solidity ^0.8.0;
contract Voting {
uint256 votesCount;
struct Vote {
bool hasVoted;
string vote;
}
mapping(address => Vote) votes;
// ; opcodes
// PUSH1 00 ; push number 0 on the stack (for slot 0)
// SLOAD ; load value at storage slot 0 (= `votesCount`)
function getVotesCount() public view returns (uint256) {
uint256 currentVotesCount = votesCount;
return currentVotesCount;
}
// ; opcodes
// PUSH1 00
// PUSH1 01 ; push 1 on the stack (storage slot nb 1 for `votes`)
// PUSH1 00
// CALLER
// PUSH20 ffffffffffffffffffffffffffffffffffffffff
// AND
// PUSH20 ffffffffffffffffffffffffffffffffffffffff
// AND
// DUP2
// MSTORE
// PUSH1 20
// ADD
// SWAP1
// DUP2
// MSTORE
// PUSH1 20
// ADD
// PUSH1 00
// SHA3
// SWAP1
// POP
// PUSH1 01 ; push `true` for `hasVoted` onto the stack
// DUP2
// PUSH1 00
// ADD
// PUSH1 00
// PUSH2 0100
// EXP
// DUP2
// SLOAD ; load the value located at the storage reference
// DUP2
// PUSH1 ff
// MUL
// NOT
// AND
// SWAP1
// DUP4
// ISZERO
// ISZERO
// MUL
// OR
// SWAP1
// SSTORE ; update the storage by marking it `hasVoted`
function hasVoted() public {
Vote storage callerVoteDetails = votes[msg.sender];
callerVoteDetails.hasVoted = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment