Skip to content

Commit

Permalink
xcall validation for network address, rollback's unittest update
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Aug 26, 2024
1 parent 3576bcd commit f41ad79
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 186 deletions.
9 changes: 2 additions & 7 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
resolution = false
skip-lint = false

[programs."https://api.devnet.solana.com"]
asset_manager = "Gk5un55A5ExrMvx7rp4HvygNik2trCrM325rTDBGGSbb"
balanced_dollar = "9cmYrvKr6JxkQfkTM5Nuf1LTGSGPRH4x3VczCa6GqjkE"
xcall_manager = "D8E4MdQH6p3vfiX23Kq5x4xM1v4aUfhp3sM2HYGinvzk"

[programs.localnet]
asset_manager = "Gk5un55A5ExrMvx7rp4HvygNik2trCrM325rTDBGGSbb"
balanced_dollar = "9cmYrvKr6JxkQfkTM5Nuf1LTGSGPRH4x3VczCa6GqjkE"
xcall_manager = "D8E4MdQH6p3vfiX23Kq5x4xM1v4aUfhp3sM2HYGinvzk"

[registry]
url = "https://api.devnet.solana.com"
url = "https://api.apr.dev"

[provider]
cluster = "Devnet"
cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[scripts]
Expand Down
9 changes: 7 additions & 2 deletions Anchor2.toml → Anchor1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
resolution = false
skip-lint = false

[programs."https://api.devnet.solana.com"]
asset_manager = "Gk5un55A5ExrMvx7rp4HvygNik2trCrM325rTDBGGSbb"
balanced_dollar = "9cmYrvKr6JxkQfkTM5Nuf1LTGSGPRH4x3VczCa6GqjkE"
xcall_manager = "D8E4MdQH6p3vfiX23Kq5x4xM1v4aUfhp3sM2HYGinvzk"

[programs.localnet]
asset_manager = "Gk5un55A5ExrMvx7rp4HvygNik2trCrM325rTDBGGSbb"
balanced_dollar = "9cmYrvKr6JxkQfkTM5Nuf1LTGSGPRH4x3VczCa6GqjkE"
xcall_manager = "D8E4MdQH6p3vfiX23Kq5x4xM1v4aUfhp3sM2HYGinvzk"

[registry]
url = "https://api.apr.dev"
url = "https://api.devnet.solana.com"

[provider]
cluster = "Localnet"
cluster = "Devnet"
wallet = "~/.config/solana/id.json"

[scripts]
Expand Down
6 changes: 4 additions & 2 deletions programs/asset-manager/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ fn handle_token_call_message<'info>(
bump,
)?;
} else if method == DEPOSIT_REVERT {
if from != state.xcall.key().to_string() {
let from_network_address = NetworkAddress::from_str(&from)?;
if from_network_address.account() != state.xcall.to_string() {
return Err(AssetManagerError::UnauthorizedCaller.into())
}

Expand Down Expand Up @@ -439,7 +440,8 @@ fn handle_native_call_message<'info>(
bump,
)?;
} else if method == DEPOSIT_REVERT {
if from != state.xcall.key().to_string() {
let from_network_address = NetworkAddress::from_str(&from)?;
if from_network_address.account() != state.xcall.to_string() {
return Err(AssetManagerError::NotIconAssetManager.into())
}
let message = decode_deposit_revert_msg(&data)?;
Expand Down
3 changes: 2 additions & 1 deletion programs/balanced-dollar/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ pub fn handle_call_message<'info>(
message: "Success".to_owned(),
});
} else if method == CROSS_TRANSFER_REVERT {
if from != state.xcall.to_string() {
let from_network_address = NetworkAddress::from_str(&from)?;
if from_network_address.account() != state.xcall.to_string() {
return Ok(HandleCallMessageResponse {
success: false,
message: BalancedDollarError::InvalidSender.to_string(),
Expand Down
112 changes: 80 additions & 32 deletions tests/balanced_dollar/balanced_dollar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const xcallProgram: anchor.Program<Xcall> = new anchor.Program(
import {
CSMessage,
CSMessageRequest,
CSMessageResult,
CSMessageType,
CSResponseType,
MessageType,
} from "../utils/types";
import { TestContext as XcallContext, XcallPDA } from "../xcall/xcall/setup";
Expand Down Expand Up @@ -203,46 +205,91 @@ describe("balanced dollar manager", () => {
const fromNetwork = "icon";
let nextReqId = xcallConfig.lastReqId.toNumber() + 1;
let nextSequenceNo = xcallConfig.sequenceNo.toNumber() + 1;

const stateAccount = await program.account.state.fetch(
BalancedDollarPDA.state().pda
let crossTransferTx = await program.methods
.crossTransfer("", new anchor.BN(1000000000), Buffer.alloc(0))
.accountsStrict({
from: withdrawerTokenAccount.address,
fromAuthority: withdrawerKeyPair.publicKey,
state: BalancedDollarPDA.state().pda,
mint: mint,
xcallManagerState: BalancedDollarPDA.xcall_manager_state().pda,
xcallConfig: XcallPDA.config().pda,
xcall: xcall_program.programId,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SYSTEM_PROGRAM_ID,
xcallAuthority: BalancedDollarPDA.xcall_authority().pda,
})
.remainingAccounts([
{
pubkey: XcallPDA.config().pda,
isSigner: false,
isWritable: true,
},
{
pubkey: XcallPDA.rollback(nextSequenceNo).pda,
isSigner: false,
isWritable: true,
},
{
pubkey: new PublicKey("Sysvar1nstructions1111111111111111111111111"),
isSigner: false,
isWritable: false,
},
{
pubkey: xcallConfig.feeHandler,
isSigner: false,
isWritable: true,
},
//connection params
{
pubkey: connectionProgram.programId,
isSigner: false,
isWritable: true,
},
{
pubkey: ConnectionPDA.config().pda,
isSigner: false,
isWritable: true,
},
{
pubkey: ConnectionPDA.network_fee("icon").pda,
isSigner: false,
isWritable: true,
},
])
.instruction();
let tx = await ctx.txnHelpers.buildV0Txn(
[crossTransferTx],
[withdrawerKeyPair]
);
let iconBnUsd = stateAccount.iconBnUsd;
let bytes = Buffer.alloc(0);
let sender = Keypair.generate();
const data = [
"xCrossTransferRevert",
withdrawerTokenAccount.address.toString(),
20000000000,
];
const rlpEncodedData = rlp.encode(data);

let request = new CSMessageRequest(
iconBnUSD,
program.programId.toString(),
try{
let txHash = await ctx.connection.sendTransaction(tx);
await txnHelpers.logParsedTx(txHash);


let result = new CSMessageResult(
nextSequenceNo,
MessageType.CallMessageWithRollback,
Buffer.from(rlpEncodedData),
[connectionProgram.programId.toString()]
CSResponseType.CSMessageFailure,
new Uint8Array([])
);

let cs_message = new CSMessage(
CSMessageType.CSMessageRequest,
request.encode()
let csMessage = new CSMessage(
CSMessageType.CSMessageResult,
result.encode()
).encode();

let recvMessageAccounts = await connectionCtx.getRecvMessageAccounts(
connSn,
nextSequenceNo,
cs_message,
CSMessageType.CSMessageRequest
csMessage,
CSMessageType.CSMessageResult
);

await connectionProgram.methods
.recvMessage(
fromNetwork,
new anchor.BN(connSn),
Buffer.from(cs_message),
Buffer.from(csMessage),
new anchor.BN(nextSequenceNo)
)
.accountsStrict({
Expand All @@ -258,30 +305,31 @@ describe("balanced dollar manager", () => {

await sleep(2);
// call xcall execute_call
let executeCallAccounts = await xcallCtx.getExecuteCallAccounts(
let executeRollbackAccounts = await xcallCtx.getExecuteRollbackAccounts(
nextReqId,
Buffer.from(rlpEncodedData),
BalancedDollarPDA.state().pda,
program.programId
);
await xcallProgram.methods
.executeCall(
new anchor.BN(nextReqId),
Buffer.from(rlpEncodedData),
.executeRollback(
new anchor.BN(nextSequenceNo),
)
.accounts({
signer: ctx.admin.publicKey,
systemProgram: SYSTEM_PROGRAM_ID,
config: XcallPDA.config().pda,
admin: xcallConfig.admin,
proxyRequest: XcallPDA.proxyRequest(nextReqId).pda,
rollbackAccount: XcallPDA.rollback(nextSequenceNo).pda,
})
.remainingAccounts([
// ACCOUNTS TO CALL CONNECTION SEND_MESSAGE
...executeCallAccounts.slice(4),
...executeRollbackAccounts.slice(4),
])
.signers([ctx.admin])
.rpc();
} catch (e){
console.log(e);
}
});

it("cross transfer test", async () => {
Expand Down
Loading

0 comments on commit f41ad79

Please sign in to comment.