-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to true bitmap to save rent over Vec<bool>
- Loading branch information
1 parent
048dbfb
commit 470e081
Showing
11 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
packages/helium-admin-cli/src/close-lazy-transaction-markers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { blockKey, init as initLazy, lazyTransactionsKey } from "@helium/lazy-transactions-sdk"; | ||
import os from "os"; | ||
import yargs from "yargs/yargs"; | ||
import { bulkSendTransactions, chunks } from "@helium/spl-utils"; | ||
import { Transaction } from "@solana/web3.js"; | ||
|
||
export async function run(args: any = process.argv) { | ||
const yarg = yargs(args).options({ | ||
wallet: { | ||
alias: "k", | ||
describe: "Anchor wallet keypair", | ||
default: `${os.homedir()}/.config/solana/id.json`, | ||
}, | ||
url: { | ||
alias: "u", | ||
default: "http://127.0.0.1:8899", | ||
describe: "The solana url", | ||
}, | ||
name: { | ||
default: "nJWGUMOK", | ||
describe: "The lazy transactions instance name" | ||
} | ||
}); | ||
const argv = await yarg.argv; | ||
process.env.ANCHOR_WALLET = argv.wallet; | ||
process.env.ANCHOR_PROVIDER_URL = argv.url; | ||
anchor.setProvider(anchor.AnchorProvider.local(argv.url)); | ||
const provider = anchor.getProvider() as anchor.AnchorProvider; | ||
const lazyProgram = await initLazy(provider); | ||
const ltKey = lazyTransactionsKey(argv.name)[0]; | ||
const lt = await lazyProgram.account.lazyTransactionsV0.fetch(ltKey); | ||
|
||
const blocks = await lazyProgram.account.block.all(); | ||
const blocksByKey = new Set(blocks.map((b) => b.publicKey.toString())); | ||
const allIndices = new Array(1 << lt.maxDepth).fill(0).map((_, i) => i); | ||
const blockIndices = allIndices.filter((bi) => | ||
blocksByKey.has(blockKey(ltKey, bi)[0].toBase58()) | ||
); | ||
if (lt.executed.length !== 1 << lt.maxDepth) { | ||
await lazyProgram.methods | ||
.reinitializeExecutedTransactionsV0() | ||
.accounts({ | ||
lazyTransactions: ltKey, | ||
}) | ||
.rpc({ skipPreflight: true }); | ||
} | ||
const instructions = await Promise.all( | ||
blockIndices.map((bi) => | ||
lazyProgram.methods | ||
.closeMarkerV0({ | ||
index: bi, | ||
}) | ||
.accounts({ | ||
refund: provider.wallet.publicKey, | ||
lazyTransactions: ltKey, | ||
authority: provider.wallet.publicKey, | ||
}) | ||
.instruction() | ||
) | ||
); | ||
|
||
console.log(`${blocks.length} blocks to close`); | ||
const txns = chunks(instructions, 10).map(chunk => { | ||
const tx = new Transaction({ | ||
feePayer: provider.wallet.publicKey, | ||
}) | ||
tx.add(...chunk) | ||
return tx | ||
}) | ||
|
||
await bulkSendTransactions(provider, txns, (status) => { | ||
console.log(`Sending ${status.currentBatchProgress} / ${status.currentBatchSize} batch. ${status.totalProgress} / ${txns.length}`) | ||
}) | ||
console.log("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
programs/lazy-transactions/src/instructions/reinitialize_executed_transactions_v0.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::state::*; | ||
use crate::util::get_bitmap_len; | ||
use anchor_lang::prelude::*; | ||
use shared_utils::resize_to_fit; | ||
|
||
#[derive(Accounts)] | ||
pub struct ReinitializeExecutedTransactionsV0<'info> { | ||
#[account(mut)] | ||
pub payer: Signer<'info>, | ||
pub authority: Signer<'info>, | ||
#[account( | ||
mut, | ||
has_one = authority | ||
)] | ||
pub lazy_transactions: Box<Account<'info, LazyTransactionsV0>>, | ||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn handler(ctx: Context<ReinitializeExecutedTransactionsV0>) -> Result<()> { | ||
ctx.accounts.lazy_transactions.executed = | ||
vec![0; get_bitmap_len(ctx.accounts.lazy_transactions.max_depth)]; | ||
|
||
resize_to_fit( | ||
&ctx.accounts.payer.to_account_info(), | ||
&ctx.accounts.system_program.to_account_info(), | ||
&ctx.accounts.lazy_transactions, | ||
)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
pub fn get_bitmap_len(max_depth: u32) -> usize { | ||
let num_txns = 1 << max_depth; | ||
// Div ceil, https://stackoverflow.com/questions/72442853/how-would-you-perform-ceiling-division | ||
((num_txns + 8 - 1) / 8) as usize | ||
} | ||
|
||
pub fn is_executed(bitmap: &Vec<u8>, index: u32) -> bool { | ||
let byte = bitmap[index as usize / 8]; | ||
let bit = 1 << (index % 8); | ||
byte & bit != 0 | ||
} | ||
|
||
pub fn set_executed(bitmap: &mut Vec<u8>, index: u32) { | ||
let byte = &mut bitmap[index as usize / 8]; | ||
let bit = 1 << (index % 8); | ||
*byte |= bit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters