Skip to content

Commit

Permalink
fix: use single-block transactions to avoid duplicated deletions (#786)
Browse files Browse the repository at this point in the history
Reading an already deleted key on a transaction will return the deleted
record anyway, so not chunking multiple blocks into a single transaction
will reduce the chances of it happening.
  • Loading branch information
sborrazas authored Jul 13, 2022
1 parent e2f728a commit 49cf42f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/ae_mdw/db/mutations/oracles_expiration_mutation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule AeMdw.Db.OraclesExpirationMutation do
state
|> Collection.stream(Model.ActiveOracleExpiration, {height, <<>>})
|> Stream.take_while(&match?({^height, _pk}, &1))
|> Enum.to_list()
|> Enum.reduce(state, fn {^height, pubkey}, state ->
Oracle.expire_oracle(state, height, pubkey)
end)
Expand Down
16 changes: 4 additions & 12 deletions lib/ae_mdw/sync/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,10 @@ defmodule AeMdw.Sync.Server do
defp exec_db_mutations(gens_mutations, state) do
gens_mutations
|> Enum.flat_map(fn {_height, blocks_mutations} -> blocks_mutations end)
|> Enum.chunk_every(2)
|> Enum.reduce(state, fn blocks_mutations_chunk, state ->
chunk_mutations =
blocks_mutations_chunk
|> Enum.flat_map(fn {_block_index, _block, block_mutations} -> block_mutations end)

new_state = State.commit_db(state, chunk_mutations)

Enum.each(blocks_mutations_chunk, fn {{_height, mbi} = _block_index, block,
_block_mutations} ->
broadcast_block(block, mbi == -1)
end)
|> Enum.reduce(state, fn {{_height, mbi}, block, block_mutations}, state ->
new_state = State.commit_db(state, block_mutations)

broadcast_block(block, mbi == -1)

new_state
end)
Expand Down

0 comments on commit 49cf42f

Please sign in to comment.