Skip to content

Commit

Permalink
refactor(libs/fraud): fraud tests overhaul (#2039)
Browse files Browse the repository at this point in the history
The main goal here is to prepare everything for extraction. There was a cyclic dependency and dependence on ExtendedHeader inside of fraud, which blocked the extraction. This patch fixes this in conjunction with cleanings for testing utilities and actual tests.

List of other notable changes:
-  Updates go-header and
	- migrates to the new API for testing in header
- Restructures fraud pkg and extract fraud.Service and related logic into sub pkg fraudserv
	- Keeping only interface definitions inside of the root fraud
- Extracts testing utilities into fraudtest
- Adds sprinkles of documentation
  • Loading branch information
Wondertan committed Apr 12, 2023
1 parent 186755f commit c37fcd4
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 540 deletions.
19 changes: 10 additions & 9 deletions das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
"github.com/celestiaorg/celestia-node/libs/fraud"
"github.com/celestiaorg/celestia-node/libs/fraud/fraudserv"
"github.com/celestiaorg/celestia-node/libs/fraud/fraudtest"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/availability/full"
"github.com/celestiaorg/celestia-node/share/availability/light"
Expand Down Expand Up @@ -160,7 +162,7 @@ func TestDASer_stopsAfter_BEFP(t *testing.T) {
getter := func(ctx context.Context, height uint64) (libhead.Header, error) {
return mockGet.GetByHeight(ctx, height)
}
f := fraud.NewProofService(ps, net.Hosts()[0], getter, ds, false, "private")
f := fraudserv.NewProofService(ps, net.Hosts()[0], getter, ds, false, "private")
require.NoError(t, f.Start(ctx))
mockGet.headers[1], _ = headertest.CreateFraudExtHeader(t, mockGet.headers[1], bServ)
newCtx := context.Background()
Expand Down Expand Up @@ -205,8 +207,8 @@ func TestDASerSampleTimeout(t *testing.T) {
})

ds := ds_sync.MutexWrap(datastore.NewMapDatastore())
sub := new(headertest.DummySubscriber)
f := new(fraud.DummyService)
sub := new(headertest.Subscriber)
f := new(fraudtest.DummyService)

// create and start DASer
daser, err := NewDASer(avail, sub, getter, ds, f, newBroadcastMock(1), WithSampleTimeout(1))
Expand All @@ -231,9 +233,9 @@ func createDASerSubcomponents(
bServ blockservice.BlockService,
numGetter,
numSub int,
) (*mockGetter, *headertest.DummySubscriber, *fraud.DummyService) {
) (*mockGetter, *headertest.Subscriber, *fraudtest.DummyService) {
mockGet, sub := createMockGetterAndSub(t, bServ, numGetter, numSub)
fraud := new(fraud.DummyService)
fraud := new(fraudtest.DummyService)
return mockGet, sub, fraud
}

Expand All @@ -242,7 +244,7 @@ func createMockGetterAndSub(
bServ blockservice.BlockService,
numGetter,
numSub int,
) (*mockGetter, *headertest.DummySubscriber) {
) (*mockGetter, *headertest.Subscriber) {
mockGet := &mockGetter{
headers: make(map[int64]*header.ExtendedHeader),
doneCh: make(chan struct{}),
Expand All @@ -251,16 +253,15 @@ func createMockGetterAndSub(

mockGet.generateHeaders(t, bServ, 0, numGetter)

sub := new(headertest.DummySubscriber)
sub := new(headertest.Subscriber)
mockGet.fillSubWithHeaders(t, sub, bServ, numGetter, numGetter+numSub)

return mockGet, sub
}

// fillSubWithHeaders generates `num` headers from the future for p2pSub to pipe through to DASer.
func (m *mockGetter) fillSubWithHeaders(
t *testing.T,
sub *headertest.DummySubscriber,
sub *headertest.Subscriber,
bServ blockservice.BlockService,
startHeight,
endHeight int,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921
github.com/benbjohnson/clock v1.3.0
github.com/celestiaorg/celestia-app v0.12.2
github.com/celestiaorg/go-header v0.2.3
github.com/celestiaorg/go-header v0.2.4
github.com/celestiaorg/go-libp2p-messenger v0.2.0
github.com/celestiaorg/nmt v0.15.0
github.com/celestiaorg/rsmt2d v0.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ github.com/celestiaorg/cosmos-sdk v1.8.0-sdk-v0.46.7 h1:EADZy33ufskVIy6Rj6jbi3SO
github.com/celestiaorg/cosmos-sdk v1.8.0-sdk-v0.46.7/go.mod h1:vg3Eza9adJJ5Mdx6boz5MpZsZcTZyrfTVYZHyi2zLm4=
github.com/celestiaorg/dagstore v0.0.0-20230404123415-177451f83136 h1:LBvY3NDA18fcS72pBAEd2pENoUpz1iV4cCXBN2Zrj/I=
github.com/celestiaorg/dagstore v0.0.0-20230404123415-177451f83136/go.mod h1:ta/DlqIH10bvhwqJIw51Nq3QU4XVMp6pz3f0Deve9fM=
github.com/celestiaorg/go-header v0.2.3 h1:41r60OtAeexWC3J3eTELgWfzcdKR2taFlfcJ/2IHZD4=
github.com/celestiaorg/go-header v0.2.3/go.mod h1:6XKf0yhoEQqfKQTZnyTZjTjF5jH5Wq9uO9AvDMkdYbs=
github.com/celestiaorg/go-header v0.2.4 h1:GZk0/CT/DI7p/3BUNC1bBmx5qk7PiujVseVzVj/LCqM=
github.com/celestiaorg/go-header v0.2.4/go.mod h1:6XKf0yhoEQqfKQTZnyTZjTjF5jH5Wq9uO9AvDMkdYbs=
github.com/celestiaorg/go-libp2p-messenger v0.2.0 h1:/0MuPDcFamQMbw9xTZ73yImqgTO3jHV7wKHvWD/Irao=
github.com/celestiaorg/go-libp2p-messenger v0.2.0/go.mod h1:s9PIhMi7ApOauIsfBcQwbr7m+HBzmVfDIS+QLdgzDSo=
github.com/celestiaorg/go-verifcid v0.0.1-lazypatch h1:9TSe3w1cmJmbWlweCwCTIZkan7jV8M+KwglXpdD+UG8=
Expand Down
48 changes: 11 additions & 37 deletions header/headertest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/ipfs/go-blockservice"
logging "github.com/ipfs/go-log/v2"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/bytes"
Expand All @@ -23,7 +22,7 @@ import (

"github.com/celestiaorg/celestia-app/pkg/da"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/test"
"github.com/celestiaorg/go-header/headertest"
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/header"
Expand All @@ -44,6 +43,10 @@ type TestSuite struct {
head *header.ExtendedHeader
}

func NewStore(t *testing.T) libhead.Store[*header.ExtendedHeader] {
return headertest.NewStore[*header.ExtendedHeader](t, NewTestSuite(t, 3), 10)
}

// NewTestSuite setups a new test suite with a given number of validators.
func NewTestSuite(t *testing.T, num int) *TestSuite {
valSet, vals := RandValidatorSet(num, 10)
Expand Down Expand Up @@ -125,18 +128,14 @@ func (s *TestSuite) Head() *header.ExtendedHeader {
func (s *TestSuite) GenExtendedHeaders(num int) []*header.ExtendedHeader {
headers := make([]*header.ExtendedHeader, num)
for i := range headers {
headers[i] = s.GenExtendedHeader()
headers[i] = s.NextHeader()
}
return headers
}

func (s *TestSuite) GetRandomHeader() *header.ExtendedHeader {
return s.GenExtendedHeader()
}

var _ test.Generator[*header.ExtendedHeader] = &TestSuite{}
var _ headertest.Generator[*header.ExtendedHeader] = &TestSuite{}

func (s *TestSuite) GenExtendedHeader() *header.ExtendedHeader {
func (s *TestSuite) NextHeader() *header.ExtendedHeader {
if s.head == nil {
s.head = s.genesis()
return s.head
Expand Down Expand Up @@ -334,33 +333,8 @@ func CreateFraudExtHeader(
return eh, extended
}

type DummySubscriber struct {
Headers []*header.ExtendedHeader
}

func (mhs *DummySubscriber) AddValidator(func(context.Context, *header.ExtendedHeader) pubsub.ValidationResult) error {
return nil
}

func (mhs *DummySubscriber) Subscribe() (libhead.Subscription[*header.ExtendedHeader], error) {
return mhs, nil
}

func (mhs *DummySubscriber) NextHeader(context.Context) (*header.ExtendedHeader, error) {
defer func() {
if len(mhs.Headers) > 1 {
// pop the already-returned header
cp := mhs.Headers
mhs.Headers = cp[1:]
} else {
mhs.Headers = make([]*header.ExtendedHeader, 0)
}
}()
if len(mhs.Headers) == 0 {
return nil, context.Canceled
}
return mhs.Headers[0], nil
type Subscriber struct {
headertest.Subscriber[*header.ExtendedHeader]
}

func (mhs *DummySubscriber) Stop(context.Context) error { return nil }
func (mhs *DummySubscriber) Cancel() {}
var _ libhead.Subscriber[*header.ExtendedHeader] = &Subscriber{}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fraud
package fraudserv

import (
"math"
Expand Down
12 changes: 9 additions & 3 deletions libs/fraud/helpers.go → libs/fraud/fraudserv/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fraud
package fraudserv

import (
"context"
Expand All @@ -7,6 +7,8 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"

"github.com/celestiaorg/celestia-node/libs/fraud"
)

func PubsubTopicID(fraudType, networkID string) string {
Expand All @@ -17,8 +19,12 @@ func protocolID(networkID string) protocol.ID {
return protocol.ID(fmt.Sprintf("/%s/fraud/v0.0.1", networkID))
}

func join(p *pubsub.PubSub, proofType ProofType, networkID string,
validate func(context.Context, ProofType, peer.ID, *pubsub.Message) pubsub.ValidationResult) (*pubsub.Topic, error) {
func join(
p *pubsub.PubSub,
proofType fraud.ProofType,
networkID string,
validate func(context.Context, fraud.ProofType, peer.ID, *pubsub.Message) pubsub.ValidationResult,
) (*pubsub.Topic, error) {
topic := PubsubTopicID(proofType.String(), networkID)
log.Infow("joining topic", "id", topic)
t, err := p.Join(topic)
Expand Down

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

File renamed without changes.
4 changes: 2 additions & 2 deletions libs/fraud/requester.go → libs/fraud/fraudserv/requester.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fraud
package fraudserv

import (
"context"
Expand All @@ -9,7 +9,7 @@ import (

"github.com/celestiaorg/go-libp2p-messenger/serde"

pb "github.com/celestiaorg/celestia-node/libs/fraud/pb"
pb "github.com/celestiaorg/celestia-node/libs/fraud/fraudserv/pb"
)

const (
Expand Down
Loading

0 comments on commit c37fcd4

Please sign in to comment.