Skip to content

Commit 534aec9

Browse files
committed
refactoring
1 parent 16f31bb commit 534aec9

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

main.go cmd/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
crypto "github.com/libp2p/go-libp2p/core/crypto"
1717
"github.com/libp2p/go-libp2p/core/network"
1818
"github.com/libp2p/go-libp2p/core/peer"
19+
"github.com/xm0onh/avid-go/network"
1920
)
2021

2122
const rendezvousString = "libp2p-mdns"

discovery.go network/discovery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package network
22

33
import (
44
"fmt"

network.go network/network.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package network
22

33
import (
44
"bufio"

rs/rs-test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package rs
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestRSEncodeDecode(t *testing.T) {
10+
originalData := "HelloLibP2P"
11+
shards, err := RSEncode(originalData)
12+
assert.NoError(t, err, "RSEncode should not return an error")
13+
14+
shards[1] = nil
15+
16+
decodedData, err := RSDecode(shards)
17+
assert.NoError(t, err, "RSDecode should not return an error")
18+
assert.Equal(t, originalData, decodedData, "Decoded data should match the original")
19+
}

util.go rs/rs.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
package main
21

2+
package rs
33
import (
44
"bytes"
55
"fmt"
66

77
"github.com/klauspost/reedsolomon"
88
)
9-
10-
func DivideIntoChunks(data string, numChunks int) []string {
11-
chunkLength := len(data) / numChunks
12-
chunks := make([]string, numChunks)
13-
14-
for i := 0; i < numChunks; i++ {
15-
start := i * chunkLength
16-
end := start + chunkLength
17-
if i == numChunks-1 {
18-
end = len(data)
19-
}
20-
chunks[i] = data[start:end]
21-
}
22-
return chunks
23-
}
24-
259
// RS parameters
2610
const dataShards = 3
2711
const parityShards = 2

utils/util.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package util
2+
3+
4+
5+
func DivideIntoChunks(data string, numChunks int) []string {
6+
chunkLength := len(data) / numChunks
7+
chunks := make([]string, numChunks)
8+
9+
for i := 0; i < numChunks; i++ {
10+
start := i * chunkLength
11+
end := start + chunkLength
12+
if i == numChunks-1 {
13+
end = len(data)
14+
}
15+
chunks[i] = data[start:end]
16+
}
17+
return chunks
18+
}

0 commit comments

Comments
 (0)