Skip to content

[WIP] Payment channel example. #2431

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

Closed
wants to merge 2 commits into from
Closed

[WIP] Payment channel example. #2431

wants to merge 2 commits into from

Conversation

chriseth
Copy link
Contributor

No description provided.

uint closeBalanceUser0;
enum State { Opened, Accepted, Closing, Closed }
State state;
function Channel(address _other, uint _timeout) payable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some whitespaces between functions, state variables and the pragma cannot hurt.

timeout = _timeout;
}
function accept() payable {
require(msg.sender == users[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since user[0] and user[1] have a very specific role, I think it would make sense naming them appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initiator and acceptor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds fine.

@chriseth
Copy link
Contributor Author

This is work in progress and requires much more documentation. You could already check whether the contract is a valid payment channel, though.

@chriseth chriseth changed the title Payment channel example. [WIP] Payment channel example. Jun 22, 2017
state = State.Accepted;
}
function requestClose(uint _balanceUser0, uint _sequenceNumber, uint8 v, bytes32 r, bytes32 s) {
require(state == State.Accepted || state == State.Closing);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why allow closing multiple times? Each time the timestamp/balance/sequence is updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriseth this is still an open question

@axic
Copy link
Member

axic commented Jul 12, 2017

I suggest not to merge this before #2553.

@axic axic force-pushed the paymentchannel branch from 0ba09ee to 736d4a8 Compare July 13, 2017 21:13
@axic
Copy link
Member

axic commented Jul 13, 2017

Rebased to trigger the tests.

require(state == State.Opened);
state = State.Accepted;
}
function requestClose(uint _balanceUser0, uint _sequenceNumber, uint8 v, bytes32 r, bytes32 s) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistency: v/r/s is not prefixed.

@axic
Copy link
Member

axic commented Dec 12, 2017

@chriseth updating this with the comments. Can you review? What is left to merge this?

@axic
Copy link
Member

axic commented Feb 26, 2018

@chriseth rebased. What is blocking this?

require(_sequenceNumber > closeSequenceNumber);
require(_balanceInitiator <= 2 * deposit);
require(closeTimestamp == 0 || now <= closeTimestamp + timeout);
require(ecrecover(sha3(_balanceInitiator, _sequenceNumber), _v, _r, _s) == counterpartyAddress(_sequenceNumber));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use keccak256.

@axic
Copy link
Member

axic commented Feb 26, 2018

Compilation failures:

Warning: This is a pre-release compiler version, please do not use it in production.
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:14:9: Warning: No visibility specified. Defaulting to "public".
        function PaymentChannel(address _other, uint _timeout) payable {
        ^
Spanning multiple lines.
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:23:9: Warning: No visibility specified. Defaulting to "public".
        function accept() payable {
        ^
Spanning multiple lines.
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:34:9: Warning: No visibility specified. Defaulting to "public".
        function requestClose(uint _balanceInitiator, uint _sequenceNumber, uint8 _v, bytes32 _r, bytes32 _s) {
        ^
Spanning multiple lines.
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:46:9: Warning: No visibility specified. Defaulting to "public".
        function close()
        ^
Spanning multiple lines.
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:15:13: Error: Undeclared identifier. Did you mean "initiator"?
            initator = msg.sender;
            ^------^
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:49:70: Error: Undeclared identifier. Did you mean "counterpartyAddress"?
            require(now >= closeTimestamp + timeout || msg.sender == counteryPartyAddress(closeSequenceNumber + 1));
                                                                     ^------------------^
/tmp/tmp.aEq6oBGgwl/test_0a4d82f0af2c945e6a612af13a590776a772d071cdf8cbc0cce41dba326a8ffb.sol:52:17: Error: Undeclared identifier. Did you mean "initiator"?
            if (initator.send(closeBalanceInitiator))
                ^------^

initator = msg.sender;
acceptor = _other;
deposit = msg.value;
require(deposit < 2**250);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about this overflow check on msg.value. I wouldn't think to do that... is it really necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why I did this. Perhaps better safe than sorry?

@chriseth
Copy link
Contributor Author

Some more comments are left to merge this. I actually would have liked to also include the frontend part. We can add a comment that this is still left to do.

@axic
Copy link
Member

axic commented Mar 27, 2018

@bit-shift @ekpyron do you want to write some actual Solidity code ^^? 😉

@smarx
Copy link

smarx commented May 14, 2018

@chriseth asked me to adapt my code from https://programtheblockchain.com/posts/2018/05/11/state-channels-for-two-player-games/ to go in the documentation.

I wonder, though, if the code from https://programtheblockchain.com/posts/2018/02/23/writing-a-simple-payment-channel/ (or perhaps https://programtheblockchain.com/posts/2018/03/02/building-long-lived-payment-channels/) might be better? That's a one-way payment channel, which is simpler to get right (and thus simpler code).

I guess I'm asking what the goal is here. If it's to get across the fundamentals of state channels, then maybe my state channel post is the most appropriate starting point. If it's to introduce payment channels, then I think my "simple payment channel" post is a good starting point.

If the goal is just to show how signatures can be used to make off-chain commitments, then https://programtheblockchain.com/posts/2018/02/17/signing-and-verifying-messages-in-ethereum/ is perhaps an even better starting point.

I'm happy to adapt any of those to a short example for the documentation, but I'd love some guidance on what people want to see.

@chriseth
Copy link
Contributor Author

I think the goal is to show how easy it is to implement payment channels in etherem as opposed to bitcoin ;)

If a two-sided payment channel is too complicated, then a one-sided will do. It would be great if it could also include the required javascript code (or at least a snippet).

From a didactic point of view, it is probably a good idea to first have a short chapter on how to to create and verify signatures and then another chapter about how that can be used in payment channels.

@chriseth
Copy link
Contributor Author

Added an issue, perhaps someone is willing to put up a bounty for this: #4131

@chriseth
Copy link
Contributor Author

@smarx did you have time to work on this yet?

@smarx
Copy link

smarx commented Jun 6, 2018

@chriseth No, sorry, I didn't. Looked like someone else is starting on the bounty, though!

@chriseth
Copy link
Contributor Author

chriseth commented Jul 5, 2018

There are much better examples now.

@chriseth chriseth closed this Jul 5, 2018
@axic
Copy link
Member

axic commented Jul 24, 2018

This was obsoleted by #4212.

@axic axic deleted the paymentchannel branch July 24, 2018 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants