|
18 | 18 | //============================================================================== |
19 | 19 | #include <ripple/app/hook/HookAPI.h> |
20 | 20 | #include <ripple/basics/StringUtilities.h> |
| 21 | +#include <ripple/beast/unit_test/suite.hpp> |
21 | 22 | #include <ripple/json/json_writer.h> |
| 23 | +#include <ripple/protocol/SField.h> |
22 | 24 | #include <ripple/protocol/STAccount.h> |
23 | 25 | #include <limits> |
24 | 26 | #include <test/app/Import_json.h> |
@@ -75,6 +77,84 @@ class HookAPI_test : public beast::unit_test::suite |
75 | 77 | BEAST_EXPECT(true); |
76 | 78 | } |
77 | 79 |
|
| 80 | + void |
| 81 | + test_prepare(FeatureBitset features) |
| 82 | + { |
| 83 | + testcase("Test prepare"); |
| 84 | + using namespace jtx; |
| 85 | + |
| 86 | + auto const alice = Account{"alice"}; |
| 87 | + auto const bob = Account{"bob"}; |
| 88 | + |
| 89 | + using namespace hook_api; |
| 90 | + Env env{*this, features}; |
| 91 | + |
| 92 | + STTx invokeTx = STTx(ttINVOKE, [&](STObject& obj) {}); |
| 93 | + OpenView ov{*env.current()}; |
| 94 | + ApplyContext applyCtx = createApplyContext(env, ov, invokeTx); |
| 95 | + |
| 96 | + STTx const emitInvokeTx = STTx(ttINVOKE, [&](STObject& obj) {}); |
| 97 | + |
| 98 | + { |
| 99 | + // PREREQUISITE_NOT_MET |
| 100 | + auto hookCtx = |
| 101 | + makeStubHookContext(applyCtx, alice.id(), alice.id(), {}); |
| 102 | + auto& api = hookCtx.api(); |
| 103 | + auto tx = emitInvokeTx; |
| 104 | + Serializer s = tx.getSerializer(); |
| 105 | + s.add8(0); // invalid value |
| 106 | + auto const result = api.prepare(s.slice()); |
| 107 | + BEAST_EXPECT(result.error() == PREREQUISITE_NOT_MET); |
| 108 | + } |
| 109 | + |
| 110 | + { |
| 111 | + // Invalid txn |
| 112 | + auto hookCtx = makeStubHookContext( |
| 113 | + applyCtx, |
| 114 | + alice.id(), |
| 115 | + alice.id(), |
| 116 | + { |
| 117 | + .expected_etxn_count = 1, |
| 118 | + }); |
| 119 | + auto& api = hookCtx.api(); |
| 120 | + auto tx = emitInvokeTx; |
| 121 | + Serializer s = tx.getSerializer(); |
| 122 | + s.add8(0); // invalid value |
| 123 | + auto const result = api.prepare(s.slice()); |
| 124 | + BEAST_EXPECT(result.error() == INVALID_ARGUMENT); |
| 125 | + } |
| 126 | + |
| 127 | + { |
| 128 | + // success |
| 129 | + auto hookCtx = makeStubHookContext( |
| 130 | + applyCtx, |
| 131 | + alice.id(), |
| 132 | + alice.id(), |
| 133 | + { |
| 134 | + .expected_etxn_count = 1, |
| 135 | + }); |
| 136 | + auto& api = hookCtx.api(); |
| 137 | + auto tx = emitInvokeTx; |
| 138 | + Serializer s = tx.getSerializer(); |
| 139 | + auto const result = api.prepare(s.slice()); |
| 140 | + BEAST_EXPECT(result.has_value()); |
| 141 | + |
| 142 | + SerialIter sit(Slice(result.value().data(), result.value().size())); |
| 143 | + STObject st(sit, sfGeneric); |
| 144 | + BEAST_EXPECT(st.getFieldAmount(sfFee) > XRPAmount(0)); |
| 145 | + BEAST_EXPECT(st.getFieldU32(sfSequence) == 0); |
| 146 | + BEAST_EXPECT(st.getAccountID(sfAccount) == alice.id()); |
| 147 | + auto const seq = applyCtx.view().info().seq; |
| 148 | + BEAST_EXPECT(st.getFieldU32(sfFirstLedgerSequence) == seq + 1); |
| 149 | + BEAST_EXPECT(st.getFieldU32(sfLastLedgerSequence) == seq + 5); |
| 150 | + BEAST_EXPECT(st.isFieldPresent(sfEmitDetails)); |
| 151 | + |
| 152 | + auto const result2 = |
| 153 | + api.emit(Slice(result.value().data(), result.value().size())); |
| 154 | + BEAST_EXPECT(result2.has_value()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
78 | 158 | void |
79 | 159 | test_emit(FeatureBitset features) |
80 | 160 | { |
@@ -4441,6 +4521,7 @@ class HookAPI_test : public beast::unit_test::suite |
4441 | 4521 | test_rollback(features); |
4442 | 4522 | testGuards(features); |
4443 | 4523 |
|
| 4524 | + test_prepare(features); |
4444 | 4525 | test_emit(features); |
4445 | 4526 | test_etxn_burden(features); |
4446 | 4527 | test_etxn_generation(features); |
|
0 commit comments