Skip to content

Commit 38c7a27

Browse files
authored
clean up WASM functions a bit (#5628)
1 parent 58741d2 commit 38c7a27

File tree

4 files changed

+114
-88
lines changed

4 files changed

+114
-88
lines changed

src/test/app/Wasm_test.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,19 @@ struct Wasm_test : public beast::unit_test::suite
141141

142142
Env env{*this};
143143
TestLedgerDataProvider hf(&env);
144-
std::string const funcName("finish");
145144

146145
std::vector<WasmImportFunc> imports;
147146
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hf, 33);
148147
auto& engine = WasmEngine::instance();
149148

150149
auto re = engine.run(
151-
wasm, funcName, {}, imports, &hf, 1'000'000, env.journal);
150+
wasm,
151+
ESCROW_FUNCTION_NAME,
152+
{},
153+
imports,
154+
&hf,
155+
1'000'000,
156+
env.journal);
152157

153158
// code takes 11 gas + 1 getLedgerSqn call
154159
if (BEAST_EXPECT(re.has_value()))
@@ -160,7 +165,8 @@ struct Wasm_test : public beast::unit_test::suite
160165
env.close();
161166

162167
// empty module - run the same instance
163-
re = engine.run({}, funcName, {}, imports, &hf, 1'000'000, env.journal);
168+
re = engine.run(
169+
{}, ESCROW_FUNCTION_NAME, {}, imports, &hf, 1'000'000, env.journal);
164170

165171
// code takes 22 gas + 2 getLedgerSqn calls
166172
if (BEAST_EXPECT(re.has_value()))
@@ -327,10 +333,9 @@ struct Wasm_test : public beast::unit_test::suite
327333
"6d756c746976616c7565";
328334
auto wasmStr = boost::algorithm::unhex(std::string(badWasmHex));
329335
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
330-
std::string funcName("finish");
331336

332-
auto const re =
333-
preflightEscrowWasm(wasm, funcName, {}, &hfs, env.journal);
337+
auto const re = preflightEscrowWasm(
338+
wasm, ESCROW_FUNCTION_NAME, {}, &hfs, env.journal);
334339
BEAST_EXPECT(!isTesSuccess(re));
335340
}
336341
}
@@ -348,8 +353,8 @@ struct Wasm_test : public beast::unit_test::suite
348353
Env env{*this};
349354
{
350355
TestHostFunctions nfs(env, 0);
351-
std::string funcName("finish");
352-
auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100'000);
356+
auto re =
357+
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
353358
if (BEAST_EXPECT(re.has_value()))
354359
{
355360
BEAST_EXPECT(re->result && (re->cost == 41'132));
@@ -369,8 +374,8 @@ struct Wasm_test : public beast::unit_test::suite
369374
}
370375
};
371376
BadTestHostFunctions nfs(env);
372-
std::string funcName("finish");
373-
auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100000);
377+
auto re =
378+
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100000);
374379
BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 5831));
375380
// std::cout << "bad case (access nonexistent field) result "
376381
// << re.error() << std::endl;
@@ -389,8 +394,8 @@ struct Wasm_test : public beast::unit_test::suite
389394
}
390395
};
391396
BadTestHostFunctions nfs(env);
392-
std::string funcName("finish");
393-
auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 100'000);
397+
auto re =
398+
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
394399
BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 5831));
395400
// std::cout << "bad case (more than MAX_PAGES) result "
396401
// << re.error() << std::endl;
@@ -431,7 +436,6 @@ struct Wasm_test : public beast::unit_test::suite
431436
{
432437
auto wasmStr = boost::algorithm::unhex(ledgerSqnHex);
433438
Bytes wasm(wasmStr.begin(), wasmStr.end());
434-
std::string const funcName("finish");
435439
TestLedgerDataProvider ledgerDataProvider(&env);
436440

437441
std::vector<WasmImportFunc> imports;
@@ -441,7 +445,13 @@ struct Wasm_test : public beast::unit_test::suite
441445
auto& engine = WasmEngine::instance();
442446

443447
auto re = engine.run(
444-
wasm, funcName, {}, imports, nullptr, 1'000'000, env.journal);
448+
wasm,
449+
ESCROW_FUNCTION_NAME,
450+
{},
451+
imports,
452+
nullptr,
453+
1'000'000,
454+
env.journal);
445455

446456
// expected import not provided
447457
BEAST_EXPECT(!re);
@@ -453,8 +463,6 @@ struct Wasm_test : public beast::unit_test::suite
453463
{
454464
testcase("wasm test host functions cost");
455465

456-
std::string const funcName("finish");
457-
458466
using namespace test::jtx;
459467

460468
Env env(*this);
@@ -471,7 +479,13 @@ struct Wasm_test : public beast::unit_test::suite
471479
i.gas = 0;
472480

473481
auto re = engine.run(
474-
wasm, funcName, {}, imp, &hfs, 1'000'000, env.journal);
482+
wasm,
483+
ESCROW_FUNCTION_NAME,
484+
{},
485+
imp,
486+
&hfs,
487+
1'000'000,
488+
env.journal);
475489

476490
if (BEAST_EXPECT(re.has_value()))
477491
{
@@ -498,7 +512,13 @@ struct Wasm_test : public beast::unit_test::suite
498512
std::vector<WasmImportFunc> const imp = createWasmImport(&hfs);
499513

500514
auto re = engine.run(
501-
wasm, funcName, {}, imp, &hfs, 1'000'000, env.journal);
515+
wasm,
516+
ESCROW_FUNCTION_NAME,
517+
{},
518+
imp,
519+
&hfs,
520+
1'000'000,
521+
env.journal);
502522

503523
if (BEAST_EXPECT(re.has_value()))
504524
{
@@ -542,7 +562,6 @@ struct Wasm_test : public beast::unit_test::suite
542562
using namespace jtx;
543563
using namespace std::chrono;
544564

545-
std::string const funcName("finish");
546565
// std::string const funcName("test");
547566
auto const& wasmHex = hfPerfTest;
548567
// auto const& wasmHex = opcCallPerfTest;
@@ -614,7 +633,7 @@ struct Wasm_test : public beast::unit_test::suite
614633

615634
PerfHostFunctions nfs(env, k, env.tx());
616635

617-
auto re = runEscrowWasm(wasm, funcName, {}, &nfs);
636+
auto re = runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs);
618637
if (BEAST_EXPECT(re.has_value()))
619638
{
620639
BEAST_EXPECT(re->result);

src/xrpld/app/misc/WasmVM.cpp

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -34,75 +34,80 @@
3434

3535
namespace ripple {
3636

37+
static void
38+
setCommonHostFunctions(HostFunctions* hfs, std::vector<WasmImportFunc>& i)
39+
{
40+
// clang-format off
41+
WASM_IMPORT_FUNC2(i, getLedgerSqn, "get_ledger_sqn", hfs, 60);
42+
WASM_IMPORT_FUNC2(i, getParentLedgerTime, "get_parent_ledger_time", hfs, 60);
43+
WASM_IMPORT_FUNC2(i, getParentLedgerHash, "get_parent_ledger_hash", hfs, 60);
44+
WASM_IMPORT_FUNC2(i, getLedgerAccountHash, "get_ledger_account_hash", hfs, 60);
45+
WASM_IMPORT_FUNC2(i, getLedgerTransactionHash, "get_ledger_tx_hash", hfs, 60);
46+
WASM_IMPORT_FUNC2(i, getBaseFee, "get_base_fee", hfs, 60);
47+
WASM_IMPORT_FUNC2(i, isAmendmentEnabled, "amendment_enabled", hfs, 60);
48+
WASM_IMPORT_FUNC2(i, cacheLedgerObj, "cache_ledger_obj", hfs, 5000);
49+
WASM_IMPORT_FUNC2(i, getTxField, "get_tx_field", hfs, 70);
50+
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjField, "get_current_ledger_obj_field", hfs, 70);
51+
WASM_IMPORT_FUNC2(i, getLedgerObjField, "get_ledger_obj_field", hfs, 70);
52+
WASM_IMPORT_FUNC2(i, getTxNestedField, "get_tx_nested_field", hfs, 110);
53+
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedField, "get_current_ledger_obj_nested_field", hfs, 110);
54+
WASM_IMPORT_FUNC2(i, getLedgerObjNestedField, "get_ledger_obj_nested_field", hfs, 110);
55+
WASM_IMPORT_FUNC2(i, getTxArrayLen, "get_tx_array_len", hfs, 40);
56+
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjArrayLen, "get_current_ledger_obj_array_len", hfs, 40);
57+
WASM_IMPORT_FUNC2(i, getLedgerObjArrayLen, "get_ledger_obj_array_len", hfs, 40);
58+
WASM_IMPORT_FUNC2(i, getTxNestedArrayLen, "get_tx_nested_array_len", hfs, 70);
59+
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedArrayLen, "get_current_ledger_obj_nested_array_len", hfs, 70);
60+
WASM_IMPORT_FUNC2(i, getLedgerObjNestedArrayLen, "get_ledger_obj_nested_array_len", hfs, 70);
61+
WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000);
62+
WASM_IMPORT_FUNC2(i, checkSignature, "check_sig", hfs, 2000);
63+
WASM_IMPORT_FUNC2(i, computeSha512HalfHash, "compute_sha512_half", hfs, 2000);
64+
WASM_IMPORT_FUNC2(i, accountKeylet, "account_keylet", hfs, 350);
65+
WASM_IMPORT_FUNC2(i, checkKeylet, "check_keylet", hfs, 350);
66+
WASM_IMPORT_FUNC2(i, credentialKeylet, "credential_keylet", hfs, 350);
67+
WASM_IMPORT_FUNC2(i, delegateKeylet, "delegate_keylet", hfs, 350);
68+
WASM_IMPORT_FUNC2(i, depositPreauthKeylet, "deposit_preauth_keylet", hfs, 350);
69+
WASM_IMPORT_FUNC2(i, didKeylet, "did_keylet", hfs, 350);
70+
WASM_IMPORT_FUNC2(i, escrowKeylet, "escrow_keylet", hfs, 350);
71+
WASM_IMPORT_FUNC2(i, lineKeylet, "line_keylet", hfs, 350);
72+
WASM_IMPORT_FUNC2(i, nftOfferKeylet, "nft_offer_keylet", hfs, 350);
73+
WASM_IMPORT_FUNC2(i, offerKeylet, "offer_keylet", hfs, 350);
74+
WASM_IMPORT_FUNC2(i, oracleKeylet, "oracle_keylet", hfs, 350);
75+
WASM_IMPORT_FUNC2(i, paychanKeylet, "paychan_keylet", hfs, 350);
76+
WASM_IMPORT_FUNC2(i, signersKeylet, "signers_keylet", hfs, 350);
77+
WASM_IMPORT_FUNC2(i, ticketKeylet, "ticket_keylet", hfs, 350);
78+
WASM_IMPORT_FUNC2(i, getNFT, "get_nft", hfs, 1000);
79+
WASM_IMPORT_FUNC2(i, getNFTIssuer, "get_nft_issuer", hfs, 60);
80+
WASM_IMPORT_FUNC2(i, getNFTTaxon, "get_nft_taxon", hfs, 60);
81+
WASM_IMPORT_FUNC2(i, getNFTFlags, "get_nft_flags", hfs, 60);
82+
WASM_IMPORT_FUNC2(i, getNFTTransferFee, "get_nft_transfer_fee", hfs, 60);
83+
WASM_IMPORT_FUNC2(i, getNFTSerial, "get_nft_serial", hfs, 60);
84+
WASM_IMPORT_FUNC (i, trace, hfs, 500);
85+
WASM_IMPORT_FUNC2(i, traceNum, "trace_num", hfs, 500);
86+
WASM_IMPORT_FUNC2(i, traceFloat, "trace_opaque_float", hfs, 500);
87+
88+
WASM_IMPORT_FUNC2(i, floatFromInt, "float_from_int", hfs, 1000);
89+
WASM_IMPORT_FUNC2(i, floatFromUint, "float_from_uint", hfs, 1000);
90+
WASM_IMPORT_FUNC2(i, floatSet, "float_set", hfs, 1000);
91+
WASM_IMPORT_FUNC2(i, floatCompare, "float_compare", hfs, 1000);
92+
WASM_IMPORT_FUNC2(i, floatAdd, "float_add", hfs, 1000);
93+
WASM_IMPORT_FUNC2(i, floatSubtract, "float_subtract", hfs, 1000);
94+
WASM_IMPORT_FUNC2(i, floatMultiply, "float_multiply", hfs, 1000);
95+
WASM_IMPORT_FUNC2(i, floatDivide, "float_divide", hfs, 1000);
96+
WASM_IMPORT_FUNC2(i, floatRoot, "float_root", hfs, 1000);
97+
WASM_IMPORT_FUNC2(i, floatPower, "float_pow", hfs, 1000);
98+
WASM_IMPORT_FUNC2(i, floatLog, "float_log", hfs, 1000);
99+
// clang-format on
100+
}
101+
37102
std::vector<WasmImportFunc>
38103
createWasmImport(HostFunctions* hfs)
39104
{
40105
std::vector<WasmImportFunc> i;
41106

42107
if (hfs)
43108
{
44-
// clang-format off
45-
46-
WASM_IMPORT_FUNC2(i, getLedgerSqn, "get_ledger_sqn", hfs, 60);
47-
WASM_IMPORT_FUNC2(i, getParentLedgerTime, "get_parent_ledger_time", hfs, 60);
48-
WASM_IMPORT_FUNC2(i, getParentLedgerHash, "get_parent_ledger_hash", hfs, 60);
49-
WASM_IMPORT_FUNC2(i, getLedgerAccountHash, "get_ledger_account_hash", hfs, 60);
50-
WASM_IMPORT_FUNC2(i, getLedgerTransactionHash, "get_ledger_tx_hash", hfs, 60);
51-
WASM_IMPORT_FUNC2(i, getBaseFee, "get_base_fee", hfs, 60);
52-
WASM_IMPORT_FUNC2(i, isAmendmentEnabled, "amendment_enabled", hfs, 60);
53-
WASM_IMPORT_FUNC2(i, cacheLedgerObj, "cache_ledger_obj", hfs, 5000);
54-
WASM_IMPORT_FUNC2(i, getTxField, "get_tx_field", hfs, 70);
55-
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjField, "get_current_ledger_obj_field", hfs, 70);
56-
WASM_IMPORT_FUNC2(i, getLedgerObjField, "get_ledger_obj_field", hfs, 70);
57-
WASM_IMPORT_FUNC2(i, getTxNestedField, "get_tx_nested_field", hfs, 110);
58-
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedField, "get_current_ledger_obj_nested_field", hfs, 110);
59-
WASM_IMPORT_FUNC2(i, getLedgerObjNestedField, "get_ledger_obj_nested_field", hfs, 110);
60-
WASM_IMPORT_FUNC2(i, getTxArrayLen, "get_tx_array_len", hfs, 40);
61-
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjArrayLen, "get_current_ledger_obj_array_len", hfs, 40);
62-
WASM_IMPORT_FUNC2(i, getLedgerObjArrayLen, "get_ledger_obj_array_len", hfs, 40);
63-
WASM_IMPORT_FUNC2(i, getTxNestedArrayLen, "get_tx_nested_array_len", hfs, 70);
64-
WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedArrayLen, "get_current_ledger_obj_nested_array_len", hfs, 70);
65-
WASM_IMPORT_FUNC2(i, getLedgerObjNestedArrayLen, "get_ledger_obj_nested_array_len", hfs, 70);
66-
WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000);
67-
WASM_IMPORT_FUNC2(i, checkSignature, "check_sig", hfs, 2000);
68-
WASM_IMPORT_FUNC2(i, computeSha512HalfHash, "compute_sha512_half", hfs, 2000);
69-
WASM_IMPORT_FUNC2(i, accountKeylet, "account_keylet", hfs, 350);
70-
WASM_IMPORT_FUNC2(i, checkKeylet, "check_keylet", hfs, 350);
71-
WASM_IMPORT_FUNC2(i, credentialKeylet, "credential_keylet", hfs, 350);
72-
WASM_IMPORT_FUNC2(i, delegateKeylet, "delegate_keylet", hfs, 350);
73-
WASM_IMPORT_FUNC2(i, depositPreauthKeylet, "deposit_preauth_keylet", hfs, 350);
74-
WASM_IMPORT_FUNC2(i, didKeylet, "did_keylet", hfs, 350);
75-
WASM_IMPORT_FUNC2(i, escrowKeylet, "escrow_keylet", hfs, 350);
76-
WASM_IMPORT_FUNC2(i, lineKeylet, "line_keylet", hfs, 350);
77-
WASM_IMPORT_FUNC2(i, nftOfferKeylet, "nft_offer_keylet", hfs, 350);
78-
WASM_IMPORT_FUNC2(i, offerKeylet, "offer_keylet", hfs, 350);
79-
WASM_IMPORT_FUNC2(i, oracleKeylet, "oracle_keylet", hfs, 350);
80-
WASM_IMPORT_FUNC2(i, paychanKeylet, "paychan_keylet", hfs, 350);
81-
WASM_IMPORT_FUNC2(i, signersKeylet, "signers_keylet", hfs, 350);
82-
WASM_IMPORT_FUNC2(i, ticketKeylet, "ticket_keylet", hfs, 350);
83-
WASM_IMPORT_FUNC2(i, getNFT, "get_nft", hfs, 1000);
84-
WASM_IMPORT_FUNC2(i, getNFTIssuer, "get_nft_issuer", hfs, 60);
85-
WASM_IMPORT_FUNC2(i, getNFTTaxon, "get_nft_taxon", hfs, 60);
86-
WASM_IMPORT_FUNC2(i, getNFTFlags, "get_nft_flags", hfs, 60);
87-
WASM_IMPORT_FUNC2(i, getNFTTransferFee, "get_nft_transfer_fee", hfs, 60);
88-
WASM_IMPORT_FUNC2(i, getNFTSerial, "get_nft_serial", hfs, 60);
89-
WASM_IMPORT_FUNC (i, trace, hfs, 500);
90-
WASM_IMPORT_FUNC2(i, traceNum, "trace_num", hfs, 500);
91-
WASM_IMPORT_FUNC2(i, traceFloat, "trace_opaque_float", hfs, 500);
92-
93-
WASM_IMPORT_FUNC2(i, floatFromInt, "float_from_int", hfs, 1000);
94-
WASM_IMPORT_FUNC2(i, floatFromUint, "float_from_uint", hfs, 1000);
95-
WASM_IMPORT_FUNC2(i, floatSet, "float_set", hfs, 1000);
96-
WASM_IMPORT_FUNC2(i, floatCompare, "float_compare", hfs, 1000);
97-
WASM_IMPORT_FUNC2(i, floatAdd, "float_add", hfs, 1000);
98-
WASM_IMPORT_FUNC2(i, floatSubtract, "float_subtract", hfs, 1000);
99-
WASM_IMPORT_FUNC2(i, floatMultiply, "float_multiply", hfs, 1000);
100-
WASM_IMPORT_FUNC2(i, floatDivide, "float_divide", hfs, 1000);
101-
WASM_IMPORT_FUNC2(i, floatRoot, "float_root", hfs, 1000);
102-
WASM_IMPORT_FUNC2(i, floatPower, "float_pow", hfs, 1000);
103-
WASM_IMPORT_FUNC2(i, floatLog, "float_log", hfs, 1000);
104-
105-
// clang-format on
109+
setCommonHostFunctions(hfs, i);
110+
WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000);
106111
}
107112

108113
return i;

0 commit comments

Comments
 (0)