From c83bfe19878bf4b7b565a35360495cea7533fd4e Mon Sep 17 00:00:00 2001 From: tequ Date: Mon, 6 Oct 2025 13:49:07 +0900 Subject: [PATCH] add slot_type API --- src/ripple/app/hook/HookAPI.h | 3 ++- src/ripple/app/hook/impl/HookAPI.cpp | 33 ++++++++++++++++++++++++- src/ripple/app/hook/impl/applyHook.cpp | 34 ++++++++------------------ 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/ripple/app/hook/HookAPI.h b/src/ripple/app/hook/HookAPI.h index 05c3f1cab..96f2d6df2 100644 --- a/src/ripple/app/hook/HookAPI.h +++ b/src/ripple/app/hook/HookAPI.h @@ -254,7 +254,8 @@ public: slot_subfield(uint32_t parent_slot, uint32_t field_id, uint32_t new_slot) const; - // slot_type + Expected, HookReturnCode> + slot_type(uint32_t slot_no, uint32_t flags) const; Expected slot_float(uint32_t slot_no) const; diff --git a/src/ripple/app/hook/impl/HookAPI.cpp b/src/ripple/app/hook/impl/HookAPI.cpp index f2f8f0a56..fc1cd1dd5 100644 --- a/src/ripple/app/hook/impl/HookAPI.cpp +++ b/src/ripple/app/hook/impl/HookAPI.cpp @@ -2353,7 +2353,38 @@ HookAPI::slot_subfield( } } -// slot_type +Expected, HookReturnCode> +HookAPI::slot_type(uint32_t slot_no, uint32_t flags) const +{ + if (hookCtx.slot.find(slot_no) == hookCtx.slot.end()) + return Unexpected(DOESNT_EXIST); + + if (hookCtx.slot[slot_no].entry == 0) + return Unexpected(INTERNAL_ERROR); + try + { + ripple::STBase& obj = const_cast( + *hookCtx.slot[slot_no].entry); //.downcast(); + if (flags == 0) + return obj; + + // this flag is for use with an amount field to determine if the amount + // is native (xrp) + if (flags == 1) + { + if (obj.getSType() != STI_AMOUNT) + return Unexpected(NOT_AN_AMOUNT); + return const_cast(*hookCtx.slot[slot_no].entry) + .downcast(); + } + + return Unexpected(INVALID_ARGUMENT); + } + catch (const std::bad_cast& e) + { + return Unexpected(INTERNAL_ERROR); + } +} Expected HookAPI::slot_float(uint32_t slot_no) const diff --git a/src/ripple/app/hook/impl/applyHook.cpp b/src/ripple/app/hook/impl/applyHook.cpp index 50466e2e9..dc4845b37 100644 --- a/src/ripple/app/hook/impl/applyHook.cpp +++ b/src/ripple/app/hook/impl/applyHook.cpp @@ -2131,34 +2131,20 @@ DEFINE_HOOK_FUNCTION(int64_t, slot_type, uint32_t slot_no, uint32_t flags) HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx, // hookCtx on current stack - if (hookCtx.slot.find(slot_no) == hookCtx.slot.end()) - return DOESNT_EXIST; + hook::HookAPI api(hookCtx); + auto const result = api.slot_type(slot_no, flags); + if (!result) + return result.error(); - if (hookCtx.slot[slot_no].entry == 0) - return INTERNAL_ERROR; - try + if (flags == 0) { - ripple::STBase& obj = const_cast( - *hookCtx.slot[slot_no].entry); //.downcast(); - if (flags == 0) - return obj.getFName().fieldCode; - - // this flag is for use with an amount field to determine if the amount - // is native (xrp) - if (flags == 1) - { - if (obj.getSType() != STI_AMOUNT) - return NOT_AN_AMOUNT; - return const_cast(*hookCtx.slot[slot_no].entry) - .downcast() - .native(); - } - - return INVALID_ARGUMENT; + auto const base = std::get<0>(*result); + return base.getFName().fieldCode; } - catch (const std::bad_cast& e) + else { - return INTERNAL_ERROR; + auto const amount = std::get<1>(*result); + return amount.native(); } HOOK_TEARDOWN();