Skip to content

Commit

Permalink
Use matching commitment when fetching blockhashes in tests (#2290)
Browse files Browse the repository at this point in the history
# Summary

You can get into some pretty weird situations in tests if all of the following are true:

- The test validator just started up
- You fetch a blockhash using `finalized` commitment before the first root has been made
- You're testing something to do with fees

In cases like this, you'll get the first blockhash ever, which basically lets you land transactions without paying a fee. If your test exercises fee-related exceptions, it just might fail.

# Test Plan

Fee-related tests in the next PR work.
  • Loading branch information
steveluscher committed Mar 12, 2024
1 parent f9fe9c5 commit 34ecac6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/rpc-api/src/__tests__/get-fee-for-message-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('getFeeForMessage', () => {
describe('when called with a recent blockhash', () => {
it('returns the result as a bigint', async () => {
expect.assertions(1);
const latestBlockhash = await rpc.getLatestBlockhash().send();
const latestBlockhash = await rpc.getLatestBlockhash({ commitment }).send();
const message = getMockTransactionMessage(latestBlockhash.value.blockhash);
const result = await rpc.getFeeForMessage(message, { commitment }).send();
expect(result).toStrictEqual({
Expand Down
10 changes: 5 additions & 5 deletions packages/rpc-api/src/__tests__/send-transaction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('sendTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand All @@ -113,7 +113,7 @@ describe('sendTransaction', () => {
});
it('fatals when called with a transaction having an invalid signature', async () => {
expect.assertions(3);
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const { value: latestBlockhash } = await rpc.getLatestBlockhash({ commitment: 'processed' }).send();
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('sendTransaction', () => {
expect.assertions(3);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('sendTransaction', () => {
])) as CryptoKeyPair;
return [keyPair.privateKey, new Uint8Array(await crypto.subtle.exportKey('raw', keyPair.publicKey))];
})(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -264,7 +264,7 @@ describe('sendTransaction', () => {
expect.assertions(2);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down
26 changes: 13 additions & 13 deletions packages/rpc-api/src/__tests__/simulate-transaction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('simulateTransaction', () => {
expect.assertions(2);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('simulateTransaction', () => {

it('throws when called with an invalid signature if `sigVerify` is true', async () => {
expect.assertions(3);
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const { value: latestBlockhash } = await rpc.getLatestBlockhash({ commitment: 'processed' }).send();
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('simulateTransaction', () => {

it('does not throw when called with an invalid signature when `sigVerify` is false', async () => {
expect.assertions(1);
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const { value: latestBlockhash } = await rpc.getLatestBlockhash({ commitment: 'processed' }).send();
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
feePayerAddressBytes: MOCK_PUBLIC_KEY_BYTES,
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('simulateTransaction', () => {
expect.assertions(3);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -427,7 +427,7 @@ describe('simulateTransaction', () => {
])) as CryptoKeyPair;
return [keyPair.privateKey, new Uint8Array(await crypto.subtle.exportKey('raw', keyPair.publicKey))];
})(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -464,7 +464,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -512,7 +512,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -560,7 +560,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessageWithAdditionalAccount({
accountAddressBytes: getBase58Encoder().encode('4QUZQ4c7bZuJ4o4L8tYAEGnePFV27SUFEVmC7BYfsXRp'), // see scripts/fixtures/vote-account.json
Expand Down Expand Up @@ -625,7 +625,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -674,7 +674,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -721,7 +721,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down Expand Up @@ -774,7 +774,7 @@ describe('simulateTransaction', () => {
expect.assertions(1);
const [secretKey, { value: latestBlockhash }] = await Promise.all([
getSecretKey(),
rpc.getLatestBlockhash().send(),
rpc.getLatestBlockhash({ commitment: 'processed' }).send(),
]);
const message = getMockTransactionMessage({
blockhash: latestBlockhash.blockhash,
Expand Down

0 comments on commit 34ecac6

Please sign in to comment.