Repository: celestiaorg/celestia-node
Pull request #681 (2022-05-06 11:50:45)

chore(ipld): minor cleanups, renaming and docs fixes
Сontinuation of chores in `ipld` pkg
* Ensure retrival tests are random on each run
* Fix logs on Retriever
* Rename PutData -> AddShares
	* We work with the notion of shares, not data
	* We add them using adder thus Add and not put
* write.go -> add.go
	* Same reason, we add nodes using adder, not writing them
* read.go -> get.go
	* All the funcs in the file start with Get and renaming makes this consistent with the file name


Patch:
diff --git a/ipld/write.go b/ipld/add.go
similarity index 89%
rename from ipld/write.go
rename to ipld/add.go
index 42eef6aa..b2aa2210 100644
--- a/ipld/write.go
+++ b/ipld/add.go
@@ -13,8 +13,8 @@ import (
 	"github.com/tendermint/tendermint/pkg/wrapper"
 )
 
-// PutData erasures and extends shares to IPLD DAG using the provided ipld.NodeAdder.
-func PutData(ctx context.Context, shares []Share, adder ipld.NodeAdder) (*rsmt2d.ExtendedDataSquare, error) {
+// AddShares erasures and extends shares to IPLD DAG using the provided ipld.NodeAdder.
+func AddShares(ctx context.Context, shares []Share, adder ipld.NodeAdder) (*rsmt2d.ExtendedDataSquare, error) {
 	if len(shares) == 0 {
 		return nil, fmt.Errorf("empty data") // empty block is not an empty Data
 	}
@@ -36,7 +36,7 @@ func PutData(ctx context.Context, shares []Share, adder ipld.NodeAdder) (*rsmt2d
 }
 
 // ExtractODS returns the original shares of the given ExtendedDataSquare. This
-// is a helper function for circumstances where PutData must be used after the EDS has already
+// is a helper function for circumstances where AddShares must be used after the EDS has already
 // been generated.
 func ExtractODS(eds *rsmt2d.ExtendedDataSquare) []Share {
 	origWidth := eds.Width() / 2
diff --git a/ipld/nmt_adder.go b/ipld/nmt_adder.go
index 187a5bd3..28ab49fa 100644
--- a/ipld/nmt_adder.go
+++ b/ipld/nmt_adder.go
@@ -10,7 +10,7 @@ import (
 )
 
 // NmtNodeAdder adds ipld.Nodes to the underlying ipld.Batch if it is inserted
-// into an nmt tree
+// into a nmt tree.
 type NmtNodeAdder struct {
 	ctx    context.Context
 	add    ipld.NodeAdder
diff --git a/ipld/retriever.go b/ipld/retriever.go
index bd33dee6..5dc1ccd8 100644
--- a/ipld/retriever.go
+++ b/ipld/retriever.go
@@ -47,8 +47,9 @@ func NewRetriever(dag format.DAGService, codec rsmt2d.Codec) *Retriever {
 }
 
 // Retrieve retrieves all the data committed to DataAvailabilityHeader.
-// It aims to request from the network only 1/4 of the data and reconstructs other 3/4 parts using the rsmt2d.Codec.
-// It steadily tries to request other 3/4 data if 1/4 is not found within RetrieveQuadrantTimeout or unrecoverable.
+// If not available locally, it aims to request from the network only 1/4 of the data and reconstructs other 3/4 parts
+// using the rsmt2d.Codec. It steadily tries to request other 3/4 data if 1/4 is not found within
+// RetrieveQuadrantTimeout or unrecoverable.
 func (r *Retriever) Retrieve(ctx context.Context, dah *da.DataAvailabilityHeader) (*rsmt2d.ExtendedDataSquare, error) {
 	ses := r.newSession(ctx, dah)
 	for _, qs := range newQuadrants(dah) {
@@ -139,7 +140,7 @@ func (r *Retriever) newSession(ctx context.Context, dah *da.DataAvailabilityHead
 }
 
 func (rs *retrieverSession) retrieve(ctx context.Context, q *quadrant) (*rsmt2d.ExtendedDataSquare, error) {
-	log.Debugw("retrieving data square", "data_hash", hex.EncodeToString(rs.dah.Hash()), "size", len(rs.square))
+	log.Debugw("retrieving data square", "data_hash", hex.EncodeToString(rs.dah.Hash()), "size", len(rs.dah.RowsRoots))
 	defer func() {
 		// all shares which were requested or repaired are written to disk with the commit
 		// we store *all*, so they are served to the network, including incorrectly committed data(BEFP case),
@@ -159,7 +160,7 @@ func (rs *retrieverSession) retrieve(ctx context.Context, q *quadrant) (*rsmt2d.
 		return nil, err
 	}
 
-	log.Infow("data square reconstructed", "data_hash", hex.EncodeToString(rs.dah.Hash()), "size", len(rs.square))
+	log.Infow("data square reconstructed", "data_hash", hex.EncodeToString(rs.dah.Hash()), "size", len(rs.dah.RowsRoots))
 	return rsmt2d.ImportExtendedDataSquare(rs.square, rs.codec, rs.treeFn)
 }
 
@@ -171,7 +172,7 @@ func (rs *retrieverSession) request(ctx context.Context, q *quadrant) {
 	wg := &sync.WaitGroup{}
 	wg.Add(size)
 
-	log.Debugw("requesting quadrant", "x", q.x, "y", q.y, "len(roots)", size)
+	log.Debugw("requesting quadrant", "x", q.x, "y", q.y, "size", size)
 	for i, root := range q.roots {
 		go func(i int, root cid.Cid) {
 			defer wg.Done()
diff --git a/service/header/header.go b/service/header/header.go
index e19606ad..e90155fe 100644
--- a/service/header/header.go
+++ b/service/header/header.go
@@ -46,7 +46,7 @@ func MakeExtendedHeader(
 	var dah DataAvailabilityHeader
 	if len(b.Txs) > 0 {
 		namespacedShares, _ := b.Data.ComputeShares()
-		extended, err := ipld.PutData(ctx, namespacedShares.RawShares(), dag)
+		extended, err := ipld.AddShares(ctx, namespacedShares.RawShares(), dag)
 		if err != nil {
 			return nil, err
 		}


Test patch:
diff --git a/ipld/read.go b/ipld/get.go
similarity index 100%
rename from ipld/read.go
rename to ipld/get.go
diff --git a/ipld/read_test.go b/ipld/get_test.go
similarity index 97%
rename from ipld/read_test.go
rename to ipld/get_test.go
index f3f72020..b2858995 100644
--- a/ipld/read_test.go
+++ b/ipld/get_test.go
@@ -38,7 +38,7 @@ func TestGetShare(t *testing.T) {
 
 	// generate random shares for the nmt
 	shares := RandShares(t, size*size)
-	eds, err := PutData(ctx, shares, dag)
+	eds, err := AddShares(ctx, shares, dag)
 	require.NoError(t, err)
 
 	for i, leaf := range shares {
@@ -168,7 +168,7 @@ func TestGetSharesByNamespace(t *testing.T) {
 			tt.rawData[(len(tt.rawData)/2)+1] = expected
 
 			// put raw data in DAG
-			eds, err := PutData(ctx, tt.rawData, dag)
+			eds, err := AddShares(ctx, tt.rawData, dag)
 			require.NoError(t, err)
 
 			for _, row := range eds.RowRoots() {
@@ -228,7 +228,7 @@ func TestGetLeavesByNamespace_AbsentNamespaceId(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			eds, err := PutData(ctx, shares, dag)
+			eds, err := AddShares(ctx, shares, dag)
 			require.NoError(t, err)
 			assertNoRowContainsNID(t, dag, eds, tt.missingNid)
 		})
@@ -254,7 +254,7 @@ func TestGetLeavesByNamespace_MultipleRowsContainingSameNamespaceId(t *testing.T
 		copy(nspace, commonNamespaceData)
 	}
 
-	eds, err := PutData(ctx, shares, dag)
+	eds, err := AddShares(ctx, shares, dag)
 	require.NoError(t, err)
 
 	for _, row := range eds.RowRoots() {
@@ -292,7 +292,7 @@ func TestBatchSize(t *testing.T) {
 			dag := merkledag.NewDAGService(blockservice.New(bs, offline.Exchange(bs)))
 
 			eds := RandEDS(t, tt.origWidth)
-			_, err := PutData(ctx, ExtractODS(eds), dag)
+			_, err := AddShares(ctx, ExtractODS(eds), dag)
 			require.NoError(t, err)
 
 			out, err := bs.AllKeysChan(ctx)
@@ -336,7 +336,7 @@ func TestGetProof(t *testing.T) {
 	dag := mdutils.Mock()
 
 	shares := RandShares(t, width*width)
-	in, err := PutData(ctx, shares, dag)
+	in, err := AddShares(ctx, shares, dag)
 	require.NoError(t, err)
 
 	dah := da.NewDataAvailabilityHeader(in)
@@ -372,7 +372,7 @@ func TestGetProofs(t *testing.T) {
 	dag := mdutils.Mock()
 
 	shares := RandShares(t, width*width)
-	in, err := PutData(ctx, shares, dag)
+	in, err := AddShares(ctx, shares, dag)
 	require.NoError(t, err)
 
 	dah := da.NewDataAvailabilityHeader(in)
diff --git a/ipld/retriever_test.go b/ipld/retriever_test.go
index ff8d757f..aeecd07e 100644
--- a/ipld/retriever_test.go
+++ b/ipld/retriever_test.go
@@ -2,6 +2,7 @@ package ipld
 
 import (
 	"context"
+	"math/rand"
 	"testing"
 	"time"
 
@@ -12,6 +13,8 @@ import (
 )
 
 func TestRetriever_Retrieve(t *testing.T) {
+	rand.Seed(time.Now().UnixNano()) // otherwise, the quadrant sampling is deterministic in tests
+
 	ctx, cancel := context.WithCancel(context.Background())
 	defer cancel()
 
@@ -33,11 +36,11 @@ func TestRetriever_Retrieve(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
 			// generate EDS
 			shares := RandShares(t, tc.squareSize*tc.squareSize)
-			in, err := PutData(ctx, shares, dag)
+			in, err := AddShares(ctx, shares, dag)
 			require.NoError(t, err)
 
 			// limit with timeout, specifically retrieval
-			ctx, cancel := context.WithTimeout(ctx, time.Minute*2) // the timeout is big for the max size which is long
+			ctx, cancel := context.WithTimeout(ctx, time.Minute*5) // the timeout is big for the max size which is long
 			defer cancel()
 
 			dah := da.NewDataAvailabilityHeader(in)
diff --git a/service/share/share_test.go b/service/share/share_test.go
index 9df3cc7a..c5f49bb9 100644
--- a/service/share/share_test.go
+++ b/service/share/share_test.go
@@ -271,7 +271,7 @@ func TestSharesRoundTrip(t *testing.T) {
 
 			// test full round trip - with IPLD + decoding shares
 			{
-				extSquare, err := ipld.PutData(ctx, namespacedShares.RawShares(), store)
+				extSquare, err := ipld.AddShares(ctx, namespacedShares.RawShares(), store)
 				require.NoError(t, err)
 
 				dah := da.NewDataAvailabilityHeader(extSquare)