From 13e7dc396b6014b075b6b7009ef3099224b9299f Mon Sep 17 00:00:00 2001 From: Nazarii Denha Date: Thu, 26 Jan 2023 13:42:59 +0100 Subject: [PATCH] add test --- coordinator/config.json | 1 + coordinator/config/config.go | 7 +-- coordinator/manager_test.go | 76 +++++++++++++++++++++++++++----- coordinator/verifier/verifier.go | 4 ++ 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/coordinator/config.json b/coordinator/config.json index d9a37892d..f7caa006b 100644 --- a/coordinator/config.json +++ b/coordinator/config.json @@ -6,6 +6,7 @@ "token_time_to_live": 60, "verifier": { "mock_mode": true, + "long_mock_mode": false, "params_path": "", "agg_vk_path": "" } diff --git a/coordinator/config/config.go b/coordinator/config/config.go index 6261e052b..e408418ba 100644 --- a/coordinator/config/config.go +++ b/coordinator/config/config.go @@ -39,9 +39,10 @@ type Config struct { // VerifierConfig load zk verifier config. type VerifierConfig struct { - MockMode bool `json:"mock_mode"` - ParamsPath string `json:"params_path"` - AggVkPath string `json:"agg_vk_path"` + MockMode bool `json:"mock_mode"` + LongMockMode bool `json:"long_mock_mode"` + ParamsPath string `json:"params_path"` + AggVkPath string `json:"agg_vk_path"` } // NewConfig returns a new instance of Config. diff --git a/coordinator/manager_test.go b/coordinator/manager_test.go index 6d077ed7d..cfee25470 100644 --- a/coordinator/manager_test.go +++ b/coordinator/manager_test.go @@ -70,6 +70,7 @@ func TestApis(t *testing.T) { // TODO: Restart roller alone when received task, can add this test case in integration-test. //t.Run("TestRollerReconnect", testRollerReconnect) t.Run("TestGracefulRestart", testGracefulRestart) + t.Run("TestVerifyProvedBatchesOnStartup", testVerifyProvedBatchesOnStartup) // Teardown t.Cleanup(func() { @@ -86,7 +87,7 @@ func testHandshake(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -107,7 +108,7 @@ func testFailedHandshake(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -173,7 +174,7 @@ func testSeveralConnections(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -226,7 +227,7 @@ func testValidProof(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 3, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 3, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -286,7 +287,7 @@ func testInvalidProof(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 3, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 3, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -345,7 +346,7 @@ func testIdleRollerSelection(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) defer func() { handler.Shutdown(context.Background()) rollerManager.Stop() @@ -414,7 +415,7 @@ func testGracefulRestart(t *testing.T) { // Setup coordinator and ws server. wsURL := "ws://" + randomURL() - rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) // create mock roller roller := newMockRoller(t, "roller_test", wsURL) @@ -431,7 +432,7 @@ func testGracefulRestart(t *testing.T) { rollerManager.Stop() // Setup new coordinator and ws server. - newRollerManager, newHandler := setupCoordinator(t, cfg.DBConfig, 1, wsURL) + newRollerManager, newHandler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) defer func() { newHandler.Shutdown(context.Background()) newRollerManager.Stop() @@ -475,14 +476,69 @@ func testGracefulRestart(t *testing.T) { } } -func setupCoordinator(t *testing.T, dbCfg *database.DBConfig, rollersPerSession uint8, wsURL string) (rollerManager *coordinator.Manager, handler *http.Server) { +func testVerifyProvedBatchesOnStartup(t *testing.T) { + // Create db handler and reset db. + l2db, err := database.NewOrmFactory(cfg.DBConfig) + assert.NoError(t, err) + assert.NoError(t, migrate.ResetDB(l2db.GetDB().DB)) + defer l2db.Close() + + var ids = make([]string, 1) + dbTx, err := l2db.Beginx() + assert.NoError(t, err) + for i := range ids { + ids[i], err = l2db.NewBatchInDBTx(dbTx, &orm.BlockInfo{Number: uint64(i)}, &orm.BlockInfo{Number: uint64(i)}, "0f", 1, 194676) + assert.NoError(t, err) + } + assert.NoError(t, dbTx.Commit()) + + // Setup coordinator and ws server. + wsURL := "ws://" + randomURL() + rollerManager, handler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, true) + + // create mock roller + roller := newMockRoller(t, "roller_test", wsURL) + roller.waitTaskAndSendProof(t, 500*time.Millisecond, false, true) + + // wait for coordinator to receive proof + <-time.After(5 * time.Second) + // the coordinator will delete the roller if the subscription is closed. + roller.close() + + // Close rollerManager and ws handler. + handler.Shutdown(context.Background()) + rollerManager.Stop() + + // Setup new coordinator and ws server. + newRollerManager, newHandler := setupCoordinator(t, cfg.DBConfig, 1, wsURL, false) + // Wait for coordinator verify task + <-time.After(6 * time.Second) + defer func() { + newHandler.Shutdown(context.Background()) + newRollerManager.Stop() + }() + + // verify that task is verified + for i := range ids { + info, err := newRollerManager.GetSessionInfo(ids[i]) + assert.Equal(t, orm.ProvingTaskAssigned.String(), info.Status) + assert.NoError(t, err) + + // at this point, roller haven't submitted + status, err := l2db.GetProvingStatusByID(ids[i]) + assert.NoError(t, err) + assert.Equal(t, orm.ProvingTaskVerified, status) + } +} + +func setupCoordinator(t *testing.T, dbCfg *database.DBConfig, rollersPerSession uint8, wsURL string, longMockMode bool) (rollerManager *coordinator.Manager, handler *http.Server) { // Get db handler. db, err := database.NewOrmFactory(dbCfg) assert.True(t, assert.NoError(t, err), "failed to get db handler.") rollerManager, err = coordinator.New(context.Background(), &coordinator_config.RollerManagerConfig{ RollersPerSession: rollersPerSession, - Verifier: &coordinator_config.VerifierConfig{MockMode: true}, + Verifier: &coordinator_config.VerifierConfig{MockMode: true, LongMockMode: longMockMode}, CollectionTime: 1, TokenTimeToLive: 5, }, db, nil) diff --git a/coordinator/verifier/verifier.go b/coordinator/verifier/verifier.go index 7098ab808..e829d5435 100644 --- a/coordinator/verifier/verifier.go +++ b/coordinator/verifier/verifier.go @@ -12,6 +12,7 @@ import "C" //nolint:typecheck import ( "encoding/json" + "time" "unsafe" "github.com/scroll-tech/go-ethereum/log" @@ -46,6 +47,9 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) { // VerifyProof Verify a ZkProof by marshaling it and sending it to the Halo2 Verifier. func (v *Verifier) VerifyProof(proof *message.AggProof) (bool, error) { if v.cfg.MockMode { + if v.cfg.LongMockMode { + <-time.After(10 * time.Second) + } log.Info("Verifier disabled, VerifyProof skipped") return true, nil