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

feat(shwap): Add cache file #3498

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions core/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (ce *Exchange) Get(ctx context.Context, hash libhead.Hash) (*header.Extende
}

// extend block data
adder := ipld.NewProofsAdder(int(block.Data.SquareSize))
adder := ipld.NewProofsAdder(int(block.Data.SquareSize), false)
defer adder.Purge()

eds, err := extendBlock(block.Data, block.Header.Version.App, nmt.NodeVisitor(adder.VisitFn()))
Expand Down Expand Up @@ -181,7 +181,7 @@ func (ce *Exchange) getExtendedHeaderByHeight(ctx context.Context, height *int64
log.Debugw("fetched signed block from core", "height", b.Header.Height)

// extend block data
adder := ipld.NewProofsAdder(int(b.Data.SquareSize))
adder := ipld.NewProofsAdder(int(b.Data.SquareSize), false)
defer adder.Purge()

eds, err := extendBlock(b.Data, b.Header.Version.App, nmt.NodeVisitor(adder.VisitFn()))
Expand Down
2 changes: 1 addition & 1 deletion core/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (cl *Listener) handleNewSignedBlock(ctx context.Context, b types.EventDataS
attribute.Int64("height", b.Header.Height),
)
// extend block data
adder := ipld.NewProofsAdder(int(b.Data.SquareSize))
adder := ipld.NewProofsAdder(int(b.Data.SquareSize), false)
defer adder.Purge()

eds, err := extendBlock(b.Data, b.Header.Version.App, nmt.NodeVisitor(adder.VisitFn()))
Expand Down
2 changes: 1 addition & 1 deletion header/headertest/fraud/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (f *FraudMaker) MakeExtendedHeader(odsSize int, edsStore *eds.Store) header

hdr := *h
if h.Height == f.height {
adder := ipld.NewProofsAdder(odsSize)
adder := ipld.NewProofsAdder(odsSize, false)
square := edstest.RandByzantineEDS(f.t, odsSize, nmt.NodeVisitor(adder.VisitFn()))
dah, err := da.NewDataAvailabilityHeader(square)
require.NoError(f.t, err)
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func BenchmarkStore(b *testing.B) {
b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
adder := ipld.NewProofsAdder(size * 2)
adder := ipld.NewProofsAdder(size*2, false)
shares := sharetest.RandShares(b, size*size)
eds, err := rsmt2d.ComputeExtendedDataSquare(
shares,
Expand Down
2 changes: 1 addition & 1 deletion share/availability/full/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (fa *ShareAvailability) SharesAvailable(ctx context.Context, header *header
return nil
}

adder := ipld.NewProofsAdder(len(dah.RowRoots))
adder := ipld.NewProofsAdder(len(dah.RowRoots), false)
ctx = ipld.CtxWithProofsAdder(ctx, adder)
defer adder.Purge()

Expand Down
2 changes: 1 addition & 1 deletion share/eds/eds.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func getProofs(ctx context.Context, eds *rsmt2d.ExtendedDataSquare) (map[cid.Cid

// this adder ignores leaves, so that they are not added to the store we iterate through in
// writeProofs
adder := ipld.NewProofsAdder(odsWidth * 2)
adder := ipld.NewProofsAdder(odsWidth*2, false)
defer adder.Purge()

eds, err := rsmt2d.ImportExtendedDataSquare(
Expand Down
17 changes: 11 additions & 6 deletions share/ipld/nmt_adder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ func BatchSize(squareSize int) int {

// ProofsAdder is used to collect proof nodes, while traversing merkle tree
type ProofsAdder struct {
lock sync.RWMutex
proofs map[cid.Cid][]byte
lock sync.RWMutex
collectShares bool
proofs map[cid.Cid][]byte
}

// NewProofsAdder creates new instance of ProofsAdder.
func NewProofsAdder(squareSize int) *ProofsAdder {
func NewProofsAdder(squareSize int, collectShares bool) *ProofsAdder {
return &ProofsAdder{
collectShares: collectShares,
// preallocate map to fit all inner nodes for given square size
proofs: make(map[cid.Cid][]byte, innerNodesAmount(squareSize)),
}
Expand Down Expand Up @@ -156,7 +158,7 @@ func (a *ProofsAdder) VisitFn() nmt.NodeVisitorFn {
if len(a.proofs) > 0 {
return nil
}
return a.visitInnerNodes
return a.visitNodes
}

// Purge removed proofs from ProofsAdder allowing GC to collect the memory
Expand All @@ -171,10 +173,13 @@ func (a *ProofsAdder) Purge() {
a.proofs = nil
}

func (a *ProofsAdder) visitInnerNodes(hash []byte, children ...[]byte) {
func (a *ProofsAdder) visitNodes(hash []byte, children ...[]byte) {
switch len(children) {
case 1:
break
if a.collectShares {
id := MustCidFromNamespacedSha256(hash)
a.addProof(id, children[0])
}
case 2:
id := MustCidFromNamespacedSha256(hash)
a.addProof(id, append(children[0], children[1]...))
Expand Down
46 changes: 46 additions & 0 deletions share/new_eds/axis_half.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package eds

import (
"fmt"

"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/shwap"
)

var codec = share.DefaultRSMT2DCodec()

// AxisHalf represents a half of data for a row or column in the EDS.
type AxisHalf struct {
Shares []share.Share
Expand All @@ -20,3 +24,45 @@ func (a AxisHalf) ToRow() shwap.Row {
}
return shwap.NewRow(a.Shares, side)
}

func (a AxisHalf) Extended() ([]share.Share, error) {
if a.IsParity {
return reconstructShares(a.Shares)
}
return extendShares(a.Shares)
}

func extendShares(original []share.Share) ([]share.Share, error) {
if len(original) == 0 {
return nil, fmt.Errorf("original shares are empty")
}

parity, err := codec.Encode(original)
if err != nil {
return nil, fmt.Errorf("encoding: %w", err)
}

sqLen := len(original) * 2
shares := make([]share.Share, sqLen)
copy(shares, original)
copy(shares[sqLen/2:], parity)
return shares, nil
}

func reconstructShares(parity []share.Share) ([]share.Share, error) {
if len(parity) == 0 {
return nil, fmt.Errorf("parity shares are empty")
}

sqLen := len(parity) * 2

shares := make([]share.Share, sqLen)
for i := sqLen / 2; i < sqLen; i++ {
shares[i] = parity[i-sqLen/2]
}
_, err := codec.Decode(shares)
if err != nil {
return nil, fmt.Errorf("reconstructing: %w", err)
}
return shares, nil
}
31 changes: 31 additions & 0 deletions share/new_eds/axis_half_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package eds

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-node/share/sharetest"
)

func TestExtendAxisHalf(t *testing.T) {
shares := sharetest.RandShares(t, 16)

original := AxisHalf{
Shares: shares,
IsParity: false,
}

extended, err := original.Extended()
require.NoError(t, err)

parity := AxisHalf{
Shares: extended[len(shares):],
IsParity: true,
}

parityExtended, err := parity.Extended()
require.NoError(t, err)

require.Equal(t, extended, parityExtended)
}
Loading
Loading