Skip to content

Commit

Permalink
Limit max signatures per batch for the sake of ledger (#560)
Browse files Browse the repository at this point in the history
* Limit max signatures per batch for the sake of ledger

* Set to 5
  • Loading branch information
ChewingGlass authored Jan 29, 2024
1 parent 8a18aa4 commit 8c593c2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 26 deletions.
45 changes: 25 additions & 20 deletions packages/spl-utils/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
SimulatedTransactionResponse,
Transaction,
TransactionInstruction,
TransactionResponse,
TransactionSignature,
VersionedTransactionResponse,
} from "@solana/web3.js";
Expand Down Expand Up @@ -518,12 +517,13 @@ export async function bulkSendTransactions(
txs: Transaction[],
onProgress?: (status: Status) => void,
triesRemaining: number = 10, // Number of blockhashes to try resending txs with before giving up
extraSigners: Keypair[] = []
extraSigners: Keypair[] = [],
maxSignatureBatch: number = TX_BATCH_SIZE,
): Promise<string[]> {
let ret: string[] = [];

// attempt to chunk by blockhash bounds (so signing doesn't take too long)
for (let chunk of chunks(txs, TX_BATCH_SIZE)) {
for (let chunk of chunks(txs, maxSignatureBatch)) {
const thisRet: string[] = [];
// Continually send in bulk while resetting blockhash until we send them all
while (true) {
Expand All @@ -536,20 +536,20 @@ export async function bulkSendTransactions(
return tx;
})
);
const signedTxs = (
await (provider as AnchorProvider).wallet.signAllTransactions(
blockhashedTxs
)
).map((tx) => {
extraSigners.forEach((signer: Keypair) => {
if (
tx.signatures.some((sig) => sig.publicKey.equals(signer.publicKey))
) {
tx.partialSign(signer);
}
}, tx);
return tx;
});
const signedTxs = (await(provider as AnchorProvider)
.wallet.signAllTransactions(blockhashedTxs))
.map((tx) => {
extraSigners.forEach((signer: Keypair) => {
if (
tx.signatures.some((sig) =>
sig.publicKey.equals(signer.publicKey)
)
) {
tx.partialSign(signer);
}
}, tx);
return tx;
});

const txsWithSigs = signedTxs.map((tx, index) => {
return {
Expand Down Expand Up @@ -703,7 +703,8 @@ export async function batchParallelInstructions(
instructions: TransactionInstruction[],
onProgress?: (status: Status) => void,
triesRemaining: number = 10, // Number of blockhashes to try resending txs with before giving up
extraSigners: Keypair[] = []
extraSigners: Keypair[] = [],
maxSignatureBatch: number = TX_BATCH_SIZE
): Promise<void> {
let currentTxInstructions: TransactionInstruction[] = [];
const blockhash = (await provider.connection.getLatestBlockhash()).blockhash;
Expand Down Expand Up @@ -757,7 +758,8 @@ export async function batchParallelInstructions(
transactions,
onProgress,
triesRemaining,
extraSigners
extraSigners,
maxSignatureBatch
);
}

Expand Down Expand Up @@ -855,12 +857,14 @@ export async function batchParallelInstructionsWithPriorityFee(
computeUnitLimit = 1000000,
basePriorityFee,
extraSigners,
maxSignatureBatch = TX_BATCH_SIZE,
}: {
onProgress?: (status: Status) => void;
triesRemaining?: number; // Number of blockhashes to try resending txs with before giving up
computeUnitLimit?: number;
basePriorityFee?: number;
extraSigners?: Keypair[];
maxSignatureBatch?: number;
} = {}
): Promise<void> {
const transactions = await batchInstructionsToTxsWithPriorityFee(
Expand All @@ -877,6 +881,7 @@ export async function batchParallelInstructionsWithPriorityFee(
transactions,
onProgress,
triesRemaining,
extraSigners
extraSigners,
maxSignatureBatch
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PublicKey, TransactionInstruction } from "@solana/web3.js";
import { useAsyncCallback } from "react-async-hook";
import { useHeliumVsrState } from "../contexts/heliumVsrContext";
import { PositionWithMeta } from "../sdk/types";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "../constants";

export const useClaimAllPositionsRewards = () => {
const { provider } = useHeliumVsrState();
Expand All @@ -21,6 +22,7 @@ export const useClaimAllPositionsRewards = () => {
programId = PROGRAM_ID,
onProgress,
onInstructions,
maxSignatureBatch = MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
}: {
positions: PositionWithMeta[];
programId?: PublicKey;
Expand All @@ -29,6 +31,7 @@ export const useClaimAllPositionsRewards = () => {
onInstructions?: (
instructions: TransactionInstruction[]
) => Promise<void>;
maxSignatureBatch?: number;
}) => {
const isInvalid =
!unixNow || !provider || !positions.every((pos) => pos.hasRewards);
Expand Down Expand Up @@ -81,7 +84,10 @@ export const useClaimAllPositionsRewards = () => {
await batchParallelInstructions(
provider,
multiDemArray.flat(),
onProgress
onProgress,
10,
[],
maxSignatureBatch
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useAsyncCallback } from "react-async-hook";
import { useHeliumVsrState } from "../contexts/heliumVsrContext";
import { PositionWithMeta } from "../sdk/types";
import { isClaimed } from "@helium/voter-stake-registry-sdk";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "../constants";

export const useClaimPositionRewards = () => {
const { provider } = useHeliumVsrState();
Expand All @@ -22,6 +23,7 @@ export const useClaimPositionRewards = () => {
programId = PROGRAM_ID,
onProgress,
onInstructions,
maxSignatureBatch = MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
}: {
position: PositionWithMeta;
programId?: PublicKey;
Expand All @@ -30,6 +32,7 @@ export const useClaimPositionRewards = () => {
onInstructions?: (
instructions: TransactionInstruction[]
) => Promise<void>;
maxSignatureBatch?: number;
}) => {
const isInvalid = !unixNow || !provider || !position.hasRewards;

Expand Down Expand Up @@ -78,7 +81,14 @@ export const useClaimPositionRewards = () => {
if (onInstructions) {
await onInstructions(instructions);
} else {
await batchParallelInstructions(provider, instructions, onProgress);
await batchParallelInstructions(
provider,
instructions,
onProgress,
10,
[],
maxSignatureBatch
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { init as initOrg, proposalKey } from "@helium/organization-sdk";
import { init as initProposal } from "@helium/proposal-sdk";
import { batchParallelInstructions, truthy } from "@helium/spl-utils";
import { Status, batchParallelInstructions, truthy } from "@helium/spl-utils";
import {
init as initVsr,
voteMarkerKey,
Expand All @@ -9,6 +9,7 @@ import { PublicKey, TransactionInstruction } from "@solana/web3.js";
import { useAsyncCallback } from "react-async-hook";
import { useHeliumVsrState } from "../contexts/heliumVsrContext";
import { PositionWithMeta } from "../sdk/types";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "../constants";

export const useRelinquishPositionVotes = () => {
const { provider } = useHeliumVsrState();
Expand All @@ -17,13 +18,17 @@ export const useRelinquishPositionVotes = () => {
position,
organization,
onInstructions,
maxSignatureBatch = MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
onProgress,
}: {
position: PositionWithMeta;
organization: PublicKey;
// Instead of sending the transaction, let the caller decide
onInstructions?: (
instructions: TransactionInstruction[]
) => Promise<void>;
maxSignatureBatch?: number;
onProgress?: (status: Status) => void;
}) => {
const isInvalid =
!provider || !provider.wallet || position.numActiveVotes === 0;
Expand Down Expand Up @@ -100,7 +105,14 @@ export const useRelinquishPositionVotes = () => {
if (onInstructions) {
await onInstructions(instructions);
} else {
await batchParallelInstructions(provider, instructions);
await batchParallelInstructions(
provider,
instructions,
onProgress,
10,
[],
maxSignatureBatch
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Status,
batchParallelInstructions,
truthy
} from "@helium/spl-utils";
Expand All @@ -8,6 +9,7 @@ import { useCallback, useMemo } from "react";
import { useAsyncCallback } from "react-async-hook";
import { useHeliumVsrState } from "../contexts/heliumVsrContext";
import { useVoteMarkers } from "./useVoteMarkers";
import { MAX_TRANSACTIONS_PER_SIGNATURE_BATCH } from "../constants";

export const useRelinquishVote = (proposal: PublicKey) => {
const { positions, provider } = useHeliumVsrState();
Expand All @@ -30,11 +32,15 @@ export const useRelinquishVote = (proposal: PublicKey) => {
async ({
choice,
onInstructions,
onProgress,
maxSignatureBatch = MAX_TRANSACTIONS_PER_SIGNATURE_BATCH,
}: {
choice: number; // Instead of sending the transaction, let the caller decide
onInstructions?: (
instructions: TransactionInstruction[]
) => Promise<void>;
onProgress?: (status: Status) => void;
maxSignatureBatch?: number;
}) => {
const isInvalid = !provider || !positions || positions.length === 0;

Expand Down Expand Up @@ -70,7 +76,14 @@ export const useRelinquishVote = (proposal: PublicKey) => {
if (onInstructions) {
await onInstructions(instructions);
} else {
await batchParallelInstructions(provider, instructions);
await batchParallelInstructions(
provider,
instructions,
onProgress,
10,
[],
maxSignatureBatch
);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion packages/voter-stake-registry-hooks/src/hooks/useVote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useProposal } from "@helium/modular-governance-hooks";
import {
Status,
batchParallelInstructions,
truthy
} from "@helium/spl-utils";
Expand Down Expand Up @@ -52,11 +53,15 @@ export const useVote = (proposalKey: PublicKey) => {
async ({
choice,
onInstructions,
onProgress,
maxSignatureBatch
}: {
choice: number; // Instead of sending the transaction, let the caller decide
onInstructions?: (
instructions: TransactionInstruction[]
) => Promise<void>;
onProgress?: (status: Status) => void;
maxSignatureBatch?: number;
}) => {
const isInvalid = !provider || !positions || positions.length === 0;

Expand Down Expand Up @@ -93,7 +98,14 @@ export const useVote = (proposalKey: PublicKey) => {
if (onInstructions) {
await onInstructions(instructions);
} else {
await batchParallelInstructions(provider, instructions);
await batchParallelInstructions(
provider,
instructions,
onProgress,
10,
[],
maxSignatureBatch
);
}
}
}
Expand Down

0 comments on commit 8c593c2

Please sign in to comment.