From 715a58ef50a35bbd464d94285a97eaf2c3a2653b Mon Sep 17 00:00:00 2001 From: Victor Fusco <1221933+vfusco@users.noreply.github.com> Date: Mon, 2 Mar 2026 01:32:47 -0300 Subject: [PATCH] refactor(repository): remove test-only methods and improve test setup --- internal/jsonrpc/jsonrpc_test.go | 817 ++++++++---------- internal/jsonrpc/util_test.go | 61 +- internal/repository/postgres/test_only.go | 135 --- internal/repository/postgres/util.go | 7 - internal/repository/repository.go | 8 - internal/repository/repotest/builders.go | 110 +-- .../repository/repotest/output_test_cases.go | 126 +-- .../repository/repotest/report_test_cases.go | 43 +- internal/repository/repotest/repotest.go | 35 + 9 files changed, 500 insertions(+), 842 deletions(-) delete mode 100644 internal/repository/postgres/test_only.go diff --git a/internal/jsonrpc/jsonrpc_test.go b/internal/jsonrpc/jsonrpc_test.go index 7c775fc16..a06d52d92 100644 --- a/internal/jsonrpc/jsonrpc_test.go +++ b/internal/jsonrpc/jsonrpc_test.go @@ -28,10 +28,12 @@ import ( "github.com/cartesi/rollups-node/internal/evmreader" "github.com/cartesi/rollups-node/internal/model" "github.com/cartesi/rollups-node/internal/repository" + "github.com/cartesi/rollups-node/internal/repository/repotest" "github.com/cartesi/rollups-node/internal/version" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type jsonrpcSchema struct { @@ -93,7 +95,7 @@ func TestMethod(t *testing.T) { s := newTestService(t, t.Name()) contents, err := os.ReadFile("jsonrpc-discover.json") - assert.Nil(t, err) + require.NoError(t, err) var expected any assert.Nil(t, json.Unmarshal(contents, &expected)) @@ -142,7 +144,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_getApplication", @@ -195,7 +197,7 @@ func TestMethod(t *testing.T) { }, }, ) - assert.Nil(t, err, "on test case: %v, when saving evm reader config", t.Name()) + require.NoError(t, err, "on test case: %v, when saving evm reader config", t.Name()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -248,15 +250,12 @@ func TestMethod(t *testing.T) { app := uint64(1) nr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -283,15 +282,12 @@ func TestMethod(t *testing.T) { app := uint64(1) nr := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -348,14 +344,12 @@ func TestMethod(t *testing.T) { app := uint64(2) enr := uint64(1) inr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -382,23 +376,17 @@ func TestMethod(t *testing.T) { app := uint64(2) enr := uint64(1) inr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - - err = s.repository.CreateInput(ctx, &model.Input{ - EpochApplicationID: appID, - EpochIndex: enr, - Index: inr, - Status: model.InputCompletionStatus_Accepted, - RawData: emptyInput(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) + appID := s.newTestApplication(ctx, t, app) + epoch := repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build() + input := repotest.NewInputBuilder(). + WithIndex(inr). + WithRawData(emptyInput()). + Build() + s.createTestEpochWithInput(ctx, t, numberToName(app), epoch, input) + s.advanceInput(ctx, t, appID, enr, inr, nil, nil) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -453,15 +441,12 @@ func TestMethod(t *testing.T) { nr := uint64(0) epochIndex := uint64(0xdeadbeef) - appID := s.newTestApplication(ctx, t, 0, nr) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: epochIndex, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, nr) + s.createTestEpoch(ctx, t, numberToName(nr), + repotest.NewEpochBuilder(appID). + WithIndex(epochIndex). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -539,14 +524,12 @@ func TestMethod(t *testing.T) { app := uint64(2) enr := uint64(1) inr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -574,31 +557,17 @@ func TestMethod(t *testing.T) { enr := uint64(1) inr := uint64(1) onr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - - err = s.repository.CreateInput(ctx, &model.Input{ - EpochApplicationID: appID, - EpochIndex: enr, - Index: inr, - Status: model.InputCompletionStatus_Accepted, - RawData: emptyInput(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) - - err = s.repository.CreateOutput(ctx, &model.Output{ - InputEpochApplicationID: appID, - InputIndex: inr, - Index: onr, - RawData: emptyVoucher(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, output_index: %v", 0, appID, onr) + appID := s.newTestApplication(ctx, t, app) + epoch := repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build() + input := repotest.NewInputBuilder(). + WithIndex(inr). + WithRawData(emptyInput()). + Build() + s.createTestEpochWithInput(ctx, t, numberToName(app), epoch, input) + s.advanceInput(ctx, t, appID, enr, inr, [][]byte{emptyVoucher()}, nil) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -663,7 +632,7 @@ func TestMethod(t *testing.T) { app := uint64(1) - s.newTestApplication(ctx, t, 0, app) + s.newTestApplication(ctx, t, app) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_getProcessedInputCount", @@ -676,7 +645,7 @@ func TestMethod(t *testing.T) { assert.Equal(t, uint64(0), uint64(resp.Result.Data)) }) - // TODO: test with inputs (need repository.CreateInput) + // TODO: test with inputs (use createTestEpochWithInput) }) //////////////////////////////////////////////////////////////////////// @@ -719,31 +688,17 @@ func TestMethod(t *testing.T) { enr := uint64(1) inr := uint64(1) onr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - - err = s.repository.CreateInput(ctx, &model.Input{ - EpochApplicationID: appID, - EpochIndex: enr, - Index: inr, - Status: model.InputCompletionStatus_Accepted, - RawData: emptyInput(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) - - err = s.repository.CreateReport(ctx, &model.Report{ - InputEpochApplicationID: appID, - InputIndex: inr, - Index: onr, - RawData: emptyVoucher(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, output_index: %v", 0, appID, onr) + appID := s.newTestApplication(ctx, t, app) + epoch := repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build() + input := repotest.NewInputBuilder(). + WithIndex(inr). + WithRawData(emptyInput()). + Build() + s.createTestEpochWithInput(ctx, t, numberToName(app), epoch, input) + s.advanceInput(ctx, t, appID, enr, inr, nil, [][]byte{emptyVoucher()}) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -805,7 +760,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listApplications", @@ -830,7 +785,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listApplications", @@ -853,7 +808,7 @@ func TestMethod(t *testing.T) { many := uint64(100) limit := uint64(many / 2) for i := range many { - s.newTestApplication(ctx, t, 0, i) + s.newTestApplication(ctx, t, i) } { // offset == 0, descending = false @@ -974,7 +929,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listEpochs", @@ -994,7 +949,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listEpochs", @@ -1020,15 +975,13 @@ func TestMethod(t *testing.T) { nr := uint64(1) many := uint64(100) limit := uint64(many / 2) - appID := s.newTestApplication(ctx, t, 0, nr) + appID := s.newTestApplication(ctx, t, nr) for i := range many { - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: i, - VirtualIndex: i, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + s.createTestEpoch(ctx, t, numberToName(nr), + repotest.NewEpochBuilder(appID). + WithIndex(i). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) } { // offset == 0, descending = false @@ -1153,7 +1106,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listInputs", @@ -1166,7 +1119,7 @@ func TestMethod(t *testing.T) { assert.Equal(t, 0, len(resp.Result.Data)) }) - // TODO: test many inputs in the database (requires: repository.CreateInput) + // TODO: test many inputs in the database (use createTestEpochWithInput) }) //////////////////////////////////////////////////////////////////////// @@ -1201,7 +1154,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listOutputs", @@ -1223,35 +1176,24 @@ func TestMethod(t *testing.T) { app := uint64(3) enr := uint64(1) inr := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - - err = s.repository.CreateInput(ctx, &model.Input{ - EpochApplicationID: appID, - EpochIndex: enr, - Index: inr, - Status: model.InputCompletionStatus_Accepted, - RawData: emptyInput(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) + appID := s.newTestApplication(ctx, t, app) + epoch := repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build() + input := repotest.NewInputBuilder(). + WithIndex(inr). + WithRawData(emptyInput()). + Build() + s.createTestEpochWithInput(ctx, t, numberToName(app), epoch, input) many := uint64(100) limit := uint64(many / 2) - for onr := range many { - err = s.repository.CreateOutput(ctx, &model.Output{ - InputEpochApplicationID: appID, - InputIndex: inr, - Index: onr, - RawData: emptyVoucher(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, output_index: %v", 0, appID, onr) + outputData := make([][]byte, many) + for i := range many { + outputData[i] = emptyVoucher() } + s.advanceInput(ctx, t, appID, enr, inr, outputData, nil) type Result struct { EpochIndex hex64 `json:"epoch_index"` @@ -1386,7 +1328,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listReports", @@ -1408,35 +1350,24 @@ func TestMethod(t *testing.T) { app := uint64(3) enr := uint64(1) inr := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - - err = s.repository.CreateInput(ctx, &model.Input{ - EpochApplicationID: appID, - EpochIndex: enr, - Index: inr, - Status: model.InputCompletionStatus_Accepted, - RawData: emptyInput(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) + appID := s.newTestApplication(ctx, t, app) + epoch := repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build() + input := repotest.NewInputBuilder(). + WithIndex(inr). + WithRawData(emptyInput()). + Build() + s.createTestEpochWithInput(ctx, t, numberToName(app), epoch, input) many := uint64(100) limit := uint64(many / 2) - for onr := range many { - err = s.repository.CreateReport(ctx, &model.Report{ - InputEpochApplicationID: appID, - InputIndex: inr, - Index: onr, - RawData: emptyVoucher(), - }) - assert.Nil(t, err, "on test case: %v, application: %v, report_index: %v", 0, appID, onr) + reportData := make([][]byte, many) + for i := range many { + reportData[i] = emptyVoucher() } + s.advanceInput(ctx, t, appID, enr, inr, nil, reportData) type Result struct { EpochIndex hex64 `json:"epoch_index"` @@ -1578,20 +1509,18 @@ func TestMethod(t *testing.T) { enr := uint64(1) tnr := uint64(1) // correct (register) wrong := uint64(2) // incorrect (query) - appID := s.newTestApplication(ctx, t, 0, app) - err = s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: enr, - Address: common.HexToAddress(hexutil.EncodeUint64(tnr)), - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + err = s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(enr). + WithAddress(common.HexToAddress(hexutil.EncodeUint64(tnr))). + Build()) + require.NoError(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -1619,20 +1548,18 @@ func TestMethod(t *testing.T) { app := uint64(3) enr := uint64(1) tnr := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) - err = s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: enr, - Address: common.HexToAddress(hexutil.EncodeUint64(tnr)), - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + err = s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(enr). + WithAddress(common.HexToAddress(hexutil.EncodeUint64(tnr))). + Build()) + require.NoError(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -1698,7 +1625,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listTournaments", @@ -1720,24 +1647,22 @@ func TestMethod(t *testing.T) { ctx := context.Background() app := uint64(3) - appID := s.newTestApplication(ctx, t, 0, app) + appID := s.newTestApplication(ctx, t, app) many := uint64(100) limit := uint64(many / 2) for tnr := range many { - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: tnr, - VirtualIndex: tnr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: tnr, - Address: common.HexToAddress(hexutil.EncodeUint64(tnr)), - }) - assert.Nil(t, err, "on test case: %v, application: %v, index: %v", 0, appID, tnr) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(tnr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(tnr). + WithAddress(common.HexToAddress(hexutil.EncodeUint64(tnr))). + Build()) + require.NoError(t, err, "on test case: %v, application: %v, index: %v", 0, appID, tnr) } { // offset == 0, descending = false @@ -1869,15 +1794,12 @@ func TestMethod(t *testing.T) { app := uint64(1) nr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -1908,30 +1830,27 @@ func TestMethod(t *testing.T) { address := common.HexToAddress("0x01") commitment := common.HexToHash("0xdeadbeef") - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "failed to create epoch. on test case: %v, application: %v, epoch_index: %v.", 0, appID, nr) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: nr, - Address: address, - }) - assert.Nil(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - Commitment: commitment, - }) - assert.Nil(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(nr). + WithAddress(address). + Build()) + require.NoError(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + err = s.repository.CreateCommitment(ctx, numberToName(app), + repotest.NewCommitmentBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + WithCommitmentHash(commitment). + Build()) + require.NoError(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -1990,15 +1909,12 @@ func TestMethod(t *testing.T) { app := uint64(1) nr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -2029,39 +1945,38 @@ func TestMethod(t *testing.T) { address := common.HexToAddress("0x03") idHash := common.HexToHash("0x04") - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "failed to create epoch. on test case: %v, application: %v, epoch_index: %v.", 0, appID, nr) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: nr, - Address: address, - }) - assert.Nil(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - }) - assert.Nil(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateMatch(ctx, numberToName(app), &model.Match{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - Winner: model.WinnerCommitment_NONE, - DeletionReason: model.MatchDeletionReason_TIMEOUT, - IDHash: idHash, - }) - assert.Nil(t, err, "failed to create match. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(nr). + WithAddress(address). + Build()) + require.NoError(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + commitment := repotest.NewCommitmentBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + Build() + err = s.repository.CreateCommitment(ctx, numberToName(app), commitment) + require.NoError(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + err = s.repository.CreateMatch(ctx, numberToName(app), + repotest.NewMatchBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + WithIDHash(idHash). + WithCommitmentOne(commitment.Commitment). + WithCommitmentTwo(commitment.Commitment). + WithWinner(model.WinnerCommitment_NONE). + WithDeletionReason(model.MatchDeletionReason_TIMEOUT). + Build()) + require.NoError(t, err, "failed to create match. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -2121,15 +2036,12 @@ func TestMethod(t *testing.T) { app := uint64(1) nr := uint64(0) - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -2162,48 +2074,47 @@ func TestMethod(t *testing.T) { idHash := common.HexToHash("0x04") parent := common.HexToHash("0x05") - appID := s.newTestApplication(ctx, t, 0, app) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: nr, - OutputsMerkleRoot: &common.Hash{}, - ClaimTransactionHash: &common.Hash{}, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "failed to create epoch. on test case: %v, application: %v, epoch_index: %v.", 0, appID, nr) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: nr, - Address: address, - }) - assert.Nil(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - }) - assert.Nil(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateMatch(ctx, numberToName(app), &model.Match{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - Winner: model.WinnerCommitment_NONE, - DeletionReason: model.MatchDeletionReason_TIMEOUT, - IDHash: idHash, - }) - assert.Nil(t, err, "failed to create match. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) - - err = s.repository.CreateMatchAdvanced(ctx, numberToName(app), &model.MatchAdvanced{ - ApplicationID: appID, - EpochIndex: nr, - TournamentAddress: address, - IDHash: idHash, - OtherParent: parent, - }) - assert.Nil(t, err, "failed to create match advanced. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + appID := s.newTestApplication(ctx, t, app) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(nr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(nr). + WithAddress(address). + Build()) + require.NoError(t, err, "failed to create tournament. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + commitment := repotest.NewCommitmentBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + Build() + err = s.repository.CreateCommitment(ctx, numberToName(app), commitment) + require.NoError(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + err = s.repository.CreateMatch(ctx, numberToName(app), + repotest.NewMatchBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + WithIDHash(idHash). + WithCommitmentOne(commitment.Commitment). + WithCommitmentTwo(commitment.Commitment). + WithWinner(model.WinnerCommitment_NONE). + WithDeletionReason(model.MatchDeletionReason_TIMEOUT). + Build()) + require.NoError(t, err, "failed to create match. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) + + err = s.repository.CreateMatchAdvanced(ctx, numberToName(app), + repotest.NewMatchAdvancedBuilder(appID). + WithEpochIndex(nr). + WithTournamentAddress(address). + WithIDHash(idHash). + WithOtherParent(parent). + Build()) + require.NoError(t, err, "failed to create match advanced. on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", @@ -2257,7 +2168,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listMatches", @@ -2279,7 +2190,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() app := uint64(3) - appID := s.newTestApplication(ctx, t, 0, app) + appID := s.newTestApplication(ctx, t, app) many := uint64(100) limit := uint64(many / 2) @@ -2290,40 +2201,38 @@ func TestMethod(t *testing.T) { commitmentOne := common.HexToHash(hexutil.EncodeUint64(tnr)) commitmentTwo := common.HexToHash(hexutil.EncodeUint64(tnr)) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: tnr, - VirtualIndex: tnr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: tnr, - Address: common.HexToAddress(hexutil.EncodeUint64(tnr)), - }) - assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) - - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: tnr, - TournamentAddress: address, - Commitment: commitmentOne, - }) - assert.Nil(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) - - err = s.repository.CreateMatch(ctx, numberToName(app), &model.Match{ - ApplicationID: appID, - EpochIndex: tnr, - TournamentAddress: address, - Winner: model.WinnerCommitment_NONE, - DeletionReason: model.MatchDeletionReason_TIMEOUT, - IDHash: idHash, - CommitmentOne: commitmentOne, - CommitmentTwo: commitmentTwo, - }) - assert.Nil(t, err, "on test case: %v, application: %v, report_index: %v", 0, appID, tnr) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(tnr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(tnr). + WithAddress(common.HexToAddress(hexutil.EncodeUint64(tnr))). + Build()) + require.NoError(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) + + err = s.repository.CreateCommitment(ctx, numberToName(app), + repotest.NewCommitmentBuilder(appID). + WithEpochIndex(tnr). + WithTournamentAddress(address). + WithCommitmentHash(commitmentOne). + Build()) + require.NoError(t, err, "failed to create commitment. on test case: %v, application: %v, epoch_index: %v", 0, appID, tnr) + + err = s.repository.CreateMatch(ctx, numberToName(app), + repotest.NewMatchBuilder(appID). + WithEpochIndex(tnr). + WithTournamentAddress(address). + WithIDHash(idHash). + WithCommitmentOne(commitmentOne). + WithCommitmentTwo(commitmentTwo). + WithWinner(model.WinnerCommitment_NONE). + WithDeletionReason(model.MatchDeletionReason_TIMEOUT). + Build()) + require.NoError(t, err, "on test case: %v, application: %v, report_index: %v", 0, appID, tnr) } { // offset == 0, descending = false @@ -2455,7 +2364,7 @@ func TestMethod(t *testing.T) { app := uint64(0) nr := uint64(1) - s.newTestApplication(ctx, t, 0, app) + s.newTestApplication(ctx, t, app) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listMatchAdvances", @@ -2481,59 +2390,57 @@ func TestMethod(t *testing.T) { ctx := context.Background() app := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) + appID := s.newTestApplication(ctx, t, app) enr := uint64(2) tournamentAddress := common.HexToAddress(hexutil.EncodeUint64(enr)) commitment := common.HexToHash(hexutil.EncodeUint64(enr)) idHash := common.HexToHash(hexutil.EncodeUint64(enr)) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: enr, - Address: tournamentAddress, - }) - assert.Nil(t, err) - - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: enr, - TournamentAddress: tournamentAddress, - Commitment: commitment, - }) - assert.Nil(t, err) - - err = s.repository.CreateMatch(ctx, numberToName(app), &model.Match{ - ApplicationID: appID, - EpochIndex: enr, - TournamentAddress: tournamentAddress, - IDHash: idHash, - Winner: model.WinnerCommitment_NONE, - DeletionReason: model.MatchDeletionReason_NOT_DELETED, - CommitmentOne: commitment, - CommitmentTwo: commitment, - }) - assert.Nil(t, err) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(enr). + WithAddress(tournamentAddress). + Build()) + require.NoError(t, err) + + err = s.repository.CreateCommitment(ctx, numberToName(app), + repotest.NewCommitmentBuilder(appID). + WithEpochIndex(enr). + WithTournamentAddress(tournamentAddress). + WithCommitmentHash(commitment). + Build()) + require.NoError(t, err) + + err = s.repository.CreateMatch(ctx, numberToName(app), + repotest.NewMatchBuilder(appID). + WithEpochIndex(enr). + WithTournamentAddress(tournamentAddress). + WithIDHash(idHash). + WithCommitmentOne(commitment). + WithCommitmentTwo(commitment). + WithWinner(model.WinnerCommitment_NONE). + WithDeletionReason(model.MatchDeletionReason_NOT_DELETED). + Build()) + require.NoError(t, err) many := uint64(100) limit := uint64(many / 2) for nr := range many { otherParent := common.HexToHash(hexutil.EncodeUint64(nr)) - err = s.repository.CreateMatchAdvanced(ctx, numberToName(app), &model.MatchAdvanced{ - ApplicationID: appID, - EpochIndex: enr, - TournamentAddress: tournamentAddress, - IDHash: idHash, - OtherParent: otherParent, - }) - assert.Nil(t, err, "failed to create match advance, app: %v, nr: %v", app, nr) + err = s.repository.CreateMatchAdvanced(ctx, numberToName(app), + repotest.NewMatchAdvancedBuilder(appID). + WithEpochIndex(enr). + WithTournamentAddress(tournamentAddress). + WithIDHash(idHash). + WithOtherParent(otherParent). + Build()) + require.NoError(t, err, "failed to create match advance, app: %v, nr: %v", app, nr) } { // offset == 0, descending = false @@ -2676,7 +2583,7 @@ func TestMethod(t *testing.T) { ctx := context.Background() nr := uint64(1) - s.newTestApplication(ctx, t, 0, nr) + s.newTestApplication(ctx, t, nr) body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ "jsonrpc": "2.0", "method": "cartesi_listCommitments", @@ -2697,37 +2604,35 @@ func TestMethod(t *testing.T) { ctx := context.Background() app := uint64(1) - appID := s.newTestApplication(ctx, t, 0, app) + appID := s.newTestApplication(ctx, t, app) enr := uint64(2) tournamentAddress := common.HexToAddress(hexutil.EncodeUint64(enr)) - err := s.repository.CreateEpoch(ctx, &model.Epoch{ - ApplicationID: appID, - Index: enr, - VirtualIndex: enr, - Status: model.EpochStatus_ClaimAccepted, - }) - assert.Nil(t, err) - - err = s.repository.CreateTournament(ctx, numberToName(app), &model.Tournament{ - ApplicationID: appID, - EpochIndex: enr, - Address: tournamentAddress, - }) - assert.Nil(t, err) + s.createTestEpoch(ctx, t, numberToName(app), + repotest.NewEpochBuilder(appID). + WithIndex(enr). + WithStatus(model.EpochStatus_ClaimAccepted). + Build()) + + err := s.repository.CreateTournament(ctx, numberToName(app), + repotest.NewTournamentBuilder(appID). + WithEpochIndex(enr). + WithAddress(tournamentAddress). + Build()) + require.NoError(t, err) many := uint64(100) limit := uint64(many / 2) for nr := range many { commitment := common.HexToHash(hexutil.EncodeUint64(nr)) - err = s.repository.CreateCommitment(ctx, numberToName(app), &model.Commitment{ - ApplicationID: appID, - EpochIndex: enr, - TournamentAddress: tournamentAddress, - Commitment: commitment, - }) - assert.Nil(t, err) + err = s.repository.CreateCommitment(ctx, numberToName(app), + repotest.NewCommitmentBuilder(appID). + WithEpochIndex(enr). + WithTournamentAddress(tournamentAddress). + WithCommitmentHash(commitment). + Build()) + require.NoError(t, err) } { // offset == 0, descending = false @@ -2822,11 +2727,11 @@ func TestMethod(t *testing.T) { // tested methods, implemented methods and discover methods must match: data, err := discoverSpec.ReadFile("jsonrpc-discover.json") - assert.Nil(t, err) + require.NoError(t, err) var schema jsonrpcSchema err = json.Unmarshal(data, &schema) - assert.Nil(t, err) + require.NoError(t, err) allMethods := make(map[string]bool) tested := make(map[string]bool) diff --git a/internal/jsonrpc/util_test.go b/internal/jsonrpc/util_test.go index eb857bd7e..629d56b51 100644 --- a/internal/jsonrpc/util_test.go +++ b/internal/jsonrpc/util_test.go @@ -19,11 +19,12 @@ import ( "github.com/cartesi/rollups-node/internal/config" "github.com/cartesi/rollups-node/internal/model" "github.com/cartesi/rollups-node/internal/repository/factory" + "github.com/cartesi/rollups-node/internal/repository/repotest" "github.com/cartesi/rollups-node/pkg/service" "github.com/cartesi/rollups-node/test/tooling/db" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // histogram to gather statistics for each method @@ -73,16 +74,16 @@ func newTestService(t *testing.T, name string) *Service { ctx := context.Background() dbTestEndpoint, err := db.GetTestDatabaseEndpoint() - assert.Nil(t, err) + require.NoError(t, err) err = db.SetupTestPostgres(dbTestEndpoint) - assert.Nil(t, err) + require.NoError(t, err) repo, err := factory.NewRepositoryFromConnectionString(ctx, dbTestEndpoint) - assert.Nil(t, err) + require.NoError(t, err) logLevel, err := config.GetLogLevel() - assert.Nil(t, err) + require.NoError(t, err) ci := CreateInfo{ CreateInfo: service.CreateInfo{ @@ -93,7 +94,7 @@ func newTestService(t *testing.T, name string) *Service { Repository: repo, } s, err := Create(ctx, &ci) - assert.Nil(t, err, "on new test service") + require.NoError(t, err, "on new test service") return s } @@ -108,17 +109,14 @@ func numberToName(x uint64) string { } // create an application with mostly stub values. -func (s *Service) newTestApplication(ctx context.Context, t *testing.T, test, i uint64) int64 { +func (s *Service) newTestApplication(ctx context.Context, t *testing.T, i uint64) int64 { hex := numberToName(i) - id, err := s.repository.CreateApplication(ctx, &model.Application{ - Name: hex, - IApplicationAddress: common.HexToAddress(hex), - DataAvailability: []byte{0x00, 0x00, 0x00, 0x00}, - State: model.ApplicationState_Enabled, - ConsensusType: model.Consensus_Authority, - }, false) - assert.Nil(t, err, "on test case: %v, when creating application: %v", test, i) - return id + app := repotest.NewApplicationBuilder(). + WithName(hex). + WithAddress(common.HexToAddress(hex)). + WithDataAvailability([]byte{0x00, 0x00, 0x00, 0x00}). + Create(ctx, t, s.repository) + return app.ID } func (s *Service) doRequest(t *testing.T, i uint64, reqData []byte) []byte { @@ -134,7 +132,7 @@ func (s *Service) doRequest(t *testing.T, i uint64, reqData []byte) []byte { body := new(strings.Builder) _, err := io.Copy(body, w.Result().Body) - assert.Nil(t, err, "on test case: %v", i) + require.NoError(t, err, "on test case: %v", i) return []byte(body.String()) } @@ -151,6 +149,35 @@ func emptyVoucher() []byte { return raw } +// createTestEpoch creates an epoch using production CreateEpochsAndInputs. +func (s *Service) createTestEpoch(ctx context.Context, t *testing.T, appName string, epoch *model.Epoch) { + t.Helper() + err := s.repository.CreateEpochsAndInputs(ctx, appName, + map[*model.Epoch][]*model.Input{epoch: {}}, 10) + require.NoError(t, err) +} + +// createTestEpochWithInput creates an epoch with one input using production CreateEpochsAndInputs. +func (s *Service) createTestEpochWithInput( + ctx context.Context, t *testing.T, appName string, + epoch *model.Epoch, input *model.Input, +) { + t.Helper() + err := s.repository.CreateEpochsAndInputs(ctx, appName, + map[*model.Epoch][]*model.Input{epoch: {input}}, 10) + require.NoError(t, err) +} + +// advanceInput stores an advance result (outputs/reports) for an input using production StoreAdvanceResult. +func (s *Service) advanceInput( + ctx context.Context, t *testing.T, appID int64, + epochIndex, inputIndex uint64, outputs [][]byte, reports [][]byte, +) { + t.Helper() + repotest.StoreAdvanceResult(ctx, t, s.repository, appID, epochIndex, inputIndex, + model.InputCompletionStatus_Accepted, outputs, reports) +} + type listTournamentsResult struct { EpochIndex hex64 `json:"epoch_index"` Address common.Address `json:"address"` diff --git a/internal/repository/postgres/test_only.go b/internal/repository/postgres/test_only.go deleted file mode 100644 index 0ec2ec7ec..000000000 --- a/internal/repository/postgres/test_only.go +++ /dev/null @@ -1,135 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package postgres - -import ( - "context" - - "github.com/cartesi/rollups-node/internal/model" - "github.com/cartesi/rollups-node/internal/repository/postgres/db/rollupsdb/public/table" -) - -func (r *PostgresRepository) CreateEpoch( - ctx context.Context, - e *model.Epoch, -) error { - insertStmt := table.Epoch.INSERT( - table.Epoch.ApplicationID, - table.Epoch.Index, - table.Epoch.FirstBlock, - table.Epoch.LastBlock, - table.Epoch.InputIndexLowerBound, - table.Epoch.InputIndexUpperBound, - table.Epoch.MachineHash, - table.Epoch.OutputsMerkleRoot, - table.Epoch.OutputsMerkleProof, - table.Epoch.Commitment, - table.Epoch.CommitmentProof, - table.Epoch.ClaimTransactionHash, - table.Epoch.TournamentAddress, - table.Epoch.Status, - table.Epoch.VirtualIndex, - ).VALUES( - e.ApplicationID, - e.Index, - e.FirstBlock, - e.LastBlock, - e.InputIndexLowerBound, - e.InputIndexUpperBound, - hashToBytes(e.MachineHash), - e.OutputsMerkleRoot, - encodeSiblings(e.OutputsMerkleProof), - hashToBytes(e.Commitment), - encodeSiblings(e.CommitmentProof), - hashToBytes(e.ClaimTransactionHash), - addressToBytes(e.TournamentAddress), - e.Status, - e.VirtualIndex, - ) - - sqlStr, args := insertStmt.Sql() - _, err := r.db.Exec(ctx, sqlStr, args...) - return err -} - -func (r *PostgresRepository) CreateInput( - ctx context.Context, - inp *model.Input, -) error { - insertStmt := table.Input.INSERT( - table.Input.EpochApplicationID, - table.Input.EpochIndex, - table.Input.Index, - table.Input.BlockNumber, - table.Input.RawData, - table.Input.Status, - table.Input.MachineHash, - table.Input.OutputsHash, - table.Input.TransactionReference, - table.Input.SnapshotURI, - ).VALUES( - inp.EpochApplicationID, - inp.EpochIndex, - inp.Index, - inp.BlockNumber, - inp.RawData, - inp.Status, - hashToBytes(inp.MachineHash), - hashToBytes(inp.OutputsHash), - inp.TransactionReference, - inp.SnapshotURI, - ) - - sqlStr, args := insertStmt.Sql() - _, err := r.db.Exec(ctx, sqlStr, args...) - return err -} - -func (r *PostgresRepository) CreateOutput( - ctx context.Context, - out *model.Output, -) error { - insertStmt := table.Output.INSERT( - table.Output.InputEpochApplicationID, - table.Output.InputIndex, - table.Output.Index, - table.Output.RawData, - table.Output.Hash, - table.Output.OutputHashesSiblings, - table.Output.ExecutionTransactionHash, - ).VALUES( - out.InputEpochApplicationID, - out.InputIndex, - out.Index, - out.RawData, - hashToBytes(out.Hash), - encodeSiblings(out.OutputHashesSiblings), - hashToBytes(out.ExecutionTransactionHash), - ) - - sqlStr, args := insertStmt.Sql() - _, err := r.db.Exec(ctx, sqlStr, args...) - return err -} - -func (r *PostgresRepository) CreateReport( - ctx context.Context, - out *model.Report, -) error { - insertStmt := table.Report.INSERT( - table.Report.InputEpochApplicationID, - table.Report.InputIndex, - table.Report.Index, - table.Report.RawData, - ).VALUES( - out.InputEpochApplicationID, - out.InputIndex, - out.Index, - out.RawData, - ) - - sqlStr, args := insertStmt.Sql() - _, err := r.db.Exec(ctx, sqlStr, args...) - return err -} diff --git a/internal/repository/postgres/util.go b/internal/repository/postgres/util.go index 6dacc4140..0dd443f81 100644 --- a/internal/repository/postgres/util.go +++ b/internal/repository/postgres/util.go @@ -41,13 +41,6 @@ func hashToBytes(h *common.Hash) any { return (*h)[:] } -func addressToBytes(a *common.Address) any { - if a == nil { - return nil - } - return (*a)[:] -} - // SubstrBytea returns a SUBSTR expression properly typed as ByteaExpression. func SubstrBytea(col postgres.ColumnBytea, from, count int64) postgres.ByteaExpression { qualified := pgx.Identifier{col.TableName(), col.Name()}.Sanitize() diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 380c96a8a..e479f8818 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -210,13 +210,6 @@ type ClaimerRepository interface { ) error } -type TestRepository interface { - CreateEpoch(ctx context.Context, e *Epoch) error - CreateInput(ctx context.Context, inp *Input) error - CreateOutput(ctx context.Context, out *Output) error - CreateReport(ctx context.Context, report *Report) error -} - type Repository interface { ApplicationRepository EpochRepository @@ -231,7 +224,6 @@ type Repository interface { BulkOperationsRepository NodeConfigRepository ClaimerRepository - TestRepository Close() } diff --git a/internal/repository/repotest/builders.go b/internal/repository/repotest/builders.go index dcf0d4161..e4fac2969 100644 --- a/internal/repository/repotest/builders.go +++ b/internal/repository/repotest/builders.go @@ -135,14 +135,10 @@ func NewEpochBuilder(appID int64) *EpochBuilder { } } +// WithIndex sets the epoch's physical Index. +// Note: VirtualIndex is auto-assigned by CreateEpochsAndInputs. func (b *EpochBuilder) WithIndex(i uint64) *EpochBuilder { b.epoch.Index = i - b.epoch.VirtualIndex = i - return b -} - -func (b *EpochBuilder) WithVirtualIndex(i uint64) *EpochBuilder { - b.epoch.VirtualIndex = i return b } @@ -209,6 +205,11 @@ func (b *InputBuilder) WithIndex(i uint64) *InputBuilder { return b } +func (b *InputBuilder) WithEpochApplicationID(id int64) *InputBuilder { + b.input.EpochApplicationID = id + return b +} + func (b *InputBuilder) WithEpochIndex(i uint64) *InputBuilder { b.input.EpochIndex = i return b @@ -240,103 +241,6 @@ func (b *InputBuilder) Build() *Input { return &i } -// --------------------------------------------------------------------------- -// OutputBuilder -// --------------------------------------------------------------------------- - -type OutputBuilder struct { - output *Output -} - -func NewOutputBuilder(appID int64) *OutputBuilder { - return &OutputBuilder{ - output: &Output{ - InputEpochApplicationID: appID, - EpochIndex: 0, - InputIndex: 0, - Index: 0, - RawData: []byte("output-data"), - }, - } -} - -func (b *OutputBuilder) WithEpochIndex(i uint64) *OutputBuilder { - b.output.EpochIndex = i - return b -} - -func (b *OutputBuilder) WithInputIndex(i uint64) *OutputBuilder { - b.output.InputIndex = i - return b -} - -func (b *OutputBuilder) WithIndex(i uint64) *OutputBuilder { - b.output.Index = i - return b -} - -func (b *OutputBuilder) WithRawData(data []byte) *OutputBuilder { - b.output.RawData = data - return b -} - -func (b *OutputBuilder) WithHash(h common.Hash) *OutputBuilder { - b.output.Hash = &h - return b -} - -// Build returns a copy of the Output model without persisting it. -func (b *OutputBuilder) Build() *Output { - o := *b.output - return &o -} - -// --------------------------------------------------------------------------- -// ReportBuilder -// --------------------------------------------------------------------------- - -type ReportBuilder struct { - report *Report -} - -func NewReportBuilder(appID int64) *ReportBuilder { - return &ReportBuilder{ - report: &Report{ - InputEpochApplicationID: appID, - EpochIndex: 0, - InputIndex: 0, - Index: 0, - RawData: []byte("report-data"), - }, - } -} - -func (b *ReportBuilder) WithEpochIndex(i uint64) *ReportBuilder { - b.report.EpochIndex = i - return b -} - -func (b *ReportBuilder) WithInputIndex(i uint64) *ReportBuilder { - b.report.InputIndex = i - return b -} - -func (b *ReportBuilder) WithIndex(i uint64) *ReportBuilder { - b.report.Index = i - return b -} - -func (b *ReportBuilder) WithRawData(data []byte) *ReportBuilder { - b.report.RawData = data - return b -} - -// Build returns a copy of the Report model without persisting it. -func (b *ReportBuilder) Build() *Report { - r := *b.report - return &r -} - // --------------------------------------------------------------------------- // TournamentBuilder // --------------------------------------------------------------------------- diff --git a/internal/repository/repotest/output_test_cases.go b/internal/repository/repotest/output_test_cases.go index e554c68d9..f8c3838bb 100644 --- a/internal/repository/repotest/output_test_cases.go +++ b/internal/repository/repotest/output_test_cases.go @@ -21,11 +21,8 @@ func (s *OutputSuite) TestGetOutput() { s.Run("ExistingOutput", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0). - WithRawData([]byte("output-data")).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("output-data")}, nil) got, err := s.Repo.GetOutput(s.Ctx, seed.App.IApplicationAddress.String(), 0) s.Require().NoError(err) @@ -54,12 +51,9 @@ func (s *OutputSuite) TestListOutputs() { s.Run("ReturnsAllOutputs", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1"), []byte("o2")}, nil) outputs, total, err := s.Repo.ListOutputs( s.Ctx, seed.App.IApplicationAddress.String(), @@ -100,12 +94,8 @@ func (s *OutputSuite) TestListOutputs() { s.Run("FilterByInputIndex", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1"), []byte("o2")}, nil) inputIdx := uint64(0) outputs, total, err := s.Repo.ListOutputs( @@ -172,17 +162,8 @@ func (s *OutputSuite) TestListOutputs() { rawWithOther := make([]byte, 32) copy(rawWithOther[0:4], otherType) - out0 := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0). - WithRawData(rawWithType).Build() - err := s.Repo.CreateOutput(s.Ctx, out0) - s.Require().NoError(err) - - out1 := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(1). - WithRawData(rawWithOther).Build() - err = s.Repo.CreateOutput(s.Ctx, out1) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{rawWithType, rawWithOther}, nil) outputs, total, err := s.Repo.ListOutputs( s.Ctx, seed.App.IApplicationAddress.String(), @@ -207,17 +188,8 @@ func (s *OutputSuite) TestListOutputs() { rawWithOther := make([]byte, 64) copy(rawWithOther[16:36], otherAddr.Bytes()) - out0 := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0). - WithRawData(rawWithVoucher).Build() - err := s.Repo.CreateOutput(s.Ctx, out0) - s.Require().NoError(err) - - out1 := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(1). - WithRawData(rawWithOther).Build() - err = s.Repo.CreateOutput(s.Ctx, out1) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{rawWithVoucher, rawWithOther}, nil) outputs, total, err := s.Repo.ListOutputs( s.Ctx, seed.App.IApplicationAddress.String(), @@ -231,12 +203,12 @@ func (s *OutputSuite) TestListOutputs() { s.Run("Pagination", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(5) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) + + data := make([][]byte, 5) + for i := range data { + data[i] = []byte("output-data") } + s.storeAdvanceResult(seed.App.ID, 0, 0, data, nil) outputs, total, err := s.Repo.ListOutputs( s.Ctx, seed.App.IApplicationAddress.String(), @@ -249,12 +221,9 @@ func (s *OutputSuite) TestListOutputs() { s.Run("Descending", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1"), []byte("o2")}, nil) outputs, _, err := s.Repo.ListOutputs( s.Ctx, seed.App.IApplicationAddress.String(), @@ -273,14 +242,16 @@ func (s *OutputSuite) TestUpdateOutputsExecution() { s.Run("UpdatesExecutionHash", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("output-data")}, nil) txHash := UniqueHash() - out.ExecutionTransactionHash = &txHash - err = s.Repo.UpdateOutputsExecution( + out := &Output{ + InputEpochApplicationID: seed.App.ID, + Index: 0, + ExecutionTransactionHash: &txHash, + } + err := s.Repo.UpdateOutputsExecution( s.Ctx, seed.App.IApplicationAddress.String(), []*Output{out}, 100) s.Require().NoError(err) @@ -295,13 +266,8 @@ func (s *OutputSuite) TestUpdateOutputsExecution() { s.Run("MultipleOutputsUpdatedAtomically", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - // Create 3 outputs - for i := range uint64(3) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1"), []byte("o2")}, nil) txHash := UniqueHash() outputs := make([]*Output, 3) @@ -331,17 +297,15 @@ func (s *OutputSuite) TestUpdateOutputsExecution() { s.Run("NilExecutionHashReturnsError", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("output-data")}, nil) // ExecutionTransactionHash is nil — should fail badOutput := &Output{ InputEpochApplicationID: seed.App.ID, Index: 0, } - err = s.Repo.UpdateOutputsExecution( + err := s.Repo.UpdateOutputsExecution( s.Ctx, seed.App.IApplicationAddress.String(), []*Output{badOutput}, 100) s.Require().Error(err) @@ -354,13 +318,8 @@ func (s *OutputSuite) TestUpdateOutputsExecution() { s.Run("RollbackOnPartialFailure", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - // Create 2 valid outputs (index 0 and 1) - for i := range uint64(2) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1")}, nil) txHash := UniqueHash() outputs := []*Output{ @@ -407,12 +366,8 @@ func (s *OutputSuite) TestUpdateOutputsExecution() { s.Run("NilHashMidLoopRollsBackPrior", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(2) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1")}, nil) txHash := UniqueHash() outputs := []*Output{ @@ -503,13 +458,8 @@ func (s *OutputSuite) TestGetNumberOfExecutedOutputs() { s.Run("ReturnsCountAfterExecution", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - // Create outputs - for i := range uint64(3) { - out := NewOutputBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateOutput(s.Ctx, out) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + [][]byte{[]byte("o0"), []byte("o1"), []byte("o2")}, nil) // Execute 2 of the 3 outputs txHash := UniqueHash() diff --git a/internal/repository/repotest/report_test_cases.go b/internal/repository/repotest/report_test_cases.go index 3ecaf1f29..8d5c74571 100644 --- a/internal/repository/repotest/report_test_cases.go +++ b/internal/repository/repotest/report_test_cases.go @@ -20,11 +20,8 @@ func (s *ReportSuite) TestGetReport() { s.Run("ExistingReport", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - report := NewReportBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(0). - WithRawData([]byte("report-payload")).Build() - err := s.Repo.CreateReport(s.Ctx, report) - s.Require().NoError(err) + s.storeAdvanceResult(seed.App.ID, 0, 0, + nil, [][]byte{[]byte("report-payload")}) got, err := s.Repo.GetReport(s.Ctx, seed.App.IApplicationAddress.String(), 0) s.Require().NoError(err) @@ -53,12 +50,9 @@ func (s *ReportSuite) TestListReports() { s.Run("ReturnsAllReports", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - r := NewReportBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateReport(s.Ctx, r) - s.Require().NoError(err) - } + + s.storeAdvanceResult(seed.App.ID, 0, 0, + nil, [][]byte{[]byte("r0"), []byte("r1"), []byte("r2")}) reports, total, err := s.Repo.ListReports( s.Ctx, seed.App.IApplicationAddress.String(), @@ -99,12 +93,8 @@ func (s *ReportSuite) TestListReports() { s.Run("FilterByInputIndex", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - r := NewReportBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateReport(s.Ctx, r) - s.Require().NoError(err) - } + s.storeAdvanceResult(seed.App.ID, 0, 0, + nil, [][]byte{[]byte("r0"), []byte("r1"), []byte("r2")}) inputIdx := uint64(0) reports, total, err := s.Repo.ListReports( @@ -118,12 +108,12 @@ func (s *ReportSuite) TestListReports() { s.Run("Pagination", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(5) { - r := NewReportBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateReport(s.Ctx, r) - s.Require().NoError(err) + + data := make([][]byte, 5) + for i := range data { + data[i] = []byte("report-data") } + s.storeAdvanceResult(seed.App.ID, 0, 0, nil, data) reports, total, err := s.Repo.ListReports( s.Ctx, seed.App.IApplicationAddress.String(), @@ -136,12 +126,9 @@ func (s *ReportSuite) TestListReports() { s.Run("Descending", func() { seed := Seed(s.Ctx, s.T(), s.Repo) - for i := range uint64(3) { - r := NewReportBuilder(seed.App.ID). - WithEpochIndex(0).WithInputIndex(0).WithIndex(i).Build() - err := s.Repo.CreateReport(s.Ctx, r) - s.Require().NoError(err) - } + + s.storeAdvanceResult(seed.App.ID, 0, 0, + nil, [][]byte{[]byte("r0"), []byte("r1"), []byte("r2")}) reports, _, err := s.Repo.ListReports( s.Ctx, seed.App.IApplicationAddress.String(), diff --git a/internal/repository/repotest/repotest.go b/internal/repository/repotest/repotest.go index 612278e10..c594b249f 100644 --- a/internal/repository/repotest/repotest.go +++ b/internal/repository/repotest/repotest.go @@ -8,7 +8,9 @@ import ( "testing" "time" + . "github.com/cartesi/rollups-node/internal/model" "github.com/cartesi/rollups-node/internal/repository" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -33,6 +35,39 @@ type BaseSuite struct { cancel context.CancelFunc } +// StoreAdvanceResult is a test helper that creates outputs and/or reports +// for an input via the production StoreAdvanceResult method. +func StoreAdvanceResult( + ctx context.Context, t *testing.T, repo repository.Repository, + appID int64, epochIdx, inputIdx uint64, + status InputCompletionStatus, outputs [][]byte, reports [][]byte, +) { + t.Helper() + result := &AdvanceResult{ + EpochIndex: epochIdx, + InputIndex: inputIdx, + Status: status, + Outputs: outputs, + Reports: reports, + OutputsProof: OutputsProof{ + OutputsHash: UniqueHash(), + MachineHash: UniqueHash(), + }, + } + err := repo.StoreAdvanceResult(ctx, appID, result) + require.NoError(t, err) +} + +// storeAdvanceResult delegates to the standalone StoreAdvanceResult helper +// with InputCompletionStatus_Accepted as the default status. +func (s *BaseSuite) storeAdvanceResult( + appID int64, epochIdx, inputIdx uint64, outputs [][]byte, reports [][]byte, +) { + s.T().Helper() + StoreAdvanceResult(s.Ctx, s.T(), s.Repo, appID, epochIdx, inputIdx, + InputCompletionStatus_Accepted, outputs, reports) +} + func (s *BaseSuite) SetupSuite() { s.Ctx, s.cancel = context.WithTimeout(context.Background(), testTimeout) }