Skip to content

Instantly share code, notes, and snippets.

@dodikk
Created October 17, 2018 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodikk/9b9e671fbb9fef1c466d759ffa0250e1 to your computer and use it in GitHub Desktop.
Save dodikk/9b9e671fbb9fef1c466d759ffa0250e1 to your computer and use it in GitHub Desktop.
Embark gas consumption issue
pragma solidity ^0.4.23;
contract AdkSplitterForTwo
{
event LogBeginSplit();
event LogEndSplit();
event LogSamePersonRevert();
event LogTransferToFirstReceiverBegin();
event LogTransferToFirstReceiverEnd();
event LogTransferToSecondReceiverBegin();
event LogTransferToSecondReceiverEnd();
event LogTransferChangeToSenderBegin();
event LogTransferChangeToSenderEnd();
function () payable public
{
revert("the contract is not supposed to hold ether");
}
function GetContractAddress()
public
constant
returns (address result)
{
result = this;
}
function Split(
address firstReceiver ,
address secondReceiver)
public
payable
{
emit LogBeginSplit();
// https://medium.com/blockchannel/the-use-of-revert-assert-and-require-in-solidity-and-the-new-revert-opcode-in-the-evm-1a3a7990e06e
require(msg.value >= 2, "Not enough wei for splitting");
// ???
// require(msg.sender.balance >= msg.value, "Sender has insufficient wei");
bool isAllActorsSamePerson =
(msg.sender == firstReceiver )
&& (msg.sender == secondReceiver);
if (isAllActorsSamePerson)
{
assert(firstReceiver == secondReceiver);
// seems like no work needs to be done
// refund the gas to the sender
revert("Sender and both receivers are same. Nothing to do. ==> reverting");
emit LogSamePersonRevert();
return;
}
uint fundsToSplit = msg.value;
uint shareOfReceivers = fundsToSplit / 2;
uint change = fundsToSplit - 2 * shareOfReceivers;
assert(shareOfReceivers > 0);
// if (msg.sender != firstReceiver)
// {
// still need to transfer back to the sender
// because the sent ether now belongs to the contract
emit LogTransferToFirstReceiverBegin();
firstReceiver.transfer(shareOfReceivers);
emit LogTransferToFirstReceiverEnd();
// }
// if (msg.sender != secondReceiver)
// {
// still need to transfer back to the sender
// because the sent ether now belongs to the contract
emit LogTransferToSecondReceiverBegin();
secondReceiver.transfer(shareOfReceivers);
emit LogTransferToSecondReceiverEnd();
// }
if (0 != change)
{
emit LogTransferChangeToSenderBegin();
msg.sender.transfer(change);
emit LogTransferChangeToSenderEnd();
}
emit LogEndSplit();
} // function Split()
} // contract AdkSplitterForTwo
$ embark test
Compiling contracts
AdkSplitterForTwo
✓ should revert when fallback function is called (169ms) - [21181 gas]
1) should revert undividable one wei - [25247 gas]
2) should revert undividable zero wei - [25247 gas]
3) should create no transactions if all users are same - [25393 gas]
4) should give equal shares to strangers - [44230 gas]
5) should give equal shares to strangers -- odd amount - [53287 gas]
6) should make only one transaction if first receiver is same as sender - [44252 gas]
7) should make only one transaction if second receiver is same as sender - [44230 gas]
1 passing (8s) - [Total: 732782 gas]
7 failing
1) AdkSplitterForTwo
should revert undividable one wei:
AssertionError [ERR_ASSERTION]: unexpected sender balance change when split is supposed to do nothing
+ expected - actual
-99999949506000000000
+100000000000000000000
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:219:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
2) AdkSplitterForTwo
should revert undividable zero wei:
AssertionError [ERR_ASSERTION]: unexpected sender balance change when split is supposed to do nothing
+ expected - actual
-99999899012000000000
+99999949506000000000
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:308:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
3) AdkSplitterForTwo
should create no transactions if all users are same:
AssertionError [ERR_ASSERTION]: unexpected balance change when split is supposed to do nothing
+ expected - actual
-99999848226000000000
+99999899012000000000
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:401:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
4) AdkSplitterForTwo
should give equal shares to strangers:
ReferenceError: strActualSecondStrangerbalance is not defined
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:529:53)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
5) AdkSplitterForTwo
should give equal shares to strangers -- odd amount:
AssertionError [ERR_ASSERTION]: sender balance mismatch
+ expected - actual
-99999741651999999986
+99999848225999999986
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:658:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
6) AdkSplitterForTwo
should make only one transaction if first receiver is same as sender:
AssertionError [ERR_ASSERTION]: sender balance mismatch
+ expected - actual
-99999911495999999000
+99999999999999999000
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:769:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
7) AdkSplitterForTwo
should make only one transaction if second receiver is same as sender:
AssertionError [ERR_ASSERTION]: sender balance mismatch
+ expected - actual
-99999911539999999000
+99999999999999999000
at Context.<anonymous> (test/AdkSplitterForTwo_spec.js:881:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
> Total number of failures: 7
$ truffle test
Using network 'development'.
Contract: AdkSplitterForTwo
✓ should revert when fallback function is called (1030ms)
✓ should revert undividable one wei (1123ms)
✓ should revert undividable zero wei (1084ms)
✓ should create no transactions if all users are same (547ms)
✓ should give equal shares to strangers (1173ms)
✓ should give equal shares to strangers -- odd amount (1235ms)
✓ should make only one transaction if first receiver is same as sender (1236ms)
✓ should make only one transaction if second receiver is same as sender (1278ms)
8 passing (9s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment