|
| 1 | +defmodule DB.Chain do |
| 2 | + import DB.API |
| 3 | + |
| 4 | + def tip(db_opts \\ %{}) do RocksDB.get("temporal_tip", db_handle(db_opts, :sysconf, %{})) end |
| 5 | + |
| 6 | + def tip_entry(db_opts \\ %{}) do |
| 7 | + entry(tip(db_opts), db_opts) |
| 8 | + end |
| 9 | + |
| 10 | + def height(db_opts \\ %{}) do tip_entry(db_opts).header_unpacked.height end |
| 11 | + def epoch(db_opts \\ %{}) do div(height(db_opts), 100_000) end |
| 12 | + |
| 13 | + def rooted_tip(db_opts \\ %{}) do RocksDB.get("rooted_tip", db_handle(db_opts, :sysconf, %{})) end |
| 14 | + |
| 15 | + def rooted_tip_entry(db_opts \\ %{}) do |
| 16 | + entry(rooted_tip(db_opts), db_opts) |
| 17 | + end |
| 18 | + |
| 19 | + def rooted_height(db_opts \\ %{}) do |
| 20 | + entry = rooted_tip_entry(db_opts) |
| 21 | + if entry do |
| 22 | + entry.header_unpacked.height |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def muts(hash, db_opts \\ %{}) do RocksDB.get(hash, db_handle(db_opts, :muts, %{term: true})) end |
| 27 | + def muts_rev(hash, db_opts \\ %{}) do RocksDB.get(hash, db_handle(db_opts, :muts_rev, %{term: true})) end |
| 28 | + |
| 29 | + def segment_vr_hash(db_opts \\ %{}) do |
| 30 | + RocksDB.get("bic:epoch:segment_vr_hash", db_handle(db_opts, :contractstate, %{})) |
| 31 | + end |
| 32 | + |
| 33 | + def diff_bits(db_opts \\ %{}) do |
| 34 | + RocksDB.get("bic:epoch:diff_bits", db_handle(db_opts, :contractstate, %{to_integer: true})) || 24 |
| 35 | + end |
| 36 | + |
| 37 | + def total_sols(db_opts \\ %{}) do |
| 38 | + RocksDB.get("bic:epoch:total_sols", db_handle(db_opts, :contractstate, %{to_integer: true})) || 0 |
| 39 | + end |
| 40 | + |
| 41 | + def pop(pk, db_opts \\ %{}) do |
| 42 | + RocksDB.get("bic:epoch:pop:#{pk}", db_handle(db_opts, :contractstate, %{})) |
| 43 | + end |
| 44 | + |
| 45 | + def nonce(pk, db_opts \\ %{}) do |
| 46 | + RocksDB.get("bic:base:nonce:#{pk}", db_handle(db_opts, :contractstate, %{to_integer: true})) |
| 47 | + end |
| 48 | + |
| 49 | + def balance(pk, symbol \\ "AMA", db_opts \\ %{}) do |
| 50 | + RocksDB.get("bic:coin:balance:#{pk}:#{symbol}", db_handle(db_opts, :contractstate, %{to_integer: true})) || 0 |
| 51 | + end |
| 52 | + |
| 53 | + def entry(hash, db_opts \\ %{}) do |
| 54 | + RocksDB.get(hash, db_handle(db_opts, :entry, %{term: true})) |
| 55 | + |> Entry.unpack() |
| 56 | + end |
| 57 | + |
| 58 | + def entries_by_height(height, db_opts \\ %{}) do |
| 59 | + RocksDB.get_prefix("#{height}:", db_handle(db_opts, :entry_by_height, %{})) |
| 60 | + |> Enum.map(& Entry.unpack( entry(elem(&1,0), db_opts) )) |
| 61 | + end |
| 62 | + |
| 63 | + def entry_seentime(hash, db_opts \\ %{}) do |
| 64 | + RocksDB.get(hash, db_handle(db_opts, :my_seen_time_for_entry, %{term: true})) |
| 65 | + end |
| 66 | + |
| 67 | + def tx(tx_hash, db_opts \\ %{}) do |
| 68 | + map = RocksDB.get(tx_hash, db_handle(db_opts, :tx, %{term: true})) |
| 69 | + if map do |
| 70 | + entry_bytes = RocksDB.get(map.entry_hash, db_handle(db_opts, :entry, %{})) |
| 71 | + entry = DB.Chain.entry(map.entry_hash) |
| 72 | + tx_bytes = binary_part(entry_bytes, map.index_start, map.index_size) |
| 73 | + TX.unpack(tx_bytes) |
| 74 | + |> Map.put(:result, map[:result]) |
| 75 | + |> Map.put(:metadata, %{entry_hash: map.entry_hash, entry_height: entry.header_unpacked.height, entry_slot: entry.header_unpacked.slot}) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + """ |
| 80 | + [entry] |
| 81 | + hash blob |
| 82 | +
|
| 83 | + [entry_meta] |
| 84 | + seentime:{hash} "{ts_millisecond}" |
| 85 | + height:{height}:{hash} hash |
| 86 | +
|
| 87 | + prev:{hash} hash |
| 88 | + next:{hash} hash |
| 89 | + in_chain:{hash} "" / None |
| 90 | + in_chain_height:{height} hash |
| 91 | +
|
| 92 | + [attestation] |
| 93 | + attestation:{hash}:{signer}:{muthash} attestation |
| 94 | + attestation_agg:{hash}:{muthash} consensus |
| 95 | +
|
| 96 | + [tx] |
| 97 | + tx:{txhash} "{index_ptr_map}" |
| 98 | +
|
| 99 | + [tx_meta] |
| 100 | + tx_out:{account}:{nonce} txhash |
| 101 | + tx_in:{account}:{nonce} txhash |
| 102 | + "tx_account_nonce|account:nonce->txhash", |
| 103 | + "tx_receiver_nonce|receiver:nonce->txhash", |
| 104 | + """ |
| 105 | + |
| 106 | + |
| 107 | + def is_validator(pk \\ nil, db_opts \\ %{}) do |
| 108 | + pks = if pk do [pk] else |
| 109 | + Application.fetch_env!(:ama, :keys_all_pks) |
| 110 | + end |
| 111 | + validators = validators_for_height(height()+1, db_opts) |
| 112 | + delta = validators -- pks |
| 113 | + length(validators) != length(delta) |
| 114 | + end |
| 115 | + |
| 116 | + def validators_for_height(height, db_opts \\ %{}) do |
| 117 | + opts = db_handle(db_opts, :contractstate, %{term: true}) |
| 118 | + cond do |
| 119 | + height in 3195570..3195575 -> |
| 120 | + RocksDB.get("bic:epoch:trainers:height:000000319557", opts) |
| 121 | + true -> |
| 122 | + {_, value} = RocksDB.get_prev_or_first("bic:epoch:trainers:height:", String.pad_leading("#{height}", 12, "0"), opts) |
| 123 | + value |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + def validator_for_height(height, db_opts \\ %{}) do |
| 128 | + validators = validators_for_height(height, db_opts) |
| 129 | + index = rem(height, length(validators)) |
| 130 | + Enum.at(validators, index) |
| 131 | + end |
| 132 | + |
| 133 | + def validator_for_height_current(db_opts \\ %{}) do |
| 134 | + validator_for_height(height(db_opts), db_opts) |
| 135 | + end |
| 136 | + |
| 137 | + def validator_for_height_next(db_opts \\ %{}) do |
| 138 | + validator_for_height(height(db_opts) + 1, db_opts) |
| 139 | + end |
| 140 | +end |
0 commit comments