Skip to content

Commit

Permalink
feat(#376): Replace lazy transactions markers with a bitmap to reclai…
Browse files Browse the repository at this point in the history
…m rent
  • Loading branch information
ChewingGlass committed Aug 29, 2023
1 parent 93e9a2c commit 048dbfb
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions packages/migration-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,8 @@ async function getTransactions(
.blockhash;

console.log(`Found ${results.length} transactions to migrate}`);
const blocks = results.map((r) => blockKey(lazyTransactions, r.id)[0]);
const blocksExist = (
await Promise.all(
chunks(blocks, 100).map(
async (chunk) =>
await provider.connection.getMultipleAccountsInfo(
chunk as PublicKey[],
"confirmed"
)
)
)
).flat();
const lt = await program.account.lazyTransactionsV0.fetch(lazyTransactions);
const executed = lt.executed;
const lazyTxns = await program.account.lazyTransactionsV0.fetch(
lazyTransactions
);
Expand All @@ -156,7 +146,7 @@ async function getTransactions(
},
idx
) => {
const hasRun = blocksExist[idx];
const hasRun = executed[idx];
const compiledTx = decompress(compiled);
const block = blockKey(lazyTransactions, id)[0];
const signers = decompressSigners(signersRaw);
Expand Down
2 changes: 1 addition & 1 deletion packages/spl-utils/src/mplAssetAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function getAssets(
});

const result = response.data
? response.data.map((res) => res?.result || undefined)
? response.data.map((res: any) => res?.result || undefined)
: [];

return [
Expand Down
1 change: 1 addition & 0 deletions programs/lazy-transactions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ anchor-lang = { workspace = true }
anchor-spl = { workspace = true }
spl-concurrent-merkle-tree = "0.1.2"
bytemuck = "1.13.0"
shared-utils = { path = "../../utils/shared-utils" }
3 changes: 3 additions & 0 deletions programs/lazy-transactions/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ pub enum ErrorCode {

#[msg("Invalid canopy length")]
CanopyLengthMismatch,

#[msg("Transaction has already been executed")]
TransactionAlreadyExecuted,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_lang::prelude::*;

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct CloseMarkerArgsV0 {
pub index: u64,
pub index: u32,
}

#[derive(Accounts)]
Expand All @@ -13,6 +13,7 @@ pub struct CloseMarkerV0<'info> {
/// CHECK: Just receiving funds
pub refund: UncheckedAccount<'info>,
#[account(
mut,
has_one = authority
)]
pub lazy_transactions: Account<'info, LazyTransactionsV0>,
Expand All @@ -26,6 +27,8 @@ pub struct CloseMarkerV0<'info> {
pub block: Account<'info, Block>,
}

pub fn handler(_ctx: Context<CloseMarkerV0>, _args: CloseMarkerArgsV0) -> Result<()> {
pub fn handler(ctx: Context<CloseMarkerV0>, args: CloseMarkerArgsV0) -> Result<()> {
ctx.accounts.lazy_transactions.executed[args.index as usize] = true;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub struct ExecuteTransactionV0<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
has_one = canopy
has_one = canopy,
constraint = lazy_transactions.executed[args.index as usize] == false @ ErrorCode::TransactionAlreadyExecuted,
)]
pub lazy_transactions: Account<'info, LazyTransactionsV0>,
/// CHECK: Verified by has one
Expand All @@ -42,18 +43,20 @@ pub struct ExecuteTransactionV0<'info> {
)]
/// CHECK: You can throw things behind this signer and it will sign the tx via cpi
pub lazy_signer: AccountInfo<'info>,
/// CHECK: Temporary. We can remove this once executed txns is fully populated
#[account(
init,
payer = payer,
space = 8,
constraint = block.lamports() == 0,
constraint = block.data.borrow().len() == 0,
seeds = ["block".as_bytes(), lazy_transactions.key().as_ref(), &args.index.to_le_bytes()],
bump
)]
pub block: Account<'info, Block>,
pub block: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<ExecuteTransactionV0>, args: ExecuteTransactionArgsV0) -> Result<()> {
ctx.accounts.lazy_transactions.executed[args.index as usize] = true;

let largest_acct_idx: usize = (*args
.instructions
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct InitializeLazyTransactionsV0<'info> {
#[account(
init,
payer = payer,
space = 8 + 60 + std::mem::size_of::<LazyTransactionsV0>(),
space = 8 + 60 + std::mem::size_of::<LazyTransactionsV0>() + (1 << args.max_depth),
seeds = ["lazy_transactions".as_bytes(), args.name.as_bytes()],
bump,
)]
Expand Down Expand Up @@ -50,6 +50,7 @@ pub fn handler(
canopy: ctx.accounts.canopy.key(),
max_depth: args.max_depth,
bump_seed: ctx.bumps["lazy_transactions"],
executed: vec![false; 1 << args.max_depth],
});

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions programs/lazy-transactions/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ pub mod close_canopy_v0;
pub mod close_marker_v0;
pub mod execute_transaction_v0;
pub mod initialize_lazy_transactions_v0;
pub mod reinitialize_executed_transactions_v0;
pub mod set_canopy_v0;
pub mod update_lazy_transactions_v0;

pub use close_canopy_v0::*;
pub use close_marker_v0::*;
pub use execute_transaction_v0::*;
pub use initialize_lazy_transactions_v0::*;
pub use reinitialize_executed_transactions_v0::*;
pub use set_canopy_v0::*;
pub use update_lazy_transactions_v0::*;
6 changes: 6 additions & 0 deletions programs/lazy-transactions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ pub mod lazy_transactions {
pub fn set_canopy_v0(ctx: Context<SetCanopyV0>, args: SetCanopyArgsV0) -> Result<()> {
set_canopy_v0::handler(ctx, args)
}

pub fn reinitialize_executed_transactions_v0(
ctx: Context<ReinitializeExecutedTransactionsV0>,
) -> Result<()> {
reinitialize_executed_transactions_v0::handler(ctx)
}
}
1 change: 1 addition & 0 deletions programs/lazy-transactions/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct LazyTransactionsV0 {
pub authority: Pubkey,
pub canopy: Pubkey,
pub bump_seed: u8,
pub executed: Vec<bool>,
}

#[account]
Expand Down

0 comments on commit 048dbfb

Please sign in to comment.