Skip to content

Commit

Permalink
add autofill + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 committed Mar 21, 2023
1 parent b03d2e2 commit 3f63e10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
27 changes: 17 additions & 10 deletions src/test/app/NetworkID_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class NetworkID_test : public beast::unit_test::suite
void
testNetworkID()
{
testcase(
"Require txn NetworkID to be specified (or not) depending on the "
"network ID of the node");
testcase("network_id");
using namespace jtx;

auto const alice = Account{"alice"};
Expand All @@ -68,14 +66,9 @@ class NetworkID_test : public beast::unit_test::suite
jv[jss::Destination] = alice.human();
jv[jss::TransactionType] = "Payment";
jv[jss::Amount] = "10000000000";
if (env.app().config().NETWORK_ID > 1024)
jv[jss::NetworkID] =
std::to_string(env.app().config().NETWORK_ID);

env(jv, fee(1000), sig(env.master));
}

// run tx
env(jv, fee(1000), ter(expectedOutcome));
env.close();
};
Expand Down Expand Up @@ -128,11 +121,25 @@ class NetworkID_test : public beast::unit_test::suite
test::jtx::Env env{*this, makeNetworkConfig(1025)};
BEAST_EXPECT(env.app().config().NETWORK_ID == 1025);

// try to submit a txn without network id, this should not work
{
env.fund(XRP(200), alice);
// try to submit a txn without network id, this should not work
Json::Value jvn;
jvn[jss::Account] = alice.human();
jvn[jss::TransactionType] = jss::AccountSet;
jvn[jss::Fee] = to_string(env.current()->fees().base);
jvn[jss::Sequence] = env.seq(alice);
jvn[jss::LastLedgerSequence] = env.current()->info().seq + 2;
auto jt = env.jtnofill(jvn);
Serializer s;
jt.stx->add(s);
BEAST_EXPECT(env.rpc("submit", strHex(s.slice()))[jss::result][jss::engine_result] == "telREQUIRES_NETWORK_ID");
env.close();
}

Json::Value jv;
jv[jss::Account] = alice.human();
jv[jss::TransactionType] = jss::AccountSet;
runTx(env, jv, telREQUIRES_NETWORK_ID);

// try to submit with wrong network id
jv[jss::NetworkID] = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/test/jtx/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ class Env
return jt;
}

/** Create a JTx from parameters. */
template <class JsonValue, class... FN>
JTx
jtnofill(JsonValue&& jv, FN const&... fN)
{
JTx jt(std::forward<JsonValue>(jv));
invoke(jt, fN...);
autofill_sig(jt);
jt.stx = st(jt);
return jt;
}

/** Create JSON from parameters.
This will apply funclets and autofill.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/test/jtx/impl/Env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,11 @@ Env::autofill(JTx& jt)
jtx::fill_fee(jv, *current());
if (jt.fill_seq)
jtx::fill_seq(jv, *current());

uint32_t networkID = app().config().NETWORK_ID;
if (networkID > 1024)
jv[jss::NetworkID] = networkID;

if (!jv.isMember(jss::NetworkID) && networkID > 1024)
jv[jss::NetworkID] = std::to_string(networkID);
// Must come last
try
{
Expand Down

0 comments on commit 3f63e10

Please sign in to comment.