diff --git a/bridge/l2/relayer_test.go b/bridge/l2/relayer_test.go index 6ca6d6a13..82c8b3939 100644 --- a/bridge/l2/relayer_test.go +++ b/bridge/l2/relayer_test.go @@ -189,7 +189,9 @@ func testL2RelayerProcessCommittedBatches(t *testing.T) { tProof := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} tInstanceCommitments := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} - err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, 100) + finalPair := []byte{0, 1} + vk := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} + err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, finalPair, vk, 100) assert.NoError(t, err) err = db.UpdateProvingStatus(batchID, orm.ProvingTaskVerified) assert.NoError(t, err) @@ -229,7 +231,9 @@ func testL2RelayerSkipBatches(t *testing.T) { tProof := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} tInstanceCommitments := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} - err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, 100) + finalPair := []byte{0, 1} + vk := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} + err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, finalPair, vk, 100) assert.NoError(t, err) err = db.UpdateProvingStatus(batchID, provingStatus) assert.NoError(t, err) diff --git a/bridge/tests/l2_message_relay_test.go b/bridge/tests/l2_message_relay_test.go index d5387fdb2..e6cf841b8 100644 --- a/bridge/tests/l2_message_relay_test.go +++ b/bridge/tests/l2_message_relay_test.go @@ -105,7 +105,9 @@ func testRelayL2MessageSucceed(t *testing.T) { // add dummy proof tProof := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} tInstanceCommitments := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} - err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, 100) + finalPair := []byte{0, 1} + vk := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} + err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, finalPair, vk, 100) assert.NoError(t, err) err = db.UpdateProvingStatus(batchID, orm.ProvingTaskVerified) assert.NoError(t, err) diff --git a/bridge/tests/rollup_test.go b/bridge/tests/rollup_test.go index 3a697e9b6..0b93401e6 100644 --- a/bridge/tests/rollup_test.go +++ b/bridge/tests/rollup_test.go @@ -107,7 +107,9 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) { // add dummy proof tProof := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} tInstanceCommitments := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} - err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, 100) + finalPair := []byte{0, 1} + vk := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} + err = db.UpdateProofByID(context.Background(), batchID, tProof, tInstanceCommitments, finalPair, vk, 100) assert.NoError(t, err) err = db.UpdateProvingStatus(batchID, orm.ProvingTaskVerified) assert.NoError(t, err) diff --git a/coordinator/manager.go b/coordinator/manager.go index 137b8458b..0d38c0f5f 100644 --- a/coordinator/manager.go +++ b/coordinator/manager.go @@ -206,6 +206,41 @@ func (m *Manager) restorePrevSessions() { } go m.CollectProofs(sess) + + id := sess.info.ID + batches, err := m.orm.GetBlockBatches(map[string]interface{}{"id": id}) + if err != nil || len(batches) == 0 { + log.Error("Failed to GetBlockBatches", "batch_id", id, "err", err) + continue + } + m.tryVerify(batches[0]) + } + } +} + +// TryVerify verifies a proof for batch received form roller if previous verification had interrupted +func (m *Manager) tryVerify(batch *orm.BlockBatch) { + proof := &message.AggProof{ + Proof: batch.Proof, + Instance: batch.InstanceCommitments, + FinalPair: batch.FinalPair, + Vk: batch.Vk, + } + success, err := m.verifier.VerifyProof(proof) + if err != nil { + success = false + log.Error("Failed to verify zk proof", "proof id", batch.ID, "error", err) + } else { + log.Info("Verify zk proof successfully", "verification result", success, "proof id", batch.ID) + } + + if success { + if dbErr := m.orm.UpdateProvingStatus(batch.ID, orm.ProvingTaskVerified); dbErr != nil { + log.Error( + "failed to update proving_status", + "msg.ID", batch.ID, + "status", orm.ProvingTaskVerified, + "error", dbErr) } } } @@ -282,7 +317,7 @@ func (m *Manager) handleZkProof(pk string, msg *message.ProofDetail) error { } // store proof content - if dbErr = m.orm.UpdateProofByID(m.ctx, msg.ID, msg.Proof.Proof, msg.Proof.FinalPair, proofTimeSec); dbErr != nil { + if dbErr = m.orm.UpdateProofByID(m.ctx, msg.ID, msg.Proof.Proof, msg.Proof.Instance, msg.Proof.FinalPair, msg.Proof.Vk, proofTimeSec); dbErr != nil { log.Error("failed to store proof into db", "error", dbErr) return dbErr } diff --git a/database/migrate/migrations/00004_block_batch.sql b/database/migrate/migrations/00004_block_batch.sql index 4d952db82..b5a8475c3 100644 --- a/database/migrate/migrations/00004_block_batch.sql +++ b/database/migrate/migrations/00004_block_batch.sql @@ -15,6 +15,8 @@ create table block_batch proving_status INTEGER DEFAULT 1, proof BYTEA DEFAULT NULL, instance_commitments BYTEA DEFAULT NULL, + final_pair BYTEA DEFAULT NULL, + vk BYTEA DEFAULT NULL, proof_time_sec INTEGER DEFAULT 0, rollup_status INTEGER DEFAULT 1, commit_tx_hash VARCHAR DEFAULT NULL, diff --git a/database/orm/block_batch.go b/database/orm/block_batch.go index c2b997df1..034a83a48 100644 --- a/database/orm/block_batch.go +++ b/database/orm/block_batch.go @@ -89,6 +89,8 @@ type BlockBatch struct { ProvingStatus ProvingStatus `json:"proving_status" db:"proving_status"` Proof []byte `json:"proof" db:"proof"` InstanceCommitments []byte `json:"instance_commitments" db:"instance_commitments"` + FinalPair []byte `json:"final_pair" db:"final_pair"` + Vk []byte `json:"vk" db:"vk"` ProofTimeSec uint64 `json:"proof_time_sec" db:"proof_time_sec"` RollupStatus RollupStatus `json:"rollup_status" db:"rollup_status"` CommitTxHash sql.NullString `json:"commit_tx_hash" db:"commit_tx_hash"` @@ -159,11 +161,11 @@ func (o *blockBatchOrm) GetVerifiedProofAndInstanceByID(id string) ([]byte, []by return proof, instance, nil } -func (o *blockBatchOrm) UpdateProofByID(ctx context.Context, id string, proof, instanceCommitments []byte, proofTimeSec uint64) error { +func (o *blockBatchOrm) UpdateProofByID(ctx context.Context, id string, proof, instanceCommitments, finalPair, vk []byte, proofTimeSec uint64) error { db := o.db if _, err := db.ExecContext(ctx, - db.Rebind(`UPDATE block_batch set proof = ?, instance_commitments = ?, proof_time_sec = ? where id = ?;`), - proof, instanceCommitments, proofTimeSec, id, + db.Rebind(`UPDATE block_batch set proof = ?, instance_commitments = ?, final_pair = ?, vk = ?, proof_time_sec = ? where id = ?;`), + proof, instanceCommitments, finalPair, vk, proofTimeSec, id, ); err != nil { log.Error("failed to update proof", "err", err) } diff --git a/database/orm/interface.go b/database/orm/interface.go index 65e9e6205..d305486b4 100644 --- a/database/orm/interface.go +++ b/database/orm/interface.go @@ -140,7 +140,7 @@ type BlockBatchOrm interface { GetBlockBatches(fields map[string]interface{}, args ...string) ([]*BlockBatch, error) GetProvingStatusByID(id string) (ProvingStatus, error) GetVerifiedProofAndInstanceByID(id string) ([]byte, []byte, error) - UpdateProofByID(ctx context.Context, id string, proof, instanceCommitments []byte, proofTimeSec uint64) error + UpdateProofByID(ctx context.Context, id string, proof, instanceCommitments, finalPair, vk []byte, proofTimeSec uint64) error UpdateProvingStatus(id string, status ProvingStatus) error ResetProvingStatusFor(before ProvingStatus) error NewBatchInDBTx(dbTx *sqlx.Tx, startBlock *BlockInfo, endBlock *BlockInfo, parentHash string, totalTxNum uint64, gasUsed uint64) (string, error) diff --git a/database/orm_test.go b/database/orm_test.go index 9206458f6..e5f3edc7c 100644 --- a/database/orm_test.go +++ b/database/orm_test.go @@ -298,7 +298,7 @@ func testOrmBlockBatch(t *testing.T) { provingStatus, err := ormBatch.GetProvingStatusByID(batchID1) assert.NoError(t, err) assert.Equal(t, orm.ProvingTaskUnassigned, provingStatus) - err = ormBatch.UpdateProofByID(context.Background(), batchID1, []byte{1}, []byte{2}, 1200) + err = ormBatch.UpdateProofByID(context.Background(), batchID1, []byte{1}, []byte{2}, []byte{3}, []byte{4}, 1200) assert.NoError(t, err) err = ormBatch.UpdateProvingStatus(batchID1, orm.ProvingTaskVerified) assert.NoError(t, err)