Skip to content

Commit

Permalink
removing ShareService
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Dec 20, 2022
1 parent d96f79e commit f5d47bf
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 129 deletions.
17 changes: 3 additions & 14 deletions nodebuilder/share/constructors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package share

import (
"context"

"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/host"
Expand All @@ -13,7 +11,7 @@ import (
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/availability/cache"
disc "github.com/celestiaorg/celestia-node/share/availability/discovery"
"github.com/celestiaorg/celestia-node/share/service"
"github.com/celestiaorg/celestia-node/share/getters"
)

func discovery(cfg Config) func(routing.ContentRouting, host.Host) *disc.Discovery {
Expand All @@ -40,15 +38,6 @@ func cacheAvailability[A share.Availability](lc fx.Lifecycle, ds datastore.Batch
return ca
}

func newModule(lc fx.Lifecycle, bServ blockservice.BlockService, avail share.Availability) Module {
serv := service.NewShareService(bServ, avail)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return serv.Start(ctx)
},
OnStop: func(ctx context.Context) error {
return serv.Stop(ctx)
},
})
return &module{serv}
func newModule(bServ blockservice.BlockService, avail share.Availability) Module {
return &module{getters.NewIPLDGetter(bServ), avail}
}
8 changes: 4 additions & 4 deletions nodebuilder/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/celestiaorg/nmt/namespace"

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

var _ Module = (*API)(nil)
Expand Down Expand Up @@ -89,9 +88,10 @@ func (api *API) GetSharesByNamespace(
}

type module struct {
*service.ShareService
share.Getter
share.Availability
}

func (m *module) SharesAvailable(ctx context.Context, root *share.Root) error {
return m.ShareService.SharesAvailable(ctx, root)
func (m module) SharesAvailable(ctx context.Context, root *share.Root) error {
return m.Availability.SharesAvailable(ctx, root)
}
24 changes: 11 additions & 13 deletions share/availability/cache/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/celestiaorg/celestia-node/share"
availability_test "github.com/celestiaorg/celestia-node/share/availability/test"
"github.com/celestiaorg/celestia-node/share/service"
)

// TestCacheAvailability tests to ensure that the successful result of a
Expand All @@ -31,27 +30,27 @@ func TestCacheAvailability(t *testing.T) {
lightLocalServ, dah1 := RandLightLocalServiceWithSquare(t, 16)

var tests = []struct {
service *service.ShareService
root *share.Root
avail share.Availability
root *share.Root
}{
{
service: fullLocalServ,
root: dah0,
avail: fullLocalServ,
root: dah0,
},
{
service: lightLocalServ,
root: dah1,
avail: lightLocalServ,
root: dah1,
},
}

for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ca := tt.service.Availability.(*ShareAvailability)
ca := tt.avail.(*ShareAvailability)
// ensure the dah isn't yet in the cache
exists, err := ca.ds.Has(ctx, rootKey(tt.root))
require.NoError(t, err)
assert.False(t, exists)
err = tt.service.SharesAvailable(ctx, tt.root)
err = tt.avail.SharesAvailable(ctx, tt.root)
require.NoError(t, err)
// ensure the dah was stored properly
exists, err = ca.ds.Has(ctx, rootKey(tt.root))
Expand All @@ -72,9 +71,8 @@ func TestCacheAvailability_Failed(t *testing.T) {
defer cancel()

ca := NewShareAvailability(&dummyAvailability{}, sync.MutexWrap(datastore.NewMapDatastore()))
serv := service.NewShareService(mdutils.Bserv(), ca)

err := serv.SharesAvailable(ctx, &invalidHeader)
err := ca.SharesAvailable(ctx, &invalidHeader)
require.Error(t, err)
// ensure the dah was NOT cached
exists, err := ca.ds.Has(ctx, rootKey(&invalidHeader))
Expand Down Expand Up @@ -112,10 +110,10 @@ func TestCacheAvailability_MinRoot(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

fullLocalServ, _ := RandFullLocalServiceWithSquare(t, 16)
fullLocalAvail, _ := RandFullLocalServiceWithSquare(t, 16)
minDAH := da.MinDataAvailabilityHeader()

err := fullLocalServ.SharesAvailable(ctx, &minDAH)
err := fullLocalAvail.SharesAvailable(ctx, &minDAH)
assert.NoError(t, err)
}

Expand Down
9 changes: 4 additions & 5 deletions share/availability/cache/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@ import (
"github.com/celestiaorg/celestia-node/share/availability/full"
"github.com/celestiaorg/celestia-node/share/availability/light"
availability_test "github.com/celestiaorg/celestia-node/share/availability/test"
"github.com/celestiaorg/celestia-node/share/service"
)

// RandLightLocalServiceWithSquare is the same as light.RandServiceWithSquare, except
// the share.Availability is wrapped with cache availability.
func RandLightLocalServiceWithSquare(t *testing.T, n int) (*service.ShareService, *share.Root) {
func RandLightLocalServiceWithSquare(t *testing.T, n int) (share.Availability, *share.Root) {
bServ := mdutils.Bserv()
store := dssync.MutexWrap(ds.NewMapDatastore())
avail := NewShareAvailability(
light.TestAvailability(bServ),
store,
)
return service.NewShareService(bServ, avail), availability_test.RandFillBS(t, n, bServ)
return avail, availability_test.RandFillBS(t, n, bServ)
}

// RandFullLocalServiceWithSquare is the same as full.RandServiceWithSquare, except
// the share.Availability is wrapped with cache availability.
func RandFullLocalServiceWithSquare(t *testing.T, n int) (*service.ShareService, *share.Root) {
func RandFullLocalServiceWithSquare(t *testing.T, n int) (share.Availability, *share.Root) {
bServ := mdutils.Bserv()
store := dssync.MutexWrap(ds.NewMapDatastore())
avail := NewShareAvailability(
full.TestAvailability(bServ),
store,
)
return service.NewShareService(bServ, avail), availability_test.RandFillBS(t, n, bServ)
return avail, availability_test.RandFillBS(t, n, bServ)
}
4 changes: 2 additions & 2 deletions share/availability/full/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSharesAvailable_Full(t *testing.T) {
defer cancel()

// RandServiceWithSquare creates a NewShareAvailability inside, so we can test it
service, dah := RandServiceWithSquare(t, 16)
err := service.SharesAvailable(ctx, dah)
_, availability, dah := RandServiceWithSquare(t, 16)
err := availability.SharesAvailable(ctx, dah)
assert.NoError(t, err)
}
11 changes: 6 additions & 5 deletions share/availability/full/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/availability/discovery"
availability_test "github.com/celestiaorg/celestia-node/share/availability/test"
"github.com/celestiaorg/celestia-node/share/service"
"github.com/celestiaorg/celestia-node/share/getters"
)

// RandServiceWithSquare provides a service.ShareService filled with 'n' NMT
// RandServiceWithSquare provides a share.Getter filled with 'n' NMT
// trees of 'n' random shares, essentially storing a whole square.
func RandServiceWithSquare(t *testing.T, n int) (*service.ShareService, *share.Root) {
func RandServiceWithSquare(t *testing.T, n int) (share.Getter, share.Availability, *share.Root) {
bServ := mdutils.Bserv()
return service.NewShareService(bServ, TestAvailability(bServ)), availability_test.RandFillBS(t, n, bServ)
return getters.NewIPLDGetter(bServ), TestAvailability(bServ), availability_test.RandFillBS(t, n, bServ)
}

// RandNode creates a Full Node filled with a random block of the given size.
Expand All @@ -31,7 +31,8 @@ func RandNode(dn *availability_test.TestDagNet, squareSize int) (*availability_t
// Node creates a new empty Full Node.
func Node(dn *availability_test.TestDagNet) *availability_test.TestNode {
nd := dn.NewTestNode()
nd.ShareService = service.NewShareService(nd.BlockService, TestAvailability(nd.BlockService))
nd.Getter = getters.NewIPLDGetter(nd.BlockService)
nd.Availability = TestAvailability(nd.BlockService)
return nd
}

Expand Down
42 changes: 16 additions & 26 deletions share/availability/light/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestSharesAvailable(t *testing.T) {
defer cancel()

// RandServiceWithSquare creates a Light ShareAvailability inside, so we can test it
service, dah := RandServiceWithSquare(t, 16)
err := service.SharesAvailable(ctx, dah)
_, avail, dah := RandServiceWithSquare(t, 16)
err := avail.SharesAvailable(ctx, dah)
assert.NoError(t, err)
}

Expand All @@ -44,9 +44,9 @@ func TestSharesAvailableFailed(t *testing.T) {
defer cancel()

// RandServiceWithSquare creates a Light ShareAvailability inside, so we can test it
s, _ := RandServiceWithSquare(t, 16)
_, avail, _ := RandServiceWithSquare(t, 16)
empty := header.EmptyDAH()
err := s.SharesAvailable(ctx, &empty)
err := avail.SharesAvailable(ctx, &empty)
assert.Error(t, err)
}

Expand All @@ -68,20 +68,15 @@ func TestGetShare(t *testing.T) {
defer cancel()

n := 16
serv, dah := RandServiceWithSquare(t, n)
err := serv.Start(ctx)
require.NoError(t, err)
getter, _, dah := RandServiceWithSquare(t, n)

for i := range make([]bool, n) {
for j := range make([]bool, n) {
sh, err := serv.GetShare(ctx, dah, i, j)
sh, err := getter.GetShare(ctx, dah, i, j)
assert.NotNil(t, sh)
assert.NoError(t, err)
}
}

err = serv.Stop(ctx)
require.NoError(t, err)
}

func TestService_GetSharesByNamespace(t *testing.T) {
Expand All @@ -96,7 +91,7 @@ func TestService_GetSharesByNamespace(t *testing.T) {

for _, tt := range tests {
t.Run("size: "+strconv.Itoa(tt.squareSize), func(t *testing.T) {
serv, bServ := RandService()
getter, _, bServ := RandService()
n := tt.squareSize * tt.squareSize
randShares := share.RandShares(t, n)
idx1 := (n - 1) / 2
Expand All @@ -108,7 +103,7 @@ func TestService_GetSharesByNamespace(t *testing.T) {
root := availability_test.FillBS(t, bServ, randShares)
randNID := randShares[idx1][:8]

shares, err := serv.GetSharesByNamespace(context.Background(), root, randNID)
shares, err := getter.GetSharesByNamespace(context.Background(), root, randNID)
require.NoError(t, err)
assert.Len(t, shares, tt.expectedShareCount)
for _, value := range shares {
Expand All @@ -128,11 +123,9 @@ func TestGetShares(t *testing.T) {
defer cancel()

n := 16
serv, dah := RandServiceWithSquare(t, n)
err := serv.Start(ctx)
require.NoError(t, err)
getter, _, dah := RandServiceWithSquare(t, n)

shares, err := serv.GetShares(ctx, dah)
shares, err := getter.GetShares(ctx, dah)
require.NoError(t, err)

flattened := make([][]byte, 0, len(shares)*2)
Expand All @@ -147,16 +140,13 @@ func TestGetShares(t *testing.T) {
gotDAH := da.NewDataAvailabilityHeader(eds)

require.True(t, dah.Equals(&gotDAH))

err = serv.Stop(ctx)
require.NoError(t, err)
}

func TestService_GetSharesByNamespaceNotFound(t *testing.T) {
serv, root := RandServiceWithSquare(t, 1)
getter, _, root := RandServiceWithSquare(t, 1)
root.RowsRoots = nil

shares, err := serv.GetSharesByNamespace(context.Background(), root, []byte{1, 1, 1, 1, 1, 1, 1, 1})
shares, err := getter.GetSharesByNamespace(context.Background(), root, []byte{1, 1, 1, 1, 1, 1, 1, 1})
assert.Len(t, shares, 0)
assert.NoError(t, err)
}
Expand All @@ -173,20 +163,20 @@ func BenchmarkService_GetSharesByNamespace(b *testing.B) {
for _, tt := range tests {
b.Run(strconv.Itoa(tt.amountShares), func(b *testing.B) {
t := &testing.T{}
serv, root := RandServiceWithSquare(t, tt.amountShares)
getter, _, root := RandServiceWithSquare(t, tt.amountShares)
randNID := root.RowsRoots[(len(root.RowsRoots)-1)/2][:8]
root.RowsRoots[(len(root.RowsRoots) / 2)] = root.RowsRoots[(len(root.RowsRoots)-1)/2]
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := serv.GetSharesByNamespace(context.Background(), root, randNID)
_, err := getter.GetSharesByNamespace(context.Background(), root, randNID)
require.NoError(t, err)
}
})
}
}

func TestSharesRoundTrip(t *testing.T) {
serv, store := RandService()
getter, _, store := RandService()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -325,7 +315,7 @@ func TestSharesRoundTrip(t *testing.T) {
require.NoError(t, err)

dah := da.NewDataAvailabilityHeader(extSquare)
shares, err := serv.GetSharesByNamespace(ctx, &dah, namespace)
shares, err := getter.GetSharesByNamespace(ctx, &dah, namespace)
require.NoError(t, err)
require.NotEmpty(t, shares)

Expand Down
17 changes: 9 additions & 8 deletions share/availability/light/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/availability/discovery"
availability_test "github.com/celestiaorg/celestia-node/share/availability/test"
"github.com/celestiaorg/celestia-node/share/service"
"github.com/celestiaorg/celestia-node/share/getters"
)

// RandServiceWithSquare provides a share.Service filled with 'n' NMT
// RandServiceWithSquare provides a share.Getter/share.Availability filled with 'n' NMT
// trees of 'n' random shares, essentially storing a whole square.
func RandServiceWithSquare(t *testing.T, n int) (*service.ShareService, *share.Root) {
func RandServiceWithSquare(t *testing.T, n int) (share.Getter, share.Availability, *share.Root) {
bServ := mdutils.Bserv()

return service.NewShareService(bServ, TestAvailability(bServ)), availability_test.RandFillBS(t, n, bServ)
return getters.NewIPLDGetter(bServ), TestAvailability(bServ), availability_test.RandFillBS(t, n, bServ)
}

// RandService provides an unfilled share.Service with corresponding
// RandService provides an unfilled share.Getter/share.Availability with corresponding
// blockservice.BlockService than can be filled by the test.
func RandService() (*service.ShareService, blockservice.BlockService) {
func RandService() (share.Getter, share.Availability, blockservice.BlockService) {
bServ := mdutils.Bserv()
return service.NewShareService(bServ, TestAvailability(bServ)), bServ
return getters.NewIPLDGetter(bServ), TestAvailability(bServ), bServ
}

// RandNode creates a Light Node filled with a random block of the given size.
Expand All @@ -39,7 +39,8 @@ func RandNode(dn *availability_test.TestDagNet, squareSize int) (*availability_t
// Node creates a new empty Light Node.
func Node(dn *availability_test.TestDagNet) *availability_test.TestNode {
nd := dn.NewTestNode()
nd.ShareService = service.NewShareService(nd.BlockService, TestAvailability(nd.BlockService))
nd.Getter = getters.NewIPLDGetter(nd.BlockService)
nd.Availability = TestAvailability(nd.BlockService)
return nd
}

Expand Down
4 changes: 2 additions & 2 deletions share/availability/test/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/service"
)

// RandFillBS fills the given BlockService with a random block of a given size.
Expand All @@ -38,7 +37,8 @@ func FillBS(t *testing.T, bServ blockservice.BlockService, shares []share.Share)

type TestNode struct {
net *TestDagNet
*service.ShareService
share.Getter
share.Availability
blockservice.BlockService
host.Host
}
Expand Down
Loading

0 comments on commit f5d47bf

Please sign in to comment.