Skip to content

Commit 79fdafe

Browse files
authored
Support Cron in util_keylet Hook API (#612)
1 parent 2a10013 commit 79fdafe

File tree

5 files changed

+700
-603
lines changed

5 files changed

+700
-603
lines changed

hook/hookapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define KEYLET_NFT_OFFER 23
3838
#define KEYLET_HOOK_DEFINITION 24
3939
#define KEYLET_HOOK_STATE_DIR 25
40+
#define KEYLET_CRON 26
4041

4142
#define COMPARE_EQUAL 1U
4243
#define COMPARE_LESS 2U

src/ripple/app/hook/Enum.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ enum keylet_code : uint32_t {
278278
NFT_OFFER = 23,
279279
HOOK_DEFINITION = 24,
280280
HOOK_STATE_DIR = 25,
281-
LAST_KLTYPE_V0 = HOOK_DEFINITION,
282-
LAST_KLTYPE_V1 = HOOK_STATE_DIR,
281+
CRON = 26
283282
};
284283
}
285284

src/ripple/app/hook/impl/applyHook.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,17 +2903,6 @@ DEFINE_HOOK_FUNCTION(
29032903
if (write_len < 34)
29042904
return TOO_SMALL;
29052905

2906-
bool const v1 = applyCtx.view().rules().enabled(featureHooksUpdate1);
2907-
2908-
if (keylet_type == 0)
2909-
return INVALID_ARGUMENT;
2910-
2911-
auto const last =
2912-
v1 ? keylet_code::LAST_KLTYPE_V1 : keylet_code::LAST_KLTYPE_V0;
2913-
2914-
if (keylet_type > last)
2915-
return INVALID_ARGUMENT;
2916-
29172906
try
29182907
{
29192908
switch (keylet_type)
@@ -3015,7 +3004,8 @@ DEFINE_HOOK_FUNCTION(
30153004
return serialize_keylet(kl, memory, write_ptr, write_len);
30163005
}
30173006

3018-
// keylets that take 20 byte account id, and 4 byte uint
3007+
// keylets that take 20 byte account id, and (4 byte uint for 32
3008+
// byte hash)
30193009
case keylet_code::OFFER:
30203010
case keylet_code::CHECK:
30213011
case keylet_code::ESCROW:
@@ -3058,6 +3048,33 @@ DEFINE_HOOK_FUNCTION(
30583048
return serialize_keylet(kl, memory, write_ptr, write_len);
30593049
}
30603050

3051+
// keylets that take 20 byte account id, and 4 byte uint
3052+
case keylet_code::CRON: {
3053+
if (!applyCtx.view().rules().enabled(featureCron))
3054+
return INVALID_ARGUMENT;
3055+
3056+
if (a == 0 || b == 0)
3057+
return INVALID_ARGUMENT;
3058+
if (e != 0 || f != 0 || d != 0)
3059+
return INVALID_ARGUMENT;
3060+
3061+
uint32_t read_ptr = a, read_len = b;
3062+
3063+
if (NOT_IN_BOUNDS(read_ptr, read_len, memory_length))
3064+
return OUT_OF_BOUNDS;
3065+
3066+
if (read_len != 20)
3067+
return INVALID_ARGUMENT;
3068+
3069+
ripple::AccountID id = AccountID::fromVoid(memory + read_ptr);
3070+
3071+
uint32_t seq = c;
3072+
3073+
ripple::Keylet kl = ripple::keylet::cron(seq, id);
3074+
3075+
return serialize_keylet(kl, memory, write_ptr, write_len);
3076+
}
3077+
30613078
// keylets that take a 32 byte uint and an 8byte uint64
30623079
case keylet_code::PAGE: {
30633080
if (a == 0 || b == 0)
@@ -3105,6 +3122,9 @@ DEFINE_HOOK_FUNCTION(
31053122
}
31063123

31073124
case keylet_code::HOOK_STATE_DIR: {
3125+
if (!applyCtx.view().rules().enabled(featureHooksUpdate1))
3126+
return INVALID_ARGUMENT;
3127+
31083128
if (a == 0 || b == 0 || c == 0 || d == 0)
31093129
return INVALID_ARGUMENT;
31103130

@@ -3279,7 +3299,7 @@ DEFINE_HOOK_FUNCTION(
32793299
return INTERNAL_ERROR;
32803300
}
32813301

3282-
return NO_SUCH_KEYLET;
3302+
return INVALID_ARGUMENT;
32833303

32843304
HOOK_TEARDOWN();
32853305
}

src/test/app/SetHook_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11147,6 +11147,7 @@ class SetHook0_test : public beast::unit_test::suite
1114711147
#define KEYLET_PAYCHAN 21
1114811148
#define KEYLET_EMITTED_TXN 22
1114911149
#define KEYLET_NFT_OFFER 23
11150+
#define KEYLET_CRON 26
1115011151
#define ASSERT(x)\
1115111152
if (!(x))\
1115211153
rollback((uint32_t)#x, sizeof(#x), __LINE__);
@@ -11209,6 +11210,9 @@ class SetHook0_test : public beast::unit_test::suite
1120911210
// Test min size
1121011211
ASSERT(util_keylet((uint32_t)buf, 33, KEYLET_SKIP, 0,0,0,0,0,0) == TOO_SMALL);
1121111212

11213+
// Invalid keylet type
11214+
ASSERT(util_keylet((uint32_t)buf, 34, 0, 0,0,0,0,0,0) == INVALID_ARGUMENT);
11215+
ASSERT(util_keylet((uint32_t)buf, 34, 0x99999999, 0,0,0,0,0,0) == INVALID_ARGUMENT);
1121211216

1121311217
// Test one of each type
1121411218
ASSERT(34 == (e=util_keylet(buf, 34, KEYLET_HOOK,
@@ -11651,6 +11655,17 @@ class SetHook0_test : public beast::unit_test::suite
1165111655
0,0
1165211656
)));
1165311657

11658+
ASSERT(34 == (e=util_keylet(buf, 34, KEYLET_CRON, SBUF(a), 1, 0, 0, 0)));
11659+
{
11660+
uint8_t ans[] =
11661+
{
11662+
0x00U,0x41U,0xF7U,0xB6U,0x45U,0x43U,0x61U,0x87U,0xCCU,0x61U,
11663+
0x00U,0x00U,0x00U,0x01U,0x0AU,0x45U,0x80U,0x75U,0x7CU,0xDAU,
11664+
0xD9U,0x16U,0x7EU,0xEEU,0xC1U,0x3CU,0x6CU,0x15U,0xD5U,0x17U,
11665+
0xE2U,0x72U,0x9EU,0xC8
11666+
};
11667+
ASSERT_KL_EQ(ans);
11668+
}
1165411669
accept(0,0,0);
1165511670
}
1165611671
)[test.hook]"];

0 commit comments

Comments
 (0)