From 87ece3a84e2ceb301edff6c4d0c8647c006ee28c Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:07:28 +0100 Subject: [PATCH 1/4] fix(nodebuilder/da): remove random print (#4029) --- nodebuilder/da/service.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/nodebuilder/da/service.go b/nodebuilder/da/service.go index aded1f0ea9..c7e403fbff 100644 --- a/nodebuilder/da/service.go +++ b/nodebuilder/da/service.go @@ -4,7 +4,6 @@ import ( "context" "encoding/binary" "encoding/json" - "fmt" "strings" logging "github.com/ipfs/go-log/v2" @@ -256,7 +255,6 @@ func (s *Service) Validate( // invalid proof") but analysis of the code in celestia-node implies this should never happen - // maybe it's caused by openrpc? there is no way of gently handling errors here, but returned // value is fine for us - fmt.Println("proof", proofs[i] == nil, "commitment", commitment == nil) isIncluded, _ := s.blobServ.Included(ctx, height, ns, proofs[i], commitment) included[i] = isIncluded } From c0055c51dad43a80927c0f107753b3c0ed753238 Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Thu, 9 Jan 2025 17:14:33 +0200 Subject: [PATCH 2/4] feat(modshare): implement GetRow (#4002) Co-authored-by: Hlib Kanunnikov --- nodebuilder/share/mocks/api.go | 15 ++++++ nodebuilder/share/share.go | 19 +++++++ share/availability/light/availability_test.go | 6 ++- share/shwap/getter.go | 2 + share/shwap/getters/cascade.go | 12 +++++ share/shwap/getters/mock/getter.go | 15 ++++++ share/shwap/getters/testing.go | 17 ++++++ share/shwap/p2p/bitswap/getter.go | 26 +++++++++ share/shwap/p2p/shrex/shrex_getter/shrex.go | 4 ++ share/shwap/row.go | 54 +++++++++++++++++++ share/shwap/row_test.go | 28 ++++++++++ store/getter.go | 15 ++++++ store/getter_test.go | 20 +++++++ 13 files changed, 232 insertions(+), 1 deletion(-) diff --git a/nodebuilder/share/mocks/api.go b/nodebuilder/share/mocks/api.go index 7fde2338cc..d392f39f5b 100644 --- a/nodebuilder/share/mocks/api.go +++ b/nodebuilder/share/mocks/api.go @@ -84,6 +84,21 @@ func (mr *MockModuleMockRecorder) GetRange(arg0, arg1, arg2, arg3 interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRange", reflect.TypeOf((*MockModule)(nil).GetRange), arg0, arg1, arg2, arg3) } +// GetRow mocks base method. +func (m *MockModule) GetRow(arg0 context.Context, arg1 uint64, arg2 int) (shwap.Row, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRow", arg0, arg1, arg2) + ret0, _ := ret[0].(shwap.Row) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRow indicates an expected call of GetRow. +func (mr *MockModuleMockRecorder) GetRow(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRow", reflect.TypeOf((*MockModule)(nil).GetRow), arg0, arg1, arg2) +} + // GetSamples mocks base method. func (m *MockModule) GetSamples(arg0 context.Context, arg1 *header.ExtendedHeader, arg2 []shwap.SampleCoords) ([]shwap.Sample, error) { m.ctrl.T.Helper() diff --git a/nodebuilder/share/share.go b/nodebuilder/share/share.go index 783018fe47..46df1e2379 100644 --- a/nodebuilder/share/share.go +++ b/nodebuilder/share/share.go @@ -50,6 +50,8 @@ type Module interface { GetSamples(ctx context.Context, header *header.ExtendedHeader, indices []shwap.SampleCoords) ([]shwap.Sample, error) // GetEDS gets the full EDS identified by the given extended header. GetEDS(ctx context.Context, height uint64) (*rsmt2d.ExtendedDataSquare, error) + // GetRow gets all shares from specified row. + GetRow(context.Context, uint64, int) (shwap.Row, error) // GetNamespaceData gets all shares from an EDS within the given namespace. // Shares are returned in a row-by-row order if the namespace spans multiple rows. GetNamespaceData( @@ -77,6 +79,11 @@ type API struct { ctx context.Context, height uint64, ) (*rsmt2d.ExtendedDataSquare, error) `perm:"read"` + GetRow func( + context.Context, + uint64, + int, + ) (shwap.Row, error) `perm:"read"` GetNamespaceData func( ctx context.Context, height uint64, @@ -108,6 +115,10 @@ func (api *API) GetEDS(ctx context.Context, height uint64) (*rsmt2d.ExtendedData return api.Internal.GetEDS(ctx, height) } +func (api *API) GetRow(ctx context.Context, height uint64, rowIdx int) (shwap.Row, error) { + return api.Internal.GetRow(ctx, height, rowIdx) +} + func (api *API) GetRange(ctx context.Context, height uint64, start, end int) (*GetRangeResult, error) { return api.Internal.GetRange(ctx, height, start, end) } @@ -196,3 +207,11 @@ func (m module) GetNamespaceData( } return m.getter.GetNamespaceData(ctx, header, namespace) } + +func (m module) GetRow(ctx context.Context, height uint64, rowIdx int) (shwap.Row, error) { + header, err := m.hs.GetByHeight(ctx, height) + if err != nil { + return shwap.Row{}, err + } + return m.getter.GetRow(ctx, header, rowIdx) +} diff --git a/share/availability/light/availability_test.go b/share/availability/light/availability_test.go index 191c286938..df441c46a1 100644 --- a/share/availability/light/availability_test.go +++ b/share/availability/light/availability_test.go @@ -290,7 +290,7 @@ func (g successGetter) checkOnce(t *testing.T) { } } -func (g successGetter) GetSamples(_ context.Context, hdr *header.ExtendedHeader, +func (g successGetter) GetSamples(_ context.Context, _ *header.ExtendedHeader, indices []shwap.SampleCoords, ) ([]shwap.Sample, error) { g.Lock() @@ -305,6 +305,10 @@ func (g successGetter) GetSamples(_ context.Context, hdr *header.ExtendedHeader, return smpls, nil } +func (g successGetter) GetRow(_ context.Context, _ *header.ExtendedHeader, _ int) (shwap.Row, error) { + panic("not implemented") +} + func (g successGetter) GetEDS(_ context.Context, _ *header.ExtendedHeader) (*rsmt2d.ExtendedDataSquare, error) { panic("not implemented") } diff --git a/share/shwap/getter.go b/share/shwap/getter.go index 9e0a5d3131..f56c8edc63 100644 --- a/share/shwap/getter.go +++ b/share/shwap/getter.go @@ -39,6 +39,8 @@ type Getter interface { // GetEDS gets the full EDS identified by the given extended header. GetEDS(context.Context, *header.ExtendedHeader) (*rsmt2d.ExtendedDataSquare, error) + // GetRow gets Row by its index committed to the given extended header. + GetRow(ctx context.Context, header *header.ExtendedHeader, rowIdx int) (Row, error) // GetNamespaceData gets all shares from an EDS within the given namespace. // Shares are returned in a row-by-row order if the namespace spans multiple rows. // Inclusion of returned data could be verified using Verify method on NamespacedShares. diff --git a/share/shwap/getters/cascade.go b/share/shwap/getters/cascade.go index 39ceb2fdb1..c9ac091ddd 100644 --- a/share/shwap/getters/cascade.go +++ b/share/shwap/getters/cascade.go @@ -71,6 +71,18 @@ func (cg *CascadeGetter) GetEDS( return cascadeGetters(ctx, cg.getters, get) } +// GetRow gets row shares from any of registered shwap.Getters in cascading +// order. +func (cg *CascadeGetter) GetRow(ctx context.Context, header *header.ExtendedHeader, rowIdx int) (shwap.Row, error) { + ctx, span := tracer.Start(ctx, "cascade/get-row") + defer span.End() + + get := func(ctx context.Context, get shwap.Getter) (shwap.Row, error) { + return get.GetRow(ctx, header, rowIdx) + } + return cascadeGetters(ctx, cg.getters, get) +} + // GetNamespaceData gets NamespacedShares from any of registered shwap.Getters in cascading // order. func (cg *CascadeGetter) GetNamespaceData( diff --git a/share/shwap/getters/mock/getter.go b/share/shwap/getters/mock/getter.go index 7e4dacb24a..ade35b39f8 100644 --- a/share/shwap/getters/mock/getter.go +++ b/share/shwap/getters/mock/getter.go @@ -68,6 +68,21 @@ func (mr *MockGetterMockRecorder) GetNamespaceData(arg0, arg1, arg2 interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNamespaceData", reflect.TypeOf((*MockGetter)(nil).GetNamespaceData), arg0, arg1, arg2) } +// GetRow mocks base method. +func (m *MockGetter) GetRow(arg0 context.Context, arg1 *header.ExtendedHeader, arg2 int) (shwap.Row, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRow", arg0, arg1, arg2) + ret0, _ := ret[0].(shwap.Row) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRow indicates an expected call of GetRow. +func (mr *MockGetterMockRecorder) GetRow(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRow", reflect.TypeOf((*MockGetter)(nil).GetRow), arg0, arg1, arg2) +} + // GetSamples mocks base method. func (m *MockGetter) GetSamples(arg0 context.Context, arg1 *header.ExtendedHeader, arg2 []shwap.SampleCoords) ([]shwap.Sample, error) { m.ctrl.T.Helper() diff --git a/share/shwap/getters/testing.go b/share/shwap/getters/testing.go index a3ee53753d..c244204aba 100644 --- a/share/shwap/getters/testing.go +++ b/share/shwap/getters/testing.go @@ -58,6 +58,23 @@ func (seg *SingleEDSGetter) GetSamples(ctx context.Context, hdr *header.Extended return smpls, nil } +func (seg *SingleEDSGetter) GetRow( + ctx context.Context, + header *header.ExtendedHeader, + rowIdx int, +) (shwap.Row, error) { + err := seg.checkRoots(header.DAH) + if err != nil { + return shwap.Row{}, err + } + + axisHalf, err := seg.EDS.AxisHalf(ctx, rsmt2d.Row, rowIdx) + if err != nil { + return shwap.Row{}, err + } + return axisHalf.ToRow(), nil +} + // GetEDS returns a kept EDS if the correct root is given. func (seg *SingleEDSGetter) GetEDS( _ context.Context, diff --git a/share/shwap/p2p/bitswap/getter.go b/share/shwap/p2p/bitswap/getter.go index db2938663c..f008809833 100644 --- a/share/shwap/p2p/bitswap/getter.go +++ b/share/shwap/p2p/bitswap/getter.go @@ -134,6 +134,32 @@ func (g *Getter) GetSamples( return smpls, nil } +func (g *Getter) GetRow(ctx context.Context, hdr *header.ExtendedHeader, rowIdx int) (shwap.Row, error) { + ctx, span := tracer.Start(ctx, "get-eds") + defer span.End() + + blk, err := NewEmptyRowBlock(hdr.Height(), rowIdx, len(hdr.DAH.RowRoots)) + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, "NewEmptyRowBlock") + return shwap.Row{}, err + } + + isArchival := g.isArchival(hdr) + span.SetAttributes(attribute.Bool("is_archival", isArchival)) + + ses, release := g.getSession(isArchival) + defer release() + + err = Fetch(ctx, g.exchange, hdr.DAH, []Block{blk}, WithFetcher(ses)) + if err != nil { + span.RecordError(err) + span.SetStatus(codes.Error, "Fetch") + return shwap.Row{}, err + } + return blk.Container, nil +} + // GetEDS uses [RowBlock] and [Fetch] to get half of the first EDS quadrant(ODS) and // recomputes the whole EDS from it. // We fetch the ODS or Q1 to ensure better compatibility with archival nodes that only diff --git a/share/shwap/p2p/shrex/shrex_getter/shrex.go b/share/shwap/p2p/shrex/shrex_getter/shrex.go index 6c91a44736..142b3f3ab2 100644 --- a/share/shwap/p2p/shrex/shrex_getter/shrex.go +++ b/share/shwap/p2p/shrex/shrex_getter/shrex.go @@ -150,6 +150,10 @@ func (sg *Getter) GetSamples(context.Context, *header.ExtendedHeader, []shwap.Sa return nil, fmt.Errorf("getter/shrex: GetShare %w", shwap.ErrOperationNotSupported) } +func (sg *Getter) GetRow(_ context.Context, _ *header.ExtendedHeader, _ int) (shwap.Row, error) { + return shwap.Row{}, fmt.Errorf("getter/shrex: GetRow %w", shwap.ErrOperationNotSupported) +} + func (sg *Getter) GetEDS(ctx context.Context, header *header.ExtendedHeader) (*rsmt2d.ExtendedDataSquare, error) { var err error ctx, span := tracer.Start(ctx, "shrex/get-eds") diff --git a/share/shwap/row.go b/share/shwap/row.go index 7057680573..87faff4875 100644 --- a/share/shwap/row.go +++ b/share/shwap/row.go @@ -2,6 +2,7 @@ package shwap import ( "bytes" + "encoding/json" "fmt" "github.com/celestiaorg/celestia-app/v3/pkg/wrapper" @@ -171,6 +172,33 @@ func (r *Row) verifyInclusion(roots *share.AxisRoots, idx int) error { return nil } +// MarshalJSON encodes row to the json encoded bytes. +func (r Row) MarshalJSON() ([]byte, error) { + jsonRow := struct { + Shares []libshare.Share `json:"shares"` + Side string `json:"side"` + }{ + Shares: r.shares, + Side: r.side.String(), + } + return json.Marshal(&jsonRow) +} + +// UnmarshalJSON decodes json bytes to the row. +func (r *Row) UnmarshalJSON(data []byte) error { + jsonRow := struct { + Shares []libshare.Share `json:"shares"` + Side string `json:"side"` + }{} + err := json.Unmarshal(data, &jsonRow) + if err != nil { + return err + } + r.shares = jsonRow.Shares + r.side = toRowSide(jsonRow.Side) + return nil +} + // ToProto converts a RowSide to its protobuf representation. func (s RowSide) ToProto() pb.Row_HalfSide { if s == Left { @@ -186,3 +214,29 @@ func sideFromProto(side pb.Row_HalfSide) RowSide { } return Right } + +func (s RowSide) String() string { + switch s { + case Left: + return "LEFT" + case Right: + return "RIGHT" + case Both: + return "BOTH" + default: + panic("invalid row side") + } +} + +func toRowSide(s string) RowSide { + switch s { + case "LEFT": + return Left + case "RIGHT": + return Right + case "BOTH": + return Both + default: + panic("invalid row side") + } +} diff --git a/share/shwap/row_test.go b/share/shwap/row_test.go index 16bce3893b..3cf80befca 100644 --- a/share/shwap/row_test.go +++ b/share/shwap/row_test.go @@ -1,6 +1,7 @@ package shwap import ( + "encoding/json" "testing" "github.com/stretchr/testify/require" @@ -29,6 +30,33 @@ func TestRowShares(t *testing.T) { } } +func TestRowMarshal(t *testing.T) { + const odsSize = 8 + eds := edstest.RandEDS(t, odsSize) + for rowIdx := 0; rowIdx < odsSize*2; rowIdx++ { + for _, side := range []RowSide{Left, Right, Both} { + row, err := RowFromEDS(eds, rowIdx, side) + require.NoError(t, err) + rowData, err := json.Marshal(row) + require.NoError(t, err) + + decodedRow := &Row{} + err = json.Unmarshal(rowData, decodedRow) + require.NoError(t, err) + + require.Equal(t, side, decodedRow.side) + extended, err := decodedRow.Shares() + require.NoError(t, err) + + shares, err := row.Shares() + require.NoError(t, err) + + require.Equal(t, shares, extended) + require.Equal(t, row.side, decodedRow.side) + } + } +} + func TestRowValidate(t *testing.T) { const odsSize = 8 eds := edstest.RandEDS(t, odsSize) diff --git a/store/getter.go b/store/getter.go index 1315561730..7a94c408ac 100644 --- a/store/getter.go +++ b/store/getter.go @@ -71,6 +71,21 @@ func (g *Getter) GetEDS(ctx context.Context, h *header.ExtendedHeader) (*rsmt2d. return rsmt2d.ExtendedDataSquare, nil } +func (g *Getter) GetRow(ctx context.Context, h *header.ExtendedHeader, rowIdx int) (shwap.Row, error) { + acc, err := g.store.GetByHeight(ctx, h.Height()) + if err != nil { + if errors.Is(err, ErrNotFound) { + return shwap.Row{}, shwap.ErrNotFound + } + return shwap.Row{}, fmt.Errorf("getting accessor from store: %w", err) + } + axisHalf, err := acc.AxisHalf(ctx, rsmt2d.Row, rowIdx) + if err != nil { + return shwap.Row{}, fmt.Errorf("getting axis half from accessor: %w", err) + } + return axisHalf.ToRow(), nil +} + func (g *Getter) GetNamespaceData( ctx context.Context, h *header.ExtendedHeader, diff --git a/store/getter_test.go b/store/getter_test.go index b0b027fc19..0042f43ecf 100644 --- a/store/getter_test.go +++ b/store/getter_test.go @@ -69,6 +69,26 @@ func TestStoreGetter(t *testing.T) { require.ErrorIs(t, err, shwap.ErrNotFound) }) + t.Run("GetRow", func(t *testing.T) { + eds, roots := randomEDS(t) + eh := headertest.RandExtendedHeaderWithRoot(t, roots) + height := height.Add(1) + eh.RawHeader.Height = int64(height) + + err := edsStore.PutODSQ4(ctx, eh.DAH, height, eds) + require.NoError(t, err) + + for i := 0; i < len(eh.DAH.RowRoots); i++ { + row, err := sg.GetRow(ctx, eh, i) + require.NoError(t, err) + retreivedShrs, err := row.Shares() + require.NoError(t, err) + edsShrs, err := libshare.FromBytes(eds.Row(uint(i))) + require.NoError(t, err) + require.Equal(t, edsShrs, retreivedShrs) + } + }) + t.Run("GetNamespaceData", func(t *testing.T) { ns := libshare.RandomNamespace() eds, roots := edstest.RandEDSWithNamespace(t, ns, 8, 16) From 66b94a84fa22dc681dcbae082e70a87d8b989127 Mon Sep 17 00:00:00 2001 From: Hlib Kanunnikov Date: Mon, 13 Jan 2025 11:04:02 +0100 Subject: [PATCH 3/4] Revert "fix(p2p): disable quic (#3937)" (#4039) This reverts commit e6dbb54dd28462c2820ae455295acc856af92b64 as https://github.com/quic-go/quic-go/issues/4712 is solved and fixed. It's not yet released, but the bug is not critical to block on it. --- nodebuilder/p2p/addrs.go | 17 +++-------------- nodebuilder/p2p/host.go | 24 +++++++----------------- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/nodebuilder/p2p/addrs.go b/nodebuilder/p2p/addrs.go index 607d63c2bd..1a928e3be2 100644 --- a/nodebuilder/p2p/addrs.go +++ b/nodebuilder/p2p/addrs.go @@ -2,7 +2,6 @@ package p2p import ( "fmt" - "slices" p2pconfig "github.com/libp2p/go-libp2p/config" hst "github.com/libp2p/go-libp2p/core/host" @@ -12,22 +11,12 @@ import ( // Listen returns invoke function that starts listening for inbound connections with libp2p.Host. func Listen(cfg *Config) func(h hst.Host) (err error) { return func(h hst.Host) (err error) { - maListen := make([]ma.Multiaddr, 0, len(cfg.ListenAddresses)) - for _, addr := range cfg.ListenAddresses { - maddr, err := ma.NewMultiaddr(addr) + maListen := make([]ma.Multiaddr, len(cfg.ListenAddresses)) + for i, addr := range cfg.ListenAddresses { + maListen[i], err = ma.NewMultiaddr(addr) if err != nil { return fmt.Errorf("failure to parse config.P2P.ListenAddresses: %w", err) } - if !enableQUIC { - // TODO(@walldiss): Remove this check when QUIC is stable - if slices.ContainsFunc(maddr.Protocols(), func(p ma.Protocol) bool { - return p.Code == ma.P_QUIC_V1 || p.Code == ma.P_WEBTRANSPORT - }) { - continue - } - } - - maListen = append(maListen, maddr) } return h.Network().Listen(maListen...) } diff --git a/nodebuilder/p2p/host.go b/nodebuilder/p2p/host.go index c38713f6a3..1003970199 100644 --- a/nodebuilder/p2p/host.go +++ b/nodebuilder/p2p/host.go @@ -3,7 +3,6 @@ package p2p import ( "context" "fmt" - "os" "strings" "github.com/libp2p/go-libp2p" @@ -28,8 +27,6 @@ import ( "github.com/celestiaorg/celestia-node/nodebuilder/node" ) -var enableQUIC = os.Getenv("CELESTIA_ENABLE_QUIC") == "1" - // routedHost constructs a wrapped Host that may fallback to address discovery, // if any top-level operation on the Host is provided with PeerID(Hash(PbK)) only. func routedHost(base HostBase, r routing.PeerRouting) hst.Host { @@ -83,19 +80,6 @@ func host(params hostParams) (HostBase, error) { params.Cfg.Upgrade() } - transports := []libp2p.Option{ - libp2p.Transport(tcp.NewTCPTransport), - libp2p.Transport(libp2pwebrtc.New), - wsTransport(tlsCfg), - } - - // disable quic and webtransport client support until it is stable - if enableQUIC { - transports = append(transports, - libp2p.Transport(quic.NewTransport), - libp2p.Transport(webtransport.New)) - } - opts := []libp2p.Option{ libp2p.NoListenAddrs, // do not listen automatically libp2p.AddrsFactory(params.AddrF), @@ -108,7 +92,13 @@ func host(params hostParams) (HostBase, error) { libp2p.DisableRelay(), libp2p.BandwidthReporter(params.Bandwidth), libp2p.ResourceManager(params.ResourceManager), - libp2p.ChainOptions(transports...), + libp2p.ChainOptions( + libp2p.Transport(tcp.NewTCPTransport), + libp2p.Transport(quic.NewTransport), + libp2p.Transport(webtransport.New), + libp2p.Transport(libp2pwebrtc.New), + wsTransport(tlsCfg), + ), // to clearly define what defaults we rely upon libp2p.DefaultSecurity, libp2p.DefaultMuxers, From c599285bba4d46cc945d094bcf1a22644bf34bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8B=E3=81=92?= <47621124+ronething-bot@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:42:21 +0800 Subject: [PATCH 4/4] chore(pruner): remove SpacedHeaderGenerator (#4032) --- header/headertest/testing.go | 19 ++++++++++++++++--- pruner/service_test.go | 33 ++------------------------------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/header/headertest/testing.go b/header/headertest/testing.go index 251592a9a5..2e25592f27 100644 --- a/header/headertest/testing.go +++ b/header/headertest/testing.go @@ -15,7 +15,6 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/tendermint/tendermint/proto/tendermint/version" "github.com/tendermint/tendermint/types" - tmtime "github.com/tendermint/tendermint/types/time" "github.com/celestiaorg/celestia-app/v3/pkg/da" libhead "github.com/celestiaorg/go-header" @@ -40,6 +39,7 @@ type TestSuite struct { // blockTime is optional - if set, the test suite will generate // blocks timestamped at the specified interval blockTime time.Duration + startTime time.Time } func NewStore(t *testing.T) libhead.Store[*header.ExtendedHeader] { @@ -62,6 +62,18 @@ func NewTestSuite(t *testing.T, numValidators int, blockTime time.Duration) *Tes vals: vals, valSet: valSet, blockTime: blockTime, + startTime: time.Now(), + } +} + +func NewTestSuiteWithGenesisTime(t *testing.T, startTime time.Time, blockTime time.Duration) *TestSuite { + valSet, vals := RandValidatorSet(3, 1) + return &TestSuite{ + t: t, + vals: vals, + valSet: valSet, + blockTime: blockTime, + startTime: startTime, } } @@ -74,10 +86,11 @@ func (s *TestSuite) genesis() *header.ExtendedHeader { gen.ValidatorsHash = s.valSet.Hash() gen.NextValidatorsHash = s.valSet.Hash() gen.Height = 1 + gen.Time = s.startTime voteSet := types.NewVoteSet(gen.ChainID, gen.Height, 0, tmproto.PrecommitType, s.valSet) blockID := RandBlockID(s.t) blockID.Hash = gen.Hash() - commit, err := MakeCommit(blockID, gen.Height, 0, voteSet, s.vals, time.Now()) + commit, err := MakeCommit(blockID, gen.Height, 0, voteSet, s.vals, s.startTime) require.NoError(s.t, err) eh := &header.ExtendedHeader{ @@ -199,7 +212,7 @@ func (s *TestSuite) Commit(h *header.RawHeader) *types.Commit { ValidatorIndex: int32(i), Height: h.Height, Round: round, - Timestamp: tmtime.Now().UTC(), + Timestamp: h.Time, Type: tmproto.PrecommitType, BlockID: bid, } diff --git a/pruner/service_test.go b/pruner/service_test.go index c7ebeac03a..75e0e0198e 100644 --- a/pruner/service_test.go +++ b/pruner/service_test.go @@ -248,8 +248,8 @@ func TestFindPruneableHeaders(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) - headerGenerator := NewSpacedHeaderGenerator(t, tc.startTime, tc.blockTime) - store := headertest.NewCustomStore(t, headerGenerator, tc.headerAmount) + suite := headertest.NewTestSuiteWithGenesisTime(t, tc.startTime, tc.blockTime) + store := headertest.NewCustomStore(t, suite, tc.headerAmount) mp := &mockPruner{} @@ -317,32 +317,3 @@ func (mp *mockPruner) Prune(_ context.Context, h *header.ExtendedHeader) error { mp.deletedHeaderHashes = append(mp.deletedHeaderHashes, pruned{hash: h.Hash().String(), height: h.Height()}) return nil } - -// TODO @renaynay @distractedm1nd: Deduplicate via headertest utility. -// https://github.com/celestiaorg/celestia-node/issues/3278. -type SpacedHeaderGenerator struct { - t *testing.T - TimeBetweenHeaders time.Duration - currentTime time.Time - currentHeight int64 -} - -func NewSpacedHeaderGenerator( - t *testing.T, startTime time.Time, timeBetweenHeaders time.Duration, -) *SpacedHeaderGenerator { - return &SpacedHeaderGenerator{ - t: t, - TimeBetweenHeaders: timeBetweenHeaders, - currentTime: startTime, - currentHeight: 1, - } -} - -func (shg *SpacedHeaderGenerator) NextHeader() *header.ExtendedHeader { - h := headertest.RandExtendedHeaderAtTimestamp(shg.t, shg.currentTime) - h.RawHeader.Height = shg.currentHeight - h.RawHeader.Time = shg.currentTime - shg.currentHeight++ - shg.currentTime = shg.currentTime.Add(shg.TimeBetweenHeaders) - return h -}