Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for find_secondary host functions #1024

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3352,4 +3352,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( get_code_hash_tests, T, validating_testers ) { tr
check("test"_n, 3);
} FC_LOG_AND_RETHROW() }

//test that find_secondary_key behaves like lowerbound
BOOST_AUTO_TEST_CASE_TEMPLATE( find_seconary_key, T, validating_testers ) {
try {
T t;
t.create_account("secfind"_n);
t.set_code("secfind"_n, eosio::testing::test_contracts::db_find_secondary_test_wasm());
t.set_abi("secfind"_n, eosio::testing::test_contracts::db_find_secondary_test_abi());
t.push_action("secfind"_n, "doit"_n, "secfind"_n, variant_object());
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()
2 changes: 1 addition & 1 deletion unittests/test-contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ add_subdirectory( bls_primitives_test )
add_subdirectory( get_block_num_test )
add_subdirectory( nested_container_multi_index )
add_subdirectory( savanna )

add_subdirectory( db_find_secondary_test )
10 changes: 10 additions & 0 deletions unittests/test-contracts/db_find_secondary_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(EOSIO_COMPILE_TEST_CONTRACTS)
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/db_find_secondary_test.wasm"
COMMAND "${CDT_ROOT}/bin/eosio-wast2wasm" "${CMAKE_CURRENT_SOURCE_DIR}/db_find_secondary_test.wast" -o "${CMAKE_CURRENT_BINARY_DIR}/db_find_secondary_test.wasm"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/db_find_secondary_test.wast")

add_custom_target(gen_db_find_secondary_test_wasm ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/db_find_secondary_test.wasm")
else()
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/db_find_secondary_test.wasm ${CMAKE_CURRENT_BINARY_DIR}/db_find_secondary_test.wasm COPYONLY )
endif()
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/db_find_secondary_test.abi ${CMAKE_CURRENT_BINARY_DIR}/db_find_secondary_test.abi COPYONLY )
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "eosio::abi/1.0",
"types": [],
"structs": [{
"name": "empty",
"base": "",
"fields": []
}
],
"actions": [{
"name": "doit",
"type": "empty",
"ricardian_contract": ""
}
],
"tables": [],
"ricardian_clauses": [],
"error_messages": [],
"abi_extensions": [],
"variants": [],
"action_results": []
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
(module
(import "env" "db_idx64_store" (func $db_idx64_store (param i64 i64 i64 i64 i32) (result i32)))
(import "env" "db_idx64_find_secondary" (func $db_idx64_find_secondary (param i64 i64 i64 i32 i32) (result i32)))
(import "env" "db_idx128_store" (func $db_idx128_store (param i64 i64 i64 i64 i32) (result i32)))
(import "env" "db_idx128_find_secondary" (func $db_idx128_find_secondary (param i64 i64 i64 i32 i32) (result i32)))
(import "env" "db_idx256_store" (func $db_idx256_store (param i64 i64 i64 i64 i32 i32) (result i32)))
(import "env" "db_idx256_find_secondary" (func $db_idx256_find_secondary (param i64 i64 i64 i32 i32 i32) (result i32)))
(import "env" "db_idx_double_store" (func $db_idx_double_store (param i64 i64 i64 i64 i32) (result i32)))
(import "env" "db_idx_double_find_secondary" (func $db_idx_double_find_secondary (param i64 i64 i64 i32 i32) (result i32)))
(import "env" "db_idx_long_double_store" (func $db_idx_long_double_store (param i64 i64 i64 i64 i32) (result i32)))
(import "env" "db_idx_long_double_find_secondary" (func $db_idx_long_double_find_secondary (param i64 i64 i64 i32 i32) (result i32)))

(memory 1)
(export "apply" (func $apply))

(func $apply (param $receiver i64) (param $account i64) (param $action_name i64)
;;;;;;;;;;idx64
;;load 2 items with sec key at 16
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 7) (i32.const 16)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 14) (i32.const 16)))
;;load items with sec key at 48 -- this is what will be tested against
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 15) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 8) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 1) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 4) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 90) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 42) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 3) (i32.const 48)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 60) (i32.const 48)))
;;load 2 items with sec key at 80
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 0) (i32.const 80)))
(drop (call $db_idx64_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 88) (i32.const 80)))

;;;;;;;;;;idx128
;;load 2 items with sec key at 16
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 7) (i32.const 16)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 14) (i32.const 16)))
;;load items with sec key at 48 -- this is what will be tested against
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 15) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 8) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 1) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 4) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 90) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 42) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 3) (i32.const 48)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 60) (i32.const 48)))
;;load 2 items with sec key at 80
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 0) (i32.const 80)))
(drop (call $db_idx128_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 88) (i32.const 80)))

;;;;;;;;;;idx256
;;load 2 items with sec key at 16
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 7) (i32.const 16) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 14) (i32.const 16) (i32.const 2)))
;;load items with sec key at 48 -- this is what will be tested against
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 15) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 8) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 1) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 4) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 90) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 42) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 3) (i32.const 48) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 60) (i32.const 48) (i32.const 2)))
;;load 2 items with sec key at 80
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 0) (i32.const 80) (i32.const 2)))
(drop (call $db_idx256_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 88) (i32.const 80) (i32.const 2)))

;;;;;;;;;;double
;;load 2 items with sec key at 16
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 7) (i32.const 16)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 14) (i32.const 16)))
;;load items with sec key at 48 -- this is what will be tested against
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 15) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 8) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 1) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 4) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 90) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 42) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 3) (i32.const 48)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 60) (i32.const 48)))
;;load 2 items with sec key at 80
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 0) (i32.const 80)))
(drop (call $db_idx_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 88) (i32.const 80)))

;;;;;;;;;;long double (f128)
;;load 2 items with sec key at 16
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 7) (i32.const 16)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 14) (i32.const 16)))
;;load items with sec key at 48 -- this is what will be tested against
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 15) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 8) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 1) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 4) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 90) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 42) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 3) (i32.const 48)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 60) (i32.const 48)))
;;load 2 items with sec key at 80
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 0) (i32.const 80)))
(drop (call $db_idx_long_double_store (get_local $receiver) (get_local $receiver) (get_local $receiver) (i64.const 88) (i32.const 80)))

;;test each index type. expect to find primary key of 1 when searching seckey at 48 since 1 was the lowest that was stored for the
;; searched seckey (the primary key will be placed at memory location 1024)
(drop (call $db_idx64_find_secondary (get_local $receiver) (get_local $receiver) (get_local $receiver) (i32.const 48) (i32.const 1024)))
(if (i64.ne (i64.load (i32.const 1024)) (i64.const 1)) (then unreachable))
(drop (call $db_idx128_find_secondary (get_local $receiver) (get_local $receiver) (get_local $receiver) (i32.const 48) (i32.const 1024)))
(if (i64.ne (i64.load (i32.const 1024)) (i64.const 1)) (then unreachable))
(drop (call $db_idx256_find_secondary (get_local $receiver) (get_local $receiver) (get_local $receiver) (i32.const 48) (i32.const 2) (i32.const 1024)))
(if (i64.ne (i64.load (i32.const 1024)) (i64.const 1)) (then unreachable))
(drop (call $db_idx_double_find_secondary (get_local $receiver) (get_local $receiver) (get_local $receiver) (i32.const 48) (i32.const 1024)))
(if (i64.ne (i64.load (i32.const 1024)) (i64.const 1)) (then unreachable))
(drop (call $db_idx_long_double_find_secondary (get_local $receiver) (get_local $receiver) (get_local $receiver) (i32.const 48) (i32.const 1024)))
(if (i64.ne (i64.load (i32.const 1024)) (i64.const 1)) (then unreachable))
)

;;secondary keys to be used
(data (i32.const 16) "\00\00\00\00\01\02\03\04AbcdefghABCDEFGH01234567")
(data (i32.const 48) "\00\00\00\00\01\02\03\05abcdefghABCDEFGH99999999")
(data (i32.const 80) "\00\00\00\00\01\02\03\06aBcdefghABCDEFGH00000000")
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm maybe I should have just done this in a .cpp contract, but I would have had to use the internal_use_do_not_use functions which I wanted to avoid.

1 change: 1 addition & 0 deletions unittests/test_contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace eosio {
MAKE_READ_WASM_ABI(crypto_primitives_test,crypto_primitives_test,test-contracts)
MAKE_READ_WASM_ABI(bls_primitives_test, bls_primitives_test, test-contracts)
MAKE_READ_WASM_ABI(get_block_num_test, get_block_num_test, test-contracts)
MAKE_READ_WASM_ABI(db_find_secondary_test,db_find_secondary_test,test-contracts)
MAKE_READ_WASM_ABI(nested_container_multi_index, nested_container_multi_index, test-contracts)
MAKE_READ_WASM_ABI(ibc, ibc, test-contracts/savanna)

Expand Down