|
1 | 1 | defmodule BIC.Base do |
2 | 2 | import ConsensusKV |
3 | 3 |
|
4 | | - def exec_cost(epoch, txu) do |
5 | | - bytes = byte_size(txu.tx_encoded) + 32 + 96 |
6 | | - BIC.Coin.to_cents( 1 + div(bytes, 1024) * 1 ) |
7 | | - |
8 | | - #for future update |
9 | | - #BIC.Coin.to_tenthousandth( 18 + div(bytes, 256) * 3 ) |
10 | | - end |
11 | | - |
12 | 4 | def seed_random(vr, txhash, action_index, call_cnt) do |
13 | 5 | seed_bin = <<seed::256-little>> = Blake3.hash( |
14 | 6 | <<vr::binary, txhash::binary, action_index::binary, call_cnt::binary>>) |
15 | 7 | :rand.seed(:exsss, seed) |
16 | 8 | seed_bin |
17 | 9 | end |
18 | 10 |
|
19 | | - def call_txs_pre_parallel(env, txus) do |
20 | | - Process.delete(:mutations) |
21 | | - Process.delete(:mutations_reverse) |
22 | | - |
23 | | - Enum.each(txus, fn(txu)-> |
24 | | - kv_put("bic:base:nonce:#{txu.tx.signer}", txu.tx.nonce, %{to_integer: true}) |
25 | | - exec_cost = exec_cost(env.entry_epoch, txu) |
26 | | - kv_increment("bic:coin:balance:#{txu.tx.signer}:AMA", -exec_cost) |
27 | | - |
28 | | - #burn 50% to prevent MEV/FreeChainGrowth attack |
29 | | - half_exec_cost = div(exec_cost, 2) |
30 | | - kv_increment("bic:coin:balance:#{env.entry_signer}:AMA", half_exec_cost) |
31 | | - kv_increment("bic:coin:balance:#{BIC.Coin.burn_address()}:AMA", half_exec_cost) |
32 | | - end) |
33 | | - |
34 | | - #parallel verify sols |
35 | | - segment_vr_hash = kv_get("bic:epoch:segment_vr_hash") |
36 | | - diff_bits = kv_get("bic:epoch:diff_bits", %{to_integer: true}) || 24 |
37 | | - steam = Task.async_stream(txus, fn txu -> |
38 | | - sol = Enum.find_value(txu.tx.actions, fn(a)-> a.function == "submit_sol" and length(a.args) != [] and hd(a.args) end) |
39 | | - if sol do |
40 | | - hash = Blake3.hash(sol) |
41 | | - opts = %{hash: hash, vr_b3: env.entry_vr_b3, segment_vr_hash: segment_vr_hash, diff_bits: diff_bits} |
42 | | - valid = try do BIC.Sol.verify(sol, opts) catch _,_ -> false end |
43 | | - %{hash: hash, valid: valid} |
44 | | - end |
45 | | - end) |
46 | | - |
47 | | - sol_verified_cache = for {error, spec} <- steam, |
48 | | - error == :ok and spec != nil, |
49 | | - into: %{}, |
50 | | - do: {spec.hash, spec.valid} |
51 | | - |
52 | | - Process.put(SolVerifiedCache, sol_verified_cache) |
53 | | - |
54 | | - {Process.get(:mutations, []), Process.get(:mutations_reverse, [])} |
55 | | - end |
56 | | - |
57 | 11 | def call_exit(env) do |
58 | 12 | Process.delete(:mutations) |
59 | 13 | Process.delete(:mutations_reverse) |
|
0 commit comments