Skip to content

Commit

Permalink
ipld: move for dag service to block service
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed May 19, 2022
1 parent d99e946 commit fe0268a
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 134 deletions.
17 changes: 9 additions & 8 deletions das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"testing"
"time"

"github.com/ipfs/go-blockservice"

"github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
format "github.com/ipfs/go-ipld-format"
mdutils "github.com/ipfs/go-merkledag/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -23,7 +24,7 @@ var timeout = time.Second * 15
// the DASer checkpoint is updated to network head.
func TestDASerLifecycle(t *testing.T) {
ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
dag := mdutils.Mock()
dag := mdutils.Bserv()

// 15 headers from the past and 15 future headers
mockGet, shareServ, sub := createDASerSubcomponents(t, dag, 15, 15)
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestDASerLifecycle(t *testing.T) {

func TestDASer_Restart(t *testing.T) {
ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
dag := mdutils.Mock()
dag := mdutils.Bserv()

// 15 headers from the past and 15 future headers
mockGet, shareServ, sub := createDASerSubcomponents(t, dag, 15, 15)
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestDASer_Restart(t *testing.T) {

func TestDASer_catchUp(t *testing.T) {
ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
dag := mdutils.Mock()
dag := mdutils.Bserv()

mockGet, shareServ, _ := createDASerSubcomponents(t, dag, 5, 0)

Expand Down Expand Up @@ -165,7 +166,7 @@ func TestDASer_catchUp(t *testing.T) {
// difference of 1
func TestDASer_catchUp_oneHeader(t *testing.T) {
ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
dag := mdutils.Mock()
dag := mdutils.Bserv()

mockGet, shareServ, _ := createDASerSubcomponents(t, dag, 6, 0)
daser := NewDASer(shareServ, nil, mockGet, ds)
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestDASer_catchUp_oneHeader(t *testing.T) {
// mockGetter, share.Service, and mock header.Subscriber.
func createDASerSubcomponents(
t *testing.T,
dag format.DAGService,
dag blockservice.BlockService,
numGetter,
numSub int,
) (*mockGetter, *share.Service, *header.DummySubscriber) {
Expand All @@ -236,7 +237,7 @@ func createDASerSubcomponents(
func (m *mockGetter) fillSubWithHeaders(
t *testing.T,
sub *header.DummySubscriber,
dag format.DAGService,
dag blockservice.BlockService,
startHeight,
endHeight int,
) {
Expand Down Expand Up @@ -266,7 +267,7 @@ type mockGetter struct {
headers map[int64]*header.ExtendedHeader
}

func (m *mockGetter) generateHeaders(t *testing.T, dag format.DAGService, startHeight, endHeight int) {
func (m *mockGetter) generateHeaders(t *testing.T, dag blockservice.BlockService, startHeight, endHeight int) {
for i := startHeight; i < endHeight; i++ {
dah := share.RandFillDAG(t, 16, dag)

Expand Down
2 changes: 1 addition & 1 deletion fraud/bad_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestFraudProofValidation(t *testing.T) {
dag := mdutils.Mock()
dag := mdutils.Bserv()
eds := ipld.RandEDS(t, 2)

shares := ipld.ExtractEDS(eds)
Expand Down
5 changes: 3 additions & 2 deletions fraud/pb/proof.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions header/core/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"context"
"fmt"

format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-blockservice"

logging "github.com/ipfs/go-log/v2"

tmbytes "github.com/tendermint/tendermint/libs/bytes"
Expand All @@ -18,11 +19,11 @@ var log = logging.Logger("header/core")

type Exchange struct {
fetcher *core.BlockFetcher
shareStore format.DAGService
shareStore blockservice.BlockService
construct header.ConstructFn
}

func NewExchange(fetcher *core.BlockFetcher, dag format.DAGService, construct header.ConstructFn) *Exchange {
func NewExchange(fetcher *core.BlockFetcher, dag blockservice.BlockService, construct header.ConstructFn) *Exchange {
return &Exchange{
fetcher: fetcher,
shareStore: dag,
Expand Down
2 changes: 1 addition & 1 deletion header/core/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestCoreExchange_RequestHeaders(t *testing.T) {
fetcher := createCoreFetcher(t)
store := mdutils.Mock()
store := mdutils.Bserv()

// generate 10 blocks
generateBlocks(t, fetcher)
Expand Down
7 changes: 4 additions & 3 deletions header/core/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"fmt"

format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-blockservice"

pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/tendermint/tendermint/types"

Expand All @@ -22,15 +23,15 @@ import (
type Listener struct {
bcast header.Broadcaster
fetcher *core.BlockFetcher
dag format.DAGService
dag blockservice.BlockService
construct header.ConstructFn
cancel context.CancelFunc
}

func NewListener(
bcast header.Broadcaster,
fetcher *core.BlockFetcher,
dag format.DAGService,
dag blockservice.BlockService,
construct header.ConstructFn,
) *Listener {
return &Listener{
Expand Down
2 changes: 1 addition & 1 deletion header/core/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ func createListener(
require.NoError(t, err)
})

return NewListener(p2pSub, fetcher, mdutils.Mock(), header.MakeExtendedHeader)
return NewListener(p2pSub, fetcher, mdutils.Bserv(), header.MakeExtendedHeader)
}
5 changes: 3 additions & 2 deletions header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"context"
"fmt"

"github.com/ipfs/go-blockservice"

logging "github.com/ipfs/go-log/v2"

format "github.com/ipfs/go-ipld-format"
bts "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/pkg/da"
core "github.com/tendermint/tendermint/types"
Expand Down Expand Up @@ -43,7 +44,7 @@ func MakeExtendedHeader(
b *core.Block,
comm *core.Commit,
vals *core.ValidatorSet,
dag format.NodeAdder,
dag blockservice.BlockService,
) (*ExtendedHeader, error) {
var dah DataAvailabilityHeader
if len(b.Txs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMakeExtendedHeaderForEmptyBlock(t *testing.T) {
_, client := core.StartTestClient(t)
fetcher := core.NewBlockFetcher(client)

store := mdutils.Mock()
store := mdutils.Bserv()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
5 changes: 3 additions & 2 deletions header/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"fmt"

format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-blockservice"

pubsub "github.com/libp2p/go-libp2p-pubsub"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
core "github.com/tendermint/tendermint/types"
Expand All @@ -17,7 +18,7 @@ type ConstructFn = func(
*core.Block,
*core.Commit,
*core.ValidatorSet,
format.NodeAdder,
blockservice.BlockService,
) (*ExtendedHeader, error)

// Validator aliases a func that validates ExtendedHeader.
Expand Down
3 changes: 2 additions & 1 deletion header/p2p/pb/extended_header_request.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions header/pb/extended_header.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions ipld/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ import (
"fmt"
"math"

ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-blockservice"

"github.com/celestiaorg/celestia-node/ipld/plugin"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"

"github.com/tendermint/tendermint/pkg/wrapper"
)

// AddShares erasures and extends shares to IPLD DAG using the provided ipld.NodeAdder.
func AddShares(ctx context.Context, shares []Share, adder ipld.NodeAdder) (*rsmt2d.ExtendedDataSquare, error) {
func AddShares(
ctx context.Context,
shares []Share,
na blockservice.BlockService,
) (*rsmt2d.ExtendedDataSquare, error) {
if len(shares) == 0 {
return nil, fmt.Errorf("empty data") // empty block is not an empty Data
}
squareSize := int(math.Sqrt(float64(len(shares))))
// create nmt adder wrapping batch adder with calculated size
bs := batchSize(squareSize * 2)
batchAdder := NewNmtNodeAdder(ctx, ipld.NewBatch(ctx, adder, ipld.MaxSizeBatchOption(bs)))
batchAdder := NewNmtNodeAdder(ctx, plugin.NewBatch(ctx, na, plugin.MaxSizeBatchOption(bs)))
// create the nmt wrapper to generate row and col commitments
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(squareSize), nmt.NodeVisitor(batchAdder.Visit))
// recompute the eds
Expand All @@ -36,14 +41,18 @@ func AddShares(ctx context.Context, shares []Share, adder ipld.NodeAdder) (*rsmt
}

// ImportShares imports flattend chunks of data into Extended Data square and saves it in IPLD DAG
func ImportShares(ctx context.Context, shares [][]byte, na ipld.NodeAdder) (*rsmt2d.ExtendedDataSquare, error) {
func ImportShares(
ctx context.Context,
shares [][]byte,
na blockservice.BlockService,
) (*rsmt2d.ExtendedDataSquare, error) {
if len(shares) == 0 {
return nil, fmt.Errorf("ipld: importing empty data")
}
squareSize := int(math.Sqrt(float64(len(shares))))
// create nmt adder wrapping batch adder with calculated size
bs := batchSize(squareSize * 2)
batchAdder := NewNmtNodeAdder(ctx, ipld.NewBatch(ctx, na, ipld.MaxSizeBatchOption(bs)))
batchAdder := NewNmtNodeAdder(ctx, plugin.NewBatch(ctx, na, plugin.MaxSizeBatchOption(bs)))
// create the nmt wrapper to generate row and col commitments
tree := wrapper.NewErasuredNamespacedMerkleTree(uint64(squareSize/2), nmt.NodeVisitor(batchAdder.Visit))
// recompute the eds
Expand Down
Loading

0 comments on commit fe0268a

Please sign in to comment.