Files
xahaud/src/ripple/protocol/impl/STVar.cpp
Scott Determan 6de5de02cb 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>
2025-06-17 12:15:52 +09:00

249 lines
6.4 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/protocol/impl/STVar.h>
#include <ripple/basics/contract.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STArray.h>
#include <ripple/protocol/STBase.h>
#include <ripple/protocol/STBitString.h>
#include <ripple/protocol/STBlob.h>
#include <ripple/protocol/STInteger.h>
#include <ripple/protocol/STIssue.h>
#include <ripple/protocol/STObject.h>
#include <ripple/protocol/STPathSet.h>
#include <ripple/protocol/STVector256.h>
#include <ripple/protocol/STXChainBridge.h>
#include <ripple/protocol/XChainAttestations.h>
namespace ripple {
namespace detail {
defaultObject_t defaultObject;
nonPresentObject_t nonPresentObject;
//------------------------------------------------------------------------------
STVar::~STVar()
{
destroy();
}
STVar::STVar(STVar const& other)
{
if (other.p_ != nullptr)
p_ = other.p_->copy(max_size, &d_);
}
STVar::STVar(STVar&& other)
{
if (other.on_heap())
{
p_ = other.p_;
other.p_ = nullptr;
}
else
{
p_ = other.p_->move(max_size, &d_);
}
}
STVar&
STVar::operator=(STVar const& rhs)
{
if (&rhs != this)
{
destroy();
if (rhs.p_)
p_ = rhs.p_->copy(max_size, &d_);
else
p_ = nullptr;
}
return *this;
}
STVar&
STVar::operator=(STVar&& rhs)
{
if (&rhs != this)
{
destroy();
if (rhs.on_heap())
{
p_ = rhs.p_;
rhs.p_ = nullptr;
}
else
{
p_ = rhs.p_->move(max_size, &d_);
}
}
return *this;
}
STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
{
}
STVar::STVar(nonPresentObject_t, SField const& name)
: STVar(STI_NOTPRESENT, name)
{
}
STVar::STVar(SerialIter& sit, SField const& name, int depth)
{
if (depth > 10)
Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
switch (name.fieldType)
{
case STI_NOTPRESENT:
construct<STBase>(name);
return;
case STI_UINT8:
construct<STUInt8>(sit, name);
return;
case STI_UINT16:
construct<STUInt16>(sit, name);
return;
case STI_UINT32:
construct<STUInt32>(sit, name);
return;
case STI_UINT64:
construct<STUInt64>(sit, name);
return;
case STI_AMOUNT:
construct<STAmount>(sit, name);
return;
case STI_UINT128:
construct<STUInt128>(sit, name);
return;
case STI_UINT160:
construct<STUInt160>(sit, name);
return;
case STI_UINT256:
construct<STUInt256>(sit, name);
return;
case STI_VECTOR256:
construct<STVector256>(sit, name);
return;
case STI_VL:
construct<STBlob>(sit, name);
return;
case STI_ACCOUNT:
construct<STAccount>(sit, name);
return;
case STI_PATHSET:
construct<STPathSet>(sit, name);
return;
case STI_OBJECT:
construct<STObject>(sit, name, depth);
return;
case STI_ARRAY:
construct<STArray>(sit, name, depth);
return;
case STI_ISSUE:
construct<STIssue>(sit, name);
return;
case STI_XCHAIN_BRIDGE:
construct<STXChainBridge>(sit, name);
return;
default:
Throw<std::runtime_error>("Unknown object type");
}
}
STVar::STVar(SerializedTypeID id, SField const& name)
{
assert((id == STI_NOTPRESENT) || (id == name.fieldType));
switch (id)
{
case STI_NOTPRESENT:
construct<STBase>(name);
return;
case STI_UINT8:
construct<STUInt8>(name);
return;
case STI_UINT16:
construct<STUInt16>(name);
return;
case STI_UINT32:
construct<STUInt32>(name);
return;
case STI_UINT64:
construct<STUInt64>(name);
return;
case STI_AMOUNT:
construct<STAmount>(name);
return;
case STI_UINT128:
construct<STUInt128>(name);
return;
case STI_UINT160:
construct<STUInt160>(name);
return;
case STI_UINT256:
construct<STUInt256>(name);
return;
case STI_VECTOR256:
construct<STVector256>(name);
return;
case STI_VL:
construct<STBlob>(name);
return;
case STI_ACCOUNT:
construct<STAccount>(name);
return;
case STI_PATHSET:
construct<STPathSet>(name);
return;
case STI_OBJECT:
construct<STObject>(name);
return;
case STI_ARRAY:
construct<STArray>(name);
return;
case STI_ISSUE:
construct<STIssue>(name);
return;
case STI_XCHAIN_BRIDGE:
construct<STXChainBridge>(name);
return;
default:
Throw<std::runtime_error>("Unknown object type");
}
}
void
STVar::destroy()
{
if (on_heap())
delete p_;
else
p_->~STBase();
p_ = nullptr;
}
} // namespace detail
} // namespace ripple