Skip to content

Commit c811e0b

Browse files
committed
epoch 413 migration
1 parent 9707dee commit c811e0b

File tree

11 files changed

+142
-44
lines changed

11 files changed

+142
-44
lines changed

ex/lib/api/api_chain.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ defmodule API.Chain do
100100
last_100/50
101101
end
102102

103+
def count_wallets() do
104+
key = "account:#{:binary.copy(<<0>>, 48)}:balance:AMA"
105+
count_wallets_1(key, 0)
106+
end
107+
def count_wallets_1(key, acc) do
108+
%{db: db, cf: cf} = :persistent_term.get({:rocksdb, Fabric})
109+
seek = RocksDB.seek_next(key, %{db: db, cf: cf.contractstate})
110+
case seek do
111+
{<<"account:", pk::384, ":balance:AMA">>, _} ->
112+
key = <<"account:", (pk+1)::384, ":balance:AMA">>
113+
count_wallets_1(key, acc + 1)
114+
{<<"account:", pk::384, _::binary>>, _} ->
115+
key = <<"account:", pk::384, ":balance:AMA">>
116+
count_wallets_1(key, acc)
117+
{_, _} -> acc
118+
end
119+
end
120+
103121
def format_entry_for_client(nil) do nil end
104122
def format_entry_for_client(entry) do
105123
hash = entry.hash

ex/lib/api/api_proof.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule API.Proof do
2+
def proof_validators(entry_hash) do
3+
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
4+
proof = Entry.proof_validators(entry_hash)
5+
%{
6+
value: Base58.encode(proof.value),
7+
key: proof.key,
8+
validators: Enum.map(proof.validators, & Base58.encode(&1)),
9+
proof: %{
10+
nodes: Enum.map(proof.proof.nodes, & %{direction: &1.direction, hash: Base58.encode(&1.hash)}),
11+
path: Base58.encode(proof.proof.path),
12+
path: Base58.encode(proof.proof.root),
13+
path: Base58.encode(proof.proof.hash),
14+
}
15+
}
16+
end
17+
end

ex/lib/api/db_chain.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ defmodule DB.Chain do
8484
height in 3195570..3195575 ->
8585
RocksDB.get("bic:epoch:validators:height:000000319557", opts)
8686
true ->
87-
case RocksDB.get_prev_or_first("bic:epoch:validators:height:", pad_integer(height), opts) do
87+
case RocksDB.get_prev_or_first("bic:epoch:validators:height:", pad_integer(height), db_handle(db_opts, :contractstate, %{})) do
8888
{nil, nil} ->
8989
{_, value} = RocksDB.get_prev_or_first("bic:epoch:trainers:height:", pad_integer(height), opts)
9090
value
91-
{_, value} -> value
91+
{_, value} -> RDB.vecpak_decode(value)
9292
end
9393
end
9494
end

ex/lib/consensus/models/entry.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ defmodule Entry do
152152
%{error: err} = TX.validate(txu, is_special_meeting_block)
153153
err
154154
end)
155-
156155
err = Enum.find_value(steam, fn {:ok, result} -> result != :ok && result end)
157156
if err, do: throw(err)
158157

ex/lib/misc/rocksdb.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ defmodule RocksDB do
186186
end
187187
end
188188

189+
def seek_next(key, opts) do
190+
{:ok, it} = iterator(opts)
191+
192+
seek_res = RDB.iterator_move(it, {:seek, key})
193+
seek_res = case seek_res do
194+
{:ok, next_key, value} -> {next_key, value}
195+
other -> {nil, nil}
196+
end
197+
end
198+
189199
def transaction(db) do
190200
{:ok, rtx} = RDB.transaction(db)
191201
rtx

ex/native/rdb/src/consensus/bic/coin.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::panic::panic_any;
22
use crate::{bcat, consensus};
3-
use crate::consensus::consensus_kv::{kv_get, kv_put, kv_increment};
3+
use crate::consensus::consensus_kv::{kv_get, kv_put, kv_increment, kv_exists};
44
use vecpak::{encode, decode, Term};
55

66
pub const DECIMALS: u32 = 9;
@@ -110,24 +110,24 @@ pub fn call_transfer(env: &mut crate::consensus::consensus_apply::ApplyEnv, args
110110
if paused(env, symbol) { panic_any("paused") }
111111
if soulbound(env, symbol) { panic_any("soulbound") }
112112

113-
kv_increment(env, &bcat(&[b"bic:coin:balance:", env.caller_env.account_caller.as_slice(), b":", symbol]), -amount);
114-
kv_increment(env, &bcat(&[b"bic:coin:balance:", receiver, b":", symbol]), amount);
113+
if kv_exists(env, &bcat(&[b"account:", &BURN_ADDRESS, b":balance:AMA"])) {
114+
kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:", symbol]), -amount);
115+
kv_increment(env, &bcat(&[b"account:", receiver, b":balance:", symbol]), amount);
115116

116-
//Account burnt coins
117-
if symbol != b"AMA" && receiver == &BURN_ADDRESS {
118-
kv_increment(env, &bcat(&[b"bic:coin:totalSupply:", symbol]), -amount);
119-
}
120-
}
121-
122-
/*
123-
kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:", &symbol]), -amount);
124-
kv_increment(env, &bcat(&[b"account:", receiver, b":balance:", symbol]), amount);
117+
//Account burnt coins
118+
if symbol != b"AMA" && receiver == &BURN_ADDRESS {
119+
kv_increment(env, &bcat(&[b"coin:", symbol, b":totalSupply"]), -amount);
120+
}
121+
} else {
122+
kv_increment(env, &bcat(&[b"bic:coin:balance:", env.caller_env.account_caller.as_slice(), b":", symbol]), -amount);
123+
kv_increment(env, &bcat(&[b"bic:coin:balance:", receiver, b":", symbol]), amount);
125124

126-
//Account burnt coins
127-
if symbol != b"AMA" && receiver == &BURN_ADDRESS {
128-
kv_increment(env, &bcat(&[b"coin:", symbol, b":totalSupply"]), -amount);
125+
//Account burnt coins
126+
if symbol != b"AMA" && receiver == &BURN_ADDRESS {
127+
kv_increment(env, &bcat(&[b"bic:coin:totalSupply:", symbol]), -amount);
128+
}
129+
}
129130
}
130-
*/
131131

132132
pub fn call_create_and_mint(env: &mut crate::consensus::consensus_apply::ApplyEnv, args: Vec<Vec<u8>>) {
133133
if args.len() < 2 { panic_any("invalid_args") }

ex/native/rdb/src/consensus/bic/epoch.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ pub fn call_set_emission_address(env: &mut crate::consensus::consensus_apply::Ap
176176
if args.len() != 1 { panic_any("invalid_args") }
177177
let address = args[0].as_slice();
178178
if address.len() != 48 { panic_any("invalid_address_pk") }
179-
kv_put(env, &bcat(&[b"bic:epoch:emission_address:", env.caller_env.account_caller.as_slice()]), address);
179+
180+
if kv_exists(env, &crate::bcat(&[b"account:", &crate::consensus::bic::coin::BURN_ADDRESS, b":balance:AMA"])) {
181+
kv_put(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":attribute:emission_address"]), address);
182+
} else {
183+
kv_put(env, &bcat(&[b"bic:epoch:emission_address:", env.caller_env.account_caller.as_slice()]), address);
184+
}
180185
}
181186

182187
pub fn call_submit_sol(env: &mut crate::consensus::consensus_apply::ApplyEnv, args: Vec<Vec<u8>>) {
@@ -203,10 +208,19 @@ pub fn call_submit_sol(env: &mut crate::consensus::consensus_apply::ApplyEnv, ar
203208
panic_any("invalid_sol");
204209
}
205210

206-
if !kv_exists(env, &bcat(&[b"bic:epoch:pop:", usol.pk.as_slice()])) {
207-
match consensus::bls12_381::verify(&usol.pk, &usol.pop, &usol.pk, consensus::aggsig::DST_POP) {
208-
Ok(()) => kv_put(env, &bcat(&[b"bic:epoch:pop:", usol.pk.as_slice()]), &usol.pop),
209-
Err(_) => panic_any("invalid_pop")
211+
if kv_exists(env, &crate::bcat(&[b"account:", &crate::consensus::bic::coin::BURN_ADDRESS, b":balance:AMA"])) {
212+
if !kv_exists(env, &bcat(&[b"account:", &usol.pk, b":attribute:pop"])) {
213+
match consensus::bls12_381::verify(&usol.pk, &usol.pop, &usol.pk, consensus::aggsig::DST_POP) {
214+
Ok(()) => kv_put(env, &bcat(&[b"account:", &usol.pk, b":attribute:pop"]), &usol.pop),
215+
Err(_) => panic_any("invalid_pop")
216+
}
217+
}
218+
} else {
219+
if !kv_exists(env, &bcat(&[b"bic:epoch:pop:", usol.pk.as_slice()])) {
220+
match consensus::bls12_381::verify(&usol.pk, &usol.pop, &usol.pk, consensus::aggsig::DST_POP) {
221+
Ok(()) => kv_put(env, &bcat(&[b"bic:epoch:pop:", usol.pk.as_slice()]), &usol.pop),
222+
Err(_) => panic_any("invalid_pop")
223+
}
210224
}
211225
}
212226
kv_increment(env, &bcat(&[b"bic:epoch:solutions_count:", usol.pk.as_slice()]), 1);
@@ -304,21 +318,20 @@ pub fn call_slash_trainer(env: &mut crate::consensus::consensus_apply::ApplyEnv,
304318
};
305319
if !signature_valid { panic_any("invalid_signature") }
306320

307-
let mut trainers_removed = kv_get_trainers(env, &bcat(&[b"bic:epoch:trainers:removed:", epoch.to_string().as_bytes()]));
308-
trainers_removed.push(malicious_pk.to_vec());
309-
let term_trainers_removed = consensus::bic::eetf_list_of_binaries(trainers_removed).unwrap();
310-
kv_put(env, &bcat(&[b"bic:epoch:trainers:removed:", epoch.to_string().as_bytes()]), term_trainers_removed.as_slice());
311-
312-
trainers.retain(|pk| pk.as_slice() != malicious_pk);
313-
let term_trainers = consensus::bic::eetf_list_of_binaries(trainers).unwrap();
314-
kv_put(env, &bcat(&[b"bic:epoch:trainers:", epoch.to_string().as_bytes()]), term_trainers.as_slice());
315-
316-
kv_put(env, &bcat(&[b"bic:epoch:trainers:height:", &height_next]), term_trainers.as_slice());
317-
/*
318321
trainers.retain(|pk| pk.as_slice() != malicious_pk);
319-
let term_trainers = consensus::bic::list_of_binaries_to_vecpak(trainers);
320-
kv_put(env, &bcat(&[b"bic:epoch:trainers:height:", &height_next]), term_trainers.as_slice());
321-
*/
322+
if env.caller_env.entry_height >= 413_00000 {
323+
let term_trainers = consensus::bic::list_of_binaries_to_vecpak(trainers);
324+
kv_put(env, &bcat(&[b"bic:epoch:validators:height:", &height_next]), term_trainers.as_slice());
325+
} else {
326+
let mut trainers_removed = kv_get_trainers(env, &bcat(&[b"bic:epoch:trainers:removed:", epoch.to_string().as_bytes()]));
327+
trainers_removed.push(malicious_pk.to_vec());
328+
let term_trainers_removed = consensus::bic::eetf_list_of_binaries(trainers_removed).unwrap();
329+
kv_put(env, &bcat(&[b"bic:epoch:trainers:removed:", epoch.to_string().as_bytes()]), term_trainers_removed.as_slice());
330+
331+
let term_trainers = consensus::bic::eetf_list_of_binaries(trainers).unwrap();
332+
kv_put(env, &bcat(&[b"bic:epoch:trainers:", epoch.to_string().as_bytes()]), term_trainers.as_slice());
333+
kv_put(env, &bcat(&[b"bic:epoch:trainers:height:", &height_next]), term_trainers.as_slice());
334+
}
322335
}
323336

324337
pub fn next(env: &mut ApplyEnv) {

ex/native/rdb/src/consensus/bic/lockup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn call_unlock(env: &mut crate::consensus::consensus_apply::ApplyEnv, args:
3535
if env.caller_env.entry_epoch < unlock_epoch {
3636
panic_any("vault_is_locked")
3737
} else {
38-
kv_increment(env, &bcat(&[b"bic:coin:balance:", &env.caller_env.account_caller, &symbol]), amount as i128);
38+
kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:", symbol]), amount as i128);
3939
kv_delete(env, vault_key);
4040
}
4141
}

ex/native/rdb/src/consensus/bic/lockup_prime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn call_lock(env: &mut crate::consensus::consensus_apply::ApplyEnv, args: Ve
3535

3636
if amount <= to_flat(1) { panic_any("invalid_amount") }
3737
if amount > balance(env, env.caller_env.account_caller.as_slice(), b"AMA") { panic_any("insufficient_funds") }
38-
kv_increment(env, &bcat(&[b"bic:coin:balance:", &env.caller_env.account_caller, b":AMA"]), -amount);
38+
kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:AMA"]), -amount);
3939

4040
let vault_index = kv_increment(env, &bcat(&[b"bic:lockup_prime:unique_index"]), 1);
4141
let vault_value = bcat(&[
@@ -69,13 +69,13 @@ pub fn call_unlock(env: &mut crate::consensus::consensus_apply::ApplyEnv, args:
6969
let penalty = unlock_amount / 4;
7070
let disbursement = unlock_amount - penalty;
7171

72-
kv_increment(env, &bcat(&[b"bic:coin:balance:", TREASURY_DONATION_ADDRESS, b":AMA"]), penalty as i128);
72+
kv_increment(env, &bcat(&[b"account:", TREASURY_DONATION_ADDRESS, b":balance:AMA"]), penalty as i128);
7373
//Lockup for 5 epochs
7474
create_lock(env, env.caller_env.account_caller.to_vec().as_slice(), b"AMA", disbursement as i128, env.caller_env.entry_epoch.saturating_add(5));
7575
} else {
7676
let prime_points = unlock_amount * multiplier;
7777
mint(env, b"PRIME", prime_points as i128, env.caller_env.account_caller.to_vec().as_slice());
78-
kv_increment(env, &bcat(&[b"bic:coin:balance:", &env.caller_env.account_caller, b":AMA"]), unlock_amount as i128);
78+
kv_increment(env, &bcat(&[b"account:", &env.caller_env.account_caller, b":balance:AMA"]), unlock_amount as i128);
7979
}
8080

8181
kv_delete(env, vault_key);

ex/native/rdb/src/consensus/bic/protocol.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ pub fn tx_cost_per_byte(_epoch: u64, tx_encoded_len: usize) -> i128 {
1010
}
1111

1212
pub fn pay_cost(env: &mut crate::consensus::consensus_apply::ApplyEnv, cost: i128) {
13+
if consensus_kv::kv_exists(env, &crate::bcat(&[b"account:", &coin::BURN_ADDRESS, b":balance:AMA"])) {
14+
consensus_kv::kv_increment(env, &crate::bcat(&[b"account:", &env.caller_env.account_origin, b":balance:AMA"]), -cost);
15+
// Increment validator / burn
16+
consensus_kv::kv_increment(env, &crate::bcat(&[b"account:", &env.caller_env.entry_signer, b":balance:AMA"]), cost/2);
17+
consensus_kv::kv_increment(env, &crate::bcat(&[b"account:", &coin::BURN_ADDRESS, b":balance:AMA"]), cost/2);
18+
} else {
19+
1320
// Deduct tx cost
1421
consensus_kv::kv_increment(env, &crate::bcat(&[b"bic:coin:balance:", env.caller_env.account_origin.as_slice(), b":AMA"]), -cost);
1522
// Increment validator / burn
1623
consensus_kv::kv_increment(env, &crate::bcat(&[b"bic:coin:balance:", env.caller_env.entry_signer.as_slice(), b":AMA"]), cost/2);
1724
consensus_kv::kv_increment(env, &crate::bcat(&[b"bic:coin:balance:", &coin::BURN_ADDRESS, b":AMA"]), cost/2);
25+
}
1826
}

0 commit comments

Comments
 (0)