Skip to content

Commit

Permalink
Upgrade prism (#791)
Browse files Browse the repository at this point in the history
* Mint FRA for test address

* upgrade prism

* make fmt

* make test

* fix make test

* fix withdraw_fra and parse_deposit_asset
  • Loading branch information
liyue201 authored Jan 18, 2023
1 parent 84f9e70 commit 996f59a
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 574 deletions.
5 changes: 3 additions & 2 deletions src/components/abciapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ tempfile = "3.1.0"
baseapp = { path = "../contracts/baseapp" }
fc-rpc = { path = "../contracts/rpc" }
fp-storage = { path = "../contracts/primitives/storage" }
fp-utils = { path = "../contracts/primitives/utils" }
fp-types = {path = "../contracts/primitives/types"}
fp-utils = {path = "../contracts/primitives/utils" }


enterprise-web3 = { path = "../contracts/primitives/enterprise-web3", optional = true }
Expand All @@ -75,5 +76,5 @@ vergen = "=3.1.0"
[features]
default = ["diskcache"]
diskcache = ["ledger/diskcache"]
debug_env = ["ledger/debug_env", "config/debug_env"]
debug_env = ["ledger/debug_env", "config/debug_env", "baseapp/debug_env"]
web3_service = ["enterprise-web3", "baseapp/web3_service"]
108 changes: 80 additions & 28 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use {
staking::KEEP_HIST,
store::api_cache,
},
module_evm::utils::{deposit_asset_event_topic_str, parse_deposit_asset_event},
parking_lot::{Mutex, RwLock},
protobuf::RepeatedField,
ruc::*,
Expand Down Expand Up @@ -374,30 +375,85 @@ pub fn deliver_tx(
}
let mut resp = s.account_base_app.write().deliver_tx(req);

if td_height > CFG.checkpoint.fix_prism_mint_pay && 0 == resp.code {
let mut la = s.la.write();
let mut laa = la.get_committed_state().write();
if let Some(tx) = staking::system_prism_mint_pay(
&mut laa,
&mut s.account_base_app.write(),
) {
drop(laa);
if la.cache_transaction(tx).is_ok() {
return resp;
if td_height > CFG.checkpoint.prismxx_inital_height && 0 == resp.code {
let deposit_asset_topic = deposit_asset_event_topic_str();

for evt in resp.events.iter() {
if evt.field_type == *"ethereum_ContractLog" {
let mut bridge_contract_found = false;
let mut deposit_asset_foud = false;

for pair in evt.attributes.iter() {
let key = String::from_utf8(pair.key.clone())
.unwrap_or_default();
if key == *"address" {
let addr = String::from_utf8(pair.value.clone())
.unwrap_or_default();
if addr
== CFG
.checkpoint
.prism_bridge_address
.to_lowercase()
{
bridge_contract_found = true
}
}
if key == *"topics" {
let topic = String::from_utf8(pair.value.clone())
.unwrap_or_default();
if topic == deposit_asset_topic {
deposit_asset_foud = true
}
}
if key == *"data"
&& bridge_contract_found
&& deposit_asset_foud
{
let data = String::from_utf8(pair.value.clone())
.unwrap_or_default();

let data_vec = serde_json::from_str(&data).unwrap();

let deposit_asset =
parse_deposit_asset_event(data_vec);

match deposit_asset {
Ok(deposit) => {
let mut la = s.la.write();
let mut laa =
la.get_committed_state().write();
if let Some(tx) =
staking::system_prism_mint_pay(
&mut laa, deposit,
)
{
drop(laa);
if la.cache_transaction(tx).is_ok() {
return resp;
}
resp.code = 1;
s.account_base_app
.read()
.deliver_state
.state
.write()
.discard_session();
s.account_base_app
.read()
.deliver_state
.db
.write()
.discard_session();
}
}
Err(e) => {
resp.code = 1;
resp.log = e.to_string();
}
}
}
}
}
resp.code = 1;
s.account_base_app
.read()
.deliver_state
.state
.write()
.discard_session();
s.account_base_app
.read()
.deliver_state
.db
.write()
.discard_session();
}
}
resp
Expand Down Expand Up @@ -434,11 +490,7 @@ pub fn end_block(
// mint coinbase, cache system transactions to ledger
{
let mut laa = la.get_committed_state().write();
if let Some(tx) = staking::system_mint_pay(
td_height,
&mut laa,
&mut s.account_base_app.write(),
) {
if let Some(tx) = staking::system_mint_pay(td_height, &mut laa) {
drop(laa);
// this unwrap should be safe
la.cache_transaction(tx).unwrap();
Expand Down
137 changes: 37 additions & 100 deletions src/components/abciapp/src/abci/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mod test;
use {
crate::abci::server::callback::TENDERMINT_BLOCK_HEIGHT,
abci::{Evidence, Header, LastCommitInfo, PubKey, ValidatorUpdate},
baseapp::BaseApp as AccountBaseApp,
config::abci::global_cfg::CFG,
fp_types::actions::xhub::NonConfidentialOutput,
lazy_static::lazy_static,
ledger::{
data_model::{
Expand Down Expand Up @@ -285,120 +285,57 @@ fn system_governance(staking: &mut Staking, bz: &ByzantineInfo) -> Result<()> {
/// Pay for freed 'Delegations' and 'FraDistributions'.
pub fn system_prism_mint_pay(
la: &mut LedgerState,
account_base_app: &mut AccountBaseApp,
mint: NonConfidentialOutput,
) -> Option<Transaction> {
let mut mints = Vec::new();

if let Some(account_mint) = account_base_app.consume_mint() {
for mint in account_mint {
if mint.asset != ASSET_TYPE_FRA {
let atc = AssetTypeCode { val: mint.asset };
let at = if let Some(mut at) = la.get_asset_type(&atc) {
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};
if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}
at.properties.asset_rules.decimals = mint.decimal;
at
} else {
let mut at = AssetType::default();
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};
if mint.asset != ASSET_TYPE_FRA {
let atc = AssetTypeCode { val: mint.asset };
let at = if let Some(mut at) = la.get_asset_type(&atc) {
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};
if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}
at.properties.asset_rules.decimals = mint.decimal;
at
} else {
let mut at = AssetType::default();
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};

if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}
at.properties.asset_rules.decimals = mint.decimal;
if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}
at.properties.asset_rules.decimals = mint.decimal;

at.properties.code = AssetTypeCode { val: mint.asset };
at.properties.code = AssetTypeCode { val: mint.asset };

at
};
at
};

la.insert_asset_type(atc, at);
}
la.insert_asset_type(atc, at);
}

let mint_entry = MintEntry::new(
MintKind::Other,
mint.target,
None,
mint.amount,
mint.asset,
);
let mint_entry =
MintEntry::new(MintKind::Other, mint.target, None, mint.amount, mint.asset);

mints.push(mint_entry);
}
}
mints.push(mint_entry);

if mints.is_empty() {
None
} else {
let mint_ops =
Operation::MintFra(MintFraOps::new(la.get_staking().cur_height(), mints));
Some(Transaction::from_operation_coinbase_mint(
mint_ops,
la.get_state_commitment().1,
))
}
let mint_ops =
Operation::MintFra(MintFraOps::new(la.get_staking().cur_height(), mints));
Some(Transaction::from_operation_coinbase_mint(
mint_ops,
la.get_state_commitment().1,
))
}

/// Pay for freed 'Delegations' and 'FraDistributions'.
pub fn system_mint_pay(
td_height: i64,
la: &mut LedgerState,
account_base_app: &mut AccountBaseApp,
) -> Option<Transaction> {
pub fn system_mint_pay(td_height: i64, la: &mut LedgerState) -> Option<Transaction> {
let mut mints = Vec::new();

if td_height <= CFG.checkpoint.fix_prism_mint_pay {
if let Some(account_mint) = account_base_app.consume_mint() {
for mint in account_mint {
if mint.asset != ASSET_TYPE_FRA {
let atc = AssetTypeCode { val: mint.asset };
let at = if let Some(mut at) = la.get_asset_type(&atc) {
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};

if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}

at
} else {
let mut at = AssetType::default();
at.properties.issuer = IssuerPublicKey {
key: *BLACK_HOLE_PUBKEY_STAKING,
};

if mint.max_supply != 0 {
at.properties.asset_rules.max_units = Some(mint.max_supply);
}

at.properties.code = AssetTypeCode { val: mint.asset };

at
};

la.insert_asset_type(atc, at);
}

let mint_entry = MintEntry::new(
MintKind::Other,
mint.target,
None,
mint.amount,
mint.asset,
);

mints.push(mint_entry);
}
}
}

let staking = la.get_staking();
let mut limit = staking.coinbase_balance() as i128;

Expand Down
7 changes: 3 additions & 4 deletions src/components/config/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub struct CheckPointConfig {
pub unbond_block_cnt: u64,
pub prismxx_inital_height: i64,
pub enable_triple_masking_height: i64,
pub fix_prism_mint_pay: i64,
pub fix_exec_code: i64,

// https://github.com/FindoraNetwork/platform/pull/307
Expand Down Expand Up @@ -117,13 +116,14 @@ impl CheckPointConfig {
enable_triple_masking_height: 0,
fix_unpaid_delegation_height: 0,
fix_undelegation_missing_reward_height: 0,
fix_prism_mint_pay: 0,
fix_exec_code: 0,
evm_checktx_nonce: 0,
utxo_checktx_height: 0,
utxo_asset_prefix_height: 0,
nonce_bug_fix_height: 0,
prism_bridge_address: String::new(),
prism_bridge_address:
"0xfcfe4ff1006a7721cee870b56ee2b5c250aec13b"
.to_owned(),
proper_gas_set_height: 0,
fix_delegators_am_height: 0,
validators_limit_v2_height: 0,
Expand All @@ -145,7 +145,6 @@ impl CheckPointConfig {
prismxx_inital_height: 30000000,
enable_triple_masking_height: 30000000,
fix_unpaid_delegation_height: 2261885,
fix_prism_mint_pay: 30000000,
fix_exec_code: 30000000,
evm_checktx_nonce: 30000000,
utxo_checktx_height: 30000000,
Expand Down
3 changes: 2 additions & 1 deletion src/components/contracts/baseapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ evm-precompile = {path = "../modules/evm/precompile"}

[features]
abci_mock = []
web3_service = ["enterprise-web3", "module-account/web3_service", "module-ethereum/web3_service", "module-evm/web3_service"]
web3_service = ["enterprise-web3", "module-account/web3_service", "module-ethereum/web3_service", "module-evm/web3_service"]
debug_env = []
Loading

0 comments on commit 996f59a

Please sign in to comment.