Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

swap: exchange chain id on handshake #1913

Merged
merged 9 commits into from
Nov 12, 2019
Merged

swap: exchange chain id on handshake #1913

merged 9 commits into from
Nov 12, 2019

Conversation

ralph-pichler
Copy link
Member

@ralph-pichler ralph-pichler commented Oct 28, 2019

This PR

  • adds the chain id to the swap handshake and aborts on mismatch
  • includes a complete rewrite of the handshake test which was broken before (inspired by the protocol tests from the network package)
  • introduces tests for all scenarios where the handshake should fail

@ralph-pichler ralph-pichler added in progress incentives builds on open PR Builds on a PR that is not yet merged. It is blocked until then and the diff won't make sense yet. labels Oct 28, 2019
@ralph-pichler ralph-pichler changed the title swap: drop non-swap peers swap: exchange chain id on handshake Oct 28, 2019
@ralph-pichler ralph-pichler force-pushed the swap-drop-peers branch 2 times, most recently from 4559b5d to 2c2f6a7 Compare November 5, 2019 11:19
@ralph-pichler ralph-pichler removed the builds on open PR Builds on a PR that is not yet merged. It is blocked until then and the diff won't make sense yet. label Nov 5, 2019

// sign the cheque
cheque.Signature, err = cheque.Sign(swap.owner.privateKey)
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Isn't a TestDisconnected returning an err if it does not find the expected disconnect? So wouldn't here err be nil if it actually found the provided Disconnect, which is saying stay connected, which is what you wanted to test?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, on a disconnect err would be nil. Which is an error because because this is where we we want to make sure that there is no disconnect. (This function is copied from the network tests btw).

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit counter-intuitive to pass a Disconnect object when not expecting a disconnect...Can't this just be called with an empty object, which would signal that we don't expect a disconnect?

Then err would return nil only if there would be no disconnect.

If that's not possible, I guess that's the way to do it and we keep it (I understand you copied it from elsewhere)

Copy link
Member Author

Choose a reason for hiding this comment

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

Afaik there is no other way to check if the node is disconnected or not. (It also would not be possible, if we passed nil, then the function would not know which peer to check).

I would keep the function as it is, so that we remain consistent with the handshake test style from the other protocols.

swap/types.go Outdated Show resolved Hide resolved
swap/protocol.go Outdated
@@ -34,6 +34,9 @@ var (
// ErrEmptyAddressInSignature is used when the empty address is used for the chequebook in the handshake
ErrEmptyAddressInSignature = errors.New("empty address in handshake")

// ErrDifferentChainId is used when the chain id exchanged during the handshake does not match
ErrDifferentChainId = errors.New("different chain id")
Copy link
Contributor

Choose a reason for hiding this comment

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

var ErrDifferentChainId should be ErrDifferentChainID -- go-lint

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that in the linter output when developing. Looks like I forgot to actually commit that. Interesting that the CI didn't catch this. Should be fixed now.

swap/types.go Outdated
@@ -36,6 +36,7 @@ type Cheque struct {

// HandshakeMsg is exchanged on peer handshake
type HandshakeMsg struct {
ChainID uint64
Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave a comment here, specifying that this is the chainID of the Ethereum network

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@ralph-pichler ralph-pichler deleted the swap-drop-peers branch November 11, 2019 11:08
@ralph-pichler ralph-pichler restored the swap-drop-peers branch November 11, 2019 11:08
@ralph-pichler ralph-pichler reopened this Nov 11, 2019
@@ -98,6 +101,10 @@ func (s *Swap) verifyHandshake(msg interface{}) error {
return ErrEmptyAddressInSignature
}

if handshake.ChainID != s.chainID {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you update the comment to the function verifyHandshake? Right now it says that it is only verifying the chequebook address transmitted

Copy link
Member Author

Choose a reason for hiding this comment

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

done

newSwapHandshakeMsg(common.Address{}, 1234),
&p2ptest.Disconnect{
Peer: protocolTester.Nodes[0].ID(),
Error: errors.New("Handshake error: Message handler error: (msg code 0): empty address in handshake"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you somehow create this error by including the (exported) error messages from the different protocols? Such that when if the content of the error message is changed in one protocol, it does not make our test fail?

For example, the expected error message here is a concatenation of:
protocols.ErrHandshake + protocols.ErrHandler + swap.ErrDifferentChainID.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@Eknir Eknir left a comment

Choose a reason for hiding this comment

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

Good work! Small minor things, but I approve regardless.

}

// creates a test exchange for the handshakes
func HandshakeMsgExchange(lhs, rhs *HandshakeMsg, id enode.ID) []p2ptest.Exchange {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this function exported?

@@ -130,7 +131,7 @@ func swapRotatingFileHandler(logdir string) (log.Handler, error) {
}

// newSwapInstance is a swap constructor function without integrity checks
func newSwapInstance(stateStore state.Store, owner *Owner, backend contract.Backend, params *Params, chequebookFactory contract.SimpleSwapFactory) *Swap {
func newSwapInstance(stateStore state.Store, owner *Owner, backend contract.Backend, chainID uint64, params *Params, chequebookFactory contract.SimpleSwapFactory) *Swap {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great to have the order of function arguments the same as the order of the functions in the swap struct

Copy link
Member Author

Choose a reason for hiding this comment

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

This already wasn't the case prior to the PR

Copy link
Member Author

Choose a reason for hiding this comment

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

That being said I want the backend and chain id next to each other since they kind of belong together. So it should be changed in the struct.

@ralph-pichler ralph-pichler merged commit 313e851 into master Nov 12, 2019
@ralph-pichler ralph-pichler deleted the swap-drop-peers branch November 12, 2019 21:38
@acud acud added this to the 0.5.3 milestone Nov 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants