Skip to content

Commit

Permalink
Transferdomain vmmap (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Sep 11, 2023
1 parent a499df2 commit e8416ac
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,21 @@ pub fn evm_try_bridge_dst20(
}
}

/// Retrieves a raw tx's transaction hash
/// # Arguments
///
/// * `raw_tx` - The transaction as raw hex
///
/// # Returns
///
/// Returns the transaction's hash
pub fn evm_try_get_tx_hash(result: &mut ffi::CrossBoundaryResult, raw_tx: &str) -> String {
match SignedTx::try_from(raw_tx) {
Ok(signed_tx) => cross_boundary_success_return(result, format!("{:?}", signed_tx.hash())),
Err(e) => cross_boundary_error_return(result, e.to_string()),
}
}

/// Retrieves the queue target block
///
/// # Arguments
Expand Down
1 change: 1 addition & 0 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub mod ffi {
result: &mut CrossBoundaryResult,
tx_hash: &str,
) -> EVMTransaction;
fn evm_try_get_tx_hash(result: &mut CrossBoundaryResult, raw_tx: &str) -> String;

fn evm_try_create_dst20(
result: &mut CrossBoundaryResult,
Expand Down
18 changes: 17 additions & 1 deletion src/masternodes/consensus/xvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,16 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {

auto attributes = mnview.GetAttributes();
auto stats = attributes->GetValue(CTransferDomainStatsLive::Key, CTransferDomainStatsLive{});
std::string evmTxHash;

// Iterate over array of transfers
for (const auto &[src, dst] : obj.transfers) {
CrossBoundaryResult result;
auto hash = evm_try_get_tx_hash(result, HexStr(dst.data));
if (!result.ok) {
return Res::Err("Error bridging DFI: %s", result.reason);
}
evmTxHash = std::string(hash.data(), hash.length()).substr(2);

if (src.domain == static_cast<uint8_t>(VMDomain::DVM) && dst.domain == static_cast<uint8_t>(VMDomain::EVM)) {
// Subtract balance from DFI address
Expand All @@ -205,7 +212,6 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {
ExtractDestination(dst.address, dest);

auto tokenId = dst.amount.nTokenId;
CrossBoundaryResult result;
if (tokenId == DCT_ID{0}) {
evm_unsafe_try_add_balance_in_q(result, evmQueueId, HexStr(dst.data), tx.GetHash().GetHex());
if (!result.ok) {
Expand Down Expand Up @@ -263,6 +269,16 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {
}
}

auto txHash = tx.GetHash().GetHex();
res = mnview.SetVMDomainTxEdge(VMDomainEdge::DVMToEVM, txHash, evmTxHash);
if (!res) {
LogPrintf("Failed to store DVMtoEVM TX hash for DFI TX %s\n", txHash);
}
res = mnview.SetVMDomainTxEdge(VMDomainEdge::EVMToDVM, evmTxHash, txHash);
if (!res) {
LogPrintf("Failed to store EVMToDVM TX hash for DFI TX %s\n", txHash);
}

attributes->SetValue(CTransferDomainStatsLive::Key, stats);
return mnview.SetVariable(*attributes);
}
Expand Down
117 changes: 117 additions & 0 deletions test/functional/feature_evm_vmmap_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,120 @@ def logvmmaps_invalid_block_should_fail(self):
assert_equal("garbage" not in list(list_block["indexes"].values()), True)
assert_equal("0x" not in list(list_block["indexes"].values()), True)

def vmmap_transfer_domain(self):
self.rollback_to(self.start_block_height)

# Evm in
tx = self.nodes[0].transferdomain(
[
{
"src": {"address": self.address, "amount": "100@DFI", "domain": 2},
"dst": {
"address": self.ethAddress,
"amount": "100@DFI",
"domain": 3,
},
}
]
)
self.nodes[0].generate(1)

evm_block = self.nodes[0].eth_getBlockByNumber("latest")
evm_tx = self.nodes[0].vmmap(tx, VMMapType.TxHashDVMToEVM)["output"]
assert_equal(evm_block["transactions"][0], evm_tx)
native_tx = self.nodes[0].vmmap(evm_tx, VMMapType.TxHashEVMToDVM)["output"]
assert_equal(tx, native_tx)

# Evm out
tx = self.nodes[0].transferdomain(
[
{
"src": {"address": self.ethAddress, "amount": "100@DFI", "domain": 3},
"dst": {
"address": self.address,
"amount": "100@DFI",
"domain": 2,
},
}
]
)
self.nodes[0].generate(1)

evm_block = self.nodes[0].eth_getBlockByNumber("latest")
evm_tx = self.nodes[0].vmmap(tx, VMMapType.TxHashDVMToEVM)["output"]
assert_equal(evm_block["transactions"][0], evm_tx)
native_tx = self.nodes[0].vmmap(evm_tx, VMMapType.TxHashEVMToDVM)["output"]
assert_equal(tx, native_tx)

def vmmap_transfer_domain_dst20(self):
self.rollback_to(self.start_block_height)

self.nodes[0].createtoken(
{
"symbol": "BTC",
"name": "BTC token",
"isDAT": True,
"collateralAddress": self.address,
}
)
self.nodes[0].generate(1)
self.nodes[0].minttokens("1@BTC")
self.nodes[0].generate(1)

self.nodes[0].setgov(
{
"ATTRIBUTES": {
"v0/params/feature/transferdomain": "true",
"v0/transferdomain/dvm-evm/enabled": "true",
"v0/transferdomain/dvm-evm/dat-enabled": "true",
"v0/transferdomain/evm-dvm/dat-enabled": "true",
}
}
)
self.nodes[0].generate(1)

# Evm in
tx = self.nodes[0].transferdomain(
[
{
"src": {"address": self.address, "amount": "1@BTC", "domain": 2},
"dst": {
"address": self.ethAddress,
"amount": "1@BTC",
"domain": 3,
},
}
]
)
self.nodes[0].generate(1)

evm_block = self.nodes[0].eth_getBlockByNumber("latest")
evm_tx = self.nodes[0].vmmap(tx, VMMapType.TxHashDVMToEVM)["output"]
assert_equal(evm_block["transactions"][0], evm_tx)
native_tx = self.nodes[0].vmmap(evm_tx, VMMapType.TxHashEVMToDVM)["output"]
assert_equal(tx, native_tx)

# Evm out
tx = self.nodes[0].transferdomain(
[
{
"src": {"address": self.ethAddress, "amount": "1@BTC", "domain": 3},
"dst": {
"address": self.address,
"amount": "1@BTC",
"domain": 2,
},
}
]
)
self.nodes[0].generate(1)

evm_block = self.nodes[0].eth_getBlockByNumber("latest")
evm_tx = self.nodes[0].vmmap(tx, VMMapType.TxHashDVMToEVM)["output"]
assert_equal(evm_block["transactions"][0], evm_tx)
native_tx = self.nodes[0].vmmap(evm_tx, VMMapType.TxHashEVMToDVM)["output"]
assert_equal(tx, native_tx)

def run_test(self):
self.setup()
# vmmap tests
Expand All @@ -377,6 +491,9 @@ def run_test(self):
self.vmmap_invalid_block_number_should_fail()
self.vmmap_rollback_should_succeed()
self.vmmap_auto_invalid_input_should_fail()
self.vmmap_transfer_domain()
self.vmmap_transfer_domain_dst20()

# logvmmap tests
self.logvmmaps_tx_exist()
self.logvmmaps_invalid_tx_should_fail()
Expand Down

0 comments on commit e8416ac

Please sign in to comment.