Skip to content

Commit

Permalink
tests(swamp): befp testing (#2584)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs authored Sep 4, 2023
1 parent 80d6dba commit d75a743
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 156 deletions.
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()))
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

0 comments on commit d75a743

Please sign in to comment.