From 5edc35069e94a89be9563f5133508aa11b509329 Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Wed, 6 Oct 2021 12:21:43 +0100 Subject: [PATCH] refactor prependNode --- ipld/plugin/nmt.go | 16 +++++++++------- ipld/read.go | 2 +- ipld/share.go | 2 -- ipld/test_helpers.go | 2 +- ipld/write.go | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ipld/plugin/nmt.go b/ipld/plugin/nmt.go index 4a15879eef..f38b61dafe 100644 --- a/ipld/plugin/nmt.go +++ b/ipld/plugin/nmt.go @@ -35,11 +35,13 @@ const ( // DagParserFormatName can be used when putting into the IPLD Dag DagParserFormatName = "extended-square-row-or-col" + // ShareSize system wide default size for data shares. + ShareSize = 256 + // Repeated here to avoid a dependency to the wrapping repo as this makes // it hard to compile and use the plugin against a local ipfs version. // TODO: plugins have config options; make this configurable instead namespaceSize = 8 - shareSize = 256 // nmtHashSize is the size of a digest created by an NMT in bytes. nmtHashSize = 2*namespaceSize + sha256.Size ) @@ -113,7 +115,7 @@ func DataSquareRowOrColumnRawInputParser(r io.Reader, _mhType uint64, _mhLen int ) for { - namespacedLeaf := make([]byte, shareSize+namespaceSize) + namespacedLeaf := make([]byte, ShareSize+namespaceSize) if _, err := io.ReadFull(br, namespacedLeaf); err != nil { if err == io.EOF { break @@ -137,7 +139,7 @@ type nmtNodeCollector struct { func newNodeCollector() *nmtNodeCollector { // extendedRowOrColumnSize is hardcoded here to avoid importing - const extendedRowOrColumnSize = 2 * 128 + extendedRowOrColumnSize := 2 * 128 return &nmtNodeCollector{nodes: make([]ipld.Node, 0, extendedRowOrColumnSize)} } @@ -165,10 +167,10 @@ func (n *nmtNodeCollector) visit(hash []byte, children ...[]byte) { } func prependNode(newNode ipld.Node, nodes []ipld.Node) []ipld.Node { - nodes = append(nodes, ipld.Node(nil)) - copy(nodes[1:], nodes) - nodes[0] = newNode - return nodes + prepended := make([]ipld.Node, len(nodes)+1) + prepended[0] = newNode + copy(prepended[1:], nodes) + return prepended } func NmtNodeParser(block blocks.Block) (ipld.Node, error) { diff --git a/ipld/read.go b/ipld/read.go index a907643ba6..78653c756a 100644 --- a/ipld/read.go +++ b/ipld/read.go @@ -140,7 +140,7 @@ func (sc *shareCounter) retrieveShare( } } - if len(data) < ShareSize { + if len(data) < plugin.ShareSize { return } diff --git a/ipld/share.go b/ipld/share.go index 1b0294f7b9..3139d1a0c3 100644 --- a/ipld/share.go +++ b/ipld/share.go @@ -5,8 +5,6 @@ import "github.com/celestiaorg/nmt/namespace" const ( // MaxSquareSize is currently the maximum size supported for unerasured data in rsmt2d.ExtendedDataSquare. MaxSquareSize = 128 - // ShareSize system wide default size for data shares. - ShareSize = 256 // NamespaceSize is a system wide size for NMT namespaces. // TODO(Wondertan): Should be part of IPLD/NMT plugin NamespaceSize = 8 diff --git a/ipld/test_helpers.go b/ipld/test_helpers.go index 0a989b2c16..e39c41c81b 100644 --- a/ipld/test_helpers.go +++ b/ipld/test_helpers.go @@ -73,7 +73,7 @@ func RandNamespacedShares(t *testing.T, total int) NamespacedShares { shares := make(NamespacedShares, total) for i := 0; i < total; i++ { shares[i].ID = data[i] - shares[i].Share = make([]byte, NamespaceSize+ShareSize) + shares[i].Share = make([]byte, NamespaceSize+plugin.ShareSize) copy(shares[i].Share[:NamespaceSize], data[i]) _, err := mrand.Read(shares[i].Share[NamespaceSize:]) // nolint:gosec // G404: Use of weak random number generator require.NoError(t, err) diff --git a/ipld/write.go b/ipld/write.go index 0fef408dfc..befe7154b8 100644 --- a/ipld/write.go +++ b/ipld/write.go @@ -33,6 +33,7 @@ func PutData(ctx context.Context, shares [][]byte, adder ipld.NodeAdder) (*rsmt2 return eds, batchAdder.Commit() } +// convertEDStoShares returns the original shares of the given ExtendedDataSquare. func convertEDStoShares(eds *rsmt2d.ExtendedDataSquare) [][]byte { origWidth := eds.Width() / 2 origShares := make([][]byte, origWidth*origWidth)