From 8dddfb2e331b0d046edb11b1e05be17c3b7e6f59 Mon Sep 17 00:00:00 2001 From: Ariel Mendelzon Date: Fri, 5 Jan 2024 08:00:49 +1300 Subject: [PATCH] Unified wa_store function into unique WA_STORE macro (#166) --- ledger/src/signer/src/bc_advance.c | 33 ++++++--------------------- ledger/src/signer/src/bc_ancestor.c | 27 ++++------------------ ledger/src/signer/src/bc_blockutils.h | 15 ++++++++++++ 3 files changed, 26 insertions(+), 49 deletions(-) diff --git a/ledger/src/signer/src/bc_advance.c b/ledger/src/signer/src/bc_advance.c index 0f84c376..5f975712 100644 --- a/ledger/src/signer/src/bc_advance.c +++ b/ledger/src/signer/src/bc_advance.c @@ -82,25 +82,6 @@ static uint8_t expected_state; // Storage utilities // ----------------------------------------------------------------------- -/* - * Store the given buffer in the block's work area. - * - * @arg[in] buf buffer to store - * @arg[in] size buffer size in bytes - */ -static void wa_store(const uint8_t* buf, uint16_t size) { - SAFE_MEMMOVE(block.wa_buf, - sizeof(block.wa_buf), - block.wa_off, - buf, - size, - MEMMOVE_ZERO_OFFSET, - size, - FAIL(BUFFER_OVERFLOW)); - - block.wa_off += size; -} - /* * Process merkle proof chunk. * @@ -123,7 +104,7 @@ static void process_merkle_proof(const uint8_t* chunk, uint16_t size) { if (block.wa_off < HASH_SIZE) { uint8_t old_offset = offset; offset += HASH_SIZE - block.wa_off; - wa_store(chunk + old_offset, HASH_SIZE - block.wa_off); + WA_STORE(chunk + old_offset, HASH_SIZE - block.wa_off); } fold_left(&block.ctx, block.merkle_proof_left, block.wa_buf); block.wa_off = 0; @@ -131,7 +112,7 @@ static void process_merkle_proof(const uint8_t* chunk, uint16_t size) { // Copy any remaining bytes in the chunk to the work area if (offset < size) { - wa_store(chunk + offset, size - offset); + WA_STORE(chunk + offset, size - offset); } } @@ -573,23 +554,23 @@ static void str_chunk(const uint8_t* chunk, const size_t size) { } if (block.field == F_PARENT_HASH) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_BLOCK_DIFF) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_BLOCK_NUM) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_MM_HEADER) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_UMM_ROOT && HAS_FLAG(block.flags, HAS_UMM_ROOT)) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_MERKLE_PROOF && !BLOCK_ALREADY_VALID()) { diff --git a/ledger/src/signer/src/bc_ancestor.c b/ledger/src/signer/src/bc_ancestor.c index 57aa5df8..db7d4d56 100644 --- a/ledger/src/signer/src/bc_ancestor.c +++ b/ledger/src/signer/src/bc_ancestor.c @@ -55,25 +55,6 @@ static uint32_t curr_block; // Expected OP for next message static uint8_t expected_state; -/* - * Store the given buffer in the block's work area. - * - * @arg[in] buf buffer to store - * @arg[in] size buffer size in bytes - */ -static void wa_store(const uint8_t* buf, uint16_t size) { - SAFE_MEMMOVE(block.wa_buf, - sizeof(block.wa_buf), - block.wa_off, - buf, - size, - MEMMOVE_ZERO_OFFSET, - size, - FAIL(BUFFER_OVERFLOW)); - - block.wa_off += size; -} - // ----------------------------------------------------------------------- // Update ancestor validations // ----------------------------------------------------------------------- @@ -194,20 +175,20 @@ static void str_chunk(const uint8_t* chunk, const size_t size) { } if (block.field == F_PARENT_HASH) { - wa_store(chunk, size); + WA_STORE(chunk, size); } // Store receipt only for last block if (block.field == F_RECEIPT_ROOT && curr_block + 1 == expected_blocks) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_BLOCK_NUM) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (block.field == F_MM_HEADER) { - wa_store(chunk, size); + WA_STORE(chunk, size); } if (SHOULD_COMPUTE_BLOCK_HASH) { diff --git a/ledger/src/signer/src/bc_blockutils.h b/ledger/src/signer/src/bc_blockutils.h index bb0c73b4..28f05044 100644 --- a/ledger/src/signer/src/bc_blockutils.h +++ b/ledger/src/signer/src/bc_blockutils.h @@ -73,4 +73,19 @@ #define MM_HASH_LAST_FIELD \ (block.network_upgrade >= NU_PAPYRUS ? F_UMM_ROOT : F_UMM_ROOT - 1) +// Store the given buffer in the block's work area +#define WA_STORE(buf, bufsize) \ + { \ + SAFE_MEMMOVE(block.wa_buf, \ + sizeof(block.wa_buf), \ + block.wa_off, \ + (buf), \ + (bufsize), \ + MEMMOVE_ZERO_OFFSET, \ + (bufsize), \ + FAIL(BUFFER_OVERFLOW)); \ + \ + block.wa_off += (bufsize); \ + } + #endif // __BC_BLOCKUTILS_H