Skip to content

Commit

Permalink
update import
Browse files Browse the repository at this point in the history
  • Loading branch information
shaorongqiang committed Jun 27, 2023
1 parent b9c3d17 commit 2af1fe5
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 222 deletions.
9 changes: 6 additions & 3 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,12 @@ pub fn begin_block(
.map(|v| (v.id, v.clone()))
.collect();

EVM_STAKING
.get()
.map(|staking| staking.write().import_validators(&validators, &delegations));
if let Err(e) = EVM_STAKING.get().c(d!()).and_then(|staking| {
staking.write().import_validators(&validators, &delegations)
}) {
println!("import_validators error {:?}", e);
panic!()
};
}
if CFG.checkpoint.disable_evm_block_height < header.height
&& header.height < CFG.checkpoint.enable_frc20_height
Expand Down
2 changes: 1 addition & 1 deletion src/components/config/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ lazy_static! {
validator_whitelist_v3: vec![],
max_gas_price_limit: 0,
evm_staking_inital_height: 128,
evm_staking_address: "0x34d4F6946Ceb1014BF13603fA7e7fC943952e8a4".to_owned(),
evm_staking_address: "0x84db796A3F8F02396f82219e3197933d15960771".to_owned(),
};
}

Expand Down
101 changes: 59 additions & 42 deletions src/components/contracts/baseapp/src/staking.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::BaseApp;
use config::abci::global_cfg::CFG;
use ethereum_types::{H160, U256};
use fp_traits::{
account::AccountAsset,
Expand Down Expand Up @@ -43,76 +44,92 @@ impl EVMStaking for BaseApp {
});
}
}
let mut ds: BTreeMap<XfrPublicKey, BTreeMap<u64, Vec<(H160, U256)>>> =
BTreeMap::new();
let power = vs.iter().map(|v| v.power.as_u128()).sum::<u128>();

let evm_staking_address =
H160::from_str(CFG.checkpoint.evm_staking_address.as_str()).c(d!())?;

module_account::App::<BaseApp>::mint(
&self.deliver_state,
&Address::from(evm_staking_address),
U256::from(power),
)?;

if let Err(e) =
self.modules
.evm_module
.import_validators(&self.deliver_state, from, &vs)
{
for delegation in delegations.values() {
let d = delegation
.delegations
.iter()
.map(|(validator_addr, amount)| {
(mapping_address(validator_addr), U256::from(*amount))
})
.collect::<Vec<_>>();
let mut v = BTreeMap::new();
v.insert(delegation.end_height, d.clone());
self.deliver_state.state.write().discard_session();
self.deliver_state.db.write().discard_session();
return Err(e);
}

let mut ds: BTreeMap<(XfrPublicKey, H160), Vec<(u64, U256)>> = BTreeMap::new();
{
for delegation in delegations.values() {
let public_key = delegation.receiver_pk.unwrap_or(delegation.id);
ds.entry(public_key)
.and_modify(|rem| {
rem.entry(delegation.end_height)
.and_modify(|rem1| rem1.extend(d.clone().into_iter()))
.or_insert(d);
})
.or_insert(v);

for (validator, amount) in delegation.delegations.iter() {
let amount = U256::from(*amount);
ds.entry((public_key, mapping_address(validator)))
.and_modify(|am| {
am.push((delegation.end_height, amount));
})
.or_insert(vec![(delegation.end_height, amount)]);
}
}
}
let mut delegators = vec![];
let mut undelegation_infos = vec![];
{
for (public_key, delegations) in ds.iter() {
let mut bound_amount = vec![];
let mut unbound_amount = vec![];
let address = mapping_address(public_key);
for (end_height, values) in delegations.iter() {
let v = values.to_vec();
for ((public_key, validator_address), value) in ds.iter() {
let mut bound_amount = U256::zero();
let mut unbound_amount = U256::zero();

let delegator_address = mapping_address(public_key);

for (end_height, amount) in value.iter() {
if BLOCK_HEIGHT_MAX == *end_height {
bound_amount.extend(v);
bound_amount = bound_amount.checked_add(*amount).c(d!())?;
} else {
unbound_amount.extend(v);
let v = values
.iter()
.map(|(validator_addr, amount)| UndelegationInfos {
validator: *validator_addr,
delegator: address,
amount: *amount,
height: U256::from(*end_height),
})
.collect::<Vec<_>>();
undelegation_infos.extend(v);
unbound_amount = unbound_amount.checked_add(*amount).c(d!())?;
undelegation_infos.push(UndelegationInfos {
validator: *validator_address,
delegator: delegator_address,
amount: *amount,
height: U256::from(*end_height),
});
}
}

delegators.push(DelegatorParam {
delegator: address,
validator: *validator_address,
delegator: delegator_address,
delegator_pk: public_key.as_bytes().to_vec(),
bound_amount,
unbound_amount,
});
}
}
if let Err(e) = self.modules.evm_module.import_validators(

if let Err(e) = self.modules.evm_module.import_delegators(
&self.deliver_state,
from,
&vs,
&delegators,
) {
self.deliver_state.state.write().discard_session();
self.deliver_state.db.write().discard_session();
return Err(e);
}
if let Err(e) = self.modules.evm_module.import_undelegations(
&self.deliver_state,
from,
&undelegation_infos,
) {
self.deliver_state.state.write().discard_session();
self.deliver_state.db.write().discard_session();
return Err(e);
}

self.deliver_state.state.write().commit_session();
self.deliver_state.db.write().commit_session();
Ok(())
Expand Down
Loading

0 comments on commit 2af1fe5

Please sign in to comment.