Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(share/eds): adding test utility for generating embedded test data #1320

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
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`.