From 98d39db3056bc6e1d2b95ef689b280310f622cc3 Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Tue, 30 Apr 2024 10:17:36 +1000 Subject: [PATCH] otxnid, trace apis for js hooks --- src/ripple/app/hook/applyHook.h | 21 +++++++- src/ripple/app/hook/impl/applyHook.cpp | 74 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/ripple/app/hook/applyHook.h b/src/ripple/app/hook/applyHook.h index 253a906e9..8df5a174b 100644 --- a/src/ripple/app/hook/applyHook.h +++ b/src/ripple/app/hook/applyHook.h @@ -379,6 +379,14 @@ DECLARE_WASM_FUNCTION( uint32_t dread_ptr, uint32_t dread_len, uint32_t as_hex); + +DECLARE_JS_FUNCTION( + JSValue, + trace, + JSValue msg, + JSValue data, + JSValue as_hex); + DECLARE_WASM_FUNCTION( int64_t, trace_num, @@ -406,6 +414,12 @@ DECLARE_WASM_FUNCTION( uint32_t write_ptr, uint32_t write_len, uint32_t flags); + +DECLARE_JS_FUNCTION( + JSValue, + otxn_id, + flags); + DECLARE_WASM_FUNCNARG(int64_t, otxn_type); DECLARE_WASM_FUNCTION(int64_t, otxn_slot, uint32_t slot_no); DECLARE_WASM_FUNCTION( @@ -960,7 +974,8 @@ public: JS_SetRuntimeOpaque(rt, hookCtx); ADD_JS_FUNCTION(accept, ctx); - /*ADD_JS_FUNCTION(rollback, ctx); + ADD_JS_FUNCTION(rollback, ctx); + /* ADD_JS_FUNCTION(util_raddr, ctx); ADD_JS_FUNCTION(util_accid, ctx); ADD_JS_FUNCTION(util_verify, ctx); @@ -1001,7 +1016,9 @@ public: ADD_JS_FUNCTION(otxn_burden, ctx); ADD_JS_FUNCTION(otxn_generation, ctx); ADD_JS_FUNCTION(otxn_field, ctx); + */ ADD_JS_FUNCTION(otxn_id, ctx); + /* ADD_JS_FUNCTION(otxn_type, ctx); ADD_JS_FUNCTION(otxn_slot, ctx); ADD_JS_FUNCTION(otxn_param, ctx); @@ -1036,7 +1053,9 @@ public: ADD_JS_FUNCTION(slot_type, ctx); ADD_JS_FUNCTION(slot_float, ctx); + */ ADD_JS_FUNCTION(trace, ctx); + /* ADD_JS_FUNCTION(trace_num, ctx); ADD_JS_FUNCTION(trace_float, ctx); diff --git a/src/ripple/app/hook/impl/applyHook.cpp b/src/ripple/app/hook/impl/applyHook.cpp index d90b7f663..a149f11a1 100644 --- a/src/ripple/app/hook/impl/applyHook.cpp +++ b/src/ripple/app/hook/impl/applyHook.cpp @@ -1318,6 +1318,58 @@ DEFINE_WASM_FUNCTION( WASM_HOOK_TEARDOWN(); } +DEFINE_JS_FUNCTION( + int64_t, + trace, + JSValue msg, + JSValue data, + JSValue as_hex) +{ + + JS_HOOK_SETUP(); + + std::string out; + if (JS_IsString(msg)) + { + // RH TODO: check if there's a way to ensure the string isn't arbitrarily long before + // calling ToCStringLen + size_t len; + const char* cstr = JS_ToCStringLen(ctx, &len, msg); + if (len > 256) + len = 256; + out = std::string(cstr, len); + JS_FreeCString(ctx, cstr); + } + + + out += ": "; + + if (JS_IsBool(as_hex) && !!JS_ToBool(ctx, as_hex)) + { + // RH TODO + } + else + { + JSValue sdata = JS_JSONStringify(ctx, data, JS_UNDEFINED, JS_UNDEFINED); + size_t len; + const char* cstr = JS_ToCStringLen(ctx, &len, sdata); + if (len > 1023) + len = 1023; + out += std::string(cstr, len); + JS_FreeCString(ctx, cstr); + } + + + if (out.size() > 0) + j.trace() << "HookTrace[" << HC_ACC() << "]: " + << out; + + return JS_NewInt64(ctx, 0); +// return JS_NewString(ctx, out.c_str()); + + JS_HOOK_TEARDOWN(); +} + DEFINE_WASM_FUNCTION( int64_t, trace, @@ -2251,6 +2303,28 @@ DEFINE_WASM_FUNCTION( WASM_HOOK_TEARDOWN(); } +DEFINE_JS_FUNCTION( + JSValue, + otxn_id, + JSValue flags_in) +{ + JS_HOOK_SETUP(); + + int64_t flags = 0; + + if (JS_IsNumber(flags_in)) + JS_ToInt64(ctx, &flags, flags_in); + + auto const& txID = + (hookCtx.emitFailure && !flags + ? applyCtx.tx.getFieldH256(sfTransactionHash) + : applyCtx.tx.getTransactionID()); + + return JS_NewArrayBufferCopy(ctx, txID.data(), txID.size()); + + JS_HOOK_TEARDOWN(); +} + // Return the tt (Transaction Type) numeric code of the originating transaction DEFINE_WASM_FUNCNARG(int64_t, otxn_type) {