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: splice bytes in-place when reading buffers #2315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 4 additions & 8 deletions packages/library-legacy/src/message/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,21 @@ export class Message {
const accountCount = shortvec.decodeLength(byteArray);
let accountKeys = [];
for (let i = 0; i < accountCount; i++) {
const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
accountKeys.push(new PublicKey(Buffer.from(account)));
}

const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);

const instructionCount = shortvec.decodeLength(byteArray);
let instructions: CompiledInstruction[] = [];
for (let i = 0; i < instructionCount; i++) {
const programIdIndex = byteArray.shift()!;
const accountCount = shortvec.decodeLength(byteArray);
const accounts = byteArray.slice(0, accountCount);
byteArray = byteArray.slice(accountCount);
const accounts = byteArray.splice(0, accountCount);
const dataLength = shortvec.decodeLength(byteArray);
const dataSlice = byteArray.slice(0, dataLength);
const dataSlice = byteArray.splice(0, dataLength);
const data = bs58.encode(Buffer.from(dataSlice));
byteArray = byteArray.slice(dataLength);
instructions.push({
programIdIndex,
accounts,
Expand Down
3 changes: 1 addition & 2 deletions packages/library-legacy/src/transaction/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,7 @@ export class Transaction {
const signatureCount = shortvec.decodeLength(byteArray);
let signatures = [];
for (let i = 0; i < signatureCount; i++) {
const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
signatures.push(bs58.encode(Buffer.from(signature)));
}

Expand Down
6 changes: 2 additions & 4 deletions packages/library-legacy/src/validator-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ export class ValidatorInfo {

const configKeys: Array<ConfigKey> = [];
for (let i = 0; i < 2; i++) {
const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
const isSigner = byteArray.slice(0, 1)[0] === 1;
byteArray = byteArray.slice(1);
const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
const isSigner = byteArray.splice(0, 1)[0] === 1;
configKeys.push({publicKey, isSigner});
}

Expand Down
Loading