Skip to content

Commit

Permalink
feat(tcp-udp): add the multiaddr udp connection
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 committed Nov 7, 2024
1 parent c1670f8 commit 9f277b9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/k8s/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
trustedPeerFileDA = "/tmp/CONSENSUS_NODE_SERVICE"
nodeIpFile = "/tmp/NODE_IP"
cmd = `$(ifconfig | grep -oE 'inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | grep -v '127.0.0.1' | awk '{print substr($2, 6)}')`
trustedPeerPrefix = "/ip4/" + cmd + "/tcp/2121/p2p/"
trustedPeerPrefix = "/ip4/" + cmd + "/tcp/2121/p2p/,/ip4/" + cmd + "/udp/2121/quic-v1/p2p/"
)

// CreateFileWithEnvVar creates the file in the FS with the node to connect.
Expand Down
38 changes: 29 additions & 9 deletions pkg/k8s/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const case1 = "Case 1: Successfully script generated."

// TestCreateFileWithEnvVar validates the node types and their path
func TestCreateFileWithEnvVar(t *testing.T) {
t.Parallel()

type args struct {
nodeToFile string
nodeType string
Expand Down Expand Up @@ -42,16 +44,21 @@ echo -n "/tmp/CONSENSUS_NODE_SERVICE" > "/tmp/CONSENSUS_NODE_SERVICE"`},
}

for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got := CreateFileWithEnvVar(tt.args.nodeToFile, tt.args.nodeType); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateFileWithEnvVar() = got: \n%v, \nwant \n%v", got, tt.want)
t.Parallel()
got := CreateFileWithEnvVar(tt.args.nodeToFile, tt.args.nodeType)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateFileWithEnvVar() = \ngot: %#v\nwant: %#v", got, tt.want)
}
})
}
}

// TestCreateTrustedPeerCommand checks the script to generate the multi address.
func TestCreateTrustedPeerCommand(t *testing.T) {
t.Parallel()

tests := []struct {
name string
want []string
Expand Down Expand Up @@ -79,16 +86,21 @@ cat "/tmp/TP-ADDR"
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got := CreateTrustedPeerCommand(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTrustedPeerCommand() = %v, want %v", got, tt.want)
t.Parallel()
got := CreateTrustedPeerCommand()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTrustedPeerCommand() = \ngot: %#v\nwant: %#v", got, tt.want)
}
})
}
}

// TestGetNodeIP gets the IP of the node and add it to a file
func TestGetNodeIP(t *testing.T) {
t.Parallel()

tests := []struct {
name string
want []string
Expand All @@ -97,21 +109,26 @@ func TestGetNodeIP(t *testing.T) {
name: case1,
want: []string{"sh", "-c", `
#!/bin/sh
echo -n "/ip4/$(ifconfig | grep -oE 'inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | grep -v '127.0.0.1' | awk '{print substr($2, 6)}')/tcp/2121/p2p/" > "/tmp/NODE_IP"
echo -n "/ip4/$(ifconfig | grep -oE 'inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | grep -v '127.0.0.1' | awk '{print substr($2, 6)}')/tcp/2121/p2p/,/ip4/$(ifconfig | grep -oE 'inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' | grep -v '127.0.0.1' | awk '{print substr($2, 6)}')/udp/2121/quic-v1/p2p/" >> "/tmp/NODE_IP"
cat "/tmp/NODE_IP"`},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got := GetNodeIP(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetNodeIP() = got: \n%v, \nwant: \n%v", got, tt.want)
t.Parallel()
got := GetNodeIP()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetNodeIP() = \ngot: %#v\nwant: %#v", got, tt.want)
}
})
}
}

// TestWriteToFile writes content to a file
func TestWriteToFile(t *testing.T) {
t.Parallel()

type args struct {
content string
file string
Expand Down Expand Up @@ -145,9 +162,12 @@ cat "/tmp/file_with_content"`},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got := WriteToFile(tt.args.content, tt.args.file); !reflect.DeepEqual(got, tt.want) {
t.Errorf("WriteToFile() = %v, want %v", got, tt.want)
t.Parallel()
got := WriteToFile(tt.args.content, tt.args.file)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("WriteToFile() = \ngot: %#v\nwant: %#v", got, tt.want)
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/nodes/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestSetConsNodeDefault(t *testing.T) {
NodeType: "consensus",
ContainerName: "consensus",
ContainerSetupName: "consensus-setup",
Namespace: "default",
ConnectsAsEnvVar: false,
ConnectsTo: nil,
DnsConnections: nil,
Expand All @@ -49,6 +50,7 @@ func TestSetConsNodeDefault(t *testing.T) {
NodeType: "consensus",
ContainerName: "consensus",
ContainerSetupName: "consensus-setup",
Namespace: "default",
ConnectsAsEnvVar: false,
ConnectsTo: nil,
DnsConnections: nil,
Expand All @@ -58,7 +60,7 @@ func TestSetConsNodeDefault(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SetConsNodeDefault(tt.args.peer); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SetConsNodeDefault() = %v, want %v", got, tt.want)
t.Errorf("SetConsNodeDefault() = \n%v, \nwant \n%v", got, tt.want)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/nodes/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func SetDaNodeDefault(peer config.Peer) config.Peer {
if peer.Namespace == "" {
peer.Namespace = ns
}
peer.ConnectsAsEnvVar = false
peer.ConnectsTo = nil
peer.DnsConnections = nil
return peer
}

Expand Down Expand Up @@ -152,6 +155,7 @@ func SetIdPrefix(peer config.Peer, c string, i int) (string, error) {
// check if we are using DNS or IP
if len(peer.DnsConnections) > 0 {
c = "/dns/" + peer.DnsConnections[i] + "/tcp/2121/p2p/" + c
c = "," + "/dns/" + peer.DnsConnections[i] + "/udp/2121/quic-v1/p2p/" + c
} else {
comm := k8s.GetNodeIP()
output, err := k8s.RunRemoteCommand(
Expand Down
50 changes: 40 additions & 10 deletions pkg/nodes/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func TestHasAddrAlready(t *testing.T) {
peer: config.Peer{
NodeName: "da-full-1",
NodeType: "da",
ConnectsTo: []string{"/dns/da-bridge-1/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw"},
ConnectsTo: []string{"/dns/da-bridge-1/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw,/dns/da-bridge-1/udp/2121/quic-v1/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw"},
},
i: 0,
c: "",
addPrefix: false,
},
want: "/dns/da-bridge-1/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw",
want: "/dns/da-bridge-1/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw,/dns/da-bridge-1/udp/2121/quic-v1/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw",
want1: false,
},
{
Expand All @@ -42,13 +42,13 @@ func TestHasAddrAlready(t *testing.T) {
peer: config.Peer{
NodeName: "da-full-1",
NodeType: "da",
ConnectsTo: []string{"/ip4/192.168.1.100/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw"},
ConnectsTo: []string{"/ip4/192.168.1.100/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw,/ip4/192.168.1.100/udp/2121/quic-v1/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw"},
},
i: 0,
c: "",
addPrefix: false,
},
want: "/ip4/192.168.1.100/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw",
want: "/ip4/192.168.1.100/tcp/2121/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw,/ip4/192.168.1.100/udp/2121/quic-v1/p2p/12D3KooWH1pTTJR5NXPYs2huVcJ9srmmiyGU4txHm2qgdaUVPYAw",
want1: false,
},
{
Expand Down Expand Up @@ -81,9 +81,29 @@ func TestHasAddrAlready(t *testing.T) {
want: "da-bridge-1,da-bridge-2",
want1: false,
},
{
name: "Case 4: Empty ConnectsTo slice",
args: args{
peer: config.Peer{
NodeName: "da-full-1",
NodeType: "da",
ConnectsTo: []string{},
},
i: 0,
c: "",
addPrefix: false,
},
want: "",
want1: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.args.peer.ConnectsTo == nil || len(tt.args.peer.ConnectsTo) <= tt.args.i {
t.Skip("Skipping test case with invalid index or empty ConnectsTo")
}
got, got1 := VerifyAndUpdateMultiAddress(tt.args.peer, tt.args.i, tt.args.c, tt.args.addPrefix)
if got != tt.want {
t.Errorf("VerifyAndUpdateMultiAddress() got = %v, want %v", got, tt.want)
Expand All @@ -96,6 +116,7 @@ func TestHasAddrAlready(t *testing.T) {
}

func TestSetDaNodeDefault(t *testing.T) {
t.Parallel()
type args struct {
peer config.Peer
}
Expand All @@ -117,6 +138,7 @@ func TestSetDaNodeDefault(t *testing.T) {
NodeType: "da",
ContainerName: "da",
ContainerSetupName: "da-setup",
Namespace: ns,
ConnectsAsEnvVar: false,
ConnectsTo: nil,
DnsConnections: nil,
Expand All @@ -130,13 +152,15 @@ func TestSetDaNodeDefault(t *testing.T) {
NodeType: "da",
ContainerName: "da",
ContainerSetupName: "da-setup",
Namespace: "custom-namespace",
},
},
want: config.Peer{
NodeName: "da-bridge-1",
NodeType: "da",
ContainerName: "da",
ContainerSetupName: "da-setup",
Namespace: "custom-namespace",
ConnectsAsEnvVar: false,
ConnectsTo: nil,
DnsConnections: nil,
Expand All @@ -145,15 +169,19 @@ func TestSetDaNodeDefault(t *testing.T) {
}

for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got := SetDaNodeDefault(tt.args.peer); !reflect.DeepEqual(got, tt.want) {
t.Parallel()
got := SetDaNodeDefault(tt.args.peer)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SetDaNodeDefault() = %v, want %v", got, tt.want)
}
})
}
}

func TestTruncateString(t *testing.T) {
t.Parallel()
type args struct {
input string
maxLength int
Expand Down Expand Up @@ -197,13 +225,15 @@ func TestTruncateString(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
if got, err := TruncateString(tt.args.input, tt.args.maxLength); got != tt.want {
if err != nil {
t.Errorf("ERROR: TruncateString() = %v, want %v", got, tt.want)
}
t.Parallel()
got, err := TruncateString(tt.args.input, tt.args.maxLength)
if err != nil {
t.Fatalf("TruncateString() unexpected error = %v", err)
}
if got != tt.want {
t.Errorf("TruncateString() = %v, want %v", got, tt.want)

}
})
}
Expand Down

0 comments on commit 9f277b9

Please sign in to comment.