Skip to content

Commit

Permalink
flamenco: add instrs reset to txn generation
Browse files Browse the repository at this point in the history
  • Loading branch information
arjain4 committed May 16, 2024
1 parent a1c97a7 commit ee2e75e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/flamenco/txn/fd_txn_generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ fd_txn_base_generate( uchar out_txn_meta[ static FD_TXN_MAX_SZ ],
fd_txn_accounts_t * accounts,
uchar * opt_recent_blockhash ) {

FD_TEST(num_signatures <= 128);
/* Number of signatures cannot exceed 127. */
FD_TEST(num_signatures <= FD_TXN_SIG_MAX);
*out_txn_payload = (uchar)num_signatures;

/* Fill out txn metadata */
fd_txn_t * txn_meta = (fd_txn_t *) out_txn_meta;
txn_meta->acct_addr_cnt = accounts->acct_cnt;
Expand Down Expand Up @@ -105,13 +107,13 @@ fd_txn_add_instr( uchar * txn_meta_ptr,
txn_meta->instr_cnt++;
uchar * write_ptr = instr_start;

uchar compact_instr_cnt_sz = (uchar) fd_cu16_enc( (ushort)txn_meta->instr_cnt, write_ptr );
uint compact_instr_cnt_sz = fd_cu16_enc( (ushort)txn_meta->instr_cnt, write_ptr );
FD_TEST( compact_instr_cnt_sz == 1 );

write_ptr += compact_instr_cnt_sz;

/* Calculate offset of next instruction. */
if ( txn_meta->instr_cnt > 1 ) {
if ( FD_UNLIKELY( txn_meta->instr_cnt > 1 ) ) {
write_ptr = out_txn_payload + txn_meta->instr[txn_meta->instr_cnt-2].data_off + txn_meta->instr[txn_meta->instr_cnt-2].data_sz;
}

Expand All @@ -120,9 +122,10 @@ fd_txn_add_instr( uchar * txn_meta_ptr,
*write_ptr = program_id;
write_ptr += sizeof(uchar);

uchar compact_accts_len_sz = (uchar) fd_cu16_enc( (ushort)accounts_sz, write_ptr );
uint compact_accts_len_sz = fd_cu16_enc( (ushort)accounts_sz, write_ptr );
write_ptr += compact_accts_len_sz;

ushort acct_off = (ushort) (write_ptr - out_txn_payload);
fd_memcpy( write_ptr, accounts, accounts_sz );
write_ptr += accounts_sz;

Expand All @@ -134,11 +137,24 @@ fd_txn_add_instr( uchar * txn_meta_ptr,
ushort data_off = (ushort) (write_ptr - out_txn_payload);
fd_memcpy( write_ptr, instr_buf, data_sz );
write_ptr += data_sz;
ushort acct_off = (ushort) (instr_start + sizeof(uchar) + compact_accts_len_sz - out_txn_payload);

(void) fd_txn_instr_meta_generate( (uchar*)&txn_meta->instr[txn_meta->instr_cnt-1],
program_id,
(ushort)accounts_sz,
data_sz, acct_off, data_off );
return (ulong)(write_ptr - out_txn_payload);
return (ulong)(instr_start - out_txn_payload);
}

void
fd_txn_reset_instrs( uchar * txn_meta_ptr,
uchar out_txn_payload[ static FD_TXN_MTU ] ) {
fd_txn_t * txn_meta = (fd_txn_t *)txn_meta_ptr;
if( FD_UNLIKELY( txn_meta->instr_cnt == 0 ) ) {
return;
}

ulong instr_start = txn_meta->recent_blockhash_off + FD_TXN_BLOCKHASH_SZ;

*(out_txn_payload + instr_start) = 0;
txn_meta->instr_cnt = 0;
}
13 changes: 11 additions & 2 deletions src/flamenco/txn/fd_txn_generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ typedef struct fd_txn_accounts fd_txn_accounts_t;

FD_PROTOTYPES_BEGIN

/* Method used to create a template for a txn (useful for pre-staging and re-use) */
/* Method used to create a template for a txn (useful for pre-staging and re-use)
Num signatures is restricted to < 128. Returns the offset to the start of the
insructions. */
ulong
fd_txn_base_generate( uchar out_txn_meta[ static FD_TXN_MAX_SZ ],
uchar out_txn_payload[ static FD_TXN_MTU ],
Expand All @@ -37,7 +39,8 @@ fd_txn_base_generate( uchar out_txn_meta[ static FD_TXN_MAX_SZ ],
/* Method used for adding an instruction to a txn being generated.
The accounts param is a list of indices to the accounts in the txn.
The instruction buffer contains the data for the instruction to
be added. */
be added. Returns the offset to the start of the instruction added
in the transaction buffer. */
ulong
fd_txn_add_instr( uchar * txn_meta_ptr,
uchar out_txn_payload[ static FD_TXN_MTU ],
Expand All @@ -46,4 +49,10 @@ fd_txn_add_instr( uchar * txn_meta_ptr,
ulong accounts_sz,
uchar const * instr_buf,
ulong instr_buf_sz );

/* Helper method to reset the list of instrs in the metadata and
remove all instrs from the txn payload. */
void
fd_txn_reset_instrs( uchar * txn_meta_ptr,
uchar out_txn_payload[ static FD_TXN_MTU ] );
FD_PROTOTYPES_END

0 comments on commit ee2e75e

Please sign in to comment.