Solana中的跨合约调用
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Solana中的跨合约调用相关的知识,希望对你有一定的参考价值。
1. 引言
Solana runtime支持合约之间通过cross-program invocation 进行相互调用。跨合约调用是通过一个合约触发另一个合约的指令来完成的,invoking合约将halted直到invoked合约处理完该指令。
假设有2个链上合约token
和acme
,token
合约实现了pay()
,acme
合约实现了launch_missiles()
,同时在acme合约中实现了pay_and_launch_missiles()
接口来跨链调用token合约的pay()函数。
mod acme {
use token_instruction;
fn launch_missiles(accounts: &[AccountInfo]) -> Result<()> {
...
}
fn pay_and_launch_missiles(accounts: &[AccountInfo]) -> Result<()> {
let alice_pubkey = accounts[1].key;
let instruction = token_instruction::pay(&alice_pubkey);
invoke(&instruction, accounts)?;
launch_missiles(accounts)?;
}
invoke()
是默认嵌入在Solana runtime中的,负责routing the given instruction to the token
program via the instruction’s program_id
field。
注意,invoke
要求caller传输触发指令所需要的的所有accounts。即要求包含:
- the executable account (the ones that matches the instruction’s program id)
- 以及 the accounts passed to the instruction processor.
Before invoking pay()
, the runtime must ensure that acme
didn’t modify any accounts owned by token
. It does this by applying the runtime’s policy to the current state of the accounts at the time acme
calls invoke
vs. the initial state of the accounts at the beginning of the acme
's instruction. After pay()
completes, the runtime must again ensure that token
didn’t modify any accounts owned by acme
by again applying the runtime’s policy, but this time with the token
program ID. Lastly, after pay_and_launch_missiles()
completes, the runtime must apply the runtime policy one more time, where it normally would, but using all updated pre_*
variables. If executing pay_and_launch_missiles()
up to pay()
made no invalid account changes, pay()
made no invalid changes, and executing from pay()
until pay_and_launch_missiles()
returns made no invalid changes, then the runtime can transitively assume pay_and_launch_missiles()
as whole made no invalid account changes, and therefore commit all these account modifications.
客户端可创建一笔交易来修改2个accounts,每个account属于不同的链上合约:
let message = Message::new(vec![
token_instruction::pay(&alice_pubkey),
acme_instruction::launch_missiles(&bob_pubkey),
]);
client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], &message);
也可以直接在客户端调用acme
合约,通过acme
合约接口来触发token
指令:
let message = Message::new(vec![
acme_instruction::pay_and_launch_missiles(&alice_pubkey, &bob_pubkey),
]);
client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], &message);
参考资料
以上是关于Solana中的跨合约调用的主要内容,如果未能解决你的问题,请参考以下文章