Skip to content

Commit

Permalink
Merge pull request #92 from makoto/no-return
Browse files Browse the repository at this point in the history
No return
  • Loading branch information
makoto authored Oct 8, 2017
2 parents 6a64981 + 9fee714 commit 482467b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 73 deletions.
72 changes: 21 additions & 51 deletions contracts/Conference.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,13 @@ contract Conference is Destructible {
event ClearEvent(address addr, uint256 leftOver);

/* Modifiers */
modifier sentDepositOrReturn {
if (msg.value == deposit) {
_;
}else{
assert(msg.sender.send(msg.value));
}
}

modifier onlyActive {
if (ended == false) {
_;
}
}

modifier onlyActiveOrReturn {
if (ended == false) {
_;
}else{
assert(msg.sender.send(msg.value));
}
}

modifier withinLimitOrReturn {
if (registered < limitOfParticipants ) {
_;
}else{
assert(msg.sender.send(msg.value));
}
}

modifier isEnded {
if (ended){
_;
}
}

modifier onlyAfter(uint _time) {
if (now > _time){
_;
}
require(!ended);
_;
}

modifier ifConfirmed(bytes32 _code){
require(confirmationRepository.claim(_code, msg.sender));
modifier onlyEnded {
require(ended);
_;
}

Expand Down Expand Up @@ -127,32 +90,37 @@ contract Conference is Destructible {
}
}

function registerWithEncryption(string _participant, string _encrypted) public payable{
function registerWithEncryption(string _participant, string _encrypted) public payable onlyActive{
registerInternal(_participant);
RegisterEvent(msg.sender, _participant, _encrypted);
}

function register(string _participant) public payable{
function register(string _participant) public payable onlyActive{
registerInternal(_participant);
RegisterEvent(msg.sender, _participant, '');
}

function registerInternal(string _participant) internal sentDepositOrReturn withinLimitOrReturn onlyActiveOrReturn {
function registerInternal(string _participant) internal {
require(msg.value == deposit);
require(registered < limitOfParticipants);
require(!isRegistered(msg.sender));

registered++;
participantsIndex[registered] = msg.sender;
participants[msg.sender] = Participant(_participant, msg.sender, false, false);
}

function attendWithConfirmation(bytes32 _confirmation) public ifConfirmed(_confirmation){
function attendWithConfirmation(bytes32 _confirmation) public onlyActive{
require(isRegistered(msg.sender));
require(!isAttended(msg.sender));
require(confirmationRepository.claim(_confirmation, msg.sender));

participants[msg.sender].attended = true;
attended++;
AttendEvent(msg.sender);
}

function withdraw() public{
function withdraw() public onlyEnded{
require(payoutAmount > 0);
Participant participant = participants[msg.sender];
require(participant.addr == msg.sender);
Expand Down Expand Up @@ -192,14 +160,14 @@ contract Conference is Destructible {

/* Admin only functions */

function payback() onlyOwner{
function payback() public onlyOwner onlyActive{
payoutAmount = payout();
ended = true;
endedAt = now;
PaybackEvent(payoutAmount);
}

function cancel() onlyOwner onlyActive{
function cancel() public onlyOwner onlyActive{
payoutAmount = deposit;
cancelled = true;
ended = true;
Expand All @@ -208,13 +176,15 @@ contract Conference is Destructible {
}

/* return the remaining of balance if there are any unclaimed after cooling period */
function clear() public onlyOwner isEnded onlyAfter(endedAt + coolingPeriod) {
function clear() public onlyOwner onlyEnded{
require(now > endedAt + coolingPeriod);
require(ended);
var leftOver = totalBalance();
require(owner.send(leftOver));
ClearEvent(owner, leftOver);
assert(owner.send(leftOver));
}

function setLimitOfParticipants(uint _limitOfParticipants) public onlyOwner{
function setLimitOfParticipants(uint _limitOfParticipants) public onlyOwner onlyActive{
limitOfParticipants = _limitOfParticipants;
}

Expand Down
10 changes: 5 additions & 5 deletions log/stress_0002.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type gasUsed gasPrice 1ETH*USD gasUsed*gasPrice(Ether) gasUsed*gasPrice(USD)
create 1692580 2 303 0.00338516 1.02570348
create 1686592 2 303 0.003373184 1.022074752
addConfirmation 89601 2 303 0.000179202 0.054298206
register 119815 2 303 0.00023963 0.07260789000000001
batchAttend 67251 2 303 0.000134502 0.040754106
payback 84325 2 303 0.00016865 0.05110095
withdraw 37660 2 303 0.00007532 0.022821960000000002
register 119996 2 303 0.000239992 0.07271757599999999
batchAttend 67248 2 303 0.000134496 0.040752288000000005
payback 84552 2 303 0.000169104 0.051238512
withdraw 37890 2 303 0.00007578 0.02296134
12 changes: 6 additions & 6 deletions log/stress_0020.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type gasUsed gasPrice 1ETH*USD gasUsed*gasPrice(Ether) gasUsed*gasPrice(USD)
create 1692580 2 303 0.00338516 1.02570348
addConfirmation 694234 2 303 0.001388468 0.42070580399999996
register 119815 2 303 0.00023963 0.07260789000000001
batchAttend 331022 2 303 0.000662044 0.200599332
payback 84325 2 303 0.00016865 0.05110095
withdraw 37660 2 303 0.00007532 0.022821960000000002
create 1686592 2 303 0.003373184 1.022074752
addConfirmation 694042 2 303 0.001388084 0.420589452
register 119996 2 303 0.000239992 0.07271757599999999
batchAttend 331147 2 303 0.000662294 0.200675082
payback 84552 2 303 0.000169104 0.051238512
withdraw 37890 2 303 0.00007578 0.02296134
12 changes: 6 additions & 6 deletions log/stress_0100.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type gasUsed gasPrice 1ETH*USD gasUsed*gasPrice(Ether) gasUsed*gasPrice(USD)
create 1692580 2 303 0.00338516 1.02570348
addConfirmation 3380971 2 303 0.006761942 2.048868426
register 119815 2 303 0.00023963 0.07260789000000001
batchAttend 1503538 2 303 0.003007076 0.911144028
payback 84325 2 303 0.00016865 0.05110095
withdraw 37660 2 303 0.00007532 0.022821960000000002
create 1686592 2 303 0.003373184 1.022074752
addConfirmation 3381099 2 303 0.006762198 2.048945994
register 119996 2 303 0.000239992 0.07271757599999999
batchAttend 1503663 2 303 0.003007326 0.911219778
payback 84552 2 303 0.000169104 0.051238512
withdraw 37890 2 303 0.00007578 0.02296134
48 changes: 43 additions & 5 deletions test/conference.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,30 @@ contract('Conference', function(accounts) {
assert.strictEqual(web3.eth.getBalance(conference.address).toNumber(), beforeContractBalance.toNumber());
assert.equal(await conference.isRegistered.call(owner), false);
})

it('cannot register twice with same address', async function(){
await conference.register.sendTransaction(twitterHandle, {from:owner, value:deposit});
await conference.register.sendTransaction(twitterHandle, {from:owner, value:deposit}).catch(function(){});
assert.strictEqual(web3.eth.getBalance(conference.address).toNumber(), deposit);
assert.equal(await conference.registered.call(), 1);
assert.equal(await conference.isRegistered.call(owner), true);
})
})

describe('on attend', function(){
var non_registered = accounts[4];

beforeEach(async function(){
await conference.register(twitterHandle, {value:deposit, from:non_owner});
})

it('isAttended is true if owner calls attend function', async function(){
it('can be called by owner', async function(){
await conference.attend([non_owner], {from:owner});
assert.equal(await conference.isAttended.call(non_owner), true);
assert.equal((await conference.attended.call()).toNumber(), 1);
})

it('isAttended is false if non owner calls attend function', async function(){
it('cannot be called by non owner', async function(){
await conference.attend([non_owner], {from:non_owner}).catch(function(){});
assert.equal(await conference.isAttended.call(non_owner), false);
assert.equal(await conference.attended.call(), 0);
Expand All @@ -123,6 +133,20 @@ contract('Conference', function(accounts) {
it('isAttended is false if attended function for the account is not called', async function(){
assert.equal(await conference.isAttended.call(owner), false);
})

it('cannot be attended if the list includes non registered address', async function(){
await conference.attend([non_owner, non_registered], {from:owner}).catch(function(){});
assert.equal(await conference.isAttended.call(non_owner), false);
assert.equal(await conference.isAttended.call(non_registered), false);
assert.equal((await conference.attended.call()).toNumber(), 0);
})

it('cannot be attended twice', async function(){
await conference.attend([non_owner], {from:owner});
await conference.attend([non_owner], {from:owner}).catch(function(){});
assert.equal(await conference.isAttended.call(non_owner), true);
assert.equal((await conference.attended.call()).toNumber(), 1);
})
})

describe('self attend with code', function(){
Expand Down Expand Up @@ -166,6 +190,20 @@ contract('Conference', function(accounts) {
assert.equal(await conference.attended.call(), 1);
assert.equal(await confirmation.report.call(confirmation_code), non_owner);
})

it('cannot be attended twice', async function(){
await conference.register(twitterHandle, {from:non_owner, value:deposit});
await conference.attend([non_owner], {from:owner});
await conference.attendWithConfirmation([non_owner], {from:non_owner}).catch(function(){});
assert.equal(await conference.isAttended.call(non_owner), true);
assert.equal((await conference.attended.call()).toNumber(), 1);
})

it('cannot be attended if not registered', async function(){
await conference.attendWithConfirmation([non_owner], {from:non_owner}).catch(function(){});
assert.equal(await conference.isAttended.call(non_owner), false);
assert.equal((await conference.attended.call()).toNumber(), 0);
})
})

describe('on payback', function(){
Expand Down Expand Up @@ -209,7 +247,7 @@ contract('Conference', function(accounts) {
it('cannot register any more', async function(){
await conference.payback({from:owner});
currentRegistered = await conference.registered.call();
await conference.register('some handler', {from:notRegistered, value:deposit});
await conference.register('some handler', {from:notRegistered, value:deposit}).catch(function(){});
assert.strictEqual((await conference.registered.call()).toNumber(), currentRegistered.toNumber());
assert.equal(await conference.ended.call(), true);
})
Expand All @@ -218,7 +256,7 @@ contract('Conference', function(accounts) {
it('cannot attend any more', async function(){
await conference.payback({from:owner});
currentAttended = await conference.attended.call();
await conference.attend([notAttended], {from:owner});
await conference.attend([notAttended], {from:owner}).catch(function(){});
assert.strictEqual((await conference.attended.call()).toNumber(), currentAttended.toNumber());
assert.equal(await conference.ended.call(), true);
})
Expand Down Expand Up @@ -267,7 +305,7 @@ contract('Conference', function(accounts) {
it('cannot register any more', async function(){
await conference.cancel();
currentRegistered = await conference.registered.call();
await conference.register('some handler', {from:notRegistered, value:deposit});
await conference.register('some handler', {from:notRegistered, value:deposit}).catch(function(){});
assert.strictEqual((await conference.registered.call()).toNumber(), currentRegistered.toNumber());
assert.equal(await conference.ended.call(), true);
})
Expand Down

0 comments on commit 482467b

Please sign in to comment.