-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add support for libp2p pnet #60
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
Open
aschmahmann
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-pnet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,42 @@ | ||
| package vole | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/libp2p/go-libp2p/core/pnet" | ||
| ) | ||
|
|
||
| type pnetPSKContextKey struct{} | ||
|
|
||
| func WithPnetPSK(ctx context.Context, psk pnet.PSK) context.Context { | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| cpy := make([]byte, len(psk)) | ||
| copy(cpy, psk) | ||
| return context.WithValue(ctx, pnetPSKContextKey{}, pnet.PSK(cpy)) | ||
| } | ||
|
|
||
| func pnetPSKFromContext(ctx context.Context) (pnet.PSK, bool) { | ||
| if ctx == nil { | ||
| return nil, false | ||
| } | ||
| psk, ok := ctx.Value(pnetPSKContextKey{}).(pnet.PSK) | ||
| return psk, ok | ||
| } | ||
|
|
||
| func LoadPnetPSK(path string) (pnet.PSK, error) { | ||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read pnet swarm key %q: %w", path, err) | ||
| } | ||
| defer func() { _ = f.Close() }() | ||
|
|
||
| psk, err := pnet.DecodeV1PSK(f) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("decode pnet swarm key %q: %w", path, err) | ||
| } | ||
| return psk, nil | ||
| } | ||
This file contains hidden or 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,141 @@ | ||
| package vole | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/libp2p/go-libp2p/core/peer" | ||
| corepnet "github.com/libp2p/go-libp2p/core/pnet" | ||
| ) | ||
|
|
||
| const testSwarmKeyV1 = "/key/swarm/psk/1.0.0/\n/base16/\n0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n" | ||
| const testSwarmKeyV1Alt = "/key/swarm/psk/1.0.0/\n/base16/\nfedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210\n" | ||
|
|
||
| func writeTempFile(t *testing.T, contents string) string { | ||
| t.Helper() | ||
| f, err := os.CreateTemp(t.TempDir(), "swarmkey-*") | ||
| if err != nil { | ||
| t.Fatalf("CreateTemp: %v", err) | ||
| } | ||
| if _, err := f.WriteString(contents); err != nil { | ||
| _ = f.Close() | ||
| t.Fatalf("WriteString: %v", err) | ||
| } | ||
| if err := f.Close(); err != nil { | ||
| t.Fatalf("Close: %v", err) | ||
| } | ||
| return f.Name() | ||
| } | ||
|
|
||
| func TestLoadPnetPSK_MissingFile(t *testing.T) { | ||
| _, err := LoadPnetPSK("does-not-exist") | ||
| if err == nil { | ||
| t.Fatalf("expected error") | ||
| } | ||
| } | ||
|
|
||
| func TestLoadPnetPSK_InvalidFile(t *testing.T) { | ||
| path := writeTempFile(t, "not a swarm key") | ||
| _, err := LoadPnetPSK(path) | ||
| if err == nil { | ||
| t.Fatalf("expected error") | ||
| } | ||
| } | ||
|
|
||
| func TestPnet_AllowsOnlySameKey(t *testing.T) { | ||
| path := writeTempFile(t, testSwarmKeyV1) | ||
| psk, err := LoadPnetPSK(path) | ||
| if err != nil { | ||
| t.Fatalf("LoadPnetPSK: %v", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| hNo, err := libp2pHost(ctx) | ||
| if err != nil { | ||
| t.Fatalf("libp2pHost(no pnet): %v", err) | ||
| } | ||
| defer hNo.Close() | ||
|
|
||
| hYesA, err := libp2pHost(WithPnetPSK(ctx, psk)) | ||
| if err != nil { | ||
| t.Fatalf("libp2pHost(pnet A): %v", err) | ||
| } | ||
| defer hYesA.Close() | ||
|
|
||
| hYesB, err := libp2pHost(WithPnetPSK(ctx, psk)) | ||
| if err != nil { | ||
| t.Fatalf("libp2pHost(pnet B): %v", err) | ||
| } | ||
| defer hYesB.Close() | ||
|
|
||
| // pnet->no-pnet should fail. | ||
| aiNo := peer.AddrInfo{ID: hNo.ID(), Addrs: hNo.Addrs()} | ||
| dialCtx, dialCancel := context.WithTimeout(ctx, 3*time.Second) | ||
| err = hYesA.Connect(dialCtx, aiNo) | ||
| dialCancel() | ||
| if err == nil { | ||
| t.Fatalf("expected pnet dial to non-pnet host to fail") | ||
| } | ||
| // Depending on the transport/OS, the pnet failure may be wrapped in dial/security negotiation errors. | ||
| if !corepnet.IsPNetError(err) && !strings.Contains(strings.ToLower(err.Error()), "privnet") { | ||
| // Don't fail the test on classification; the key property is that the dial fails. | ||
| t.Logf("dial failed with non-pnet-classified error: %T: %v", err, err) | ||
| } | ||
|
|
||
| // pnet->pnet (same key) should succeed. | ||
| aiYesB := peer.AddrInfo{ID: hYesB.ID(), Addrs: hYesB.Addrs()} | ||
| dialCtx2, dialCancel2 := context.WithTimeout(ctx, 5*time.Second) | ||
| err = hYesA.Connect(dialCtx2, aiYesB) | ||
| dialCancel2() | ||
| if err != nil { | ||
| // If this fails, surface the most useful root cause. | ||
| var pnetErr corepnet.Error | ||
| if errors.As(err, &pnetErr) { | ||
| t.Fatalf("unexpected pnet error connecting same-key hosts: %v", err) | ||
| } | ||
| t.Fatalf("expected same-key hosts to connect, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestPnet_DifferentKeysFail(t *testing.T) { | ||
| pathA := writeTempFile(t, testSwarmKeyV1) | ||
| pskA, err := LoadPnetPSK(pathA) | ||
| if err != nil { | ||
| t.Fatalf("LoadPnetPSK(A): %v", err) | ||
| } | ||
|
|
||
| pathB := writeTempFile(t, testSwarmKeyV1Alt) | ||
| pskB, err := LoadPnetPSK(pathB) | ||
| if err != nil { | ||
| t.Fatalf("LoadPnetPSK(B): %v", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| hA, err := libp2pHost(WithPnetPSK(ctx, pskA)) | ||
| if err != nil { | ||
| t.Fatalf("libp2pHost(pnet A): %v", err) | ||
| } | ||
| defer hA.Close() | ||
|
|
||
| hB, err := libp2pHost(WithPnetPSK(ctx, pskB)) | ||
| if err != nil { | ||
| t.Fatalf("libp2pHost(pnet B): %v", err) | ||
| } | ||
| defer hB.Close() | ||
|
|
||
| aiB := peer.AddrInfo{ID: hB.ID(), Addrs: hB.Addrs()} | ||
| dialCtx, dialCancel := context.WithTimeout(ctx, 3*time.Second) | ||
| err = hA.Connect(dialCtx, aiB) | ||
| dialCancel() | ||
| if err == nil { | ||
| t.Fatalf("expected different-key pnet connect to fail") | ||
| } | ||
| } |
This file contains hidden or 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.
Note: I went with the context plumbing approach mostly for ease. I'm also happy to swap this to something else (e.g. passing around libp2p options or switching to passing a host explicitly and then exposing a default constructor).