Skip to content

Instantly share code, notes, and snippets.

@jyeshe
Last active October 26, 2021 15:03
Show Gist options
  • Save jyeshe/1e1c79d07b177592d48305d4ef4395dc to your computer and use it in GitHub Desktop.
Save jyeshe/1e1c79d07b177592d48305d4ef4395dc to your computer and use it in GitHub Desktop.
defmodule FindActiveOracle do
alias AeMdw.Db.Model
import AeMdw.Db.Util, only: [read_block!: 1]
require Model
def list_txs(from_height, pubkey, max_height \\ 206_911) do
IO.inspect(pubkey)
IO.inspect(:aeser_api_encoder.encode(:oracle_pubkey, pubkey))
Enum.each(from_height..max_height, fn height ->
# IO.inspect Model.block(read_block!({height, -1}), :hash)
{_key_block, micro_blocks} = node_db_get_blocks(height)
micro_blocks
|> Enum.with_index()
|> Enum.each(fn {mblock, _mbi} ->
mblock
|> :aec_blocks.txs()
|> Enum.each(fn signed_tx ->
{mod, tx} = :aetx.specialize_callback(:aetx_sign.tx(signed_tx))
type = mod.type()
{tx, type} =
if type == :ga_meta_tx or type == :paying_for_tx do
inner_signed_tx = AeMdw.Db.Sync.InnerTx.signed_tx(type, tx)
{mod, tx} = :aetx.specialize_callback(:aetx_sign.tx(inner_signed_tx))
type = mod.type()
{tx, type}
else
{tx, type}
end
delta_ttl =
case type do
:oracle_register_tx ->
if pubkey == :aeo_register_tx.account_pubkey(tx) do
:aeo_utils.ttl_delta(height, :aeo_register_tx.oracle_ttl(tx))
end
:oracle_extend_tx ->
if pubkey == :aeo_extend_tx.oracle_pubkey(tx) do
{:delta, delta_ttl} = :aeo_extend_tx.oracle_ttl(tx)
delta_ttl
end
_ ->
nil
end
delta_ttl && IO.puts("#{type}@#{height} expire=#{height + delta_ttl}")
end)
end)
end)
end
defp node_db_get_blocks(height) when is_integer(height) do
{:ok, kb_header} = :aec_chain.get_key_header_by_height(height)
{:ok, kb_hash} = :aec_headers.hash_header(kb_header)
key_block = :aec_db.get_block(kb_hash)
{key_block, do_get_micro_blocks(kb_hash)}
end
defp do_get_micro_blocks(<<next_gen_kb_hash::binary>>) do
next_gen_kb_hash
|> :aec_db.get_header()
|> :aec_headers.prev_hash()
|> Stream.unfold(&micro_block_walker/1)
|> Enum.reverse()
end
defp micro_block_walker(hash) do
with block <- :aec_db.get_block(hash),
:micro <- :aec_blocks.type(block) do
{block, :aec_blocks.prev_hash(block)}
else
:key -> nil
end
end
def test_get_blocks(height) do
{:ok, kb_header} = :aec_chain.get_key_header_by_height(height)
{:ok, kb_hash1} = :aec_headers.hash_header(kb_header)
kb_hash2 = Model.block(read_block!({height, -1}), :hash)
kb_hash1 == kb_hash2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment