Skip to content

Commit

Permalink
Unified wa_store function into unique WA_STORE macro (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
amendelzon authored Jan 4, 2024
1 parent e1dee5d commit 8dddfb2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 49 deletions.
33 changes: 7 additions & 26 deletions ledger/src/signer/src/bc_advance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -123,15 +104,15 @@ 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;
}

// 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);
}
}

Expand Down Expand Up @@ -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()) {
Expand Down
27 changes: 4 additions & 23 deletions ledger/src/signer/src/bc_ancestor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions ledger/src/signer/src/bc_blockutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8dddfb2

Please sign in to comment.