From f70ddf9e65419e5c8825708da220bf80f693ce2e Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Wed, 4 Jan 2023 11:36:01 +0000 Subject: [PATCH] add otxn_param hook api --- src/ripple/app/hook/Enum.h | 1 + src/ripple/app/hook/applyHook.h | 4 +++ src/ripple/app/hook/impl/applyHook.cpp | 49 ++++++++++++++++++++++++++ src/test/app/SetHook_test.cpp | 6 ++++ 4 files changed, 60 insertions(+) diff --git a/src/ripple/app/hook/Enum.h b/src/ripple/app/hook/Enum.h index 79d07ce9d..933aae843 100644 --- a/src/ripple/app/hook/Enum.h +++ b/src/ripple/app/hook/Enum.h @@ -545,6 +545,7 @@ namespace hook_api "otxn_generation", "otxn_id", "otxn_type", + "otxn_param", "rollback", "slot", "slot_clear", diff --git a/src/ripple/app/hook/applyHook.h b/src/ripple/app/hook/applyHook.h index e201c60cb..7405660db 100644 --- a/src/ripple/app/hook/applyHook.h +++ b/src/ripple/app/hook/applyHook.h @@ -199,6 +199,8 @@ namespace hook_api DECLARE_HOOK_FUNCTION(int64_t, otxn_id, uint32_t write_ptr, uint32_t write_len, uint32_t flags ); DECLARE_HOOK_FUNCNARG(int64_t, otxn_type ); DECLARE_HOOK_FUNCTION(int64_t, otxn_slot, uint32_t slot_no ); + DECLARE_HOOK_FUNCTION(int64_t, otxn_param, uint32_t write_ptr, uint32_t write_len, + uint32_t read_ptr, uint32_t read_len); DECLARE_HOOK_FUNCTION(int64_t, meta_slot, uint32_t slot_no ); @@ -583,6 +585,8 @@ namespace hook ADD_HOOK_FUNCTION(otxn_id, ctx); ADD_HOOK_FUNCTION(otxn_type, ctx); ADD_HOOK_FUNCTION(otxn_slot, ctx); + ADD_HOOK_FUNCTION(otxn_param, ctx); + ADD_HOOK_FUNCTION(hook_account, ctx); ADD_HOOK_FUNCTION(hook_hash, ctx); ADD_HOOK_FUNCTION(hook_again, ctx); diff --git a/src/ripple/app/hook/impl/applyHook.cpp b/src/ripple/app/hook/impl/applyHook.cpp index 31a885bf9..c3b280c85 100644 --- a/src/ripple/app/hook/impl/applyHook.cpp +++ b/src/ripple/app/hook/impl/applyHook.cpp @@ -4869,6 +4869,55 @@ DEFINE_HOOK_FUNCTION( return double_to_xfl(result); } +DEFINE_HOOK_FUNCTION( + int64_t, + otxn_param, + uint32_t write_ptr, uint32_t write_len, + uint32_t read_ptr, uint32_t read_len ) +{ + HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx, hookCtx on current stack + + if (NOT_IN_BOUNDS(read_ptr, read_len, memory_length)) + return OUT_OF_BOUNDS; + + if (read_len < 1) + return TOO_SMALL; + + if (read_len > 32) + return TOO_BIG; + + if (!applyCtx.tx.isFieldPresent(sfHookParameters)) + return DOESNT_EXIST; + + std::vector paramName { read_ptr + memory, read_ptr + read_len + memory }; + + auto const& params = applyCtx.tx.getFieldArray(sfHookParameters); + + for (auto const& param: params) + { + if (!param.isFieldPresent(sfHookParameterName) || + param.getFieldVL(sfHookParameterName) != paramName) + continue; + + if (!param.isFieldPresent(sfHookParameterValue)) + return DOESNT_EXIST; + + auto const& val = param.getFieldVL(sfHookParameterValue); + if (val.empty()) + return DOESNT_EXIST; + + if (val.size() < write_len) + return TOO_SMALL; + + WRITE_WASM_MEMORY_AND_RETURN( + write_ptr, write_len, + val.data(), val.size(), + memory, memory_length); + } + + return DOESNT_EXIST; +} + DEFINE_HOOK_FUNCTION( int64_t, hook_param, diff --git a/src/test/app/SetHook_test.cpp b/src/test/app/SetHook_test.cpp index 39220e628..7adb585e8 100644 --- a/src/test/app/SetHook_test.cpp +++ b/src/test/app/SetHook_test.cpp @@ -5146,6 +5146,11 @@ public: // RH TODO: test behaviour on emit failure } + void + test_otxn_param() + { + } + void test_slot() { @@ -9703,6 +9708,7 @@ public: test_otxn_id(); // test_otxn_slot(); // test_otxn_type(); // + test_otxn_param(); test_slot(); // test_slot_clear(); //