Skip to content

Commit

Permalink
Add memo to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
akoptelov committed Nov 2, 2023
1 parent 46c17d6 commit de0b176
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/load-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ export interface SendConfig {
readonly rotateNodes?: boolean;

readonly rotateSenders?: boolean;

readonly validTime?: number;
}

const WAIT_MAX_RETRIES = 6;

const DEFAULT_VALID_TIME = 290;

export interface WaitConfig {
// wait for a transaction: 20s * 30 attempts * 6 retries = 1h for each transaction.
readonly waitMaxRetries?: number;
Expand Down Expand Up @@ -141,7 +145,7 @@ export class LoadGenerator {
async setup(ttx: TransactionTemplate): Promise<void> {
this.log.info('setting up transactions...');
this.log.debug('sending setup transaction...');
const [id, _] = await this.send(ttx);
const [id, _] = await this.send(ttx, DEFAULT_VALID_TIME);
if (id.isSuccess) {
this.log.debug('waiting for setup transaction to be included...');
await this.bcTransactions.waitAll([id]);
Expand All @@ -153,13 +157,19 @@ export class LoadGenerator {
}
}

getTxParams(date: Date, _validTime: number): [string, number | undefined] {
return [date.toISOString(), undefined];
}

async send(
ttx: TransactionTemplate,
validTime: number,
nonce?: number,
retry?: boolean
): Promise<[TransactionId, number | undefined]> {
this.log.debug(`using nonce ${nonce}`);
const tx = ttx.getSigned(nonce);
const [memo, validUntil] = this.getTxParams(new Date(), validTime);
const tx = ttx.getSigned(nonce, memo, validUntil);
this.log.silly('signed tx:', tx.toPretty());
const id = await sendTransaction(tx, this.mina);
if (!id.isSuccess) {
Expand All @@ -172,7 +182,7 @@ export class LoadGenerator {
isMinaGraphQL(this.mina)
) {
let account = await fetchAccount(ttx.getFeePayer(), this.mina);
return await this.send(ttx, account.inferredNonce, true);
return await this.send(ttx, validTime, account.inferredNonce, true);
}
}
throw new Error(`failed to send a transaction`, {
Expand Down Expand Up @@ -236,6 +246,7 @@ export class LoadGenerator {
packSize = 1,
duration,
interval = 0,
validTime = DEFAULT_VALID_TIME,
rotateNodes = false,
rotateSenders = false,
}: SendConfig
Expand All @@ -259,6 +270,7 @@ export class LoadGenerator {
this.log.info(`sending tx #${i}...`);
const [id, n]: [TransactionId, number | undefined] = await this.send(
ttx,
validTime,
nonce
);
this.log.info(`tx #${i} is sent, hash is ${id.hash()}`);
Expand Down
11 changes: 11 additions & 0 deletions src/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ const durationOption = () =>
.argParser(durationParser)
.conflicts(['count', 'infinite']);

const validTimeOption = () =>
new Option(
'--valid-time <slots>',
'transaction validity time, in slots (unimplemented)'
)
.argParser(durationParser)
.hideHelp();

const noWaitOption = () =>
new Option('--no-wait', 'do not wait for transactions to be included');

Expand All @@ -113,6 +121,7 @@ export const runCommand = new Command()
.addOption(durationOption())
.addOption(infiniteOption())
.addOption(periodOption())
.addOption(validTimeOption())
.option(
'--dump-tx-ids <file>',
'dump transaction ids into the specified file upon completion'
Expand All @@ -132,6 +141,7 @@ LoadRegistry.registerLoadCommand(runCommand, async (opts, load, _name) => {
infinite,
period,
dumpTxIds,
validTime,
wait,
} = opts;

Expand Down Expand Up @@ -164,6 +174,7 @@ LoadRegistry.registerLoadCommand(runCommand, async (opts, load, _name) => {
interval: period,
rotateSenders: rotateKeys,
rotateNodes,
validTime,
});

intHandler = async () => {};
Expand Down
12 changes: 11 additions & 1 deletion src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ export class TransactionTemplate {
return PublicKey.fromBase58(this.tx.feePayer.body.publicKey);
}

getSigned(nonce?: number | UInt32 | Field): Mina.Transaction {
getSigned(
nonce?: number | UInt32 | Field,
memo?: string,
validUntil?: number | string | UInt32
): Mina.Transaction {
const tx = restore(this.tx);
if (nonce !== undefined)
tx.transaction.feePayer.body.nonce = Nonce.from(nonce);
if (memo !== undefined) {
tx.transaction.memo = memo;
}
if (validUntil !== undefined) {
tx.transaction.feePayer.body.validUntil = UInt32.from(validUntil);
}
return tx.sign(this.signers);
}
}
Expand Down

0 comments on commit de0b176

Please sign in to comment.