diff --git a/lib/ain-evm/src/evm.rs b/lib/ain-evm/src/evm.rs index 426bb93f0c..228135a31d 100644 --- a/lib/ain-evm/src/evm.rs +++ b/lib/ain-evm/src/evm.rs @@ -628,11 +628,15 @@ impl EVMServices { Ok(is_queued) } - pub fn get_dst20_total_supply(&self, token_id: u64) -> Result { + pub fn get_dst20_total_supply(&self, token_id: u64, state_root: H256) -> Result { 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()) diff --git a/lib/ain-rs-exports/src/evm.rs b/lib/ain-rs-exports/src/evm.rs index a245f95ace..bebf1eb37b 100644 --- a/lib/ain-rs-exports/src/evm.rs +++ b/lib/ain-rs-exports/src/evm.rs @@ -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()), } diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index 115f16b5cb..cae38eed54 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -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; } } diff --git a/src/masternodes/validation.cpp b/src/masternodes/validation.cpp index a8b80f6b7f..277147bffd 100644 --- a/src/masternodes/validation.cpp +++ b/src/masternodes/validation.cpp @@ -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(balance); @@ -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);