Skip to content

Commit

Permalink
feat!: delete getTreeNameFromConstructorFn (#287)
Browse files Browse the repository at this point in the history
Closes #286 by deleting
`getTreeNameFromConstructorFn`.
Closes #288,
#289 by fixing those tests.
Inspired by #278
Includes two breaking changes:
1. `ComputeExtendedDataSquare` now accepts a parameter `treeName:
string`
2. `ImportExtendedDataSquare` now accepts a parameter `treeName: string`

---------

Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
  • Loading branch information
rootulp and sontrinh16 committed Feb 2, 2024
1 parent 2d0cfbd commit 5a03c15
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 228 deletions.
15 changes: 6 additions & 9 deletions extendeddatacrossword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRepairExtendedDataSquare(t *testing.T) {
flattened[12], flattened[13] = nil, nil

// Re-import the data square.
eds, err := ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
eds, err := ImportExtendedDataSquare(flattened, codec, DefaultTreeName)
if err != nil {
t.Errorf("ImportExtendedDataSquare failed: %v", err)
}
Expand All @@ -67,7 +67,7 @@ func TestRepairExtendedDataSquare(t *testing.T) {
flattened[12], flattened[13], flattened[14] = nil, nil, nil

// Re-import the data square.
eds, err := ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
eds, err := ImportExtendedDataSquare(flattened, codec, DefaultTreeName)
if err != nil {
t.Errorf("ImportExtendedDataSquare failed: %v", err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func BenchmarkRepair(b *testing.B) {

// Generate a new range original data square then extend it
square := genRandDS(originalDataWidth, shareSize)
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
eds, err := ComputeExtendedDataSquare(square, codec, DefaultTreeName)
if err != nil {
b.Error(err)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func BenchmarkRepair(b *testing.B) {
}

// Re-import the data square.
eds, _ = ImportExtendedDataSquare(flattened, codec, NewDefaultTree)
eds, _ = ImportExtendedDataSquare(flattened, codec, DefaultTreeName)

b.StartTimer()

Expand All @@ -301,7 +301,7 @@ func createTestEds(codec Codec, shareSize int) *ExtendedDataSquare {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
}, codec, DefaultTreeName)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -443,10 +443,7 @@ func createTestEdsWithNMT(t *testing.T, codec Codec, shareSize, namespaceSize in
shares[i] = bytes.Repeat([]byte{byte(shareValue)}, shareSize)
}

treeConstructorFn, err := TreeFn("testing-tree")
require.NoError(t, err)

eds, err := ComputeExtendedDataSquare(shares, codec, treeConstructorFn)
eds, err := ComputeExtendedDataSquare(shares, codec, "testing-tree")
require.NoError(t, err)

return eds
Expand Down
30 changes: 12 additions & 18 deletions extendeddatasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,11 @@ func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
return err
}

var treeConstructor TreeConstructorFn
if aux.Tree == "" {
aux.Tree = DefaultTreeName
}

treeConstructor, err = TreeFn(aux.Tree)
if err != nil {
return err
}

importedEds, err := ImportExtendedDataSquare(aux.DataSquare, codecs[aux.Codec], treeConstructor)
importedEds, err := ImportExtendedDataSquare(aux.DataSquare, codecs[aux.Codec], aux.Tree)
if err != nil {
return err
}
Expand All @@ -66,7 +60,7 @@ func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
func ComputeExtendedDataSquare(
data [][]byte,
codec Codec,
treeCreatorFn TreeConstructorFn,
treeName string,
) (*ExtendedDataSquare, error) {
if len(data) > codec.MaxChunks() {
return nil, errors.New("number of chunks exceeds the maximum")
Expand All @@ -78,14 +72,14 @@ func ComputeExtendedDataSquare(
return nil, err
}

ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
treeCreatorFn, err := TreeFn(treeName)
if err != nil {
return nil, err
}

treeName := getTreeNameFromConstructorFn(treeCreatorFn)
if treeName == "" {
return nil, errors.New("tree name not found")
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
}

eds := ExtendedDataSquare{dataSquare: ds, codec: codec, treeName: treeName}
Expand All @@ -101,7 +95,7 @@ func ComputeExtendedDataSquare(
func ImportExtendedDataSquare(
data [][]byte,
codec Codec,
treeCreatorFn TreeConstructorFn,
treeName string,
) (*ExtendedDataSquare, error) {
if len(data) > 4*codec.MaxChunks() {
return nil, errors.New("number of chunks exceeds the maximum")
Expand All @@ -113,14 +107,14 @@ func ImportExtendedDataSquare(
return nil, err
}

ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
treeCreatorFn, err := TreeFn(treeName)
if err != nil {
return nil, err
}

treeName := getTreeNameFromConstructorFn(treeCreatorFn)
if treeName == "" {
return nil, errors.New("tree name not found")
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
}

eds := ExtendedDataSquare{dataSquare: ds, codec: codec, treeName: treeName}
Expand Down Expand Up @@ -254,7 +248,7 @@ func (eds *ExtendedDataSquare) erasureExtendCol(codec Codec, i uint) error {
}

func (eds *ExtendedDataSquare) deepCopy(codec Codec) (ExtendedDataSquare, error) {
imported, err := ImportExtendedDataSquare(eds.Flattened(), codec, eds.createTreeFn)
imported, err := ImportExtendedDataSquare(eds.Flattened(), codec, eds.treeName)
return *imported, err
}

Expand Down

0 comments on commit 5a03c15

Please sign in to comment.