From 14c13accbcd097c47545272acc8a24dd98dfa218 Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Mon, 23 Oct 2023 12:11:24 +0200 Subject: [PATCH] Use Xmacro to centralize selectors definition --- src/boilerplate_plugin.h | 51 +++++++++++++++++++++++++++----------- src/contract.c | 14 +++-------- src/handle_init_contract.c | 2 +- src/xmacro_helpers.h | 20 +++++++++++++++ 4 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 src/xmacro_helpers.h diff --git a/src/boilerplate_plugin.h b/src/boilerplate_plugin.h index eb329b6e..464b0a08 100644 --- a/src/boilerplate_plugin.h +++ b/src/boilerplate_plugin.h @@ -1,25 +1,51 @@ +/******************************************************************************* + * Plugin Boilerplate + * (c) 2023 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + #pragma once +#include #include "eth_internals.h" #include "eth_plugin_interface.h" -#include - -// Number of selectors defined in this plugin. Should match the enum `selector_t`. -// EDIT THIS: Put in the number of selectors your plugin is going to support. -#define NUM_SELECTORS 2 +#include "xmacro_helpers.h" // Name of the plugin. // EDIT THIS: Replace with your plugin name. #define PLUGIN_NAME "PluginBoilerplate" -// Enumeration of the different selectors possible. -// Should follow the exact same order as the array declared in main.c -// EDIT THIS: Change the naming (`selector_t`), and add your selector names. -typedef enum { - SWAP_EXACT_ETH_FOR_TOKENS = 0, - BOILERPLATE_DUMMY_2, +// All possible selectors of your plugin. +// EDIT THIS: Enter your selectors here, in the format X(NAME, value) +// A macro below will create for you: +// - an enum named selector_t with every NAME +// - a map associating each NAME with it's value +#define XMACRO(X) \ + X(SWAP_EXACT_ETH_FOR_TOKENS, 0x7ff36ab5) \ + X(BOILERPLATE_DUMMY_2, 0x13374242) + +// This enum will be automatically expanded to hold all selector names. +// The value SELECTOR_COUNT can be used to get the number of defined selectors +// Do not modify ! +typedef enum selector_e { + XMACRO(TO_ENUM) SELECTOR_COUNT, } selector_t; +// This array will be automatically expanded to map all selector_t names with the correct value. +// Do not modify ! +extern const uint32_t SELECTORS[SELECTOR_COUNT]; + // Enumeration used to parse the smart contract data. // EDIT THIS: Adapt the parameter names here. typedef enum { @@ -31,9 +57,6 @@ typedef enum { UNEXPECTED_PARAMETER, } parameter; -// EDIT THIS: Rename `BOILERPLATE` to be the same as the one initialized in `main.c`. -extern const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS]; - // Shared global memory with Ethereum app. Must be at most 5 * 32 bytes. // EDIT THIS: This struct is used by your plugin to save the parameters you parse. You // will need to adapt this struct to your plugin. diff --git a/src/contract.c b/src/contract.c index 48560b37..8decb050 100644 --- a/src/contract.c +++ b/src/contract.c @@ -17,16 +17,8 @@ #include #include "boilerplate_plugin.h" +#include "xmacro_helpers.h" -// List of selectors supported by this plugin. -// EDIT THIS: Adapt the variable names and change the `0x` values to match your selectors. -static const uint32_t SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR = 0x7ff36ab5; -static const uint32_t BOILERPLATE_DUMMY_SELECTOR_2 = 0x13374242; - -// Array of all the different boilerplate selectors. Make sure this follows the same order as the -// enum defined in `boilerplate_plugin.h` -// EDIT THIS: Use the names of the array declared above. -const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS] = { - SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR, - BOILERPLATE_DUMMY_SELECTOR_2, +const uint32_t SELECTORS[SELECTOR_COUNT] = { + XMACRO(TO_VALUE), }; diff --git a/src/handle_init_contract.c b/src/handle_init_contract.c index 3154cc1e..77728ab8 100644 --- a/src/handle_init_contract.c +++ b/src/handle_init_contract.c @@ -24,7 +24,7 @@ void handle_init_contract(ethPluginInitContract_t *msg) { memset(context, 0, sizeof(*context)); size_t index; - if (!find_selector(U4BE(msg->selector, 0), BOILERPLATE_SELECTORS, NUM_SELECTORS, &index)) { + if (!find_selector(U4BE(msg->selector, 0), SELECTORS, SELECTOR_COUNT, &index)) { PRINTF("Error: selector not found!\n"); msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE; return; diff --git a/src/xmacro_helpers.h b/src/xmacro_helpers.h new file mode 100644 index 00000000..20920baa --- /dev/null +++ b/src/xmacro_helpers.h @@ -0,0 +1,20 @@ +/******************************************************************************* + * Plugin Boilerplate + * (c) 2023 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + +// XMacro helpers to create the selector enum and map +#define TO_ENUM(name, val) name, +#define TO_VALUE(name, val) val,