Skip to content

Commit

Permalink
chore(share/eds): adding test utility for generating embedded test da…
Browse files Browse the repository at this point in the history
…ta (#1320)
  • Loading branch information
Ryan authored Nov 9, 2022
1 parent 9fc44b5 commit 3824c3b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions share/eds/eds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,40 @@ func openWrittenEDS(t *testing.T) *os.File {
require.NoError(t, err, "error opening file")
return f
}

/*
use this function as needed to create new test data.
example:
func Test_CreateData(t *testing.T) {
createTestData(t, "celestia-node/share/eds/testdata")
}
*/
func createTestData(t *testing.T, testDir string) { //nolint:unused
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
err := os.Chdir(testDir)
require.NoError(t, err, "changing to the directory")
os.RemoveAll("example.car")
require.NoError(t, err, "removing old file")
f, err := os.OpenFile("example.car", os.O_WRONLY|os.O_CREATE, 0600)
require.NoError(t, err, "opening file")

eds := share.RandEDS(t, 4)
err = WriteEDS(ctx, eds, f)
require.NoError(t, err, "writing EDS to file")
f.Close()
dah := da.NewDataAvailabilityHeader(eds)

header, err := json.MarshalIndent(dah, "", "")
require.NoError(t, err, "marshaling example root")
os.RemoveAll("example-root.json")
require.NoError(t, err, "removing old file")
f, err = os.OpenFile("example-root.json", os.O_WRONLY|os.O_CREATE, 0600)
require.NoError(t, err, "opening file")
_, err = f.Write(header)
require.NoError(t, err, "writing example root to file")
f.Close()
}
5 changes: 5 additions & 0 deletions share/eds/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CARxEDS Testdata

This directory contains an example CARv1 file of an EDS and its matching data availability header.

They might need to be regenerated when modifying constants such as the default share size. This can be done by running the test utility in `eds_test.go` called `createTestData`.

0 comments on commit 3824c3b

Please sign in to comment.