Skip to content

Commit

Permalink
first draft block store
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Oct 6, 2021
1 parent fc5d319 commit e9b49f8
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ipld/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestRetrieveBlockData(t *testing.T) {
// // generate EDS
eds := generateRandEDS(t, tc.squareSize)

shares := convertEDStoShares(eds)
shares := ConvertEDStoShares(eds)

in, err := PutData(ctx, shares, dag)
require.NoError(t, err)
Expand Down Expand Up @@ -158,7 +158,7 @@ func Test_ConvertEDStoShares(t *testing.T) {
eds, err := rsmt2d.ComputeExtendedDataSquare(rawshares, rsmt2d.NewRSGF8Codec(), tree.Constructor)
require.NoError(t, err)

resshares := convertEDStoShares(eds)
resshares := ConvertEDStoShares(eds)
require.Equal(t, rawshares, resshares)
}

Expand Down
6 changes: 4 additions & 2 deletions ipld/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func PutData(ctx context.Context, shares [][]byte, adder ipld.NodeAdder) (*rsmt2
batchAdder := NewNmtNodeAdder(ctx, ipld.NewBatch(ctx, adder))
// create the nmt wrapper to generate row and col commitments
squareSize := uint32(math.Sqrt(float64(len(shares))))
fmt.Println("square size when put data", squareSize)
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(squareSize), nmt.NodeVisitor(batchAdder.Visit))
// recompute the eds
eds, err := rsmt2d.ComputeExtendedDataSquare(shares, rsmt2d.NewRSGF8Codec(), tree.Constructor)
Expand All @@ -34,7 +33,10 @@ func PutData(ctx context.Context, shares [][]byte, adder ipld.NodeAdder) (*rsmt2
return eds, batchAdder.Commit()
}

func convertEDStoShares(eds *rsmt2d.ExtendedDataSquare) [][]byte {
// ConvertEDStoShares converts an ExtendedDataSquare back into its original shares. This
// is a helper function for circumstances where PutData must be used after the EDS has already
// been generated.
func ConvertEDStoShares(eds *rsmt2d.ExtendedDataSquare) [][]byte {
origWidth := eds.Width() / 2
origShares := make([][]byte, origWidth*origWidth)
for i := uint(0); i < origWidth; i++ {
Expand Down
1 change: 1 addition & 0 deletions service/block/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ func (s *Service) handleRawBlock(raw *RawBlock) error {
return err
}
// TODO @renaynay: store extended block

return nil
}
58 changes: 37 additions & 21 deletions service/block/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"testing"

"github.com/celestiaorg/celestia-core/testutils"
"github.com/celestiaorg/celestia-node/service/header"
md "github.com/ipfs/go-merkledag/test"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-node/service/header"
)

func Test_listenForNewBlocks(t *testing.T) {
mockFetcher := &mockFetcher{
mockNewBlockCh: make(chan *RawBlock),
}
serv := NewBlockService(mockFetcher)
serv := NewBlockService(mockFetcher, md.Mock()) // TODO @renaynay: add mock dag service

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
Expand Down Expand Up @@ -46,26 +48,40 @@ func (m *mockFetcher) UnsubscribeNewBlockEvent(ctx context.Context) error {
}

func (m *mockFetcher) generateBlocks(t *testing.T, num int) {
t.Helper()

for i := 0; i < num; i++ {
data, err := testutils.GenerateRandomBlockData(1, 1, 1, 1, 40)
if err != nil {
t.Fatal(err)
}
rawBlock := &RawBlock{
Data: data,
}
// extend the data to get the data hash
extendedData, err := extendBlockData(rawBlock)
if err != nil {
t.Fatal(err)
}
dah, err := header.DataAvailabilityHeaderFromExtendedData(extendedData)
if err != nil {
t.Fatal(err)
}
rawBlock.Header = header.RawHeader{
DataHash: dah.Hash(),
}
rawBlock, _ := generateRawAndExtendedBlock(t)
m.mockNewBlockCh <- rawBlock
}
}

func generateRawAndExtendedBlock(t *testing.T) (*RawBlock, *Block) {
t.Helper()

data, err := testutils.GenerateRandomBlockData(1, 1, 1, 1, 40)
if err != nil {
t.Fatal(err)
}
rawBlock := &RawBlock{
Data: data,
}
// extend the data to get the data hash
extendedData, err := extendBlockData(rawBlock)
if err != nil {
t.Fatal(err)
}
dah, err := header.DataAvailabilityHeaderFromExtendedData(extendedData)
if err != nil {
t.Fatal(err)
}
rawBlock.Header = header.RawHeader{
DataHash: dah.Hash(),
}
return rawBlock, &Block{
header: &header.ExtendedHeader{
DAH: &dah,
},
data: extendedData,
}
}
5 changes: 5 additions & 0 deletions service/block/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ type Fetcher interface {
SubscribeNewBlockEvent(ctx context.Context) (<-chan *RawBlock, error)
UnsubscribeNewBlockEvent(ctx context.Context) error
}

type Store interface {
GetBlockByHash(hash []byte) (*Block, error)
Store(block *Block) error
}
5 changes: 4 additions & 1 deletion service/block/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package block
import (
"context"

ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
)

Expand All @@ -14,14 +15,16 @@ import (
// 4. Serving erasure coded blocks to other `Full` node peers.
type Service struct {
fetcher Fetcher
store ipld.DAGService
}

var log = logging.Logger("block-service")

// NewBlockService creates a new instance of block Service.
func NewBlockService(fetcher Fetcher) *Service {
func NewBlockService(fetcher Fetcher, store ipld.DAGService) *Service {
return &Service{
fetcher: fetcher,
store: store,
}
}

Expand Down
21 changes: 21 additions & 0 deletions service/block/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package block

import (
"context"

"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-core/pkg/da"
"github.com/celestiaorg/celestia-node/ipld"
)

func (s *Service) StoreBlockData(ctx context.Context, data *ExtendedBlockData) error {
shares := ipld.ConvertEDStoShares(data)
// TODO @renaynay: it's inefficient that we generate the EDS twice
_, err := ipld.PutData(ctx, shares, s.store)
return err
}

func (s *Service) GetBlockData(ctx context.Context, dah *da.DataAvailabilityHeader) (*ExtendedBlockData, error) {
return ipld.RetrieveData(ctx, dah, s.store, rsmt2d.NewRSGF8Codec())
}
30 changes: 30 additions & 0 deletions service/block/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package block

import (
"context"
"testing"

md "github.com/ipfs/go-merkledag/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestService_BlockStore(t *testing.T) {
// create mock block service (fetcher is not necessary here)
mockStore := md.Mock()
serv := NewBlockService(nil, mockStore)

_, block := generateRawAndExtendedBlock(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err := serv.StoreBlockData(ctx, block.Data())
require.NoError(t, err)

eds, err := serv.GetBlockData(ctx, block.Header().DAH)
require.NoError(t, err)
assert.Equal(t, block.data.Width(), eds.Width())
assert.Equal(t, block.data.RowRoots(), eds.RowRoots())
assert.Equal(t, block.data.ColRoots(), eds.ColRoots())
}
5 changes: 5 additions & 0 deletions service/block/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (b *Block) LastCommit() *core.Commit {
return b.lastCommit
}

// DataSize returns the size of the ExtendedBlockData.
func (b *Block) DataSize() uint {
return b.data.Width()
}

// BadEncodingError contains all relevant information to
// generate a BadEncodingFraudProof.
type BadEncodingError struct{}

0 comments on commit e9b49f8

Please sign in to comment.