Skip to content

Commit

Permalink
[solana-labs#21722] Fix non-deterministic writeable account order
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Dec 9, 2021
1 parent e020960 commit d119aa4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion web3.js/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class Transaction {
accountMetas.sort(function (x, y) {
const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
const checkWritable =
x.isWritable === y.isWritable ? 0 : x.isWritable ? -1 : 1;
x.isWritable === y.isWritable ? x.pubkey.toBuffer().compare(y.pubkey.toBuffer()) : x.isWritable ? -1 : 1;
return checkSigner || checkWritable;
});

Expand Down
64 changes: 63 additions & 1 deletion web3.js/test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {expect} from 'chai';

import {Keypair} from '../src/keypair';
import {PublicKey} from '../src/publickey';
import {Transaction} from '../src/transaction';
import {Transaction, TransactionInstruction} from '../src/transaction';
import {StakeProgram} from '../src/stake-program';
import {SystemProgram} from '../src/system-program';
import {Message} from '../src/message';
Expand Down Expand Up @@ -505,4 +505,66 @@ describe('Transaction', () => {
tx.addSignature(from.publicKey, toBuffer(signature));
expect(tx.verifySignatures()).to.be.true;
});

it('can serialize, deserialize, and reserialize with a partial signer', () => {
const signer = Keypair.generate();
const acc0Writable = Keypair.generate();
const acc1Writable = Keypair.generate();
const acc2Writable = Keypair.generate();
const t0 = new Transaction({
recentBlockhash: "HZaTsZuhN1aaz9WuuimCFMyH7wJ5xiyMUHFCnZSMyguH",
feePayer: signer.publicKey
});
t0.add(new TransactionInstruction({
keys: [{
pubkey: signer.publicKey,
isWritable: true,
isSigner: true
}, {
pubkey: acc0Writable.publicKey,
isWritable: true,
isSigner: false
}],
programId: Keypair.generate().publicKey
}))
t0.add(new TransactionInstruction({
keys: [{
pubkey: acc1Writable.publicKey,
isWritable: false,
isSigner: false
}],
programId: Keypair.generate().publicKey
}))
t0.add(new TransactionInstruction({
keys: [{
pubkey: acc2Writable.publicKey,
isWritable: true,
isSigner: false
}],
programId: Keypair.generate().publicKey
}))
t0.add(new TransactionInstruction({
keys: [{
pubkey: signer.publicKey,
isWritable: true,
isSigner: true
},{
pubkey: acc0Writable.publicKey,
isWritable: false,
isSigner: false
}, {
pubkey: acc2Writable.publicKey,
isWritable: false,
isSigner: false
}, {
pubkey: acc1Writable.publicKey,
isWritable: true,
isSigner: false
}],
programId: Keypair.generate().publicKey
}))
t0.partialSign(signer);
const t1 = Transaction.from(t0.serialize());
t1.serialize()
})
});

0 comments on commit d119aa4

Please sign in to comment.