Skip to content

Commit b2a96bd

Browse files
committed
Fix return value for traces
1 parent eb18e36 commit b2a96bd

File tree

6 files changed

+304
-315
lines changed

6 files changed

+304
-315
lines changed

src/test/app/HostFuncImpl_test.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
19861986
std::string data = "abc";
19871987
auto const slice = Slice(data.data(), data.size());
19881988
auto const result = hfs.trace(msg, slice, false);
1989-
BEAST_EXPECT(result && *result == 0);
1989+
BEAST_EXPECT(result && *result == msg.size() + data.size());
19901990
}
19911991
}
19921992

@@ -2026,7 +2026,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
20262026
std::string msg = "trace number";
20272027
int64_t num = 123456789;
20282028
auto const result = hfs.traceNum(msg, num);
2029-
BEAST_EXPECT(result && *result == 0);
2029+
BEAST_EXPECT(result && *result == msg.size() + sizeof(int64_t));
20302030
}
20312031
}
20322032

@@ -2051,8 +2051,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
20512051
auto const result = hfs.traceAccount(msg, env.master.id());
20522052
if (BEAST_EXPECT(result.has_value()))
20532053
BEAST_EXPECT(
2054-
result.value() ==
2055-
msg.size() + toBase58(env.master.id()).size());
2054+
result.value() == msg.size() + env.master.id().size());
20562055
}
20572056
}
20582057

@@ -2068,7 +2067,8 @@ struct HostFuncImpl_test : public beast::unit_test::suite
20682067
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
20692068
std::string msg = "trace account";
20702069
auto const result = hfs.traceAccount(msg, env.master.id());
2071-
BEAST_EXPECT(result && *result == 0);
2070+
BEAST_EXPECT(
2071+
result && *result == msg.size() + env.master.id().size());
20722072
}
20732073
}
20742074

@@ -2092,9 +2092,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
20922092
{
20932093
auto const result = hfs.traceAmount(msg, amount);
20942094
if (BEAST_EXPECT(result.has_value()))
2095-
BEAST_EXPECT(
2096-
result.value() ==
2097-
msg.size() + amount.getFullText().size());
2095+
BEAST_EXPECT(*result == msg.size());
20982096
}
20992097

21002098
// IOU amount
@@ -2105,9 +2103,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
21052103
{
21062104
auto const result = hfs.traceAmount(msg, iouAmount);
21072105
if (BEAST_EXPECT(result.has_value()))
2108-
BEAST_EXPECT(
2109-
result.value() ==
2110-
msg.size() + iouAmount.getFullText().size());
2106+
BEAST_EXPECT(*result == msg.size());
21112107
}
21122108

21132109
// MPT amount
@@ -2117,9 +2113,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
21172113
STAmount mptAmount(mptAsset, 123456);
21182114
auto const result = hfs.traceAmount(msg, mptAmount);
21192115
if (BEAST_EXPECT(result.has_value()))
2120-
BEAST_EXPECT(
2121-
result.value() ==
2122-
msg.size() + mptAmount.getFullText().size());
2116+
BEAST_EXPECT(*result == msg.size());
21232117
}
21242118
}
21252119

@@ -2137,7 +2131,7 @@ struct HostFuncImpl_test : public beast::unit_test::suite
21372131
std::string msg = "trace amount";
21382132
STAmount amount = XRP(12345);
21392133
auto const result = hfs.traceAmount(msg, amount);
2140-
BEAST_EXPECT(result && *result == 0);
2134+
BEAST_EXPECT(result && *result == msg.size());
21412135
}
21422136
}
21432137

@@ -2187,16 +2181,14 @@ struct HostFuncImpl_test : public beast::unit_test::suite
21872181
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
21882182
BEAST_EXPECT(
21892183
result &&
2190-
*result ==
2191-
msg.size() + 14 /* error msg size*/ +
2192-
invalid.size() * 2);
2184+
*result == msg.size() + makeSlice(invalid).size());
21932185
}
21942186

21952187
{
21962188
auto const result = hfs.traceFloat(msg, makeSlice(floatMaxExp));
21972189
BEAST_EXPECT(
21982190
result &&
2199-
*result == msg.size() + 19 /* string represenation*/);
2191+
*result == msg.size() + makeSlice(floatMaxExp).size());
22002192
}
22012193
}
22022194

@@ -2215,7 +2207,9 @@ struct HostFuncImpl_test : public beast::unit_test::suite
22152207

22162208
{
22172209
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
2218-
BEAST_EXPECT(result && *result == 0);
2210+
BEAST_EXPECT(
2211+
result &&
2212+
*result == msg.size() + makeSlice(invalid).size());
22192213
}
22202214
}
22212215
}

src/test/app/TestHostFunctions.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,12 @@ struct TestHostFunctions : public HostFunctions
383383
Expected<int32_t, HostFunctionError>
384384
trace(std::string_view const& msg, Slice const& data, bool asHex) override
385385
{
386+
auto const ret = msg.size() + data.size() * (asHex ? 2 : 1);
386387
#ifdef DEBUG_OUTPUT
387388
auto& j = std::cerr;
388389
#else
389390
if (!getJournal().active(beast::severities::kTrace))
390-
return 0;
391+
return ret;
391392
auto j = getJournal().trace();
392393
#endif
393394
if (!asHex)
@@ -409,35 +410,37 @@ struct TestHostFunctions : public HostFunctions
409410
j << std::endl;
410411
#endif
411412

412-
return msg.size() + data.size() * (asHex ? 2 : 1);
413+
return ret;
413414
}
414415

415416
Expected<int32_t, HostFunctionError>
416417
traceNum(std::string_view const& msg, int64_t data) override
417418
{
419+
auto const ret = msg.size() + sizeof(data);
418420
#ifdef DEBUG_OUTPUT
419421
auto& j = std::cerr;
420422
#else
421423
if (!getJournal().active(beast::severities::kTrace))
422-
return 0;
424+
return ret;
423425
auto j = getJournal().trace();
424426
#endif
425427
j << "WASM TRACE NUM: " << msg << " " << data;
426428

427429
#ifdef DEBUG_OUTPUT
428430
j << std::endl;
429431
#endif
430-
return msg.size() + sizeof(data);
432+
return ret;
431433
}
432434

433435
Expected<int32_t, HostFunctionError>
434436
traceAccount(std::string_view const& msg, AccountID const& account) override
435437
{
438+
auto const ret = msg.size() + account.size();
436439
#ifdef DEBUG_OUTPUT
437440
auto& j = std::cerr;
438441
#else
439442
if (!getJournal().active(beast::severities::kTrace))
440-
return 0;
443+
return ret;
441444
auto j = getJournal().trace();
442445
#endif
443446
if (!account)
@@ -446,17 +449,23 @@ struct TestHostFunctions : public HostFunctions
446449
auto const accountStr = toBase58(account);
447450

448451
j << "WASM TRACE ACCOUNT: " << msg << " " << accountStr;
449-
return msg.size() + accountStr.size();
452+
453+
#ifdef DEBUG_OUTPUT
454+
j << std::endl;
455+
#endif
456+
457+
return ret;
450458
}
451459

452460
Expected<int32_t, HostFunctionError>
453461
traceFloat(std::string_view const& msg, Slice const& data) override
454462
{
463+
auto const ret = msg.size() + data.size();
455464
#ifdef DEBUG_OUTPUT
456465
auto& j = std::cerr;
457466
#else
458467
if (!getJournal().active(beast::severities::kTrace))
459-
return 0;
468+
return ret;
460469
auto j = getJournal().trace();
461470
#endif
462471
auto const s = floatToString(data);
@@ -465,22 +474,29 @@ struct TestHostFunctions : public HostFunctions
465474
#ifdef DEBUG_OUTPUT
466475
j << std::endl;
467476
#endif
468-
return msg.size() + s.size();
477+
478+
return ret;
469479
}
470480

471481
Expected<int32_t, HostFunctionError>
472482
traceAmount(std::string_view const& msg, STAmount const& amount) override
473483
{
484+
auto const ret = msg.size();
474485
#ifdef DEBUG_OUTPUT
475486
auto& j = std::cerr;
476487
#else
477488
if (!getJournal().active(beast::severities::kTrace))
478-
return 0;
489+
return ret;
479490
auto j = getJournal().trace();
480491
#endif
481492
auto const amountStr = amount.getFullText();
482493
j << "WASM TRACE AMOUNT: " << msg << " " << amountStr;
483-
return msg.size() + amountStr.size();
494+
495+
#ifdef DEBUG_OUTPUT
496+
j << std::endl;
497+
#endif
498+
499+
return ret;
484500
}
485501

486502
Expected<Bytes, HostFunctionError>

src/test/app/Wasm_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ struct Wasm_test : public beast::unit_test::suite
672672
Bytes const wasm(wasmStr.begin(), wasmStr.end());
673673
TestHostFunctions hfs(env, 0);
674674

675-
auto const allowance = 154'545;
675+
auto const allowance = 151'512;
676676
auto re = runEscrowWasm(wasm, hfs, ESCROW_FUNCTION_NAME, {}, allowance);
677677

678678
if (BEAST_EXPECT(re.has_value()))

src/test/app/wasm_fixtures/codecov_tests/src/lib.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub extern "C" fn finish() -> i32 {
248248
account.0.len(),
249249
)
250250
},
251-
47,
251+
(message.len() + 20) as i32,
252252
"trace_account",
253253
);
254254
let amount = &[0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F]; // 95 drops of XRP
@@ -261,7 +261,7 @@ pub extern "C" fn finish() -> i32 {
261261
amount.len(),
262262
)
263263
},
264-
19,
264+
message.len() as i32,
265265
"trace_amount",
266266
);
267267
let amount = &[0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; // 0 drops of XRP
@@ -274,7 +274,7 @@ pub extern "C" fn finish() -> i32 {
274274
amount.len(),
275275
)
276276
},
277-
18,
277+
message.len() as i32,
278278
"trace_amount_zero",
279279
);
280280

@@ -307,32 +307,6 @@ pub extern "C" fn finish() -> i32 {
307307
"get_parent_ledger_hash_len_too_long",
308308
)
309309
});
310-
let message = "testing trace";
311-
check_result(
312-
unsafe {
313-
host::trace_account(
314-
message.as_ptr(),
315-
message.len(),
316-
account.0.as_ptr(),
317-
account.0.len(),
318-
)
319-
},
320-
47,
321-
"trace_account",
322-
);
323-
let amount = &[0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F]; // 95 drops of XRP
324-
check_result(
325-
unsafe {
326-
host::trace_amount(
327-
message.as_ptr(),
328-
message.len(),
329-
amount.as_ptr(),
330-
amount.len(),
331-
)
332-
},
333-
19,
334-
"trace_amount",
335-
);
336310

337311
// ########################################
338312
// Step #3: Test getData[Type] edge cases

0 commit comments

Comments
 (0)