Skip to content

Commit

Permalink
Read DST20 supply with optionally state root
Browse files Browse the repository at this point in the history
  • Loading branch information
Mixa84 committed Aug 24, 2023
1 parent 28015fd commit 419d18b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,15 @@ impl EVMServices {
Ok(is_queued)
}

pub fn get_dst20_total_supply(&self, token_id: u64) -> Result<U256> {
pub fn get_dst20_total_supply(&self, token_id: u64, state_root: H256) -> Result<U256> {
let address = ain_contracts::dst20_address_from_token_id(token_id)?;
debug!("[get_dst20_total_supply] Fetching address {:#?}", address);

let backend = self.core.get_latest_block_backend()?;
let backend = if state_root == LOWER_H256 {
self.core.get_latest_block_backend()?
} else {
self.core.get_backend(state_root)?
};

let total_supply_index = H256::from_low_u64_be(2);
backend.get_contract_storage(address, total_supply_index.as_bytes())
Expand Down
12 changes: 10 additions & 2 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,16 @@ pub fn evm_try_is_dst20_deployed_or_queued(
}
}

pub fn evm_try_get_dst20_total_supply(result: &mut ffi::CrossBoundaryResult, token_id: u64) -> u64 {
match SERVICES.evm.get_dst20_total_supply(token_id) {
pub fn evm_try_get_dst20_total_supply(
result: &mut ffi::CrossBoundaryResult,
token_id: u64,
state_root: &str,
) -> u64 {
let Ok(state_root) = state_root.parse() else {
return cross_boundary_error_return(result, "Invalid state root");
};

match SERVICES.evm.get_dst20_total_supply(token_id, state_root) {
Ok(total_supply) => cross_boundary_success_return(result, total_supply.as_u64()),
Err(e) => cross_boundary_error_return(result, e.to_string()),
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ pub mod ffi {
queue_id: u64,
) -> u64;

fn evm_try_get_dst20_total_supply(result: &mut CrossBoundaryResult, token_id: u64) -> u64;
fn evm_try_get_dst20_total_supply(
result: &mut CrossBoundaryResult,
token_id: u64,
state_root: &str,
) -> u64;
}
}
6 changes: 3 additions & 3 deletions src/masternodes/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2413,8 +2413,8 @@ CTransferDomainMessage DecodeTransferDomainMessage(const CTransactionRef& tx, co
return CTransferDomainMessage{};
}

CAmount GetEvmDST20TotalSupply(const DCT_ID& id) {
auto result = XResultValue(evm_try_get_dst20_total_supply(result, id.v));
CAmount GetEvmDST20TotalSupply(const DCT_ID& id, const std::string &stateRoot = std::string()) {
auto result = XResultValue(evm_try_get_dst20_total_supply(result, id.v, stateRoot));
if (result)
if (auto balance = *result)
return static_cast<CAmount>(balance);
Expand Down Expand Up @@ -2564,7 +2564,7 @@ static Res ProcessAccountingConsensusChecks(const CBlock &block, const CBlockInd
deltaDST20EvmTotalSupply.AddBalances(evmInitialState.dst20EvmTotalSupply.balances);
auto res = Res::Ok();
cache.ForEachToken([&](DCT_ID const& id, CTokenImplementation token) {
auto supply = GetEvmDST20TotalSupply(id);
auto supply = GetEvmDST20TotalSupply(id, stateRoot);
if (supply != -1)
if (deltaDST20EvmTotalSupply.balances[id] != supply || deltaDST20EvmTotalSupply.balances[id] < 0) {
res = DeFiErrors::AccountingMissmatchEVMDST20(id.ToString(), evmInitialState.dst20EvmTotalSupply.balances[id], deltaDST20EvmTotalSupply.balances[id], supply);
Expand Down

0 comments on commit 419d18b

Please sign in to comment.