Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
test: add tests for historical queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed Nov 2, 2023
1 parent e1d3468 commit 01f8e74
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 8 deletions.
4 changes: 3 additions & 1 deletion orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ func TestProcessWithoutValsetInStore(t *testing.T) {
testnode.ImmediateProposals(codec),
qgbtesting.SetDataCommitmentWindowParams(codec, celestiatypes.Params{DataCommitmentWindow: 101}),
},
TimeIotaMs: 6048000, // to have enough time to sign attestations after they're pruned
TimeIotaMs: 6048000, // to have enough time to sign attestations after they're pruned
Pruning: "default",
TimeoutCommit: 5 * time.Millisecond,
},
)
_, err := node.CelestiaNetwork.WaitForHeight(400)
Expand Down
5 changes: 4 additions & 1 deletion orchestrator/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package orchestrator_test
import (
"context"
"testing"
"time"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
Expand Down Expand Up @@ -31,7 +32,9 @@ func (s *OrchestratorTestSuite) SetupSuite() {
testnode.ImmediateProposals(codec),
blobstreamtesting.SetDataCommitmentWindowParams(codec, types.Params{DataCommitmentWindow: 101}),
},
TimeIotaMs: 1,
TimeIotaMs: 1,
Pruning: "default",
TimeoutCommit: 5 * time.Millisecond,
},
)
s.Orchestrator = blobstreamtesting.NewOrchestrator(t, s.Node)
Expand Down
4 changes: 3 additions & 1 deletion relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func TestUseValsetFromP2P(t *testing.T) {
testnode.ImmediateProposals(codec),
qgbtesting.SetDataCommitmentWindowParams(codec, types.Params{DataCommitmentWindow: 101}),
},
TimeIotaMs: 2000000, // so attestations are pruned after they're queried
TimeIotaMs: 2000000, // so attestations are pruned after they're queried
Pruning: "default",
TimeoutCommit: 5 * time.Millisecond,
},
)

Expand Down
120 changes: 120 additions & 0 deletions rpc/app_historic_querier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package rpc_test

import (
"context"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/orchestrator-relayer/rpc"
)

func (s *HistoricQuerierTestSuite) TestQueryHistoricAttestationByNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

// this one should fail because the attestation is deleted from the state
_, err := appQuerier.QueryAttestationByNonce(context.Background(), 1)
s.Error(err)

att, err := appQuerier.QueryHistoricalAttestationByNonce(context.Background(), 1, 10)
s.NoError(err)
s.NotNil(att)
s.Equal(uint64(1), att.GetNonce())
}

func (s *HistoricQuerierTestSuite) TestQueryRecursiveHistoricAttestationByNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

// this one should fail because the attestation is deleted from the state
_, err := appQuerier.QueryAttestationByNonce(context.Background(), 1)
s.Error(err)

height, err := s.Network.LatestHeight()
s.Require().NoError(err)
att, err := appQuerier.QueryRecursiveHistoricalAttestationByNonce(context.Background(), 1, uint64(height))
s.Require().NoError(err)
s.NotNil(att)
s.Equal(uint64(1), att.GetNonce())
}

func (s *HistoricQuerierTestSuite) TestQueryHistoricalLatestAttestationNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

nonce, err := appQuerier.QueryHistoricalLatestAttestationNonce(context.Background(), 2)
s.Require().NoError(err)
s.Equal(uint64(1), nonce)
}

func (s *HistoricQuerierTestSuite) TestQueryHistoricalValsetByNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

// this one should fail because the attestation is deleted from the state
_, err := appQuerier.QueryValsetByNonce(context.Background(), 1)
s.Error(err)

att, err := appQuerier.QueryHistoricalValsetByNonce(context.Background(), 1, 10)
s.Require().NoError(err)
s.NotNil(att)
s.Equal(uint64(1), att.GetNonce())
}

func (s *HistoricQuerierTestSuite) TestQueryHistoricalLastValsetBeforeNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

// this one should fail because the attestation is deleted from the state
_, err := appQuerier.QueryLastValsetBeforeNonce(context.Background(), 2)
s.Error(err)

att, err := appQuerier.QueryHistoricalLastValsetBeforeNonce(context.Background(), 2, 102)
s.Require().NoError(err)
s.NotNil(att)
s.Equal(uint64(1), att.GetNonce())
}

func (s *HistoricQuerierTestSuite) TestQueryRecursiveHistoricalLastValsetBeforeNonce() {
appQuerier := rpc.NewAppQuerier(
s.Logger,
s.Network.GRPCAddr,
s.EncConf,
)
require.NoError(s.T(), appQuerier.Start())
defer appQuerier.Stop() //nolint:errcheck

// this one should fail because the attestation is deleted from the state
_, err := appQuerier.QueryLastValsetBeforeNonce(context.Background(), 2)
s.Error(err)

att, err := appQuerier.QueryRecursiveHistoricalLastValsetBeforeNonce(context.Background(), 2, 201)
s.Require().NoError(err)
s.NotNil(att)
s.Equal(uint64(1), att.GetNonce())
}
9 changes: 9 additions & 0 deletions rpc/app_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func (aq *AppQuerier) QueryRecursiveHistoricalAttestationByNonce(ctx context.Con
}
return unmarshalledAttestation, nil
}
if currentHeight <= uint64(BlocksIn20DaysPeriod) {
return nil, ErrNotFound
}
aq.Logger.Debug("keeping looking for attestation in archival state", "err", err.Error())
currentHeight -= uint64(BlocksIn20DaysPeriod)
}
Expand Down Expand Up @@ -278,6 +281,9 @@ func (aq *AppQuerier) QueryRecursiveLatestValset(ctx context.Context, height uin
return latestValset, nil
}

if currentHeight <= uint64(BlocksIn20DaysPeriod) {
return nil, ErrNotFound
}
aq.Logger.Debug("keeping looking for attestation in archival state", "err", err.Error())
currentHeight -= uint64(BlocksIn20DaysPeriod)
}
Expand Down Expand Up @@ -339,6 +345,9 @@ func (aq *AppQuerier) QueryRecursiveHistoricalLastValsetBeforeNonce(ctx context.
if err == nil {
return resp.Valset, err
}
if currentHeight <= uint64(BlocksIn20DaysPeriod) {
return nil, ErrNotFound
}
aq.Logger.Debug("keeping looking for attestation in archival state", "err", err.Error())
currentHeight -= uint64(BlocksIn20DaysPeriod)
}
Expand Down
51 changes: 51 additions & 0 deletions rpc/historic_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rpc_test

import (
"context"
"testing"
"time"

"github.com/celestiaorg/celestia-app/test/util/testnode"
"github.com/celestiaorg/celestia-app/x/qgb/types"
"github.com/celestiaorg/orchestrator-relayer/rpc"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
tmlog "github.com/tendermint/tendermint/libs/log"

"github.com/stretchr/testify/require"

blobstreamtesting "github.com/celestiaorg/orchestrator-relayer/testing"
"github.com/stretchr/testify/suite"
)

type HistoricQuerierTestSuite struct {
suite.Suite
Network *blobstreamtesting.CelestiaNetwork
EncConf encoding.Config
Logger tmlog.Logger
}

func (s *HistoricQuerierTestSuite) SetupSuite() {
t := s.T()
ctx := context.Background()
s.EncConf = encoding.MakeConfig(app.ModuleEncodingRegisters...)
s.Network = blobstreamtesting.NewCelestiaNetwork(
ctx,
t,
blobstreamtesting.CelestiaNetworkParams{
GenesisOpts: []testnode.GenesisOption{blobstreamtesting.SetDataCommitmentWindowParams(s.EncConf.Codec, types.Params{DataCommitmentWindow: 101})},
TimeIotaMs: 6048000, // so that old attestations are deleted as soon as a new one appears
Pruning: "nothing", // make the node an archive one
TimeoutCommit: 20 * time.Millisecond,
},
)
_, err := s.Network.WaitForHeightWithTimeout(401, 30*time.Second)
s.Logger = tmlog.NewNopLogger()
require.NoError(t, err)
rpc.BlocksIn20DaysPeriod = 100
}

func TestHistoricQueriers(t *testing.T) {
suite.Run(t, new(HistoricQuerierTestSuite))
}
15 changes: 10 additions & 5 deletions testing/celestia_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ type CelestiaNetwork struct {
}

type CelestiaNetworkParams struct {
GenesisOpts []celestiatestnode.GenesisOption
TimeIotaMs int64
GenesisOpts []celestiatestnode.GenesisOption
TimeIotaMs int64
Pruning string
TimeoutCommit time.Duration
}

func DefaultCelestiaNetworkParams() CelestiaNetworkParams {
return CelestiaNetworkParams{
GenesisOpts: nil,
TimeIotaMs: 1,
GenesisOpts: nil,
TimeIotaMs: 1,
Pruning: "default",
TimeoutCommit: 5 * time.Millisecond,
}
}

Expand All @@ -73,8 +77,9 @@ func NewCelestiaNetwork(ctx context.Context, t *testing.T, params CelestiaNetwor
}

tmCfg := celestiatestnode.DefaultTendermintConfig()
tmCfg.Consensus.TimeoutCommit = time.Millisecond * 5
tmCfg.Consensus.TimeoutCommit = params.TimeoutCommit
appConf := celestiatestnode.DefaultAppConfig()
appConf.Pruning = params.Pruning
consensusParams := celestiatestnode.DefaultParams()
consensusParams.Block.TimeIotaMs = params.TimeIotaMs

Expand Down

0 comments on commit 01f8e74

Please sign in to comment.