github.com/sunrise-zone/sunrise-node@v0.13.1-sr2/share/ipld/add.go (about)

     1  package ipld
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/ipfs/boxo/blockservice"
     8  
     9  	"github.com/celestiaorg/nmt"
    10  	"github.com/celestiaorg/rsmt2d"
    11  	"github.com/sunrise-zone/sunrise-app/pkg/wrapper"
    12  
    13  	"github.com/sunrise-zone/sunrise-node/libs/utils"
    14  	"github.com/sunrise-zone/sunrise-node/share"
    15  )
    16  
    17  // AddShares erasures and extends shares to blockservice.BlockService using the provided
    18  // ipld.NodeAdder.
    19  func AddShares(
    20  	ctx context.Context,
    21  	shares []share.Share,
    22  	adder blockservice.BlockService,
    23  ) (*rsmt2d.ExtendedDataSquare, error) {
    24  	if len(shares) == 0 {
    25  		return nil, fmt.Errorf("empty data") // empty block is not an empty Data
    26  	}
    27  	squareSize := int(utils.SquareSize(len(shares)))
    28  	// create nmt adder wrapping batch adder with calculated size
    29  	batchAdder := NewNmtNodeAdder(ctx, adder, MaxSizeBatchOption(squareSize*2))
    30  	// create the nmt wrapper to generate row and col commitments
    31  	// recompute the eds
    32  	eds, err := rsmt2d.ComputeExtendedDataSquare(
    33  		shares,
    34  		share.DefaultRSMT2DCodec(),
    35  		wrapper.NewConstructor(uint64(squareSize),
    36  			nmt.NodeVisitor(batchAdder.Visit)),
    37  	)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("failure to recompute the extended data square: %w", err)
    40  	}
    41  	// compute roots
    42  	_, err = eds.RowRoots()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	// commit the batch to ipfs
    47  	return eds, batchAdder.Commit()
    48  }
    49  
    50  // ImportShares imports flattened chunks of data into Extended Data square and saves it in
    51  // blockservice.BlockService
    52  func ImportShares(
    53  	ctx context.Context,
    54  	shares [][]byte,
    55  	adder blockservice.BlockService,
    56  ) (*rsmt2d.ExtendedDataSquare, error) {
    57  	if len(shares) == 0 {
    58  		return nil, fmt.Errorf("ipld: importing empty data")
    59  	}
    60  	squareSize := int(utils.SquareSize(len(shares)))
    61  	// create nmt adder wrapping batch adder with calculated size
    62  	batchAdder := NewNmtNodeAdder(ctx, adder, MaxSizeBatchOption(squareSize*2))
    63  	// recompute the eds
    64  	eds, err := rsmt2d.ImportExtendedDataSquare(
    65  		shares,
    66  		share.DefaultRSMT2DCodec(),
    67  		wrapper.NewConstructor(uint64(squareSize/2),
    68  			nmt.NodeVisitor(batchAdder.Visit)),
    69  	)
    70  	if err != nil {
    71  		return nil, fmt.Errorf("failure to recompute the extended data square: %w", err)
    72  	}
    73  	// compute roots
    74  	_, err = eds.RowRoots()
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	// commit the batch to DAG
    79  	return eds, batchAdder.Commit()
    80  }
    81  
    82  func ImportEDS(ctx context.Context, square *rsmt2d.ExtendedDataSquare, adder blockservice.BlockService) error {
    83  	shares := square.Flattened()
    84  	_, err := ImportShares(ctx, shares, adder)
    85  	return err
    86  }