Skip to content

Commit

Permalink
migrate to Accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jun 12, 2024
1 parent 115882c commit 0e6a558
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
7 changes: 4 additions & 3 deletions share/shwap/p2p/bitswap/block.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package bitswap

import (
"context"
"fmt"

"github.com/gogo/protobuf/proto"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
logger "github.com/ipfs/go-log/v2"

bitswappb "github.com/celestiaorg/celestia-node/share/shwap/p2p/bitswap/pb"
eds "github.com/celestiaorg/celestia-node/share/new_eds"

"github.com/celestiaorg/rsmt2d"
bitswappb "github.com/celestiaorg/celestia-node/share/shwap/p2p/bitswap/pb"

"github.com/celestiaorg/celestia-node/share"
)
Expand All @@ -29,7 +30,7 @@ type Block interface {
CID() cid.Cid
// BlockFromEDS extract Bitswap Block out of the EDS.
// TODO: Split into MarshalBinary and Populate
BlockFromEDS(*rsmt2d.ExtendedDataSquare) (blocks.Block, error)
BlockFromEDS(context.Context, eds.Accessor) (blocks.Block, error)

// IsEmpty reports whether the Block been populated with Shwap container.
// If the Block is empty, it can be populated with Fetch.
Expand Down
12 changes: 7 additions & 5 deletions share/shwap/p2p/bitswap/block_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

eds "github.com/celestiaorg/celestia-node/share/new_eds"

"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/share"
Expand Down Expand Up @@ -102,14 +104,14 @@ func fetcher(ctx context.Context, t *testing.T, bstore blockstore.Blockstore) ex
}

type testBlockstore struct {
eds *rsmt2d.ExtendedDataSquare
eds eds.Accessor
}

func newTestBlockstore(eds *rsmt2d.ExtendedDataSquare) *testBlockstore {
return &testBlockstore{eds: eds}
func newTestBlockstore(rsmt2sEds *rsmt2d.ExtendedDataSquare) *testBlockstore {
return &testBlockstore{eds: eds.Rsmt2D{ExtendedDataSquare: rsmt2sEds}}
}

func (b *testBlockstore) Get(_ context.Context, cid cid.Cid) (blocks.Block, error) {
func (b *testBlockstore) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) {
spec, ok := specRegistry[cid.Prefix().MhType]
if !ok {
return nil, fmt.Errorf("unsupported codec")
Expand All @@ -120,7 +122,7 @@ func (b *testBlockstore) Get(_ context.Context, cid cid.Cid) (blocks.Block, erro
return nil, err
}

return bldr.BlockFromEDS(b.eds)
return bldr.BlockFromEDS(ctx, b.eds)
}

func (b *testBlockstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) {
Expand Down
13 changes: 10 additions & 3 deletions share/shwap/p2p/bitswap/row_block.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package bitswap

import (
"context"
"fmt"
"sync/atomic"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"

eds "github.com/celestiaorg/celestia-node/share/new_eds"

"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/share"
Expand Down Expand Up @@ -68,9 +71,13 @@ func (rb *RowBlock) CID() cid.Cid {
return encodeCID(rb.ID, rowMultihashCode, rowCodec)
}

func (rb *RowBlock) BlockFromEDS(eds *rsmt2d.ExtendedDataSquare) (blocks.Block, error) {
row := shwap.RowFromEDS(eds, rb.ID.RowIndex, shwap.Left)
blk, err := toBlock(rb.CID(), row.ToProto())
func (rb *RowBlock) BlockFromEDS(ctx context.Context, eds eds.Accessor) (blocks.Block, error) {
half, err := eds.AxisHalf(ctx, rsmt2d.Row, rb.ID.RowIndex)
if err != nil {
return nil, fmt.Errorf("getting Row AxisHalf: %w", err)
}

blk, err := toBlock(rb.CID(), half.ToRow().ToProto())
if err != nil {
return nil, fmt.Errorf("converting Row to Bitswap block: %w", err)
}
Expand Down
11 changes: 6 additions & 5 deletions share/shwap/p2p/bitswap/row_namespace_data_block.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package bitswap

import (
"context"
"fmt"
"sync/atomic"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"

shwappb "github.com/celestiaorg/celestia-node/share/shwap/pb"
eds "github.com/celestiaorg/celestia-node/share/new_eds"

"github.com/celestiaorg/rsmt2d"
shwappb "github.com/celestiaorg/celestia-node/share/shwap/pb"

"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/shwap"
Expand Down Expand Up @@ -75,10 +76,10 @@ func (rndb *RowNamespaceDataBlock) CID() cid.Cid {
return encodeCID(rndb.ID, rowNamespaceDataMultihashCode, rowNamespaceDataCodec)
}

func (rndb *RowNamespaceDataBlock) BlockFromEDS(eds *rsmt2d.ExtendedDataSquare) (blocks.Block, error) {
rnd, err := shwap.RowNamespaceDataFromEDS(eds, rndb.ID.DataNamespace, rndb.ID.RowIndex)
func (rndb *RowNamespaceDataBlock) BlockFromEDS(ctx context.Context, eds eds.Accessor) (blocks.Block, error) {
rnd, err := eds.RowNamespaceData(ctx, rndb.ID.DataNamespace, rndb.ID.RowIndex)
if err != nil {
return nil, err
return nil, fmt.Errorf("getting RowNamespaceData: %w", err)
}

blk, err := toBlock(rndb.CID(), rnd.ToProto())
Expand Down
11 changes: 6 additions & 5 deletions share/shwap/p2p/bitswap/sample_block.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package bitswap

import (
"context"
"fmt"
"sync/atomic"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"

shwappb "github.com/celestiaorg/celestia-node/share/shwap/pb"
eds "github.com/celestiaorg/celestia-node/share/new_eds"

"github.com/celestiaorg/rsmt2d"
shwappb "github.com/celestiaorg/celestia-node/share/shwap/pb"

"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/shwap"
Expand Down Expand Up @@ -70,10 +71,10 @@ func (sb *SampleBlock) CID() cid.Cid {
return encodeCID(sb.ID, sampleMultihashCode, sampleCodec)
}

func (sb *SampleBlock) BlockFromEDS(eds *rsmt2d.ExtendedDataSquare) (blocks.Block, error) {
smpl, err := shwap.SampleFromEDS(eds, rsmt2d.Row, sb.ID.RowIndex, sb.ID.ShareIndex)
func (sb *SampleBlock) BlockFromEDS(ctx context.Context, eds eds.Accessor) (blocks.Block, error) {
smpl, err := eds.Sample(ctx, sb.ID.RowIndex, sb.ID.ShareIndex)
if err != nil {
return nil, err
return nil, fmt.Errorf("getting Sample: %w", err)
}

blk, err := toBlock(sb.CID(), smpl.ToProto())
Expand Down

0 comments on commit 0e6a558

Please sign in to comment.