-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Version handshake #180
Merged
+203
−0
Merged
Version handshake #180
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6b0440c
versionExchange: first stab in the dark
cryptix 0075a35
only send local version once
cryptix 2a5b3ea
compare versions for compatibility
cryptix 78eb349
fixed Compatible and added a small test case
cryptix 9dc5863
dont need coreos/semver yet
cryptix eab1a89
don't use my protobuf path
cryptix 6f8a89c
renamed version pkg to handshake
cryptix 67e04f0
changed message from SemVer to Handshake1
cryptix 0f47b93
addressed CR by @jbenet
cryptix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
all: semver.pb.go | ||
|
||
semver.pb.go: semver.proto | ||
protoc --gogo_out=. --proto_path=../../../../../:/usr/local/opt/protobuf/include:. $< | ||
|
||
clean: | ||
rm semver.pb.go |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package handshake; | ||
|
||
message Handshake1 { | ||
// protocolVersion determines compatibility between peers | ||
optional string protocolVersion = 1; // semver | ||
|
||
// agentVersion is like a UserAgent string in browsers, or client version in bittorrent | ||
// includes the client name and client. e.g. "go-ipfs/0.1.0" | ||
optional string agentVersion = 2; // semver | ||
|
||
// we'll have more fields here later. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package handshake | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
updates "github.com/jbenet/go-ipfs/updates" | ||
|
||
semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver" | ||
) | ||
|
||
// currentVersion holds the current protocol version for a client running this code | ||
var currentVersion *semver.Version | ||
|
||
func init() { | ||
var err error | ||
currentVersion, err = semver.NewVersion("0.0.1") | ||
if err != nil { | ||
panic(fmt.Errorf("invalid protocol version: %v", err)) | ||
} | ||
} | ||
|
||
// CurrentHandshake returns the current protocol version as a protobuf message | ||
func CurrentHandshake() *Handshake1 { | ||
return NewHandshake1(currentVersion.String(), "go-ipfs/"+updates.Version) | ||
} | ||
|
||
// ErrVersionMismatch is returned when two clients don't share a protocol version | ||
var ErrVersionMismatch = errors.New("protocol missmatch") | ||
|
||
// Compatible checks wether two versions are compatible | ||
// returns nil if they are fine | ||
func Compatible(handshakeA, handshakeB *Handshake1) error { | ||
a, err := semver.NewVersion(*handshakeA.ProtocolVersion) | ||
if err != nil { | ||
return err | ||
} | ||
b, err := semver.NewVersion(*handshakeB.ProtocolVersion) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if a.Major != b.Major { | ||
return ErrVersionMismatch | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewHandshake1 creates a new Handshake1 from the two strings | ||
func NewHandshake1(protoVer, agentVer string) *Handshake1 { | ||
return &Handshake1{ | ||
ProtocolVersion: &protoVer, | ||
AgentVersion: &agentVer, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package handshake | ||
|
||
import "testing" | ||
|
||
func TestCompatible(t *testing.T) { | ||
tcases := []struct { | ||
a, b string | ||
expected error | ||
}{ | ||
{"0.0.0", "0.0.0", nil}, | ||
{"1.0.0", "1.1.0", nil}, | ||
{"1.0.0", "1.0.1", nil}, | ||
{"0.0.0", "1.0.0", ErrVersionMismatch}, | ||
{"1.0.0", "0.0.0", ErrVersionMismatch}, | ||
} | ||
|
||
for i, tcase := range tcases { | ||
|
||
if Compatible(NewHandshake1(tcase.a, ""), NewHandshake1(tcase.b, "")) != tcase.expected { | ||
t.Fatalf("case[%d] failed", i) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No, we don't need to use a
NetMessage
here.Conn
s deal with raw bytes. (they wont after i'm done with them tonight, but i'll fix this later)