mirror of
https://github.com/Xahau/xahaud.git
synced 2026-08-23 00:10:54 +00:00
XChainBridge: Introduce sidechain support (XLS-38): (#4292)
A bridge connects two blockchains: a locking chain and an issuing chain (also called a mainchain and a sidechain). Both are independent ledgers, with their own validators and potentially their own custom transactions. Importantly, there is a way to move assets from the locking chain to the issuing chain and a way to return those assets from the issuing chain back to the locking chain: the bridge. This key operation is called a cross-chain transfer. A cross-chain transfer is not a single transaction. It happens on two chains, requires multiple transactions, and involves an additional server type called a "witness". A bridge does not exchange assets between two ledgers. Instead, it locks assets on one ledger (the "locking chain") and represents those assets with wrapped assets on another chain (the "issuing chain"). A good model to keep in mind is a box with an infinite supply of wrapped assets. Putting an asset from the locking chain into the box will release a wrapped asset onto the issuing chain. Putting a wrapped asset from the issuing chain back into the box will release one of the existing locking chain assets back onto the locking chain. There is no other way to get assets into or out of the box. Note that there is no way for the box to "run out of" wrapped assets - it has an infinite supply. Co-authored-by: Gregory Popovitch <greg7mdp@gmail.com>
This commit is contained in:
@@ -22,14 +22,331 @@
|
||||
#include <ripple/app/misc/TxQ.h>
|
||||
#include <ripple/basics/StringUtilities.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <ripple/protocol/AccountID.h>
|
||||
#include <ripple/protocol/ErrorCodes.h>
|
||||
#include <ripple/protocol/STXChainBridge.h>
|
||||
#include <ripple/protocol/jss.h>
|
||||
#include <test/app/Import_json.h>
|
||||
#include <test/jtx.h>
|
||||
#include <test/jtx/attester.h>
|
||||
#include <test/jtx/multisign.h>
|
||||
#include <test/jtx/xchain_bridge.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
namespace test {
|
||||
class LedgerRPC_XChain_test : public beast::unit_test::suite,
|
||||
public test::jtx::XChainBridgeObjects
|
||||
{
|
||||
void
|
||||
checkErrorValue(
|
||||
Json::Value const& jv,
|
||||
std::string const& err,
|
||||
std::string const& msg)
|
||||
{
|
||||
if (BEAST_EXPECT(jv.isMember(jss::status)))
|
||||
BEAST_EXPECT(jv[jss::status] == "error");
|
||||
if (BEAST_EXPECT(jv.isMember(jss::error)))
|
||||
BEAST_EXPECT(jv[jss::error] == err);
|
||||
if (msg.empty())
|
||||
{
|
||||
BEAST_EXPECT(
|
||||
jv[jss::error_message] == Json::nullValue ||
|
||||
jv[jss::error_message] == "");
|
||||
}
|
||||
else if (BEAST_EXPECT(jv.isMember(jss::error_message)))
|
||||
BEAST_EXPECT(jv[jss::error_message] == msg);
|
||||
}
|
||||
|
||||
void
|
||||
testLedgerEntryBridge()
|
||||
{
|
||||
testcase("ledger_entry: bridge");
|
||||
using namespace test::jtx;
|
||||
|
||||
Env mcEnv{*this, features};
|
||||
Env scEnv(*this, envconfig(port_increment, 3), features);
|
||||
|
||||
createBridgeObjects(mcEnv, scEnv);
|
||||
|
||||
std::string const ledgerHash{to_string(mcEnv.closed()->info().hash)};
|
||||
std::string bridge_index;
|
||||
Json::Value mcBridge;
|
||||
{
|
||||
// request the bridge via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::bridge_account] = mcDoor.human();
|
||||
jvParams[jss::bridge] = jvb;
|
||||
Json::Value const jrr = mcEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
auto r = jrr[jss::node];
|
||||
// std::cout << to_string(r) << '\n';
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::Account));
|
||||
BEAST_EXPECT(r[jss::Account] == mcDoor.human());
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::Flags));
|
||||
|
||||
BEAST_EXPECT(r.isMember(sfLedgerEntryType.jsonName));
|
||||
BEAST_EXPECT(r[sfLedgerEntryType.jsonName] == jss::Bridge);
|
||||
|
||||
// we not created an account yet
|
||||
BEAST_EXPECT(r.isMember(sfXChainAccountCreateCount.jsonName));
|
||||
BEAST_EXPECT(r[sfXChainAccountCreateCount.jsonName].asInt() == 0);
|
||||
|
||||
// we have not claimed a locking chain tx yet
|
||||
BEAST_EXPECT(r.isMember(sfXChainAccountClaimCount.jsonName));
|
||||
BEAST_EXPECT(r[sfXChainAccountClaimCount.jsonName].asInt() == 0);
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::index));
|
||||
bridge_index = r[jss::index].asString();
|
||||
mcBridge = r;
|
||||
}
|
||||
{
|
||||
// request the bridge via RPC by index
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::index] = bridge_index;
|
||||
Json::Value const jrr = mcEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
BEAST_EXPECT(jrr[jss::node] == mcBridge);
|
||||
}
|
||||
{
|
||||
// swap door accounts and make sure we get an error value
|
||||
Json::Value jvParams;
|
||||
// Sidechain door account is "master", not scDoor
|
||||
jvParams[jss::bridge_account] = Account::master.human();
|
||||
jvParams[jss::bridge] = jvb;
|
||||
jvParams[jss::ledger_hash] = ledgerHash;
|
||||
Json::Value const jrr = mcEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
checkErrorValue(jrr, "entryNotFound", "");
|
||||
}
|
||||
{
|
||||
// create two claim ids and verify that the bridge counter was
|
||||
// incremented
|
||||
mcEnv(xchain_create_claim_id(mcAlice, jvb, reward, scAlice));
|
||||
mcEnv.close();
|
||||
mcEnv(xchain_create_claim_id(mcBob, jvb, reward, scBob));
|
||||
mcEnv.close();
|
||||
|
||||
// request the bridge via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::bridge_account] = mcDoor.human();
|
||||
jvParams[jss::bridge] = jvb;
|
||||
// std::cout << to_string(jvParams) << '\n';
|
||||
Json::Value const jrr = mcEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
auto r = jrr[jss::node];
|
||||
|
||||
// we executed two create claim id txs
|
||||
BEAST_EXPECT(r.isMember(sfXChainClaimID.jsonName));
|
||||
BEAST_EXPECT(r[sfXChainClaimID.jsonName].asInt() == 2);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testLedgerEntryClaimID()
|
||||
{
|
||||
testcase("ledger_entry: xchain_claim_id");
|
||||
using namespace test::jtx;
|
||||
|
||||
Env mcEnv{*this, features};
|
||||
Env scEnv(*this, envconfig(port_increment, 3), features);
|
||||
|
||||
createBridgeObjects(mcEnv, scEnv);
|
||||
|
||||
scEnv(xchain_create_claim_id(scAlice, jvb, reward, mcAlice));
|
||||
scEnv.close();
|
||||
scEnv(xchain_create_claim_id(scBob, jvb, reward, mcBob));
|
||||
scEnv.close();
|
||||
|
||||
std::string bridge_index;
|
||||
{
|
||||
// request the xchain_claim_id via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::xchain_owned_claim_id] = jvXRPBridgeRPC;
|
||||
jvParams[jss::xchain_owned_claim_id][jss::xchain_owned_claim_id] =
|
||||
1;
|
||||
// std::cout << to_string(jvParams) << '\n';
|
||||
Json::Value const jrr = scEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
auto r = jrr[jss::node];
|
||||
// std::cout << to_string(r) << '\n';
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::Account));
|
||||
BEAST_EXPECT(r[jss::Account] == scAlice.human());
|
||||
BEAST_EXPECT(
|
||||
r[sfLedgerEntryType.jsonName] == jss::XChainOwnedClaimID);
|
||||
BEAST_EXPECT(r[sfXChainClaimID.jsonName].asInt() == 1);
|
||||
BEAST_EXPECT(r[sfOwnerNode.jsonName].asInt() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
// request the xchain_claim_id via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::xchain_owned_claim_id] = jvXRPBridgeRPC;
|
||||
jvParams[jss::xchain_owned_claim_id][jss::xchain_owned_claim_id] =
|
||||
2;
|
||||
Json::Value const jrr = scEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
auto r = jrr[jss::node];
|
||||
// std::cout << to_string(r) << '\n';
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::Account));
|
||||
BEAST_EXPECT(r[jss::Account] == scBob.human());
|
||||
BEAST_EXPECT(
|
||||
r[sfLedgerEntryType.jsonName] == jss::XChainOwnedClaimID);
|
||||
BEAST_EXPECT(r[sfXChainClaimID.jsonName].asInt() == 2);
|
||||
BEAST_EXPECT(r[sfOwnerNode.jsonName].asInt() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testLedgerEntryCreateAccountClaimID()
|
||||
{
|
||||
testcase("ledger_entry: xchain_create_account_claim_id");
|
||||
using namespace test::jtx;
|
||||
|
||||
Env mcEnv{*this, features};
|
||||
Env scEnv(*this, envconfig(port_increment, 3), features);
|
||||
|
||||
// note: signers.size() and quorum are both 5 in createBridgeObjects
|
||||
createBridgeObjects(mcEnv, scEnv);
|
||||
|
||||
auto scCarol =
|
||||
Account("scCarol"); // Don't fund it - it will be created with the
|
||||
// xchain transaction
|
||||
auto const amt = XRP(1000);
|
||||
mcEnv(sidechain_xchain_account_create(
|
||||
mcAlice, jvb, scCarol, amt, reward));
|
||||
mcEnv.close();
|
||||
|
||||
// send less than quorum of attestations (otherwise funds are
|
||||
// immediately transferred and no "claim" object is created)
|
||||
size_t constexpr num_attest = 3;
|
||||
auto attestations = create_account_attestations(
|
||||
scAttester,
|
||||
jvb,
|
||||
mcAlice,
|
||||
amt,
|
||||
reward,
|
||||
payee,
|
||||
/*wasLockingChainSend*/ true,
|
||||
1,
|
||||
scCarol,
|
||||
signers,
|
||||
UT_XCHAIN_DEFAULT_NUM_SIGNERS);
|
||||
for (size_t i = 0; i < num_attest; ++i)
|
||||
{
|
||||
scEnv(attestations[i]);
|
||||
}
|
||||
scEnv.close();
|
||||
|
||||
{
|
||||
// request the create account claim_id via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::xchain_owned_create_account_claim_id] =
|
||||
jvXRPBridgeRPC;
|
||||
jvParams[jss::xchain_owned_create_account_claim_id]
|
||||
[jss::xchain_owned_create_account_claim_id] = 1;
|
||||
// std::cout << to_string(jvParams) << '\n';
|
||||
Json::Value const jrr = scEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
// std::cout << to_string(jrr) << '\n';
|
||||
|
||||
BEAST_EXPECT(jrr.isMember(jss::node));
|
||||
auto r = jrr[jss::node];
|
||||
|
||||
BEAST_EXPECT(r.isMember(jss::Account));
|
||||
BEAST_EXPECT(r[jss::Account] == Account::master.human());
|
||||
|
||||
BEAST_EXPECT(r.isMember(sfXChainAccountCreateCount.jsonName));
|
||||
BEAST_EXPECT(r[sfXChainAccountCreateCount.jsonName].asInt() == 1);
|
||||
|
||||
BEAST_EXPECT(
|
||||
r.isMember(sfXChainCreateAccountAttestations.jsonName));
|
||||
auto attest = r[sfXChainCreateAccountAttestations.jsonName];
|
||||
BEAST_EXPECT(attest.isArray());
|
||||
BEAST_EXPECT(attest.size() == 3);
|
||||
BEAST_EXPECT(attest[Json::Value::UInt(0)].isMember(
|
||||
sfXChainCreateAccountProofSig.jsonName));
|
||||
Json::Value a[num_attest];
|
||||
for (size_t i = 0; i < num_attest; ++i)
|
||||
{
|
||||
a[i] = attest[Json::Value::UInt(0)]
|
||||
[sfXChainCreateAccountProofSig.jsonName];
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(jss::Amount) &&
|
||||
a[i][jss::Amount].asInt() == 1000 * drop_per_xrp);
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(jss::Destination) &&
|
||||
a[i][jss::Destination] == scCarol.human());
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(sfAttestationSignerAccount.jsonName) &&
|
||||
std::any_of(
|
||||
signers.begin(), signers.end(), [&](signer const& s) {
|
||||
return a[i][sfAttestationSignerAccount.jsonName] ==
|
||||
s.account.human();
|
||||
}));
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(sfAttestationRewardAccount.jsonName) &&
|
||||
std::any_of(
|
||||
payee.begin(),
|
||||
payee.end(),
|
||||
[&](Account const& account) {
|
||||
return a[i][sfAttestationRewardAccount.jsonName] ==
|
||||
account.human();
|
||||
}));
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(sfWasLockingChainSend.jsonName) &&
|
||||
a[i][sfWasLockingChainSend.jsonName] == 1);
|
||||
BEAST_EXPECT(
|
||||
a[i].isMember(sfSignatureReward.jsonName) &&
|
||||
a[i][sfSignatureReward.jsonName].asInt() ==
|
||||
1 * drop_per_xrp);
|
||||
}
|
||||
}
|
||||
|
||||
// complete attestations quorum - CreateAccountClaimID should not be
|
||||
// present anymore
|
||||
for (size_t i = num_attest; i < UT_XCHAIN_DEFAULT_NUM_SIGNERS; ++i)
|
||||
{
|
||||
scEnv(attestations[i]);
|
||||
}
|
||||
scEnv.close();
|
||||
{
|
||||
// request the create account claim_id via RPC
|
||||
Json::Value jvParams;
|
||||
jvParams[jss::xchain_owned_create_account_claim_id] =
|
||||
jvXRPBridgeRPC;
|
||||
jvParams[jss::xchain_owned_create_account_claim_id]
|
||||
[jss::xchain_owned_create_account_claim_id] = 1;
|
||||
// std::cout << to_string(jvParams) << '\n';
|
||||
Json::Value const jrr = scEnv.rpc(
|
||||
"json", "ledger_entry", to_string(jvParams))[jss::result];
|
||||
checkErrorValue(jrr, "entryNotFound", "");
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testLedgerEntryBridge();
|
||||
testLedgerEntryClaimID();
|
||||
testLedgerEntryCreateAccountClaimID();
|
||||
}
|
||||
};
|
||||
|
||||
class LedgerRPC_test : public beast::unit_test::suite
|
||||
{
|
||||
@@ -2673,6 +2990,6 @@ public:
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(LedgerRPC, app, ripple);
|
||||
BEAST_DEFINE_TESTSUITE(LedgerRPC_XChain, app, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
|
||||
Reference in New Issue
Block a user