Magicblock 的 Ephemeral Rollups 旨在解决 Solana L1 上的高延迟和吞吐量限制问题,以及 L2 链带来的流动性碎片化和互操作性问题。它通过创建临时的、高速的执行层,实现 10-50 毫秒的端到端延迟,并保持与 Solana 的同步,无需跨链桥或多链设置,并提供链上扩展功能,从而为开发者提供构建响应迅速、可扩展的区块链应用所需的快速、弹性和安全保障。
MagicBlock 的短暂 rollups 解决了构建响应迅速、可扩展的区块链应用程序的核心限制。它们消除了逐块执行的延迟,消除了对桥或 appchains 的需求,并为开发者提供了感觉像 Web2 的即时、弹性计算;但以 Web3 保证运行。
无论你是构建实时游戏、闪电般快速的 DeFi,还是传感器驱动的 DePIN 网络,MagicBlock 都能为你提供速度、规模和安全性
以下是一个事务通常如何在基础层(约 400 毫秒)上流动:
基础层执行
以下是相同的交互在 MagicBlock 短暂 Rollup(约 50 毫秒)上的运行情况:
要了解 MagicBlock 的实际应用,请查看 LanaRoads —— 一款完全使用 MagicBlock 的短暂 rollups 在 Solana 上构建的快节奏游戏
LanaRoads
要使用短暂 Rollups 构建快速、实时的应用程序,遵循一些核心原则非常重要。这些原则确保 rollup 与 Solana 保持同步,并且你的应用程序可以在大规模下可靠地工作。
Rollups 不创建帐户。所有帐户都必须源自 Solana 基础层。
委派执行高频操作的帐户。保持范围最小且有目的性。
在委托期间,帐户状态由 rollup 控制。在此期间,Solana 无法读取或写入它们。
将所有涉及委托帐户的事务发送到短暂 Rollup RPC。Solana 的 RPC 会拒绝它们。
以适当的间隔将帐户状态同步回 Solana。仅提交仍在委托的帐户。
一旦不再需要实时执行,请取消委派。该帐户再次可以在 Solana 上完全访问。
这些规则不仅仅是约定。它们由 Solana 的帐户模型在底层的工作方式强制执行。
Solana 上的每个帐户都有一个所有者程序,用于控制其写入权限。当你将帐户委托给短暂 Rollup 时,你将通过 CPI 调用委托程序,暂时将该所有权转移到 rollup 的验证器。
这会更改谁有权控制帐户:
如果你尝试从 rollup 中修改非委托帐户,则事务将失败 —— 因为 rollup 没有权限。
这种所有权切换使实时执行成为可能,而不会破坏 Solana 的安全性或可组合性模型。但这也意味着你必须小心管理委托。
让我们将一个基本的 Solana 计数器程序转换为由 MagicBlock 的短暂 Rollups 提供支持的实时应用程序。
以下是使用 Anchor 的基本程序逻辑:
##[program]
pub mod base_counter {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let counter = &mut ctx.accounts.counter; // counter 变量
counter.count = 0; // count 计数器初始化为0
Ok(())
}
pub fn increment(ctx: Context<Increment>) -> Result<()> {
let counter = &mut ctx.accounts.counter; // counter 变量
counter.count += 1; // 计数器加1
Ok(())
}
}
##[account]
pub struct Counter {
pub count: u64,
}
在你的项目中安装 SDK:
cargo add ephemeral_rollups_sdk
在你的程序中导入它:
use ephemeral_rollups_sdk::anchor::{commit, delegate, ephemeral};
use ephemeral_rollups_sdk::cpi::DelegateConfig;
use ephemeral_rollups_sdk::ephem::{commit_accounts, commit_and_undelegate_accounts};
\#[ephemeral]
宏这会将你的程序标记为与 MagicBlock 的短暂执行层兼容:
##[ephemeral]
##[program]
pub mod anchor_counter { ... }
这可确保该程序可以在 rollup 验证器中运行以进行快速执行。
你现在像以前一样定义你的程序逻辑,但添加了以下附加函数:
示例:increment_and_commit
函数
pub fn increment_and_commit(ctx: Context<IncrementAndCommit>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count += 1;
msg!("PDA {} count: {}", counter.key(), counter.count); // 打印 PDA key 和当前的计数
commit_accounts(
&ctx.accounts.payer,
vec![&ctx.accounts.counter.to_account_info()],
&ctx.accounts.magic_context,
&ctx.accounts.magic_program,
)?;
Ok(())
}
#[commit]
和 #[delegate]
宏会自动注入 MagicBlock 所需的上下文:
##[delegate]
##[derive(Accounts)]
pub struct DelegateInput<'info> {
pub payer: Signer<'info>,
#[account(mut, del)]
pub pda: AccountInfo<'info>, // 被委托的帐户
}
##[commit]
##[derive(Accounts)]
pub struct IncrementAndCommit<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut, seeds = [TEST_PDA_SEED], bump)]
pub counter: Account<'info, Counter>,
}
你现在在程序中有 3 个重要方法:
delegate
:将帐户的控制权转移到 rollup 验证器commit
:将 rollup 状态写回 Solanaundelegate
:将控制权返回给 Solana 基础层ctx.accounts.delegate_pda(
&ctx.accounts.payer,
&[TEST_PDA_SEED],
DelegateConfig::default(),
)?;
commit_and_undelegate_accounts(
&ctx.accounts.payer,
vec![&ctx.accounts.counter.to_account_info()],
&ctx.accounts.magic_context,
&ctx.accounts.magic_program,
)?;
const provider = anchor.AnchorProvider.env(); // Solana 基础层
const providerEphemeralRollup = new anchor.AnchorProvider(
new anchor.web3.Connection(
process.env.PROVIDER_ENDPOINT || "https://devnet.magicblock.app/",
{
wsEndpoint: process.env.WS_ENDPOINT || "wss://devnet.magicblock.app/",
}
),
anchor.Wallet.local()
);
it("Delegate Counter to ER", async () => {
let tx = await program.methods
.delegate()
.accounts({
owner: owner.publicKey,
pda: counterPda,
})
.transaction();
tx.feePayer = owner.publicKey;
tx.recentBlockhash = (await provider.connection.getLatestBlockhash()).blockhash;
// 由 ER 钱包签名(重要!)
tx = await providerEphemeralRollup.wallet.signTransaction(tx);
const txHash = await provider.sendAndConfirm(tx, [], {
skipPreflight: true,
commitment: "confirmed",
});
});
it("Increase Counter on ER", async () => {
let tx = await program.methods
.increment()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.transaction();
tx.feePayer = providerEphemeralRollup.wallet.publicKey;
tx.recentBlockhash = (
await providerEphemeralRollup.connection.getLatestBlockhash()
).blockhash;
tx = await providerEphemeralRollup.wallet.signTransaction(tx);
const txHash = await anchor.web3.sendAndConfirmTransaction(
providerEphemeralRollup.connection,
tx,
[owner.payer],
{
skipPreflight: true,
commitment: "confirmed",
}
);
});
it("Commit Counter on ER", async () => {
let tx = await program.methods
.commit()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.transaction();
tx.feePayer = providerEphemeralRollup.wallet.publicKey;
tx.recentBlockhash = (
await providerEphemeralRollup.connection.getLatestBlockhash()
).blockhash;
tx = await providerEphemeralRollup.wallet.signTransaction(tx);
const txHash = await anchor.web3.sendAndConfirmTransaction(
providerEphemeralRollup.connection,
tx,
[owner.payer],
{
skipPreflight: true,
commitment: "confirmed",
}
);
// 等待基础层确认包含
const txCommitSgn = await GetCommitmentSignature(
txHash,
providerEphemeralRollup.connection
);
});
it("Commit and undelegate counter on ER", async () => {
let tx = await program.methods
.undelegate()
.accounts({
owner: owner.publicKey,
counter: counterPda,
})
.transaction();
tx.feePayer = owner.publicKey;
tx.recentBlockhash = (
await provider.connection.getLatestBlockhash()
).blockhash;
tx = await providerEphemeralRollup.wallet.signTransaction(tx);
const txHash = await anchor.web3.sendAndConfirmTransaction(
providerEphemeralRollup.connection,
tx,
[owner.payer],
{
skipPreflight: true,
commitment: "confirmed",
}
);
});
在某些用例中,你可能想要组合操作:
await program.methods
.incrementAndCommit()
.accounts({ owner: owner.publicKey, counter: counterPda })
.rpc();
await program.methods
.incrementAndUndelegate()
.accounts({ owner: owner.publicKey, counter: counterPda })
.rpc();
MagicBlock 使在 Solana 上构建快速、实时的应用程序变得简单。短暂 Rollups 为你提供低延迟执行、大规模扩展和完全的可组合性,而无需忍受桥的痛苦或碎片化的流动性。
委托帐户,运行高速逻辑,并在需要时提交回 Solana。它感觉像 Web2,但以 Web3 安全性运行。
如果你正在构建游戏、DeFi 或实时系统,MagicBlock 将帮助你从零变为实时。
如果你对去中心化基础设施、链上数据系统或使用预言机网络构建真实项目感兴趣,请关注:
- 原文链接: blog.blockmagnates.com/f...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!