Skip to content

Commit

Permalink
add importCoinBase (#991)
Browse files Browse the repository at this point in the history
* add importCoinBase

* update CoinbaseMint

* update abi

* fix build

* fix code
  • Loading branch information
shaorongqiang authored and tiannian committed Jul 29, 2023
1 parent 4fad868 commit 2f3ecfd
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 70 deletions.
44 changes: 37 additions & 7 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
//! # Impl function of tendermint ABCI
//!
use globutils::wallet;
use ledger::{
data_model::ASSET_TYPE_FRA,
staking::{FF_ADDR_EXTRA_120_0000, FF_ADDR_LIST},
};
use zei::xfr::asset_record::AssetRecordType;

mod utils;

use {
Expand Down Expand Up @@ -472,23 +479,27 @@ pub fn end_block(

if header.height == CFG.checkpoint.evm_staking_inital_height {
let ledger_state = la.get_committed_state().read();
let validators = ledger_state
.get_staking()
let staking = ledger_state.get_staking();
let validators = staking
.validator_get_current()
.map(|v| v.get_validators().values().cloned().collect::<Vec<_>>())
.unwrap_or_default();

let delegations = ledger_state
.get_staking()
let delegations = staking
.get_global_delegation_records()
.values()
.map(|v| (v.id, v.clone()))
.collect();
let coinbase_balance = staking.coinbase_balance();

if let Err(e) = EVM_STAKING.get().c(d!()).and_then(|staking| {
staking.write().import_validators(&validators, &delegations)
staking.write().import_validators(
&validators,
&delegations,
coinbase_balance,
)
}) {
println!("import_validators error {:?}", e);
tracing::error!(target: "evm staking", "import_validators error:{:?}", e);
panic!()
};
}
Expand Down Expand Up @@ -527,7 +538,26 @@ pub fn end_block(
let evm_resp = if td_height <= CFG.checkpoint.disable_evm_block_height
|| td_height >= CFG.checkpoint.enable_frc20_height
{
s.account_base_app.write().end_block(req)
let mut sum = 0;
let ledger = la.get_committed_state().read();
let mut addrs = FF_ADDR_LIST.to_vec();
addrs.push(FF_ADDR_EXTRA_120_0000);
for fra_addr in addrs.iter() {
for (_, (utxo, _)) in pnk!(wallet::public_key_from_bech32(fra_addr)
.and_then(|pub_key| ledger.get_owned_utxos(&pub_key)))
{
if AssetRecordType::NonConfidentialAmount_NonConfidentialAssetType
== utxo.0.record.get_record_type()
&& Some(ASSET_TYPE_FRA) == utxo.0.record.asset_type.get_asset_type()
{
if let Some(v) = utxo.0.record.amount.get_amount() {
sum += v;
};
}
}
}

s.account_base_app.write().end_block(req, sum)
} else {
Default::default()
};
Expand Down
62 changes: 53 additions & 9 deletions src/components/contracts/baseapp/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl crate::BaseApp {

let mut staking_contract_found = false;
let mut coinbase_mint_foud = false;
let mut validator = H256::zero();
let mut delegator = H256::zero();

let claim_on_contract_address = if td_height
Expand Down Expand Up @@ -361,11 +362,22 @@ impl crate::BaseApp {
- 1],
)
{
if let Some(start) = topic.find(", 0x") {
coinbase_mint_foud = true;
delegator = match H256::from_str(
&topic[start + 2..topic.len() - 1],
) {
let hashes = topic
.strip_prefix('[')
.and_then(|v| v.strip_suffix(']'))
.unwrap_or(&topic)
.split(", ")
.collect::<Vec<_>>();

let mut validator_flag = false;
if let Some(v) = hashes.get(1) {
validator_flag = true;

let s = v
.strip_prefix(' ')
.and_then(|v| v.strip_suffix(' '))
.unwrap_or(v);
validator = match H256::from_str(s) {
Ok(v) => v,
Err(e) => {
resp.code = 1;
Expand All @@ -374,6 +386,25 @@ impl crate::BaseApp {
}
};
};
let mut delegator_flag = false;
if let Some(v) = hashes.get(2) {
delegator_flag = true;

let s = v
.strip_prefix(' ')
.and_then(|v| v.strip_suffix(' '))
.unwrap_or(v);
delegator = match H256::from_str(s) {
Ok(v) => v,
Err(e) => {
resp.code = 1;
resp.log = e.to_string();
return (resp, vec![]);
}
};
};
coinbase_mint_foud =
validator_flag && delegator_flag;
}
}
if key == *"data" {
Expand Down Expand Up @@ -412,7 +443,11 @@ impl crate::BaseApp {
let ret =
parse_evm_staking_coinbase_mint_event(
&event,
vec![event.signature(), delegator],
vec![
event.signature(),
validator,
delegator,
],
data_vec,
);

Expand Down Expand Up @@ -468,13 +503,22 @@ impl crate::BaseApp {
}

#[cfg(any(feature = "abci_mock", test))]
pub fn end_block(&mut self, _req: &RequestEndBlock) -> ResponseEndBlock {
pub fn end_block(
&mut self,
_req: &RequestEndBlock,
_ff_addr_balance: u64,
) -> ResponseEndBlock {
Default::default()
}

#[cfg(all(not(feature = "abci_mock"), not(test)))]
pub fn end_block(&mut self, req: &RequestEndBlock) -> ResponseEndBlock {
self.modules.end_block(&mut self.deliver_state, req)
pub fn end_block(
&mut self,
req: &RequestEndBlock,
ff_addr_balance: u64,
) -> ResponseEndBlock {
self.modules
.end_block(&mut self.deliver_state, req, ff_addr_balance)
}

pub fn commit(&mut self, _req: &RequestCommit) -> ResponseCommit {
Expand Down
11 changes: 6 additions & 5 deletions src/components/contracts/baseapp/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ impl ModuleManager {
&mut self,
ctx: &mut Context,
req: &RequestEndBlock,
ff_addr_balance: u64,
) -> ResponseEndBlock {
let mut resp: ResponseEndBlock = Default::default();
// Note: adding new modules need to be updated.
self.account_module.end_block(ctx, req);
let (mresp, burn_amount) = self.evm_module.end_block(ctx, req);
self.account_module.end_block(ctx, req, ff_addr_balance);
let (mresp, burn_amount) = self.evm_module.end_block(ctx, req, 0);
if !mresp.validator_updates.is_empty() {
resp.validator_updates = mresp.validator_updates;
}
Expand All @@ -93,9 +94,9 @@ impl ModuleManager {
) {
tracing::warn!("module_account::App::<BaseApp>::burn error: {:?}", e)
}
self.ethereum_module.end_block(ctx, req);
self.xhub_module.end_block(ctx, req);
self.template_module.end_block(ctx, req);
self.ethereum_module.end_block(ctx, req, 0);
self.xhub_module.end_block(ctx, req, 0);
self.template_module.end_block(ctx, req, 0);
resp
}

Expand Down
11 changes: 11 additions & 0 deletions src/components/contracts/baseapp/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl EVMStaking for BaseApp {
&self,
validators: &[Validator],
delegations: &BTreeMap<XfrPublicKey, Delegation>,
coinbase_balance: u64,
) -> Result<()> {
let from = H160::from_str(SYSTEM_ADDR).c(d!())?;

Expand Down Expand Up @@ -158,6 +159,16 @@ impl EVMStaking for BaseApp {
tracing::error!(target: "evm staking", "import_reward error:{:?}", e);
return Err(e);
}
if let Err(e) = self.modules.evm_module.import_coinbase_balance(
&self.deliver_state,
from,
coinbase_balance,
) {
self.deliver_state.state.write().discard_session();
self.deliver_state.db.write().discard_session();
tracing::error!(target: "evm staking", "import_coinbase_balance error:{:?}", e);
return Err(e);
}
self.deliver_state.state.write().commit_session();
self.deliver_state.db.write().commit_session();
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/components/contracts/modules/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl<C: Config> AppModule for App<C> {
&mut self,
ctx: &mut Context,
req: &RequestEndBlock,
_ff_addr_balance: u64,
) -> (ResponseEndBlock, U256) {
let _ = ruc::info!(self.store_block(ctx, U256::from(req.height)));
Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn test_abci_deliver_tx() {
fn test_abci_end_block() {
let mut req = RequestEndBlock::default();
req.height = 3;
let _ = BASE_APP.lock().unwrap().end_block(&req);
let _ = BASE_APP.lock().unwrap().end_block(&req, 0);
}

fn test_abci_commit() {
Expand Down
72 changes: 49 additions & 23 deletions src/components/contracts/modules/evm/contracts/EVMStaking.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
{
"indexed": true,
"internalType": "address",
"name": "delegator",
"name": "validator",
"type": "address"
},
{
"indexed": false,
"internalType": "enum IBaseEnum.PublicKeyType",
"name": "ty",
"type": "uint8"
"indexed": true,
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"indexed": false,
Expand Down Expand Up @@ -81,30 +81,12 @@
"name": "OwnershipTransferred",
"type": "event"
},
{
"inputs": [],
"name": "SYSTEM_ADDR",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "validator",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "claim",
Expand Down Expand Up @@ -240,6 +222,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "importCoinBase",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -484,6 +479,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "addr",
"type": "address"
}
],
"name": "setSystemAddr",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -525,6 +533,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "systemAddr",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -717,6 +738,11 @@
"internalType": "enum IBaseEnum.ByztineBehavior[]",
"name": "behavior",
"type": "uint8[]"
},
{
"internalType": "uint256",
"name": "preIssueAmount",
"type": "uint256"
}
],
"name": "trigger",
Expand Down
3 changes: 1 addition & 2 deletions src/components/contracts/modules/evm/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use module_evm::precompile::{Precompile, PrecompileResult};
use std::marker::PhantomData;

use evm_precompile_anemoi::Anemoi;
use evm_precompile_basic::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use evm_precompile_basic::{ECRecover, Identity, Ripemd160, Sha256};
use evm_precompile_blake2::Blake2F;
use evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use evm_precompile_frc20::FRC20;
use evm_precompile_modexp::Modexp;
use evm_precompile_sha3fips::{Sha3FIPS256, Sha3FIPS512};
use fp_core::context::Context as Context2;
use module_evm::precompile::PrecompileId;
use module_evm::Config;
Expand Down
Loading

0 comments on commit 2f3ecfd

Please sign in to comment.