Skip to content

Commit

Permalink
Adjust states in state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Dec 17, 2024
1 parent 07fc9a1 commit b104e8a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 91 deletions.
82 changes: 22 additions & 60 deletions gear-programs/vft-manager/app/src/services/request_bridging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ pub async fn handle_interrupted_transfer<T: ExecContext>(
service: &mut VftManager<T>,
msg_id: MessageId,
) -> Result<(U256, H160), Error> {
let state = service.state();

let config = service.config();
let msg_tracker = msg_tracker::msg_tracker_mut();

Expand All @@ -114,61 +112,36 @@ pub async fn handle_interrupted_transfer<T: ExecContext>(
vara_token_id,
sender,
amount,
receiver,
receiver: _,
token_supply,
} = msg_info.details;

let eth_token_id = service
.state()
.token_map
.get_eth_token_id(&vara_token_id)
.expect("Failed to get ethereum token id");

match msg_info.status {
MessageStatus::TokenDepositCompleted(true) => {
let payload = Payload {
receiver,
token_id: eth_token_id,
amount,
};

match bridge_builtin_operations::send_message_to_bridge_builtin(
state.gear_bridge_builtin,
state.erc20_manager_address,
payload,
config,
msg_id,
)
.await
{
Ok(nonce) => Ok((nonce, eth_token_id)),
Err(_) => {
match token_supply {
TokenSupply::Ethereum => {
token_operations::mint(vara_token_id, sender, amount, config, msg_id)
.await?;
}
TokenSupply::Gear => {
token_operations::unlock(vara_token_id, sender, amount, config, msg_id)
.await?;
}
}

// In case of failure, mint tokens back to the sender
Err(Error::TokensRefunded)
MessageStatus::TokenDepositCompleted(true)
| MessageStatus::SendingMessageToBridgeBuiltin
| MessageStatus::SendingMessageToReturnTokens
| MessageStatus::TokensReturnComplete(false)
| MessageStatus::BridgeResponseReceived(None) => {
set_critical_hook(msg_id);

match token_supply {
TokenSupply::Ethereum => {
token_operations::mint(vara_token_id, sender, amount, config, msg_id).await?;
}
TokenSupply::Gear => {
token_operations::unlock(vara_token_id, sender, amount, config, msg_id).await?;
}
}
}
MessageStatus::BridgeResponseReceived(Some(nonce)) => {
msg_tracker::msg_tracker_mut().remove_message_info(&msg_id);
Ok((nonce, eth_token_id))
}
MessageStatus::SendingMessageToReturnTokens | MessageStatus::TokenReturnFailed => {
// TODO: Or unlock.
token_operations::mint(vara_token_id, sender, amount, config, msg_id).await?;

Err(Error::TokensRefunded)
}
_ => {
MessageStatus::TokenDepositCompleted(false)
| MessageStatus::SendingMessageToDepositTokens
| MessageStatus::WaitingReplyFromTokenDepositMessage
| MessageStatus::WaitingReplyFromBuiltin
| MessageStatus::WaitingReplyFromTokenReturnMessage
| MessageStatus::BridgeResponseReceived(Some(_))
| MessageStatus::TokensReturnComplete(true) => {
panic!("Unexpected status or transaction completed.")
}
}
Expand All @@ -183,31 +156,20 @@ fn set_critical_hook(msg_id: MessageId) {

match msg_info.status {
MessageStatus::SendingMessageToDepositTokens => {
// If still sending, transition to `WaitingReplyFromBurn`.
msg_tracker.update_message_status(
msg_id,
MessageStatus::WaitingReplyFromTokenDepositMessage,
);
}
// MessageStatus::TokenDepositCompleted(false) => {
// // If the token burn fails, cancel the transaction.
// msg_tracker.remove_message_info(&msg_id);
// }
MessageStatus::SendingMessageToBridgeBuiltin => {
// If still sending, transition to `WaitingReplyFromBuiltin`.
msg_tracker.update_message_status(msg_id, MessageStatus::WaitingReplyFromBuiltin);
}
// MessageStatus::BridgeResponseReceived(None) => {
// // If error occurs during builtin message, go to mint step
// msg_tracker.update_message_status(msg_id, MessageStatus::ReturnTokensStep)
// }
MessageStatus::SendingMessageToReturnTokens => {
msg_tracker.update_message_status(
msg_id,
MessageStatus::WaitingReplyFromTokenReturnMessage,
);
}

_ => {}
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl MessageTracker {
pub fn check_withdraw_result(&mut self, msg_id: &MessageId) -> Result<(), Error> {
if let Some(info) = self.message_info.get(msg_id) {
match info.status {
MessageStatus::TokensReturned => Ok(()),
MessageStatus::TokenReturnFailed => Err(Error::MessageFailed),
MessageStatus::TokensReturnComplete(true) => Ok(()),
MessageStatus::TokensReturnComplete(false) => Err(Error::MessageFailed),
_ => Err(Error::InvalidMessageStatus),
}
} else {
Expand All @@ -109,10 +109,7 @@ pub enum MessageStatus {
// Return tokens statuses.
SendingMessageToReturnTokens,
WaitingReplyFromTokenReturnMessage,
TokensReturned,
TokenReturnFailed,

MessageProcessedWithSuccess(U256),
TokensReturnComplete(bool),
}

pub fn init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn mint(
config: &Config,
msg_id: MessageId,
) -> Result<(), Error> {
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToReturnTokens);
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens);

let bytes: Vec<u8> = vft_io::Mint::encode_call(receiver, amount);
utils::send_message_with_gas_for_reply(
Expand All @@ -80,7 +80,7 @@ pub async fn unlock(
config: &Config,
msg_id: MessageId,
) -> Result<(), Error> {
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToReturnTokens);
msg_tracker_mut().update_message_status(msg_id, MessageStatus::SendingMessageToWithdrawTokens);

let sender = gstd::exec::program_id();
let bytes: Vec<u8> = vft_io::TransferFrom::encode_call(sender, recepient, amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,14 @@ fn handle_reply_hook(msg_id: MessageId) {
let reply = match msg_info.details.token_supply {
TokenSupply::Ethereum => decode_burn_reply(&reply_bytes),
TokenSupply::Gear => decode_lock_reply(&reply_bytes),
};
}
.unwrap_or(false);

match reply {
Ok(reply) => {
msg_tracker
.update_message_status(msg_id, MessageStatus::TokenDepositCompleted(reply));
}
Err(_) => {
msg_tracker.remove_message_info(&msg_id);
}
};
msg_tracker.update_message_status(msg_id, MessageStatus::TokenDepositCompleted(reply));
}
MessageStatus::SendingMessageToBridgeBuiltin | MessageStatus::WaitingReplyFromBuiltin => {
let reply = decode_bridge_reply(&reply_bytes);
let result = match reply {
Ok(Some(nonce)) => Some(nonce),
_ => None,
};
msg_tracker
.update_message_status(msg_id, MessageStatus::BridgeResponseReceived(result));
let reply = decode_bridge_reply(&reply_bytes).ok().flatten();
msg_tracker.update_message_status(msg_id, MessageStatus::BridgeResponseReceived(reply));
}
MessageStatus::WaitingReplyFromTokenReturnMessage
| MessageStatus::SendingMessageToReturnTokens => {
Expand All @@ -70,13 +58,8 @@ fn handle_reply_hook(msg_id: MessageId) {
}
.unwrap_or(false);

if !reply {
msg_tracker.update_message_status(msg_id, MessageStatus::TokenReturnFailed);
} else {
msg_tracker.update_message_status(msg_id, MessageStatus::TokensReturned);
}
msg_tracker.update_message_status(msg_id, MessageStatus::TokensReturnComplete(reply));
}

_ => {}
};
}
Expand Down

0 comments on commit b104e8a

Please sign in to comment.