-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathaggregation_mode_proof.ex
More file actions
73 lines (61 loc) · 1.73 KB
/
aggregation_mode_proof.ex
File metadata and controls
73 lines (61 loc) · 1.73 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
defmodule AggregationModeProof do
require Logger
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
# Different from proofs.ex (we could use the same but the hashes are constructed different)
@primary_key {:id, :id, autogenerate: true}
schema "proofs_agg_mode" do
field(:agg_proof_id, :binary_id)
field(:proof_hash, :string)
field(:index, :integer)
belongs_to(:aggregated_proof, AggregatedProof,
define_field: false,
foreign_key: :agg_proof_id,
references: :id,
type: :binary_id
)
timestamps()
end
def changeset(proof, attrs) do
proof
|> cast(attrs, [:agg_proof_id, :proof_hash, :index])
|> validate_required([:agg_proof_id, :proof_hash, :index])
end
def insert_or_update(proof) do
changeset =
AggregationModeProof.changeset(%AggregationModeProof{}, proof)
case(
Explorer.Repo.get_by(AggregationModeProof,
agg_proof_id: proof.agg_proof_id,
proof_hash: proof.proof_hash,
index: proof.index
)
) do
nil ->
Explorer.Repo.insert(changeset)
existing_proof ->
"Updating single aggregated proof" |> Logger.debug()
Ecto.Changeset.change(existing_proof, changeset.changes)
|> Explorer.Repo.update()
end
end
def get_all_proof_hashes(id) do
query =
from(proof in AggregationModeProof,
select: proof.proof_hash,
where: proof.agg_proof_id == ^id
)
Explorer.Repo.all(query)
end
def get_newest_proof_by_hash(hash) do
query =
from(proof in AggregationModeProof,
select: proof,
where: proof.proof_hash == ^hash,
order_by: [desc: proof.inserted_at],
limit: 1
)
Explorer.Repo.one(query)
end
end