Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gwyneth develop #24

Draft
wants to merge 23 commits into
base: v43-gwyneth
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion bins/revm-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
bytes = "1.7"
hex = "0.4"
revm = { path = "../../crates/revm", version = "14.0.1", default-features=false }
revm = { path = "../../crates/revm", version = "14.0.1" }
microbench = "0.5"
alloy-sol-macro = "0.8.0"
alloy-sol-types = "0.8.0"
Expand All @@ -26,3 +26,12 @@ name = "transfer"

[[bin]]
name = "burntpix"

[[bin]]
name = "xTransfer"

[[bin]]
name = "xOwner"

[[bin]]
name = "xBallot"
187 changes: 187 additions & 0 deletions bins/revm-test/contracts/Ballot.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.12 <0.9.0;

/**
* @title Ballot
* @dev Implements voting process along with vote delegation
*/
contract Ballot {

struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}

struct Proposal {
// If you can limit the length to a certain number of bytes,
// always use one of bytes1 to bytes32 because they are much cheaper
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}

address public chairperson;

mapping(address => Voter) public voters;

Proposal[] public proposals;

/**
* @dev Create a new ballot to choose one of 'proposalNames'.
* @param proposalNames names of proposals
*/
constructor(bytes32[] memory proposalNames) {
init(proposalNames);
}

function init(bytes32[] memory proposalNames) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;

for (uint i = 0; i < proposalNames.length; i++) {
// 'Proposal({...})' creates a temporary
// Proposal object and 'proposals.push(...)'
// appends it to the end of 'proposals'.
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
}

/**
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'.
* @param voter address of voter
*/
function giveRightToVote(address voter) public {
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}

/**
* @dev Delegate your vote to the voter 'to'.
* @param to address to which vote is delegated
*/
function delegate(address to) public {
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");

while (voters[to].delegate != address(0)) {
to = voters[to].delegate;

// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
sender.voted = true;
sender.delegate = to;
Voter storage delegate_ = voters[to];
if (delegate_.voted) {
// If the delegate already voted,
// directly add to the number of votes
proposals[delegate_.vote].voteCount += sender.weight;
} else {
// If the delegate did not vote yet,
// add to her weight.
delegate_.weight += sender.weight;
}
}

/**
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'.
* @param proposal index of proposal in the proposals array
*/
function vote(uint256 proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = proposal;

// If 'proposal' is out of the range of the array,
// this will throw automatically and revert all
// changes.
proposals[proposal].voteCount += sender.weight;
}

/**
* @dev Computes the winning proposal taking all previous votes into account.
* @return winningProposal_ index of winning proposal in the proposals array
*/
function winningProposal() public view returns (uint winningProposal_)
{
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}

/**
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then
* @return winnerName_ the name of the winner
*/
function winnerName() public view returns (bytes32 winnerName_)
{
winnerName_ = proposals[winningProposal()].name;
}
}

contract BallotState {

struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}

struct Proposal {
// If you can limit the length to a certain number of bytes,
// always use one of bytes1 to bytes32 because they are much cheaper
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}

address public chairperson;

mapping(address => Voter) public voters;

Proposal[] public proposals;

constructor(bytes32[] memory proposalNames) {
EVM.xCallOnL1(true);
(bool success, ) = address(this).delegatecall(abi.encodeWithSignature("init(bytes32[])", proposalNames));
require(success, "Init delegate call failed");
}

function giveRightToVote(address voter) public {
EVM.xCallOnL1(true);
(bool success, ) = address(this).delegatecall(abi.encodeWithSignature("giveRightToVote(address)", voter));
require(success, "GiveRightToVote delegate call failed");
}

function delegate(address to) public {
EVM.xCallOnL1(true);
(bool success, ) = address(this).delegatecall(abi.encodeWithSignature("delegate(address)", to));
require(success, "Delegate delegate call failed");
}

function vote(uint256 proposal) public {
EVM.xCallOnL1(true);
(bool success, ) = address(this).delegatecall(abi.encodeWithSignature("vote(uint256)", proposal));
require(success, "Vote delegate call failed");
}
}
87 changes: 87 additions & 0 deletions bins/revm-test/contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

import "./EVM.sol";


contract ERC20 {

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

constructor(uint256 totalSupply) {
balanceOf[msg.sender] = totalSupply;
}

// ============ Transfer ============

function transfer(address to, uint256 value) public returns (uint256) {
require(balanceOf[msg.sender] >= value, "Insufficient balance");
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
emit Transfer(msg.sender, to, value);
return value;
}

function _transfer(address from, address to, uint256 value) public returns (uint256) {
require(msg.sender == address(this), "Only this contract can mint");
balanceOf[from] -= value;
balanceOf[to] += value;
return value;
}

function _mint(address to, uint256 value) public returns (uint256) {
require(msg.sender == address(this), "Only this contract can mint");
balanceOf[to] += value;
return value;
}

function xTransfer(uint256 chain, address to, uint256 value) public returns (uint256) {
balanceOf[msg.sender] -= value;
EVM.xCallOptions(chain);
return this._mint(to, value);
}

function sandboxedTransfer(uint256 chain, address to, uint256 value) public returns (uint256) {
EVM.xCallOptions(chain, true);
return this._transfer(msg.sender, to, value);
}

// ============ Approve ============

function approve(address spender, uint256 value) public returns (uint256) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return value;
}

function _approve(address owner, address spender, uint256 value) public returns (uint256) {
require(msg.sender == address(this), "Only contract itself can call this function");
allowance[owner][spender] = value;
emit Approval(owner, spender, value);
return value;
}

function xApprove(uint256 chain, address spender, uint256 value) public returns (uint256) {
EVM.xCallOptions(chain);
return this._approve(msg.sender, spender, value);
}

function transferFrom(address from, address to, uint256 value) public returns (uint256) {
require(balanceOf[from] >= value, "Insufficient balance");
if (from != msg.sender) {
require(allowance[from][msg.sender] >= value, "Allowance exceeded");
}
balanceOf[from] -= value;
balanceOf[to] += value;
if (from != msg.sender) {
allowance[from][msg.sender] -= value;
}
emit Transfer(from, to, value);
return value;
}
}
71 changes: 71 additions & 0 deletions bins/revm-test/contracts/EVM.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.12 <0.9.0;
// EVM library
library EVM {
address constant xCallOptionsAddress = address(0x04D2);

uint constant l1ChainId = 1;
uint16 constant version = 1;

function xCallOnL1()
internal
view
{
xCallOptions(l1ChainId);
}

function xCallOnL1(bool sandbox)
internal
view
{
xCallOptions(l1ChainId, sandbox);
}

function xCallOptions(uint chainID)
internal
view
{
xCallOptions(chainID, false);
}

function xCallOptions(uint chainID, bool sandbox)
internal
view
{
xCallOptions(chainID, sandbox, tx.origin, address(this));
}

function xCallOptions(uint chainID, bool sandbox, address txOrigin, address msgSender)
internal
view
{
xCallOptions(chainID, sandbox, txOrigin, msgSender, 0x0, "");
}

function xCallOptions(uint chainID, bool sandbox, bytes32 blockHash, bytes memory proof)
internal
view
{
xCallOptions(chainID, sandbox, address(0), address(0), blockHash, proof);
}

function xCallOptions(uint chainID, bool sandbox, address txOrigin, address msgSender, bytes32 blockHash, bytes memory proof)
internal
view
{
// require(chainID() != l1ChainId);

bytes memory input = abi.encodePacked(version, uint64(chainID), sandbox, txOrigin, msgSender, blockHash, proof);
(bool success, ) = xCallOptionsAddress.staticcall(input); //delegatecall
require(success);
}

function isOnL1() internal view returns (bool) {
return chainId() == l1ChainId;
}

function chainId() internal view returns (uint256) {
return block.chainid;
}
}
Loading