Skip to content

Commit

Permalink
refine domain logic
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 7, 2023
1 parent fd340d9 commit 5d66756
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 61 deletions.
115 changes: 55 additions & 60 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2224,9 +2224,10 @@ UniValue transferdomain(const JSONRPCRequest &request) {
UniValue srcDstArray(UniValue::VARR);

if (!request.params[0].isArray()) {
auto defineDomain = [](CTxDestination &dest, VMDomain domain) {
auto defineDomain = [](CTxDestination &dest) {
if (dest.index() == WitV0KeyHashType || dest.index() == PKHashType) {
return domain == VMDomain::DVM ? VMDomain::DVM : VMDomain::UTXO;
// by default for DVM in this pipeline
return VMDomain::DVM;
} else if (dest.index() == WitV16KeyEthHashType) {
return VMDomain::EVM;
} else {
Expand Down Expand Up @@ -2258,20 +2259,16 @@ UniValue transferdomain(const JSONRPCRequest &request) {
VMDomain domain = static_cast<VMDomain>(domainInt);

CTxDestination srcDest = DecodeDestination(src);
CTxDestination dstDest = DecodeDestination(dst);

VMDomain srcDomainType = defineDomain(srcDest, domain);
VMDomain dstDomainType = defineDomain(dstDest, domain);

UniValue srcObj(UniValue::VOBJ);
srcObj.pushKV("address", src);
srcObj.pushKV("amount", amt);
srcObj.pushKV("domain", static_cast<int>(srcDomainType));
srcObj.pushKV("domain", static_cast<int>(defineDomain(srcDest)));

UniValue dstObj(UniValue::VOBJ);
dstObj.pushKV("address", dst);
dstObj.pushKV("amount", amt);
dstObj.pushKV("domain", static_cast<int>(dstDomainType));
dstObj.pushKV("domain", static_cast<int>(domain));

UniValue elem(UniValue::VOBJ);
elem.pushKV("src", srcObj);
Expand Down Expand Up @@ -2322,12 +2319,12 @@ UniValue transferdomain(const JSONRPCRequest &request) {
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameters, src argument \"domain\" must not be null");
}
auto isEVMIn = dst.domain == static_cast<uint8_t>(VMDomain::EVM);
bool isEVMIn = src.domain == static_cast<uint8_t>(VMDomain::DVM) || src.domain == static_cast<uint8_t>(VMDomain::UTXO);

auto srcKey = AddrToPubKey(pwallet, ScriptToString(src.address));
if (isEVMIn) {
auths.insert(src.address);
} else if (dst.domain != static_cast<uint8_t>(VMDomain::EVM)) {
} else if (src.domain == static_cast<uint8_t>(VMDomain::EVM)) {
if (srcKey.Compress()) {
const auto auth = GetScriptForDestination(WitnessV0KeyHash(srcKey.GetID()));
auths.insert(auth);
Expand Down Expand Up @@ -2373,59 +2370,57 @@ UniValue transferdomain(const JSONRPCRequest &request) {
// dst.data.assign(dstObj["data"].getValStr().begin(), dstObj["data"].getValStr().end());

// Create signed EVM TX
if (dst.domain != static_cast<uint8_t>(VMDomain::UTXO)) {
CKey key;
if (!pwallet->GetKey(srcKey.GetID(), key)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for from address not found in wallet");
}
std::array<uint8_t, 32> privKey{};
std::copy(key.begin(), key.end(), privKey.begin());

std::string to = "";
std::string nativeAddress = "";
if (isEVMIn) {
to = ScriptToString(dst.address);
nativeAddress = ScriptToString(src.address);
} else {
nativeAddress = ScriptToString(dst.address);
}
auto dest = GetDestinationForKey(srcKey, OutputType::ERC55);
auto script = GetScriptForDestination(dest);
auto from = ScriptToString(script);

uint64_t nonce = 0;
bool useNonce = !nonceObj.isNull();
if (useNonce) {
nonce = nonceObj.get_int64();
}
const auto createResult = evm_try_create_and_sign_transfer_domain_tx(
result,
CreateTransferDomainContext{from,
to,
nativeAddress,
isEVMIn,
static_cast<uint64_t>(dst.amount.nValue),
dst.amount.nTokenId.v,
Params().GetConsensus().evmChainId,
privKey,
useNonce,
nonce});
if (!result.ok) {
throw JSONRPCError(RPC_MISC_ERROR,
strprintf("Failed to create and sign TX: %s", result.reason.c_str()));
}
CKey key;
if (!pwallet->GetKey(srcKey.GetID(), key)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for from address not found in wallet");
}
std::array<uint8_t, 32> privKey{};
std::copy(key.begin(), key.end(), privKey.begin());

std::vector<uint8_t> evmTx(createResult.tx.size());
std::copy(createResult.tx.begin(), createResult.tx.end(), evmTx.begin());
if (isEVMIn) {
dst.data = evmTx;
} else {
src.data = evmTx;
}
std::string to = "";
std::string nativeAddress = "";
if (isEVMIn) {
to = ScriptToString(dst.address);
nativeAddress = ScriptToString(src.address);
} else {
nativeAddress = ScriptToString(dst.address);
}
auto dest = GetDestinationForKey(srcKey, OutputType::ERC55);
auto script = GetScriptForDestination(dest);
auto from = ScriptToString(script);

uint64_t nonce = 0;
bool useNonce = !nonceObj.isNull();
if (useNonce) {
nonce = nonceObj.get_int64();
}
const auto createResult = evm_try_create_and_sign_transfer_domain_tx(
result,
CreateTransferDomainContext{from,
to,
nativeAddress,
isEVMIn,
static_cast<uint64_t>(dst.amount.nValue),
dst.amount.nTokenId.v,
Params().GetConsensus().evmChainId,
privKey,
useNonce,
nonce});
if (!result.ok) {
throw JSONRPCError(RPC_MISC_ERROR,
strprintf("Failed to create and sign TX: %s", result.reason.c_str()));
}

nonce_cache.push_back({from, createResult.nonce});
msg.transfers.push_back({src, dst});
std::vector<uint8_t> evmTx(createResult.tx.size());
std::copy(createResult.tx.begin(), createResult.tx.end(), evmTx.begin());
if (isEVMIn) {
dst.data = evmTx;
} else {
src.data = evmTx;
}

nonce_cache.push_back({from, createResult.nonce});
msg.transfers.push_back({src, dst});
}

int targetHeight;
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_evm_transferdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ def test_new_transfer_domain(self):
)
self.nodes[0].generate(1)

self.nodes[0].transferdomain(self.address, self.address_erc55, "100@DFI")
self.nodes[0].transferdomain(self.address, self.address_erc55, "100@DFI", 3)
balance_before = self.nodes[0].w3.eth.get_balance(self.address_erc55)
self.nodes[0].generate(1)
balance_after = self.nodes[0].w3.eth.get_balance(self.address_erc55)
Expand Down

0 comments on commit 5d66756

Please sign in to comment.