Fixed up formatting

This commit is contained in:
Denton Liu 2016-05-05 15:31:00 -04:00
parent 88bb63a507
commit ffade949de

View File

@ -39,18 +39,17 @@ of votes.
:: ::
/// @title Voting with delegation. /// @title Voting with delegation.
contract Ballot contract Ballot {
{
// This declares a new complex type which will // This declares a new complex type which will
// be used for variables later. // be used for variables later.
// It will represent a single voter. // It will represent a single voter.
struct Voter struct Voter {
{
uint weight; // weight is accumulated by delegation uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted bool voted; // if true, that person already voted
address delegate; // person delegated to address delegate; // person delegated to
uint vote; // index of the voted proposal uint vote; // index of the voted proposal
} }
// This is a type for a single proposal. // This is a type for a single proposal.
struct Proposal struct Proposal
{ {
@ -59,21 +58,23 @@ of votes.
} }
address public chairperson; address public chairperson;
// This declares a state variable that // This declares a state variable that
// stores a `Voter` struct for each possible address. // stores a `Voter` struct for each possible address.
mapping(address => Voter) public voters; mapping(address => Voter) public voters;
// A dynamically-sized array of `Proposal` structs. // A dynamically-sized array of `Proposal` structs.
Proposal[] public proposals; Proposal[] public proposals;
/// Create a new ballot to choose one of `proposalNames`. /// Create a new ballot to choose one of `proposalNames`.
function Ballot(bytes32[] proposalNames) function Ballot(bytes32[] proposalNames) {
{
chairperson = msg.sender; chairperson = msg.sender;
voters[chairperson].weight = 1; voters[chairperson].weight = 1;
// For each of the provided proposal names, // For each of the provided proposal names,
// create a new proposal object and add it // create a new proposal object and add it
// to the end of the array. // to the end of the array.
for (uint i = 0; i < proposalNames.length; i++) for (uint i = 0; i < proposalNames.length; i++) {
// `Proposal({...})` creates a temporary // `Proposal({...})` creates a temporary
// Proposal object and `proposal.push(...)` // Proposal object and `proposal.push(...)`
// appends it to the end of `proposals`. // appends it to the end of `proposals`.
@ -81,60 +82,68 @@ of votes.
name: proposalNames[i], name: proposalNames[i],
voteCount: 0 voteCount: 0
})); }));
}
} }
// Give `voter` the right to vote on this ballot. // Give `voter` the right to vote on this ballot.
// May only be called by `chairperson`. // May only be called by `chairperson`.
function giveRightToVote(address voter) function giveRightToVote(address voter) {
{ if (msg.sender != chairperson || voters[voter].voted) {
if (msg.sender != chairperson || voters[voter].voted)
// `throw` terminates and reverts all changes to // `throw` terminates and reverts all changes to
// the state and to Ether balances. It is often // the state and to Ether balances. It is often
// a good idea to use this if functions are // a good idea to use this if functions are
// called incorrectly. But watch out, this // called incorrectly. But watch out, this
// will also consume all provided gas. // will also consume all provided gas.
throw; throw;
}
voters[voter].weight = 1; voters[voter].weight = 1;
} }
/// Delegate your vote to the voter `to`. /// Delegate your vote to the voter `to`.
function delegate(address to) function delegate(address to) {
{
// assigns reference // assigns reference
Voter sender = voters[msg.sender]; Voter sender = voters[msg.sender];
if (sender.voted) if (sender.voted)
throw; throw;
// Forward the delegation as long as // Forward the delegation as long as
// `to` also delegated. // `to` also delegated.
while (voters[to].delegate != address(0) && while (voters[to].delegate != address(0) &&
voters[to].delegate != msg.sender) voters[to].delegate != msg.sender) {
to = voters[to].delegate; to = voters[to].delegate;
}
// We found a loop in the delegation, not allowed. // We found a loop in the delegation, not allowed.
if (to == msg.sender) if (to == msg.sender) {
throw; throw;
}
// Since `sender` is a reference, this // Since `sender` is a reference, this
// modifies `voters[msg.sender].voted` // modifies `voters[msg.sender].voted`
sender.voted = true; sender.voted = true;
sender.delegate = to; sender.delegate = to;
Voter delegate = voters[to]; Voter delegate = voters[to];
if (delegate.voted) if (delegate.voted) {
// If the delegate already voted, // If the delegate already voted,
// directly add to the number of votes // directly add to the number of votes
proposals[delegate.vote].voteCount += sender.weight; proposals[delegate.vote].voteCount += sender.weight;
else }
else {
// If the delegate did not vote yet, // If the delegate did not vote yet,
// add to her weight. // add to her weight.
delegate.weight += sender.weight; delegate.weight += sender.weight;
}
} }
/// Give your vote (including votes delegated to you) /// Give your vote (including votes delegated to you)
/// to proposal `proposals[proposal].name`. /// to proposal `proposals[proposal].name`.
function vote(uint proposal) function vote(uint proposal) {
{
Voter sender = voters[msg.sender]; Voter sender = voters[msg.sender];
if (sender.voted) throw; if (sender.voted)
throw;
sender.voted = true; sender.voted = true;
sender.vote = proposal; sender.vote = proposal;
// If `proposal` is out of the range of the array, // If `proposal` is out of the range of the array,
// this will throw automatically and revert all // this will throw automatically and revert all
// changes. // changes.
@ -147,10 +156,8 @@ of votes.
returns (uint winningProposal) returns (uint winningProposal)
{ {
uint winningVoteCount = 0; uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) for (uint p = 0; p < proposals.length; p++) {
{ if (proposals[p].voteCount > winningVoteCount) {
if (proposals[p].voteCount > winningVoteCount)
{
winningVoteCount = proposals[p].voteCount; winningVoteCount = proposals[p].voteCount;
winningProposal = p; winningProposal = p;
} }
@ -223,8 +230,10 @@ activate themselves.
/// Create a simple auction with `_biddingTime` /// Create a simple auction with `_biddingTime`
/// seconds bidding time on behalf of the /// seconds bidding time on behalf of the
/// beneficiary address `_beneficiary`. /// beneficiary address `_beneficiary`.
function SimpleAuction(uint _biddingTime, function SimpleAuction(
address _beneficiary) { uint _biddingTime,
address _beneficiary
) {
beneficiary = _beneficiary; beneficiary = _beneficiary;
auctionStart = now; auctionStart = now;
biddingTime = _biddingTime; biddingTime = _biddingTime;
@ -238,16 +247,19 @@ activate themselves.
// No arguments are necessary, all // No arguments are necessary, all
// information is already part of // information is already part of
// the transaction. // the transaction.
if (now > auctionStart + biddingTime) if (now > auctionStart + biddingTime) {
// Revert the call if the bidding // Revert the call if the bidding
// period is over. // period is over.
throw; throw;
if (msg.value <= highestBid) }
if (msg.value <= highestBid) {
// If the bid is not higher, send the // If the bid is not higher, send the
// money back. // money back.
throw; throw;
if (highestBidder != 0) }
if (highestBidder != 0) {
highestBidder.send(highestBid); highestBidder.send(highestBid);
}
highestBidder = msg.sender; highestBidder = msg.sender;
highestBid = msg.value; highestBid = msg.value;
HighestBidIncreased(msg.sender, msg.value); HighestBidIncreased(msg.sender, msg.value);
@ -261,6 +273,7 @@ activate themselves.
if (ended) if (ended)
throw; // this function has already been called throw; // this function has already been called
AuctionEnded(highestBidder, highestBid); AuctionEnded(highestBidder, highestBid);
// We send all the money we have, because some // We send all the money we have, because some
// of the refunds might have failed. // of the refunds might have failed.
beneficiary.send(this.balance); beneficiary.send(this.balance);
@ -319,13 +332,12 @@ high or low invalid bids.
:: ::
contract BlindAuction contract BlindAuction {
{ struct Bid {
struct Bid
{
bytes32 blindedBid; bytes32 blindedBid;
uint deposit; uint deposit;
} }
address public beneficiary; address public beneficiary;
uint public auctionStart; uint public auctionStart;
uint public biddingEnd; uint public biddingEnd;
@ -346,10 +358,11 @@ high or low invalid bids.
modifier onlyBefore(uint _time) { if (now >= _time) throw; _ } modifier onlyBefore(uint _time) { if (now >= _time) throw; _ }
modifier onlyAfter(uint _time) { if (now <= _time) throw; _ } modifier onlyAfter(uint _time) { if (now <= _time) throw; _ }
function BlindAuction(uint _biddingTime, function BlindAuction(
uint _revealTime, uint _biddingTime,
address _beneficiary) uint _revealTime,
{ address _beneficiary
) {
beneficiary = _beneficiary; beneficiary = _beneficiary;
auctionStart = now; auctionStart = now;
biddingEnd = now + _biddingTime; biddingEnd = now + _biddingTime;
@ -377,29 +390,38 @@ high or low invalid bids.
/// Reveal your blinded bids. You will get a refund for all /// Reveal your blinded bids. You will get a refund for all
/// correctly blinded invalid bids and for all bids except for /// correctly blinded invalid bids and for all bids except for
/// the totally highest. /// the totally highest.
function reveal(uint[] _values, bool[] _fake, function reveal(
bytes32[] _secret) uint[] _values,
bool[] _fake,
bytes32[] _secret
)
onlyAfter(biddingEnd) onlyAfter(biddingEnd)
onlyBefore(revealEnd) onlyBefore(revealEnd)
{ {
uint length = bids[msg.sender].length; uint length = bids[msg.sender].length;
if (_values.length != length || _fake.length != length || if (
_secret.length != length) _values.length != length
|| _fake.length != length
|| _secret.length != length
) {
throw; throw;
}
uint refund; uint refund;
for (uint i = 0; i < length; i++) for (uint i = 0; i < length; i++) {
{
var bid = bids[msg.sender][i]; var bid = bids[msg.sender][i];
var (value, fake, secret) = var (value, fake, secret) =
(_values[i], _fake[i], _secret[i]); (_values[i], _fake[i], _secret[i]);
if (bid.blindedBid != sha3(value, fake, secret)) if (bid.blindedBid != sha3(value, fake, secret)) {
// Bid was not actually revealed. // Bid was not actually revealed.
// Do not refund deposit. // Do not refund deposit.
continue; continue;
}
refund += bid.deposit; refund += bid.deposit;
if (!fake && bid.deposit >= value) if (!fake && bid.deposit >= value) {
if (placeBid(msg.sender, value)) if (placeBid(msg.sender, value))
refund -= value; refund -= value;
}
// Make it impossible for the sender to re-claim // Make it impossible for the sender to re-claim
// the same deposit. // the same deposit.
bid.blindedBid = 0; bid.blindedBid = 0;
@ -413,11 +435,13 @@ high or low invalid bids.
function placeBid(address bidder, uint value) internal function placeBid(address bidder, uint value) internal
returns (bool success) returns (bool success)
{ {
if (value <= highestBid) if (value <= highestBid) {
return false; return false;
if (highestBidder != 0) }
if (highestBidder != 0) {
// Refund the previously highest bidder. // Refund the previously highest bidder.
highestBidder.send(highestBid); highestBidder.send(highestBid);
}
highestBid = value; highestBid = value;
highestBidder = bidder; highestBidder = bidder;
return true; return true;
@ -428,7 +452,8 @@ high or low invalid bids.
function auctionEnd() function auctionEnd()
onlyAfter(revealEnd) onlyAfter(revealEnd)
{ {
if (ended) throw; if (ended)
throw;
AuctionEnded(highestBidder, highestBid); AuctionEnded(highestBidder, highestBid);
// We send all the money we have, because some // We send all the money we have, because some
// of the refunds might have failed. // of the refunds might have failed.
@ -436,7 +461,9 @@ high or low invalid bids.
ended = true; ended = true;
} }
function () { throw; } function () {
throw;
}
} }
.. index:: purchase, remote purchase, escrow .. index:: purchase, remote purchase, escrow
@ -449,39 +476,39 @@ Safe Remote Purchase
:: ::
contract Purchase contract Purchase {
{
uint public value; uint public value;
address public seller; address public seller;
address public buyer; address public buyer;
enum State { Created, Locked, Inactive } enum State { Created, Locked, Inactive }
State public state; State public state;
function Purchase()
{ function Purchase() {
seller = msg.sender; seller = msg.sender;
value = msg.value / 2; value = msg.value / 2;
if (2 * value != msg.value) throw; if (2 * value != msg.value) throw;
} }
modifier require(bool _condition)
{ modifier require(bool _condition) {
if (!_condition) throw; if (!_condition) throw;
_ _
} }
modifier onlyBuyer()
{ modifier onlyBuyer() {
if (msg.sender != buyer) throw; if (msg.sender != buyer) throw;
_ _
} }
modifier onlySeller()
{ modifier onlySeller() {
if (msg.sender != seller) throw; if (msg.sender != seller) throw;
_ _
} }
modifier inState(State _state)
{ modifier inState(State _state) {
if (state != _state) throw; if (state != _state) throw;
_ _
} }
event aborted(); event aborted();
event purchaseConfirmed(); event purchaseConfirmed();
event itemReceived(); event itemReceived();
@ -497,6 +524,7 @@ Safe Remote Purchase
seller.send(this.balance); seller.send(this.balance);
state = State.Inactive; state = State.Inactive;
} }
/// Confirm the purchase as buyer. /// Confirm the purchase as buyer.
/// Transaction has to include `2 * value` ether. /// Transaction has to include `2 * value` ether.
/// The ether will be locked until confirmReceived /// The ether will be locked until confirmReceived
@ -509,6 +537,7 @@ Safe Remote Purchase
buyer = msg.sender; buyer = msg.sender;
state = State.Locked; state = State.Locked;
} }
/// Confirm that you (the buyer) received the item. /// Confirm that you (the buyer) received the item.
/// This will release the locked ether. /// This will release the locked ether.
function confirmReceived() function confirmReceived()
@ -520,7 +549,10 @@ Safe Remote Purchase
seller.send(this.balance); seller.send(this.balance);
state = State.Inactive; state = State.Inactive;
} }
function() { throw; }
function() {
throw;
}
} }
******************** ********************