Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TransactionMessage.decompile() now counts the correct number of unsigned, writable accounts #28990

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion web3.js/src/transaction/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class TransactionMessage {
assert(numWritableSignedAccounts > 0, 'Message header is invalid');

const numWritableUnsignedAccounts =
message.staticAccountKeys.length - numReadonlyUnsignedAccounts;
message.staticAccountKeys.length -
numRequiredSignatures -
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, the test in this commit incorrectly marks the last account as writable.

numReadonlyUnsignedAccounts;
assert(numWritableUnsignedAccounts >= 0, 'Message header is invalid');

const accountKeys = message.getAccountKeys(args);
Expand Down
35 changes: 35 additions & 0 deletions web3.js/test/transaction-tests/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {expect} from 'chai';
import {sha256} from '@noble/hashes/sha256';

import {
Transaction,
TransactionInstruction,
TransactionMessage,
} from '../../src/transaction';
Expand Down Expand Up @@ -67,6 +68,40 @@ describe('TransactionMessage', () => {
expect(decompiledMessage.instructions).to.eql(instructions);
});

// Regression test for https://github.com/solana-labs/solana/issues/28900
it('decompiles a legacy message the same way as the old API', () => {
const accountKeys = createTestKeys(7);
const legacyMessage = new Message({
header: {
numRequiredSignatures: 1,
numReadonlySignedAccounts: 0,
numReadonlyUnsignedAccounts: 5,
},
recentBlockhash: bs58.encode(sha256('test')),
accountKeys,
instructions: [
{
accounts: [0, 6, 1, 3, 4, 2],
data: '',
programIdIndex: 5,
},
],
});

const transactionFromLegacyAPI = Transaction.populate(legacyMessage);
const transactionMessage = TransactionMessage.decompile(legacyMessage);

expect(transactionMessage.payerKey).to.eql(
transactionFromLegacyAPI.feePayer,
);
expect(transactionMessage.instructions).to.eql(
transactionFromLegacyAPI.instructions,
);
expect(transactionMessage.recentBlockhash).to.eql(
transactionFromLegacyAPI.recentBlockhash,
);
});

it('decompiles a V0 message', () => {
const keys = createTestKeys(7);
const recentBlockhash = bs58.encode(sha256('test'));
Expand Down