github.com/celestiaorg/celestia-node@v0.15.0-beta.1/header/headertest/fraud/testing.go (about) 1 package headerfraud 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/ipfs/boxo/blockservice" 9 "github.com/stretchr/testify/require" 10 "github.com/tendermint/tendermint/libs/bytes" 11 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 12 "github.com/tendermint/tendermint/types" 13 14 "github.com/celestiaorg/celestia-app/pkg/da" 15 "github.com/celestiaorg/nmt" 16 "github.com/celestiaorg/rsmt2d" 17 18 "github.com/celestiaorg/celestia-node/header" 19 "github.com/celestiaorg/celestia-node/header/headertest" 20 "github.com/celestiaorg/celestia-node/share/eds" 21 "github.com/celestiaorg/celestia-node/share/eds/edstest" 22 "github.com/celestiaorg/celestia-node/share/ipld" 23 ) 24 25 // FraudMaker allows to produce an invalid header at the specified height in order to produce the 26 // BEFP. 27 type FraudMaker struct { 28 t *testing.T 29 30 vals []types.PrivValidator 31 valSet *types.ValidatorSet 32 33 // height of the invalid header 34 height int64 35 36 prevHash bytes.HexBytes 37 } 38 39 func NewFraudMaker(t *testing.T, height int64, vals []types.PrivValidator, valSet *types.ValidatorSet) *FraudMaker { 40 return &FraudMaker{ 41 t: t, 42 vals: vals, 43 valSet: valSet, 44 height: height, 45 } 46 } 47 48 func (f *FraudMaker) MakeExtendedHeader(odsSize int, edsStore *eds.Store) header.ConstructFn { 49 return func( 50 h *types.Header, 51 comm *types.Commit, 52 vals *types.ValidatorSet, 53 eds *rsmt2d.ExtendedDataSquare, 54 ) (*header.ExtendedHeader, error) { 55 if h.Height < f.height { 56 return header.MakeExtendedHeader(h, comm, vals, eds) 57 } 58 59 hdr := *h 60 if h.Height == f.height { 61 adder := ipld.NewProofsAdder(odsSize) 62 square := edstest.RandByzantineEDS(f.t, odsSize, nmt.NodeVisitor(adder.VisitFn())) 63 dah, err := da.NewDataAvailabilityHeader(square) 64 require.NoError(f.t, err) 65 hdr.DataHash = dah.Hash() 66 67 ctx := ipld.CtxWithProofsAdder(context.Background(), adder) 68 require.NoError(f.t, edsStore.Put(ctx, h.DataHash.Bytes(), square)) 69 70 *eds = *square 71 } 72 if h.Height > f.height { 73 hdr.LastBlockID.Hash = f.prevHash 74 } 75 76 blockID := comm.BlockID 77 blockID.Hash = hdr.Hash() 78 voteSet := types.NewVoteSet(hdr.ChainID, hdr.Height, 0, tmproto.PrecommitType, f.valSet) 79 commit, err := headertest.MakeCommit(blockID, hdr.Height, 0, voteSet, f.vals, time.Now()) 80 require.NoError(f.t, err) 81 82 *h = hdr 83 *comm = *commit 84 f.prevHash = h.Hash() 85 return header.MakeExtendedHeader(h, comm, vals, eds) 86 } 87 } 88 func CreateFraudExtHeader( 89 t *testing.T, 90 eh *header.ExtendedHeader, 91 serv blockservice.BlockService, 92 ) *header.ExtendedHeader { 93 square := edstest.RandByzantineEDS(t, len(eh.DAH.RowRoots)) 94 err := ipld.ImportEDS(context.Background(), square, serv) 95 require.NoError(t, err) 96 dah, err := da.NewDataAvailabilityHeader(square) 97 require.NoError(t, err) 98 eh.DAH = &dah 99 eh.RawHeader.DataHash = dah.Hash() 100 return eh 101 }