forked from strangelove-ventures/horcrux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from nitronit/threshold_small_pr
Threshold to main
- Loading branch information
Showing
36 changed files
with
1,894 additions
and
491 deletions.
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
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,30 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func SanitizeAddress(address string) (string, error) { | ||
u, err := url.Parse(address) | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing peer URL: %w", err) | ||
} | ||
|
||
return u.Host, nil | ||
} | ||
|
||
func MultiAddress(addresses []string) (string, error) { | ||
grpcAddresses := make([]string, len(addresses)) | ||
|
||
for i, addr := range addresses { | ||
peerAddr, err := SanitizeAddress(addr) | ||
if err != nil { | ||
return "", err | ||
} | ||
grpcAddresses[i] = peerAddr | ||
} | ||
|
||
return fmt.Sprintf("multi:///%s", strings.Join(grpcAddresses, ",")), nil | ||
} |
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,55 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/strangelove-ventures/horcrux/client" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLeaderElectionMultiAddressDomain(t *testing.T) { | ||
addresses := []string{ | ||
"tcp://signer-1:2222", | ||
"tcp://signer-2:2222", | ||
"tcp://signer-3:2222", | ||
} | ||
|
||
multiAddress, err := client.MultiAddress(addresses) | ||
require.NoError(t, err, "failed to assemble fqdn multi address") | ||
|
||
require.Equal(t, "multi:///signer-1:2222,signer-2:2222,signer-3:2222", multiAddress) | ||
} | ||
|
||
func TestLeaderElectionMultiAddressIPv4(t *testing.T) { | ||
addresses := []string{ | ||
"tcp://10.0.0.1:2222", | ||
"tcp://10.0.0.2:2222", | ||
"tcp://10.0.0.3:2222", | ||
} | ||
|
||
multiAddress, err := client.MultiAddress(addresses) | ||
require.NoError(t, err, "failed to assemble ipv4 multi address") | ||
|
||
require.Equal(t, "multi:///10.0.0.1:2222,10.0.0.2:2222,10.0.0.3:2222", multiAddress) | ||
} | ||
|
||
func TestLeaderElectionMultiAddressIPv6(t *testing.T) { | ||
addresses := []string{ | ||
"tcp://[2001:db8:3333:4444:5555:6666:7777:8888]:2222", | ||
"tcp://[::]:2222", | ||
"tcp://[::1234:5678]:2222", | ||
"tcp://[2001:db8::]:2222", | ||
"tcp://[2001:db8::1234:5678]:2222", | ||
} | ||
|
||
multiAddress, err := client.MultiAddress(addresses) | ||
require.NoError(t, err, "failed to assemble ipv6 multi address") | ||
|
||
const expected = "multi:///" + | ||
"[2001:db8:3333:4444:5555:6666:7777:8888]:2222" + | ||
",[::]:2222,[::1234:5678]:2222" + | ||
",[2001:db8::]:2222" + | ||
",[2001:db8::1234:5678]:2222" | ||
|
||
require.Equal(t, expected, multiAddress) | ||
} |
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
Oops, something went wrong.