-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: poisson regression and gas price feature flag #1575
Closed
Closed
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
be52835
feat: skeleton for EthGasStationStrategy
856d54e
Merge branch 'master' into unnawut/gas
5aabd1b
feat: add BLOCK_SUBMIT_GAS_PRICE_STRATEGY flag
93bb53d
docs: add comment why PoissonGasStrategy.recalculate/0 doesn't do any…
8f29e86
refactor: use @behaviour
0243fde
feat: fetch gas price history
69123e8
Merge branch 'master' into unnawut/gas
51f8a05
refactor: gas history
a849d76
refactor: gas strategies
4d3e45d
feat: add block percentile gas strategy
9dff3f0
style: minor codestyle refactor
72f2425
fix: pick one price suggestion
ecfbf1e
docs: add comment on ets
d42a0df
Merge branch 'master' into unnawut/gas
16cd9af
feat: make it run
6e03b5e
docs: gas price strategies
d0fb110
refactor: cleaner gas price recalculation
0b5caea
feat: ethgasstation algo that compiles
d378539
feat: log all price suggestions
19cf820
fix: wrong price suggestion return format
27f6027
fix: poisson strategy division unit
9e416c1
feat: detailed gas price logging
6a72751
feat: add random geth-random-gas-price-tx-factory to docker-compose.d…
f0a3565
docs: list available gas price strategies
ff091b0
feat: make geth-random-gas-price-tx-factory's max gas price configurable
9a2b48b
refactor: use the more natural 100mwei unit
ed1e132
refactor: move configurations to the supervisor
791ab22
fix: upgrade block submission debug logs to info
44cc807
style: mix format
cdae33b
fix: test modules should be .exs
41b6eab
style: fix credo complaining function is too nested
dfcced3
Merge branch 'master' into unnawut/gas
5f5c132
fix: block percentile not poisson
0401a8f
refactor: more readable multiplier conditions
9742ad6
Merge branch 'master' into unnawut/gas
e77ed9a
refactor: move max gas price and gas price strategy to releases.exs
6147310
Merge branch 'master' into unnawut/gas
unnawut 13b0898
refactor: clarify gas strategy calculation
07cb629
docs: more gas calculation edge case explanation
589e185
Merge branch 'master' into unnawut/gas
c08c9b3
feat: allow configuring ethereum url to gas price logic
fb580e7
refactor: use :ets.select_delete/2 to prune gas price history
cc35c6f
refactor: replace custom pubsub with OMG.Bus
25e4839
style: mix format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
201 changes: 201 additions & 0 deletions
201
apps/omg_child_chain/lib/omg_child_chain/block_queue/block_submission.ex
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,201 @@ | ||
# Copyright 2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.ChildChain.BlockQueue.BlockSubmission do | ||
@moduledoc """ | ||
Handles block submission. | ||
""" | ||
require Logger | ||
|
||
@type hash() :: <<_::256>> | ||
@type plasma_block_num() :: non_neg_integer() | ||
|
||
@type t() :: %__MODULE__{ | ||
num: plasma_block_num(), | ||
hash: hash(), | ||
nonce: non_neg_integer(), | ||
gas_price: pos_integer() | ||
} | ||
defstruct [:num, :hash, :nonce, :gas_price] | ||
|
||
@type submit_result_t() :: {:ok, <<_::256>>} | {:error, map} | ||
|
||
@doc """ | ||
Based on the result of a submission transaction returned from the Ethereum node, figures out what to do (namely: | ||
crash on or ignore an error response that is expected). | ||
|
||
It caters for the differences of those responses between Ethereum node RPC implementations. | ||
|
||
In general terms, those are the responses handled: | ||
- **known transaction** - this is common and expected to occur, since we are tracking submissions ourselves and | ||
liberally resubmitting same transactions; this is ignored | ||
- **low replacement price** - due to the gas price selection mechanism, there are cases where transaction will get | ||
resubmitted with a lower gas price; this is ignored | ||
- **account locked** - Ethereum node reports the authority account is locked; this causes a crash | ||
- **nonce too low** - there is an inherent race condition - when we're resubmitting a block, we do it with the same | ||
nonce, meanwhile it might happen that Ethereum mines this submission in this very instant; this is ignored if we | ||
indeed have just mined that submission, causes a crash otherwise | ||
""" | ||
@spec process_result(t(), submit_result_t(), plasma_block_num()) :: | ||
{:ok, binary()} | :ok | {:error, atom} | ||
def process_result(submission, submit_result, newest_mined_blknum) | ||
|
||
def process_result(submission, {:ok, txhash}, _newest_mined_blknum) do | ||
log_success(submission, txhash) | ||
{:ok, txhash} | ||
end | ||
|
||
# https://github.com/ethereum/go-ethereum/commit/9938d954c8391682947682543cf9b52196507a88#diff-8fecce9bb4c486ebc22226cf681416e2 | ||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_000, "message" => "already known"}}, | ||
_newest_mined_blknum | ||
) do | ||
log_known_tx(submission) | ||
:ok | ||
end | ||
|
||
# maybe this will get deprecated soon once the network migrates to 1.9.11. | ||
# Look at the previous function header for commit reference. | ||
# `fmt.Errorf("known transaction: %x", hash)` has been removed | ||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_000, "message" => "known transaction" <> _}}, | ||
_newest_mined_blknum | ||
) do | ||
log_known_tx(submission) | ||
:ok | ||
end | ||
|
||
# parity error code for duplicated tx | ||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_010, "message" => "Transaction with the same hash was already imported."}}, | ||
_newest_mined_blknum | ||
) do | ||
log_known_tx(submission) | ||
:ok | ||
end | ||
|
||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_000, "message" => "replacement transaction underpriced"}}, | ||
_newest_mined_blknum | ||
) do | ||
log_low_replacement_price(submission) | ||
:ok | ||
end | ||
|
||
# parity version | ||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_010, "message" => "Transaction gas price is too low. There is another" <> _}}, | ||
_newest_mined_blknum | ||
) do | ||
log_low_replacement_price(submission) | ||
:ok | ||
end | ||
|
||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_000, "message" => "authentication needed: password or unlock"}}, | ||
newest_mined_blknum | ||
) do | ||
diagnostic = prepare_diagnostic(submission, newest_mined_blknum) | ||
log_locked(diagnostic) | ||
{:error, :account_locked} | ||
end | ||
|
||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_000, "message" => "nonce too low"}}, | ||
newest_mined_blknum | ||
) do | ||
process_nonce_too_low(submission, newest_mined_blknum) | ||
end | ||
|
||
# parity specific error for nonce-too-low | ||
def process_result( | ||
submission, | ||
{:error, %{"code" => -32_010, "message" => "Transaction nonce is too low." <> _}}, | ||
newest_mined_blknum | ||
) do | ||
process_nonce_too_low(submission, newest_mined_blknum) | ||
end | ||
|
||
# ganache has this error, but these are valid nonce_too_low errors, that just don't make any sense | ||
# `process_nonce_too_low/2` would mark it as a genuine failure and crash the BlockQueue :( | ||
# however, everything seems to just work regardless, things get retried and mined eventually | ||
# NOTE: we decide to degrade the severity to warn and continue, considering it's just `ganache` | ||
def process_result( | ||
_submission, | ||
{:error, %{"code" => -32_000, "data" => %{"stack" => "n: the tx doesn't have the correct nonce" <> _}}} = error, | ||
_newest_mined_blknum | ||
) do | ||
log_ganache_nonce_too_low(error) | ||
:ok | ||
end | ||
|
||
# Fallback for unknown server errors: https://eth.wiki/json-rpc/json-rpc-error-codes-improvement-proposal | ||
# Only server errors are handled as they are the only set of errors that we have no control of. | ||
# Returns `:ok` so that BlockQueue can continue and do a retry, similar to a low replacement price error. | ||
def process_result(submission, {:error, %{"code" => code} = error}, _newest_mined_blknum) | ||
when code >= -32_099 and code <= -32_000 do | ||
_ = Logger.error("Submission #{inspect(submission)} resulted in an unknown server error: #{inspect(error)}.") | ||
:ok | ||
end | ||
|
||
defp log_ganache_nonce_too_low(error) do | ||
# runtime sanity check if we're actually running `ganache`, if we aren't and we're here, we must crash | ||
:ganache = Application.fetch_env!(:omg_eth, :eth_node) | ||
_ = Logger.warn(inspect(error)) | ||
:ok | ||
end | ||
|
||
defp log_success(submission, txhash) do | ||
_ = Logger.info("Submitted #{inspect(submission)} at: #{inspect(txhash)}") | ||
:ok | ||
end | ||
|
||
defp log_known_tx(submission) do | ||
_ = Logger.info("Submission #{inspect(submission)} is known transaction - ignored") | ||
:ok | ||
end | ||
|
||
defp log_low_replacement_price(submission) do | ||
_ = Logger.info("Submission #{inspect(submission)} is known, but with higher price - ignored") | ||
:ok | ||
end | ||
|
||
defp log_locked(diagnostic) do | ||
_ = Logger.error("It seems that authority account is locked: #{inspect(diagnostic)}. Check README.md") | ||
:ok | ||
end | ||
|
||
defp process_nonce_too_low(%__MODULE__{num: blknum} = submission, newest_mined_blknum) do | ||
if blknum <= newest_mined_blknum do | ||
# apparently the `nonce too low` error is related to the submission having been mined while it was prepared | ||
:ok | ||
else | ||
diagnostic = prepare_diagnostic(submission, newest_mined_blknum) | ||
_ = Logger.error("Submission unexpectedly failed with nonce too low: #{inspect(diagnostic)}") | ||
{:error, :nonce_too_low} | ||
end | ||
end | ||
|
||
defp prepare_diagnostic(submission, newest_mined_blknum) do | ||
config = Application.get_all_env(:omg_eth) |> Keyword.take([:contract_addr, :authority_address, :txhash_contract]) | ||
%{submission: submission, newest_mined_blknum: newest_mined_blknum, config: config} | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an untouched extraction from
OMG.ChildChain.BlockQueue.Core