-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[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
Conversation
docs/solidity-by-example.rst
Outdated
uint closeBalanceUser0; | ||
enum State { Opened, Accepted, Closing, Closed } | ||
State state; | ||
function Channel(address _other, uint _timeout) payable { |
There was a problem hiding this comment.
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.
docs/solidity-by-example.rst
Outdated
timeout = _timeout; | ||
} | ||
function accept() payable { | ||
require(msg.sender == users[1]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initiator and acceptor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds fine.
This is work in progress and requires much more documentation. You could already check whether the contract is a valid payment channel, though. |
docs/solidity-by-example.rst
Outdated
state = State.Accepted; | ||
} | ||
function requestClose(uint _balanceUser0, uint _sequenceNumber, uint8 v, bytes32 r, bytes32 s) { | ||
require(state == State.Accepted || state == State.Closing); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
I suggest not to merge this before #2553. |
Rebased to trigger the tests. |
docs/solidity-by-example.rst
Outdated
require(state == State.Opened); | ||
state = State.Accepted; | ||
} | ||
function requestClose(uint _balanceUser0, uint _sequenceNumber, uint8 v, bytes32 r, bytes32 s) { |
There was a problem hiding this comment.
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.
@chriseth updating this with the comments. Can you review? What is left to merge this? |
@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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use keccak256
.
Compilation failures:
|
initator = msg.sender; | ||
acceptor = _other; | ||
deposit = msg.value; | ||
require(deposit < 2**250); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
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. |
@bit-shift @ekpyron do you want to write some actual Solidity code ^^? 😉 |
@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. |
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. |
Added an issue, perhaps someone is willing to put up a bounty for this: #4131 |
@smarx did you have time to work on this yet? |
@chriseth No, sorry, I didn't. Looked like someone else is starting on the bounty, though! |
There are much better examples now. |
This was obsoleted by #4212. |
No description provided.