Tidy up jtx:

* Tidy declaration order
* Remove AccountInfo
* Remove Env autofill settings
This commit is contained in:
Vinnie Falco
2015-06-13 07:38:07 -07:00
committed by Nik Bougalis
parent 9dd08e4dab
commit eb709f415b
10 changed files with 53 additions and 159 deletions

View File

@@ -37,7 +37,6 @@
#include <ripple/protocol/STTx.h>
#include <beast/is_call_possible.h>
#include <beast/unit_test/suite.h>
#include <boost/logic/tribool.hpp>
#include <beast/cxx14/type_traits.h> // <type_traits>
#include <beast/cxx14/utility.h> // <utility>
#include <functional>
@@ -49,36 +48,6 @@ namespace ripple {
namespace test {
namespace jtx {
/** A view to an account's account root. */
class AccountInfo
{
private:
Account account_;
std::shared_ptr<Ledger> ledger_;
std::shared_ptr<SLE const> root_;
public:
AccountInfo(Account const& account,
std::shared_ptr<Ledger> ledger)
: account_(account)
, ledger_(std::move(ledger))
, root_(ledger_->fetch(
getAccountRootIndex(account.id())))
{
}
STAmount
balance (Issue const& issue) const;
std::uint32_t
seq() const;
std::uint32_t
flags() const;
};
//------------------------------------------------------------------------------
namespace detail {
#ifdef _MSC_VER
@@ -164,10 +133,10 @@ public:
/** Returns the Account given the AccountID. */
/** @{ */
Account const&
lookup (std::string const& base58ID) const;
lookup (AccountID const& id) const;
Account const&
lookup (AccountID const& id) const;
lookup (std::string const& base58ID) const;
/** @} */
/** Returns the XRP balance on an account.
@@ -204,26 +173,6 @@ public:
std::shared_ptr<SLE const>
le (uint256 const& key) const;
/** Set the fee autofill setting. */
void auto_fee (bool value)
{
fill_fee_ = value;
}
/** Set the sequence number autofill setting. */
void auto_seq (bool value)
{
fill_seq_ = value;
}
/** Set the signature autofill setting.
@note autofill multisigning is not supported
*/
void auto_sig (bool value)
{
fill_sig_ = value;
}
/** Create a JTx from parameters. */
template <class JsonValue,
class... FN>
@@ -468,10 +417,6 @@ protected:
// Map of account IDs to Account
std::unordered_map<
AccountID, Account> map_;
bool fill_fee_ = true;
bool fill_seq_ = true;
bool fill_sig_ = true;
};
} // jtx

View File

@@ -23,7 +23,6 @@
#include <ripple/test/jtx/requires.h>
#include <ripple/json/json_value.h>
#include <ripple/protocol/TER.h>
#include <boost/logic/tribool.hpp>
#include <functional>
namespace ripple {
@@ -32,17 +31,15 @@ namespace jtx {
class Env;
BOOST_TRIBOOL_THIRD_STATE(use_default)
/** Execution context for applying a JSON transaction.
This augments the transaction with various settings.
*/
struct JTx
{
Json::Value jv;
boost::tribool fill_fee = boost::logic::indeterminate;
boost::tribool fill_seq = boost::logic::indeterminate;
boost::tribool fill_sig = boost::logic::indeterminate;
bool fill_fee = true;
bool fill_seq = true;
bool fill_sig = true;
std::function<void(Env&, JTx&)> signer;
requires_t requires;
TER ter = tesSUCCESS;

View File

@@ -23,7 +23,7 @@
#include <ripple/test/jtx/Env.h>
#include <ripple/test/jtx/tags.h>
#include <ripple/protocol/STAmount.h>
#include <boost/logic/tribool.hpp>
#include <boost/optional.hpp>
namespace ripple {
namespace test {
@@ -33,28 +33,26 @@ namespace jtx {
class fee
{
private:
STAmount v_;
boost::tribool b_ =
boost::logic::indeterminate;
bool manual_ = true;
boost::optional<STAmount> amount_;
public:
explicit
fee (autofill_t)
: b_(true)
: manual_(false)
{
}
explicit
fee (none_t)
: b_(false)
{
}
explicit
fee (STAmount const& v)
: v_(v)
fee (STAmount const& amount)
: amount_(amount)
{
if (! isXRP(v_))
if (! isXRP(*amount_))
throw std::runtime_error(
"fee: not XRP");
}

View File

@@ -67,16 +67,6 @@ Env::memoize (Account const& account)
map_.emplace(account.id(), account);
}
Account const&
Env::lookup (std::string const& base58ID) const
{
RippleAddress ra;
if (! ra.setAccountID(base58ID))
throw std::runtime_error(
"Env::lookup: invalid account ID");
return lookup(ra.getAccountID());
}
Account const&
Env::lookup (AccountID const& id) const
{
@@ -87,6 +77,16 @@ Env::lookup (AccountID const& id) const
return iter->second;
}
Account const&
Env::lookup (std::string const& base58ID) const
{
RippleAddress ra;
if (! ra.setAccountID(base58ID))
throw std::runtime_error(
"Env::lookup: invalid account ID");
return lookup(ra.getAccountID());
}
PrettyAmount
Env::balance (Account const& account) const
{
@@ -130,24 +130,14 @@ Env::seq (Account const& account) const
std::shared_ptr<SLE const>
Env::le (Account const& account) const
{
// VFALCO NOTE This hack should be removed
// when fetch returns shared_ptr again
auto const st = ledger->fetch(
return ledger->fetch(
getAccountRootIndex(account.id()));
if (! st)
return nullptr;
return std::make_shared<SLE const>(*st);
}
std::shared_ptr<SLE const>
Env::le (uint256 const& key) const
{
// VFALCO NOTE This hack should be removed
// when fetch returns shared_ptr again
auto const st = ledger->fetch(key);
if (! st)
return nullptr;
return std::make_shared<SLE const>(*st);
return ledger->fetch(key);
}
void
@@ -229,21 +219,13 @@ void
Env::autofill_sig (JTx& jt)
{
auto& jv = jt.jv;
auto const should = [](boost::tribool v, bool b)
{
if (boost::indeterminate(v))
return b;
return bool(v);
};
if (jt.signer)
jt.signer(*this, jt);
else if(should(jt.fill_sig, fill_sig_))
else if(jt.fill_sig)
{
auto const account =
lookup(jv[jss::Account].asString());
auto const ar =
ledger->fetch(getAccountRootIndex(account));
auto const ar = le(account);
if (ar->isFieldPresent(sfRegularKey))
jtx::sign(jv, lookup(
ar->getFieldAccount160(sfRegularKey)));
@@ -256,19 +238,10 @@ void
Env::autofill (JTx& jt)
{
auto& jv = jt.jv;
auto const should = [](boost::tribool v, bool b)
{
if (boost::indeterminate(v))
return b;
return bool(v);
};
if(should(jt.fill_fee, fill_fee_))
if(jt.fill_fee)
jtx::fill_fee(jv, *ledger);
if(should(jt.fill_seq, fill_seq_))
if(jt.fill_seq)
jtx::fill_seq(jv, *ledger);
// Must come last
autofill_sig(jt);
}

View File

@@ -165,13 +165,6 @@ public:
env.require(nflags("xavier", asfDefaultRipple));
env.fund(n, "yana");
env.require(flags("yana", asfDefaultRipple));
// fund always autofills
env.auto_fee(false);
env.auto_seq(false);
env.auto_sig(false);
env.fund(n, "zeke");
env.require(balance("zeke", n));
}
// trust
@@ -179,13 +172,6 @@ public:
Env env(*this);
env.fund(n, "alice", "bob", gw);
env(trust("alice", USD(100)), require(lines("alice", 1)));
// trust always autofills
env.auto_fee(false);
env.auto_seq(false);
env.auto_sig(false);
env.trust(USD(100), "bob");
env.require(lines("bob", 1));
}
// balance
@@ -215,7 +201,6 @@ public:
{
Env env(*this);
env.fund(n, "alice");
env.auto_fee(false);
env.require(balance("alice", n));
env(noop("alice"), fee(1), ter(telINSUF_FEE_P));
env(noop("alice"), seq(none), ter(temMALFORMED));
@@ -223,9 +208,7 @@ public:
env(noop("alice"), fee(none), ter(temMALFORMED));
env(noop("alice"), sig(none), ter(temMALFORMED));
env(noop("alice"), fee(autofill));
env.auto_seq(false);
env(noop("alice"), fee(autofill), seq(autofill));
env.auto_sig(false);
env(noop("alice"), fee(autofill), seq(autofill), sig(autofill));
}
}

View File

@@ -28,11 +28,12 @@ namespace jtx {
void
fee::operator()(Env const&, JTx& jt) const
{
if (boost::indeterminate(b_))
if (! manual_)
return;
jt.fill_fee = false;
if (amount_)
jt[jss::Fee] =
v_.getJson(0);
else
jt.fill_fee = b_;
amount_->getJson(0);
}
} // jtx

View File

@@ -28,10 +28,11 @@ namespace jtx {
void
seq::operator()(Env const&, JTx& jt) const
{
if (boost::indeterminate(b_))
jt[jss::Sequence] = v_;
else
jt.fill_seq = b_;
if (! manual_)
return;
jt.fill_seq = false;
if (num_)
jt[jss::Sequence] = *num_;
}
} // jtx

View File

@@ -28,19 +28,18 @@ namespace jtx {
void
sig::operator()(Env const&, JTx& jt) const
{
if(boost::indeterminate(b_))
if (! manual_)
return;
jt.fill_sig = false;
if(account_)
{
// VFALCO Inefficient pre-C++14
auto const account = account_;
auto const account = *account_;
jt.signer = [account](Env&, JTx& jt)
{
jtx::sign(jt.jv, account);
};
}
else
{
jt.fill_sig = b_;
}
}
} // jtx

View File

@@ -22,7 +22,7 @@
#include <ripple/test/jtx/Env.h>
#include <ripple/test/jtx/tags.h>
#include <boost/logic/tribool.hpp>
#include <boost/optional.hpp>
namespace ripple {
namespace test {
@@ -32,26 +32,24 @@ namespace jtx {
struct seq
{
private:
std::uint32_t v_;
boost::tribool b_ =
boost::logic::indeterminate;
bool manual_ = true;
boost::optional<std::uint32_t> num_;
public:
explicit
seq (autofill_t)
: b_(true)
: manual_(false)
{
}
explicit
seq (none_t)
: b_(false)
{
}
explicit
seq (std::uint32_t v)
: v_(v)
seq (std::uint32_t num)
: num_(num)
{
}

View File

@@ -21,6 +21,7 @@
#define RIPPLE_TEST_JTX_SIG_H_INCLUDED
#include <ripple/test/jtx/Env.h>
#include <boost/optional.hpp>
namespace ripple {
namespace test {
@@ -32,20 +33,18 @@ namespace jtx {
class sig
{
private:
Account account_;
boost::tribool b_ =
boost::logic::indeterminate;
bool manual_ = true;
boost::optional<Account> account_;
public:
explicit
sig (autofill_t)
: b_(true)
: manual_(false)
{
}
explicit
sig (none_t)
: b_(false)
{
}