Skip to content

Commit ee64ac7

Browse files
committed
start of large refactor
1 parent 0d91423 commit ee64ac7

41 files changed

Lines changed: 571 additions & 702 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ex/config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Config
33
config :logger, truncate: :infinity
44

55
config :iex,
6-
inspect: [width: 120, limit: 480, pretty: true, custom_options: [sort_maps: true]]
6+
inspect: [width: 120, limit: 320, pretty: true, custom_options: [sort_maps: true]]
77

88
config :ama, :version, Mix.Project.config[:version]
99

ex/config/runtime.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ config :ama, :work_folder, work_folder
1717
#Envvar.load(Path.join([work_folder, ".env"]))
1818
config :ama, :snapshot_height, (System.get_env("SNAPSHOT_HEIGHT") || "34076355") |> :erlang.binary_to_integer()
1919

20+
21+
2022
#Bind Interaces
2123
config :ama, :offline, (!!System.get_env("OFFLINE") || nil)
2224
config :ama, :testnet, (!!System.get_env("TESTNET") || nil)
@@ -54,6 +56,7 @@ keys_by_pk = Enum.into(keys, %{}, fn(key)->
5456
end)
5557
config :ama, :keys, keys
5658
config :ama, :keys_by_pk, keys_by_pk
59+
config :ama, :keys_all_pks, Enum.map(keys, & &1.pk)
5760

5861
first_key = hd(keys)
5962
config :ama, :trainer_pk, first_key.pk

ex/lib/api/api_chain.ex

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
defmodule API.Chain do
22
def entry_tip() do
3-
entry = Consensus.chain_tip_entry()
3+
entry = DB.Chain.tip_entry()
44
entry = format_entry_for_client(entry)
55
%{error: :ok, entry: entry}
66
end
77

88
def entry(entry_hash, filter_on_function \\ nil) do
99
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
10-
entry = Fabric.entry_by_hash(entry_hash)
10+
entry = DB.Chain.entry(entry_hash)
1111
if !entry do
1212
%{error: :not_found}
1313
else
@@ -27,13 +27,13 @@ defmodule API.Chain do
2727
end
2828

2929
def by_height(height) do
30-
entries = Fabric.entries_by_height(height)
30+
entries = DB.Chain.entries_by_height(height)
3131
|> Enum.map(& format_entry_for_client(&1))
3232
%{error: :ok, entries: entries}
3333
end
3434

3535
def by_height_with_txs(height) do
36-
entries = Fabric.entries_by_height(height)
36+
entries = DB.Chain.entries_by_height(height)
3737
|> Enum.map(fn(entry)->
3838
txs = API.TX.get_by_entry(entry.hash)
3939
entry = format_entry_for_client(entry)
@@ -47,7 +47,7 @@ defmodule API.Chain do
4747
if !consensuses do
4848
{nil, nil}
4949
else
50-
trainers = Consensus.trainers_for_height(height)
50+
trainers = DB.Chain.validators_for_height(height)
5151
{mut_hash, score, consensus} = Consensus.best_by_weight(trainers, consensuses)
5252
{score, mut_hash}
5353
end
@@ -59,7 +59,7 @@ defmodule API.Chain do
5959
c = put_in(c, [:mutations_hash], Base58.encode(c.mutations_hash))
6060
c = put_in(c, [:entry_hash], Base58.encode(c.entry_hash))
6161
c = put_in(c, [:aggsig], Base58.encode(c.aggsig))
62-
signers = BLS12AggSig.unmask_trainers(Consensus.trainers_for_height(height), c.mask)
62+
signers = BLS12AggSig.unmask_trainers(DB.Chain.validators_for_height(height), c.mask)
6363
|> Enum.map(& Base58.encode(&1))
6464
c = put_in(c, [:signers], signers)
6565
c = put_in(c, [:score], length(c.signers) / bit_size(c.mask))
@@ -69,7 +69,7 @@ defmodule API.Chain do
6969

7070
def pflops() do
7171
#A*B=C M=16 K=50240 N=16 u8xi8=i32
72-
height_in_epoch = rem(Consensus.chain_height(), 100_000)
72+
height_in_epoch = rem(DB.Chain.height(), 100_000)
7373
total_score = API.Epoch.score() |> Enum.map(& Enum.at(&1,1))|> Enum.sum()
7474
diff_multiplier = Bitwise.bsl(1, API.Epoch.get_diff_bits())
7575
total_calcs = total_score * diff_multiplier
@@ -82,14 +82,14 @@ defmodule API.Chain do
8282

8383
def stats() do
8484
%{
85-
height: Consensus.chain_height(),
86-
tip_hash: Consensus.chain_tip() |> Base58.encode(),
87-
tip: format_entry_for_client(Consensus.chain_tip_entry()),
85+
height: DB.Chain.height(),
86+
tip_hash: DB.Chain.tip() |> Base58.encode(),
87+
tip: format_entry_for_client(DB.Chain.tip_entry()),
8888
tx_pool_size: TXPool.size(),
89-
cur_validator: Consensus.trainer_for_slot_current() |> Base58.encode(),
90-
next_validator: Consensus.trainer_for_slot_next() |> Base58.encode(),
91-
emission_for_epoch: BIC.Coin.from_flat(BIC.Epoch.epoch_emission(Consensus.chain_epoch())),
92-
circulating: BIC.Coin.from_flat(BIC.Epoch.circulating_without_burn(Consensus.chain_epoch())),
89+
cur_validator: DB.Chain.validator_for_height_current() |> Base58.encode(),
90+
next_validator: DB.Chain.validator_for_height_next() |> Base58.encode(),
91+
emission_for_epoch: BIC.Coin.from_flat(BIC.Epoch.epoch_emission(DB.Chain.epoch())),
92+
circulating: BIC.Coin.from_flat(BIC.Epoch.circulating_without_burn(DB.Chain.epoch())),
9393
total_supply_y3: BIC.Coin.from_flat(BIC.Epoch.circulating_without_burn(500*3)),
9494
total_supply_y30: BIC.Coin.from_flat(BIC.Epoch.circulating_without_burn(500*30)),
9595
pflops: pflops(),
@@ -99,9 +99,9 @@ defmodule API.Chain do
9999
end
100100

101101
def stat_txs_sec() do
102-
height = Fabric.rooted_tip_height()
102+
height = DB.Chain.rooted_height()
103103
last_100 = Enum.sum_by((height-100)..height, fn(height)->
104-
length(Fabric.entries_by_height(height) |> List.first() |> Map.get(:txs))
104+
length(DB.Chain.entries_by_height(height) |> List.first() |> Map.get(:txs))
105105
end)
106106
last_100/50
107107
end

ex/lib/api/api_epoch.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ defmodule API.Epoch do
2626
end
2727

2828
def get_diff_bits(epoch \\ nil) do
29-
epoch = if epoch do epoch else Consensus.chain_epoch() end
29+
epoch = if epoch do epoch else DB.Chain.epoch() end
3030
API.Contract.get("bic:epoch:diff_bits:#{epoch}", :to_integer) || 24
3131
end
3232

3333
def get_total_sols(epoch \\ nil) do
34-
epoch = if epoch do epoch else Consensus.chain_epoch() end
34+
epoch = if epoch do epoch else DB.Chain.epoch() end
3535
API.Contract.get("bic:epoch:total_sols:#{epoch}", :to_integer) || 0
3636
end
3737

3838
def get_pop(pk) do
3939
pk = if byte_size(pk) != 48, do: Base58.decode(pk), else: pk
40-
Consensus.chain_pop(pk)
40+
DB.Chain.pop(pk)
4141
|> case do
4242
nil -> nil
4343
addr -> Base58.encode(addr)

ex/lib/api/api_peer.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
defmodule API.Peer do
22
def trainers(height \\ nil) do
3-
height = height || Consensus.chain_height()+1
4-
trainerForSlot = Consensus.trainer_for_slot(height, height)
3+
height = height || DB.Chain.height()+1
4+
trainerForSlot = DB.Chain.validator_for_height(height)
55

6-
Consensus.trainers_for_height(height)
6+
DB.Chain.validators_for_height(height)
77
|> Enum.map(fn(pk)->
88
p = NodeANR.get_peer_hotdata(pk)
99
inSlot = trainerForSlot == pk
@@ -90,9 +90,9 @@ defmodule API.Peer do
9090
end
9191

9292
def removed_trainers(epoch \\ nil) do
93-
epoch = if !epoch do Consensus.chain_epoch() else epoch end
94-
trainers_for_epoch = Consensus.trainers_for_height(epoch*100_000)
95-
trainers = Consensus.trainers_for_height(Consensus.chain_height()+1)
93+
epoch = if !epoch do DB.Chain.epoch() else epoch end
94+
trainers_for_epoch = DB.Chain.validators_for_height(epoch*100_000)
95+
trainers = DB.Chain.validators_for_height(DB.Chain.height()+1)
9696
(trainers_for_epoch -- trainers)
9797
|> Enum.map(& Base58.encode(&1))
9898
end

ex/lib/api/api_tx.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
defmodule API.TX do
22
def get(tx_id) do
33
tx_id = if byte_size(tx_id) != 32, do: Base58.decode(tx_id), else: tx_id
4-
Consensus.chain_tx(tx_id)
4+
DB.Chain.tx(tx_id)
55
|> format_tx_for_client()
66
end
77

88
def get_by_entry(entry_hash) do
99
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
10-
case Fabric.entry_by_hash(entry_hash) do
10+
case DB.Chain.entry(entry_hash) do
1111
nil -> nil
1212
%{hash: entry_hash, header_unpacked: %{slot: slot}, txs: txs} ->
1313
Enum.map(txs, fn(tx_packed)->

ex/lib/api/api_wallet.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule API.Wallet do
77
#def balance(pk, symbol \\ "AMA") do
88
def balance(pk, symbol \\ "AMA") do
99
pk = if byte_size(pk) != 48, do: Base58.decode(pk), else: pk
10-
coins = Consensus.chain_balance(pk, symbol)
10+
coins = DB.Chain.balance(pk, symbol)
1111
%{symbol: symbol, flat: coins, float: BIC.Coin.from_flat(coins)}
1212
end
1313

ex/lib/api/db_api.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule DB.API do
2+
def db_handle(db_opts, default_cf, merge_opts \\ %{}) do
3+
db = db_opts[:db]
4+
cf = db_opts[:cf]
5+
rtx = db_opts[:rtx]
6+
cond do
7+
!!rtx and !!cf -> Map.merge(%{rtx: rtx, cf: cf}, merge_opts)
8+
!!rtx -> Map.merge(%{rtx: rtx, cf: Map.fetch!(cf, default_cf)}, merge_opts)
9+
!!db and !!cf -> Map.merge(%{db: db, cf: cf}, merge_opts)
10+
!!db -> Map.merge(%{db: db, cf: Map.fetch!(cf, default_cf)}, merge_opts)
11+
true ->
12+
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
13+
Map.merge(%{db: db, cf: Map.fetch!(cf, default_cf)}, merge_opts)
14+
end
15+
end
16+
end

ex/lib/api/db_chain.ex

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)