@@ -44,12 +44,16 @@ defmodule Batches do
4444 response_block_number: batch_db . response_block_number ,
4545 response_transaction_hash: batch_db . response_transaction_hash ,
4646 response_timestamp: batch_db . response_timestamp ,
47- data_pointer: batch_db . data_pointer
47+ data_pointer: batch_db . data_pointer ,
4848 }
4949 end
5050
51- def generate_changeset ( % BatchDB { } = batch_db ) do
52- Batches . changeset ( % Batches { } , Map . from_struct ( Batches . cast_to_batches ( batch_db ) ) )
51+ # returns changeset for both Batches and Proofs table
52+ def generate_changesets ( % BatchDB { } = batch_db ) do
53+ batches_changeset = Batches . changeset ( % Batches { } , Map . from_struct ( Batches . cast_to_batches ( batch_db ) ) )
54+ proofs = Proofs . cast_to_proofs ( batch_db )
55+
56+ { batches_changeset , proofs }
5357 end
5458
5559 def get_batch ( % { merkle_root: merkle_root } ) do
@@ -118,54 +122,56 @@ defmodule Batches do
118122 end
119123 end
120124
121- def get_amount_of_proofs ( % { merkle_root: merkle_root } ) do
122- query = from ( b in Batches ,
123- where: b . merkle_root == ^ merkle_root ,
124- select: b . amount_of_proofs )
125-
126- case Explorer.Repo . one ( query ) do
127- nil -> nil
128- result -> result
129- end
130- end
131-
132- def insert_or_update ( changeset ) do
133- merkle_root = changeset . changes . merkle_root
125+ def insert_or_update ( batch_changeset , proofs ) do
126+ merkle_root = batch_changeset . changes . merkle_root
127+ stored_proofs = Proofs . get_proofs_from_batch ( % { merkle_root: merkle_root } )
134128 case Explorer.Repo . get ( Batches , merkle_root ) do
135129 nil ->
136- "New Batch, inserting to DB:" |> IO . puts ( )
137- case Explorer.Repo . insert ( changeset ) do
138- { :ok , _ } ->
139- "Batch inserted successfully" |> IO . puts ( )
140- { :ok , :empty }
141-
142- { :error , changeset } ->
143- "Batch insert failed #{ changeset } " |> IO . puts ( )
144- { :error , changeset }
145- end
130+ multi = Ecto.Multi . new ( )
131+ |> Ecto.Multi . insert ( :insert_batch , batch_changeset )
132+ |> Ecto.Multi . insert_all ( :insert_all , Proofs , proofs )
133+
134+ case Explorer.Repo . transaction ( multi ) do
135+ { :ok , _ } ->
136+ IO . puts ( "Batch inserted successfully" )
137+ { :ok , :success }
138+
139+ { :error , _failed_operation , failed_changeset , _reason } ->
140+ IO . puts ( "Batch insert failed:" )
141+ IO . inspect ( failed_changeset )
142+ { :error , failed_changeset }
143+ end
144+
146145 existing_batch ->
147146 try do
148- if existing_batch . is_verified != changeset . changes . is_verified
149- or existing_batch . amount_of_proofs != changeset . changes . amount_of_proofs # rewrites if it was writen with DB's default
150- or existing_batch . data_pointer != changeset . changes . data_pointer # rewrites if it was writen with DB's default
151- or existing_batch . submission_block_number != changeset . changes . submission_block_number # reorg may change submission_block_number
152- or existing_batch . submission_transaction_hash != changeset . changes . submission_transaction_hash # reorg may change submission_tx_hash
153- or ( Map . has_key? ( changeset . changes , :block_number )
154- and existing_batch . response_block_number != changeset . changes . response_block_number ) # reorg may change response_block_number
155- or ( Map . has_key? ( changeset . changes , :response_transaction_hash )
156- and existing_batch . response_transaction_hash != changeset . changes . response_transaction_hash ) # reorg may change response_tx_hash
147+ if existing_batch . is_verified != batch_changeset . changes . is_verified
148+ or existing_batch . amount_of_proofs != batch_changeset . changes . amount_of_proofs # rewrites if it was writen with DB's default
149+ or existing_batch . data_pointer != batch_changeset . changes . data_pointer # rewrites if it was writen with DB's default
150+ or existing_batch . submission_block_number != batch_changeset . changes . submission_block_number # reorg may change submission_block_number
151+ or existing_batch . submission_transaction_hash != batch_changeset . changes . submission_transaction_hash # reorg may change submission_tx_hash
152+ or ( Map . has_key? ( batch_changeset . changes , :block_number )
153+ and existing_batch . response_block_number != batch_changeset . changes . response_block_number ) # reorg may change response_block_number
154+ or ( Map . has_key? ( batch_changeset . changes , :response_transaction_hash )
155+ and existing_batch . response_transaction_hash != batch_changeset . changes . response_transaction_hash ) # reorg may change response_tx_hash
156+ or stored_proofs == nil and proofs != % { } # no proofs registered in DB, but some received
157157 do
158158 "Batch values have changed, updating in DB" |> IO . puts ( )
159- updated_changeset = Ecto.Changeset . change ( existing_batch , changeset . changes )
160- case Explorer.Repo . update ( updated_changeset ) do
159+ updated_changeset = Ecto.Changeset . change ( existing_batch , batch_changeset . changes ) # no changes in proofs table
160+
161+ multi =
162+ Ecto.Multi . new ( )
163+ |> Ecto.Multi . update ( :update_batch , updated_changeset )
164+ |> ( fn m -> if stored_proofs == nil and proofs != % { } , do: Ecto.Multi . insert_all ( m , :insert_proofs , Proofs , proofs ) , else: m end ) . ( )
165+
166+ case Explorer.Repo . transaction ( multi ) do
161167 { :ok , _ } ->
162- "Batch updated successfully" |> IO . puts ( )
168+ "Batch updated and new proofs inserted successfully" |> IO . puts ( )
163169 { :ok , :empty }
164-
165- { :error , changeset } ->
166- "Batch update failed #{ changeset } " |> IO . puts ( )
170+ { :error , _ , changeset , _ } ->
171+ "Error: #{ inspect ( changeset . errors ) } " |> IO . puts ( )
167172 { :error , changeset }
168173 end
174+
169175 end
170176 rescue
171177 error ->
0 commit comments