-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: synchronize async tasks write (#818)
- Runs aex9 tasks asynchronously - Purges aex9 in-memory and persisted records belonging to invalidated blocks - Synchronizes the writing of async task result with the main sync database transaction so that if the task finishes later it writes directly to the database. - Avoid rerunning the tasks for on-db sync (use previous in-memory task result to write to database)
- Loading branch information
Showing
30 changed files
with
1,125 additions
and
670 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule AeMdw.Db.AsyncStoreMutation do | ||
@moduledoc """ | ||
Writes all AsyncStore records to a state. | ||
""" | ||
|
||
alias AeMdw.Db.State | ||
alias AeMdw.Sync.AsyncStoreServer | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [] | ||
|
||
@opaque t() :: %__MODULE__{} | ||
|
||
@spec new() :: t() | ||
def new() do | ||
%__MODULE__{} | ||
end | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute(%__MODULE__{}, state) do | ||
AsyncStoreServer.write_async_store(state) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule AeMdw.Db.PurgeAex9StateMutation do | ||
@moduledoc """ | ||
Deletes aex9 balances and presence for invalidated balances. | ||
""" | ||
|
||
alias AeMdw.Db.Contract | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.State | ||
alias AeMdw.Node.Db | ||
|
||
require Model | ||
|
||
@derive AeMdw.Db.Mutation | ||
defstruct [:contract_pk, :accounts_pks] | ||
|
||
@opaque t() :: %__MODULE__{ | ||
contract_pk: Db.pubkey(), | ||
accounts_pks: [Db.pubkey()] | ||
} | ||
|
||
@spec new(Db.pubkey(), [Db.pubkey()]) :: t() | ||
def new(contract_pk, accounts_pks) do | ||
%__MODULE__{ | ||
contract_pk: contract_pk, | ||
accounts_pks: accounts_pks | ||
} | ||
end | ||
|
||
@spec execute(t(), State.t()) :: State.t() | ||
def execute( | ||
%__MODULE__{ | ||
contract_pk: contract_pk, | ||
accounts_pks: accounts_pks | ||
}, | ||
state | ||
) do | ||
Enum.reduce(accounts_pks, state, fn account_pk, state -> | ||
state | ||
|> Contract.aex9_delete_presence(account_pk, contract_pk) | ||
|> safe_delete_balance(contract_pk, account_pk) | ||
end) | ||
end | ||
|
||
defp safe_delete_balance(state, contract_pk, account_pk) do | ||
if State.exists?(state, Model.Aex9Balance, {contract_pk, account_pk}) do | ||
State.delete(state, Model.Aex9Balance, {contract_pk, account_pk}) | ||
else | ||
state | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.