Skip to content
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
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
be52835
feat: skeleton for EthGasStationStrategy
Jun 10, 2020
856d54e
Merge branch 'master' into unnawut/gas
Jun 10, 2020
5aabd1b
feat: add BLOCK_SUBMIT_GAS_PRICE_STRATEGY flag
Jun 10, 2020
93bb53d
docs: add comment why PoissonGasStrategy.recalculate/0 doesn't do any…
Jun 10, 2020
8f29e86
refactor: use @behaviour
Jun 11, 2020
0243fde
feat: fetch gas price history
Jun 11, 2020
69123e8
Merge branch 'master' into unnawut/gas
Jun 18, 2020
51f8a05
refactor: gas history
Jun 18, 2020
a849d76
refactor: gas strategies
Jun 18, 2020
4d3e45d
feat: add block percentile gas strategy
Jun 18, 2020
9dff3f0
style: minor codestyle refactor
Jun 18, 2020
72f2425
fix: pick one price suggestion
Jun 19, 2020
ecfbf1e
docs: add comment on ets
Jun 19, 2020
d42a0df
Merge branch 'master' into unnawut/gas
Jun 25, 2020
16cd9af
feat: make it run
Jun 25, 2020
6e03b5e
docs: gas price strategies
Jun 27, 2020
d0fb110
refactor: cleaner gas price recalculation
Jun 27, 2020
0b5caea
feat: ethgasstation algo that compiles
Jun 27, 2020
d378539
feat: log all price suggestions
Jun 27, 2020
19cf820
fix: wrong price suggestion return format
Jun 28, 2020
27f6027
fix: poisson strategy division unit
Jun 28, 2020
9e416c1
feat: detailed gas price logging
Jun 28, 2020
6a72751
feat: add random geth-random-gas-price-tx-factory to docker-compose.d…
Jun 28, 2020
f0a3565
docs: list available gas price strategies
Jun 28, 2020
ff091b0
feat: make geth-random-gas-price-tx-factory's max gas price configurable
Jun 28, 2020
9a2b48b
refactor: use the more natural 100mwei unit
Jun 28, 2020
ed1e132
refactor: move configurations to the supervisor
Jun 29, 2020
791ab22
fix: upgrade block submission debug logs to info
Jun 29, 2020
44cc807
style: mix format
Jun 29, 2020
cdae33b
fix: test modules should be .exs
Jun 29, 2020
41b6eab
style: fix credo complaining function is too nested
Jun 29, 2020
dfcced3
Merge branch 'master' into unnawut/gas
Jun 29, 2020
5f5c132
fix: block percentile not poisson
Jun 30, 2020
0401a8f
refactor: more readable multiplier conditions
Jun 30, 2020
9742ad6
Merge branch 'master' into unnawut/gas
Aug 19, 2020
e77ed9a
refactor: move max gas price and gas price strategy to releases.exs
Aug 21, 2020
6147310
Merge branch 'master' into unnawut/gas
unnawut Aug 21, 2020
13b0898
refactor: clarify gas strategy calculation
Aug 21, 2020
07cb629
docs: more gas calculation edge case explanation
Aug 25, 2020
589e185
Merge branch 'master' into unnawut/gas
Sep 1, 2020
c08c9b3
feat: allow configuring ethereum url to gas price logic
Sep 1, 2020
fb580e7
refactor: use :ets.select_delete/2 to prune gas price history
Sep 1, 2020
cc35c6f
refactor: replace custom pubsub with OMG.Bus
Sep 1, 2020
25e4839
style: mix format
Sep 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions apps/omg_child_chain/lib/omg_child_chain/block_queue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ defmodule OMG.ChildChain.BlockQueue do

alias OMG.Block
alias OMG.ChildChain.BlockQueue.Balance
alias OMG.ChildChain.BlockQueue.BlockSubmission
alias OMG.ChildChain.BlockQueue.Core
alias OMG.ChildChain.BlockQueue.Core.BlockSubmission
alias OMG.ChildChain.BlockQueue.GasAnalyzer
alias OMG.ChildChain.BlockQueue.GasPriceAdjustment
alias OMG.Eth
alias OMG.Eth.Client
alias OMG.Eth.Encoding
Expand Down Expand Up @@ -105,7 +104,7 @@ defmodule OMG.ChildChain.BlockQueue do

block_submit_every_nth = Keyword.fetch!(args, :block_submit_every_nth)
block_submit_max_gas_price = Keyword.fetch!(args, :block_submit_max_gas_price)
gas_price_adj_params = %GasPriceAdjustment{max_gas_price: block_submit_max_gas_price}
block_submit_gas_price_strategy = Keyword.fetch!(args, :block_submit_gas_price_strategy)

core =
Core.new(
Expand All @@ -115,8 +114,9 @@ defmodule OMG.ChildChain.BlockQueue do
parent_height: parent_height,
child_block_interval: child_block_interval,
block_submit_every_nth: block_submit_every_nth,
finality_threshold: finality_threshold,
gas_price_adj_params: gas_price_adj_params
block_submit_max_gas_price: block_submit_max_gas_price,
block_submit_gas_price_strategy: block_submit_gas_price_strategy,
finality_threshold: finality_threshold
)

{:ok, state} =
Expand Down Expand Up @@ -214,8 +214,7 @@ defmodule OMG.ChildChain.BlockQueue do

submit_result = Eth.submit_block(submission.hash, submission.nonce, submission.gas_price)
newest_mined_blknum = RootChain.get_mined_child_block()

final_result = Core.process_submit_result(submission, submit_result, newest_mined_blknum)
final_result = BlockSubmission.process_result(submission, submit_result, newest_mined_blknum)

final_result =
case final_result do
Expand Down
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
Copy link
Contributor Author

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

@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
Loading