Skip to content

Commit

Permalink
feat: calculate seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM committed Nov 21, 2024
1 parent 1a34f0f commit ee022c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions aggregator/db/dbstorage/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func (d *DBStorage) UpdateGeneratedProof(ctx context.Context, proof *state.Proof
*updatedAt = uint64(now.Unix())
}
_, err := e.Exec(
addGeneratedProofSQL, proof.BatchNumber, proof.BatchNumberFinal, proof.Proof, proof.ProofID,
proof.InputProver, proof.Prover, proof.ProverID, generatingSince, updatedAt,
addGeneratedProofSQL, proof.Proof, proof.ProofID, proof.InputProver,
proof.Prover, proof.ProverID, generatingSince, updatedAt, proof.BatchNumber, proof.BatchNumberFinal,
)
return err
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func (d *DBStorage) CleanupLockedProofs(ctx context.Context, duration string, db

difference := time.Now().Unix() - seconds

sql := fmt.Sprintf("DELETE FROM proof WHERE generating_since < %d", difference)
sql := fmt.Sprintf("DELETE FROM proof WHERE generating_since is not null and generating_since < %d", difference)
e := d.getExecQuerier(dbTx)
ct, err := e.Exec(sql)
if err != nil {
Expand Down
27 changes: 25 additions & 2 deletions aggregator/db/dbstorage/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var (
proofID = "proof_1"
prover = "prover_1"
proverID = "prover_id"
now = time.Now()
)

func Test_Proof(t *testing.T) {
Expand All @@ -25,6 +24,7 @@ func Test_Proof(t *testing.T) {
assert.NoError(t, err)

ctx := context.Background()
now := time.Now()

DBStorage, err := NewDBStorage(dbPath)
assert.NoError(t, err)
Expand Down Expand Up @@ -90,13 +90,17 @@ func Test_Proof(t *testing.T) {
assert.NoError(t, err)

sequence := state.Sequence{FromBatchNumber: 3, ToBatchNumber: 4}

proof3 := state.Proof{
BatchNumber: 3,
BatchNumberFinal: 3,
GeneratingSince: nil,
}

proof4 := state.Proof{
BatchNumber: 4,
BatchNumberFinal: 4,
GeneratingSince: nil,
}

err = DBStorage.AddSequence(ctx, sequence, dbtxer)
Expand All @@ -119,9 +123,28 @@ func Test_Proof(t *testing.T) {
err = DBStorage.CleanupGeneratedProofs(ctx, 1, dbtxer)
assert.NoError(t, err)

_, err = DBStorage.CleanupLockedProofs(ctx, "1s", dbtxer)
now = time.Now()

proof3.GeneratingSince = &now
proof4.GeneratingSince = &now

err = DBStorage.AddGeneratedProof(ctx, &proof3, dbtxer)
assert.NoError(t, err)

err = DBStorage.AddGeneratedProof(ctx, &proof4, dbtxer)
assert.NoError(t, err)

time.Sleep(5 * time.Second)

affected, err := DBStorage.CleanupLockedProofs(ctx, "4s", dbtxer)
assert.NoError(t, err)
require.Equal(t, int64(2), affected)

proof5, proof6, err = DBStorage.GetProofsToAggregate(ctx, dbtxer)
assert.EqualError(t, err, state.ErrNotFound.Error())
assert.Nil(t, proof5)
assert.Nil(t, proof6)

err = DBStorage.DeleteUngeneratedProofs(ctx, dbtxer)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion aggregator/db/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS proof (
prover_id varchar NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
generating_since BIGINT,
generating_since BIGINT DEFAULT NULL,
PRIMARY KEY (batch_num, batch_num_final)
);

Expand Down

0 comments on commit ee022c1

Please sign in to comment.