Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(swamp): befp testing #2584

Merged
merged 15 commits into from
Sep 4, 2023
3 changes: 2 additions & 1 deletion das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
headerfraud "github.com/celestiaorg/celestia-node/header/headertest/fraud"
"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 @@ -180,7 +181,7 @@ func TestDASer_stopsAfter_BEFP(t *testing.T) {
"private",
)
require.NoError(t, fserv.Start(ctx))
mockGet.headers[1], _ = headertest.CreateFraudExtHeader(t, mockGet.headers[1], bServ)
mockGet.headers[1] = headerfraud.CreateFraudExtHeader(t, mockGet.headers[1], bServ)
newCtx := context.Background()

// create and start DASer
Expand Down
100 changes: 100 additions & 0 deletions header/headertest/fraud/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package headerfraud

import (
"context"
"testing"
"time"

"github.com/ipfs/go-blockservice"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/bytes"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
"github.com/celestiaorg/celestia-node/share/eds"
"github.com/celestiaorg/celestia-node/share/eds/edstest"
"github.com/celestiaorg/celestia-node/share/ipld"
)

// FraudMaker allows to produce an invalid header at the specified height in order to produce the
// BEFP.
type FraudMaker struct {
t *testing.T

vals []types.PrivValidator
valSet *types.ValidatorSet

// height of the invalid header
height int64

prevHash bytes.HexBytes
}

func NewFraudMaker(t *testing.T, height int64, vals []types.PrivValidator, valSet *types.ValidatorSet) *FraudMaker {
return &FraudMaker{
t: t,
vals: vals,
valSet: valSet,
height: height,
}
}

func (f *FraudMaker) MakeExtendedHeader(odsSize int, edsStore *eds.Store) header.ConstructFn {
return func(h *types.Header,
comm *types.Commit,
vals *types.ValidatorSet,
eds *rsmt2d.ExtendedDataSquare,
) (*header.ExtendedHeader, error) {
if h.Height < f.height {
return header.MakeExtendedHeader(h, comm, vals, eds)
}

hdr := *h
if h.Height == f.height {
adder := ipld.NewProofsAdder(odsSize)
square := edstest.RandByzantineEDS(f.t, odsSize, nmt.NodeVisitor(adder.VisitFn()))
vgonkivs marked this conversation as resolved.
Show resolved Hide resolved
dah, err := da.NewDataAvailabilityHeader(square)
require.NoError(f.t, err)
hdr.DataHash = dah.Hash()

ctx := ipld.CtxWithProofsAdder(context.Background(), adder)
require.NoError(f.t, edsStore.Put(ctx, h.DataHash.Bytes(), square))

*eds = *square
}
if h.Height > f.height {
hdr.LastBlockID.Hash = f.prevHash
}

blockID := comm.BlockID
blockID.Hash = hdr.Hash()
voteSet := types.NewVoteSet(hdr.ChainID, hdr.Height, 0, tmproto.PrecommitType, f.valSet)
commit, err := headertest.MakeCommit(blockID, hdr.Height, 0, voteSet, f.vals, time.Now())
require.NoError(f.t, err)

*h = hdr
*comm = *commit
f.prevHash = h.Hash()
return header.MakeExtendedHeader(h, comm, vals, eds)
}
}
func CreateFraudExtHeader(
t *testing.T,
eh *header.ExtendedHeader,
serv blockservice.BlockService,
) *header.ExtendedHeader {
square := edstest.RandByzantineEDS(t, len(eh.DAH.RowRoots))
err := ipld.ImportEDS(context.Background(), square, serv)
require.NoError(t, err)
dah, err := da.NewDataAvailabilityHeader(square)
require.NoError(t, err)
eh.DAH = &dah
eh.RawHeader.DataHash = dah.Hash()
return eh
}
48 changes: 0 additions & 48 deletions header/headertest/testing.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package headertest

import (
"context"
"crypto/rand"
"fmt"
mrand "math/rand"
"sort"
"testing"
"time"

"github.com/ipfs/go-blockservice"
logging "github.com/ipfs/go-log/v2"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/bytes"
Expand All @@ -26,12 +23,8 @@ import (
"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/share/eds/edstest"
"github.com/celestiaorg/celestia-node/share/ipld"
)

var log = logging.Logger("headertest")

// TestSuite provides everything you need to test chain of Headers.
// If not, please don't hesitate to extend it for your case.
type TestSuite struct {
Expand Down Expand Up @@ -296,32 +289,6 @@ func RandBlockID(*testing.T) types.BlockID {
return bid
}

// FraudMaker creates a custom ConstructFn that breaks the block at the given height.
func FraudMaker(t *testing.T, faultHeight int64, bServ blockservice.BlockService) header.ConstructFn {
log.Warn("Corrupting block...", "height", faultHeight)
return func(
h *types.Header,
comm *types.Commit,
vals *types.ValidatorSet,
eds *rsmt2d.ExtendedDataSquare,
) (*header.ExtendedHeader, error) {
if h.Height == faultHeight {
eh := &header.ExtendedHeader{
RawHeader: *h,
Commit: comm,
ValidatorSet: vals,
}

eh, dataSq := CreateFraudExtHeader(t, eh, bServ)
if eds != nil {
*eds = *dataSq
}
return eh, nil
}
return header.MakeExtendedHeader(h, comm, vals, eds)
}
}

func ExtendedHeaderFromEDS(t *testing.T, height uint64, eds *rsmt2d.ExtendedDataSquare) *header.ExtendedHeader {
valSet, vals := RandValidatorSet(10, 10)
gen := RandRawHeader(t)
Expand All @@ -348,21 +315,6 @@ func ExtendedHeaderFromEDS(t *testing.T, height uint64, eds *rsmt2d.ExtendedData
return eh
}

func CreateFraudExtHeader(
t *testing.T,
eh *header.ExtendedHeader,
serv blockservice.BlockService,
) (*header.ExtendedHeader, *rsmt2d.ExtendedDataSquare) {
square := edstest.RandByzantineEDS(t, len(eh.DAH.RowRoots))
err := ipld.ImportEDS(context.Background(), square, serv)
require.NoError(t, err)
dah, err := da.NewDataAvailabilityHeader(square)
require.NoError(t, err)
eh.DAH = &dah
eh.RawHeader.DataHash = dah.Hash()
return eh, square
}

type Subscriber struct {
headertest.Subscriber[*header.ExtendedHeader]
}
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/fraud/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (breaker *ServiceBreaker[S, H]) Stop(ctx context.Context) error {
}

breaker.sub.Cancel()
breaker.cancel()
defer breaker.cancel()
return breaker.Service.Stop(ctx)
}

Expand Down
6 changes: 6 additions & 0 deletions nodebuilder/p2p/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"encoding/hex"

"github.com/ipfs/go-blockservice"
"github.com/libp2p/go-libp2p/core/crypto"
hst "github.com/libp2p/go-libp2p/core/host"
"go.uber.org/fx"
Expand Down Expand Up @@ -34,3 +35,8 @@ func WithP2PKeyStr(key string) fx.Option {
func WithHost(hst hst.Host) fx.Option {
return fxutil.ReplaceAs(hst, new(HostBase))
}

// WithBlockService allows to replace the default BlockService.
func WithBlockService(bServ blockservice.BlockService) fx.Option {
return fxutil.ReplaceAs(bServ, new(blockservice.BlockService))
}
Loading
Loading