Skip to content

Commit

Permalink
Jxiao/fix log truncation (solana-labs#121)
Browse files Browse the repository at this point in the history
* Initial implementation of log truncation fix

* Add new files

* refactor bubblegum pda

* amend me

* update basic ts sdk

* better bgum convenience

* addProof modifies instructions to add proof fluently

* better bg init

* fix rebase issues

* fix sugar shack tests

* use convenience initializer

* revert maxSeq change, update smokeTest

Co-authored-by: Noah Gundotra <noahgundotra@noahs-mbp.mynetworksettings.com>
  • Loading branch information
2 people authored and ngundotra committed Sep 8, 2022
1 parent 1f4acf0 commit d17d995
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
66 changes: 66 additions & 0 deletions contracts/sdk/gummyroll/convenience.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { PublicKey, Keypair, TransactionInstruction, SystemProgram, Connection } from "@solana/web3.js";
import { PROGRAM_ID } from ".";
import { getMerkleRollAccountSize } from "./accounts";
import * as anchor from '@project-serum/anchor';
import { Gummyroll } from "./types";
import { CANDY_WRAPPER_PROGRAM_ID } from "../utils";

export async function createAllocTreeIx(
connection: Connection,
maxBufferSize: number,
maxDepth: number,
canopyDepth: number,
payer: PublicKey,
merkleRoll: PublicKey,
): Promise<TransactionInstruction> {
const requiredSpace = getMerkleRollAccountSize(
maxDepth,
maxBufferSize,
canopyDepth ?? 0
);
return SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: merkleRoll,
lamports:
await connection.getMinimumBalanceForRentExemption(
requiredSpace
),
space: requiredSpace,
programId: PROGRAM_ID
});
}

export async function getCreateTreeIxs(
gummyroll: anchor.Program<Gummyroll>,
maxBufferSize: number,
maxDepth: number,
canopyDepth: number,
payer: PublicKey,
merkleRoll: PublicKey,
authority: Keypair,
appendAuthority: PublicKey,
): Promise<TransactionInstruction[]> {
const allocAccountIx = await createAllocTreeIx(
gummyroll.provider.connection,
maxBufferSize,
maxDepth,
canopyDepth,
payer,
merkleRoll,
);
const initIx = gummyroll.instruction.initEmptyGummyroll(
maxDepth,
maxBufferSize,
{
accounts: {
merkleRoll,
authority,
appendAuthority,
candyWrapper: CANDY_WRAPPER_PROGRAM_ID
},
signers: [authority],
},
)

return [allocAccountIx, initIx];
}
1 change: 1 addition & 0 deletions contracts/sdk/gummyroll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './instructions';
export * from './accounts';
export * from './types';
export * from './utils';
export * from './convenience';

/**
* Program address
Expand Down
46 changes: 26 additions & 20 deletions contracts/sdk/gummyroll/instructions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { Program } from '@project-serum/anchor';
import { Gummyroll } from "../types";
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { CANDY_WRAPPER_PROGRAM_ID } from '../../utils'

/**
* Modifies given instruction
*/
export function addProof(
instruction: TransactionInstruction,
nodeProof: Buffer[],
): TransactionInstruction {
instruction.keys = instruction.keys.concat(
nodeProof.map((node) => {
return {
pubkey: new PublicKey(node),
isSigner: false,
isWritable: false,
};
})
)
return instruction;
}

export function createReplaceIx(
gummyroll: Program<Gummyroll>,
Expand All @@ -12,14 +32,7 @@ export function createReplaceIx(
index: number,
proof: Buffer[]
): TransactionInstruction {
const nodeProof = proof.map((node) => {
return {
pubkey: new PublicKey(node),
isSigner: false,
isWritable: false,
};
});
return gummyroll.instruction.replaceLeaf(
return addProof(gummyroll.instruction.replaceLeaf(
Array.from(treeRoot),
Array.from(previousLeaf),
Array.from(newLeaf),
Expand All @@ -28,11 +41,11 @@ export function createReplaceIx(
accounts: {
merkleRoll,
authority: authority.publicKey,
candyWrapper: CANDY_WRAPPER_PROGRAM_ID,
},
signers: [authority],
remainingAccounts: nodeProof,
}
);
), proof);
}

export function createAppendIx(
Expand All @@ -49,6 +62,7 @@ export function createAppendIx(
merkleRoll,
authority: authority.publicKey,
appendAuthority: appendAuthority.publicKey,
candyWrapper: CANDY_WRAPPER_PROGRAM_ID,
},
signers: [authority, appendAuthority],
}
Expand Down Expand Up @@ -83,14 +97,7 @@ export function createVerifyLeafIx(
index: number,
proof: Buffer[],
): TransactionInstruction {
const nodeProof = proof.map((node) => {
return {
pubkey: new PublicKey(node),
isSigner: false,
isWritable: false,
};
});
return gummyroll.instruction.verifyLeaf(
return addProof(gummyroll.instruction.verifyLeaf(
Array.from(root),
Array.from(leaf),
index,
Expand All @@ -99,7 +106,6 @@ export function createVerifyLeafIx(
merkleRoll
},
signers: [],
remainingAccounts: nodeProof,
}
);
), proof);
}

0 comments on commit d17d995

Please sign in to comment.