Fix traces (#6127)

* Fix traces
* More tests for codecov
* Review fixes
* trace float test
* Fix return value for traces
* Remove SuiteJournalSink2
* Add explicit severity
* Move logs to ApplyView
* Add check for output strings
* Merging fix
This commit is contained in:
Olek
2026-01-13 16:38:48 -05:00
committed by GitHub
parent 1c812a6c4d
commit 81cbc91927
7 changed files with 594 additions and 404 deletions

View File

@@ -62,19 +62,23 @@ static ApplyContext
createApplyContext(
test::jtx::Env& env,
OpenView& ov,
beast::Journal j,
STTx const& tx = STTx(ttESCROW_FINISH, [](STObject&) {}))
{
ApplyContext ac{
env.app(),
ov,
tx,
tesSUCCESS,
env.current()->fees().base,
tapNONE,
env.journal};
env.app(), ov, tx, tesSUCCESS, env.current()->fees().base, tapNONE, j};
return ac;
}
static ApplyContext
createApplyContext(
test::jtx::Env& env,
OpenView& ov,
STTx const& tx = STTx(ttESCROW_FINISH, [](STObject&) {}))
{
return createApplyContext(env, ov, env.journal, tx);
}
struct HostFuncImpl_test : public beast::unit_test::suite
{
void
@@ -1912,24 +1916,63 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("trace");
using namespace test::jtx;
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kTrace};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + data.size());
std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
if (BEAST_EXPECT(result.has_value()))
{
BEAST_EXPECT(result.value() == msg.size() + data.size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.find(msg) != std::string::npos);
}
auto const resultHex = hfs.trace(msg, slice, true);
BEAST_EXPECT(resultHex.has_value());
BEAST_EXPECT(resultHex.value() == msg.size() + data.size() * 2);
auto const resultHex = hfs.trace(msg, slice, true);
if (BEAST_EXPECT(resultHex.has_value()))
{
BEAST_EXPECT(resultHex.has_value());
BEAST_EXPECT(resultHex.value() == msg.size() + data.size() * 2);
auto const messages = sink.messages().str();
std::string hex;
hex.reserve(data.size() * 2);
boost::algorithm::hex(
data.begin(), data.end(), std::back_inserter(hex));
BEAST_EXPECT(messages.find(msg) != std::string::npos);
BEAST_EXPECT(messages.find(hex) != std::string::npos);
}
}
{
// logs disabled (trace < error)
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kError};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
BEAST_EXPECT(result && *result == msg.size() + data.size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.empty());
}
}
void
@@ -1938,19 +1981,49 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceNum");
using namespace test::jtx;
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kTrace};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + sizeof(num));
std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
if (BEAST_EXPECT(result.has_value()))
{
BEAST_EXPECT(result.value() == msg.size() + sizeof(num));
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.find(msg) != std::string::npos);
BEAST_EXPECT(
messages.find(std::to_string(num)) != std::string::npos);
}
}
{
// logs disabled
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kError};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
BEAST_EXPECT(result && *result == msg.size() + sizeof(int64_t));
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.empty());
}
}
void
@@ -1959,22 +2032,47 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceAccount");
using namespace test::jtx;
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace account";
// Valid account
{
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kTrace};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace account";
auto const result = hfs.traceAccount(msg, env.master.id());
if (BEAST_EXPECT(result.has_value()))
{
BEAST_EXPECT(
result.value() ==
msg.size() + toBase58(env.master.id()).size());
result.value() == msg.size() + env.master.id().size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.find(msg) != std::string::npos);
BEAST_EXPECT(
messages.find(env.master.human()) != std::string::npos);
}
}
{
// logs disabled
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kError};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace account";
auto const result = hfs.traceAccount(msg, env.master.id());
BEAST_EXPECT(
result && *result == msg.size() + env.master.id().size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.empty());
}
}
@@ -1984,46 +2082,72 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceAmount");
using namespace test::jtx;
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace amount";
STAmount amount = XRP(12345);
{
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kTrace};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace amount";
STAmount amount = XRP(12345);
{
auto const result = hfs.traceAmount(msg, amount);
if (BEAST_EXPECT(result.has_value()))
{
BEAST_EXPECT(*result == msg.size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.find(msg) != std::string::npos);
BEAST_EXPECT(
messages.find(amount.getFullText()) !=
std::string::npos);
}
}
// IOU amount
Account const alice("alice");
env.fund(XRP(1000), alice);
env.close();
STAmount iouAmount = env.master["USD"](100);
{
auto const result = hfs.traceAmount(msg, iouAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(*result == msg.size());
}
// MPT amount
{
auto const mptId = makeMptID(42, env.master.id());
Asset mptAsset = Asset(mptId);
STAmount mptAmount(mptAsset, 123456);
auto const result = hfs.traceAmount(msg, mptAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(*result == msg.size());
}
}
{
// logs disabled
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kError};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace amount";
STAmount amount = XRP(12345);
auto const result = hfs.traceAmount(msg, amount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() == msg.size() + amount.getFullText().size());
}
// IOU amount
Account const alice("alice");
env.fund(XRP(1000), alice);
env.close();
STAmount iouAmount = env.master["USD"](100);
{
auto const result = hfs.traceAmount(msg, iouAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() ==
msg.size() + iouAmount.getFullText().size());
}
// MPT amount
{
auto const mptId = makeMptID(42, env.master.id());
Asset mptAsset = Asset(mptId);
STAmount mptAmount(mptAsset, 123456);
auto const result = hfs.traceAmount(msg, mptAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() ==
msg.size() + mptAmount.getFullText().size());
BEAST_EXPECT(result && *result == msg.size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.empty());
}
}
@@ -2058,28 +2182,51 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("FloatTrace");
using namespace test::jtx;
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace float";
{
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result &&
*result ==
msg.size() + 14 /* error msg size*/ + invalid.size() * 2);
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace float";
{
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result &&
*result == msg.size() + makeSlice(invalid).size());
}
{
auto const result = hfs.traceFloat(msg, makeSlice(floatMaxExp));
BEAST_EXPECT(
result &&
*result == msg.size() + makeSlice(floatMaxExp).size());
}
}
{
auto const result = hfs.traceFloat(msg, makeSlice(floatMaxExp));
// logs disabled
Env env(*this);
OpenView ov{*env.current()};
test::StreamSink sink{beast::severities::kError};
beast::Journal jlog{sink};
ApplyContext ac = createApplyContext(env, ov, jlog);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace float";
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result && *result == msg.size() + 19 /* string represenation*/);
result && *result == msg.size() + makeSlice(invalid).size());
auto const messages = sink.messages().str();
BEAST_EXPECT(messages.empty());
}
}