Skip to content

Commit

Permalink
Merge #4906
Browse files Browse the repository at this point in the history
4906: TransferTargetMode fix (#4192) r=EdHastingsCasperAssociation a=EdHastingsCasperAssociation

#4192 
As per title.


Co-authored-by: Ed Hastings <[email protected]>
  • Loading branch information
2 parents b25664a + d0b3345 commit 6e8cbe0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
7 changes: 3 additions & 4 deletions storage/src/global_state/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use crate::{
mint::Mint,
protocol_upgrade::{ProtocolUpgradeError, ProtocolUpgrader},
runtime_native::{Id, RuntimeNative},
transfer::{NewTransferTargetMode, TransferError, TransferRuntimeArgsBuilder},
transfer::{TransferError, TransferRuntimeArgsBuilder, TransferTargetMode},
},
tracking_copy::{TrackingCopy, TrackingCopyEntityExt, TrackingCopyError, TrackingCopyExt},
};
Expand Down Expand Up @@ -2075,11 +2075,10 @@ pub trait StateProvider {
);

match transfer_target_mode {
NewTransferTargetMode::ExistingAccount { .. }
| NewTransferTargetMode::PurseExists { .. } => {
TransferTargetMode::ExistingAccount { .. } | TransferTargetMode::PurseExists { .. } => {
// Noop
}
NewTransferTargetMode::CreateAccount(account_hash) => {
TransferTargetMode::CreateAccount(account_hash) => {
let main_purse = match runtime.mint(U512::zero()) {
Ok(uref) => uref,
Err(mint_error) => {
Expand Down
38 changes: 13 additions & 25 deletions storage/src/system/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,10 @@ impl From<TrackingCopyError> for TransferError {
}
}

/// A target mode indicates if a native transfer's arguments will resolve to an existing purse, or
/// will have to create a new account first.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TransferTargetMode {
/// Unknown target mode.
Unknown,
/// Native transfer arguments resolved into a transfer to a purse.
PurseExists(URef),
/// Native transfer arguments resolved into a transfer to an account.
CreateAccount(AccountHash),
}

/// A target mode indicates if a native transfer's arguments will resolve to an existing purse, or
/// will have to create a new account first.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum NewTransferTargetMode {
pub enum TransferTargetMode {
/// Native transfer arguments resolved into a transfer to an existing account.
ExistingAccount {
/// Existing account hash.
Expand All @@ -110,19 +98,19 @@ pub enum NewTransferTargetMode {
CreateAccount(AccountHash),
}

impl NewTransferTargetMode {
impl TransferTargetMode {
/// Target account hash, if any.
pub fn target_account_hash(&self) -> Option<AccountHash> {
match self {
NewTransferTargetMode::PurseExists {
TransferTargetMode::PurseExists {
target_account_hash,
..
} => *target_account_hash,
NewTransferTargetMode::ExistingAccount {
TransferTargetMode::ExistingAccount {
target_account_hash,
..
} => Some(*target_account_hash),
NewTransferTargetMode::CreateAccount(target_account_hash) => Some(*target_account_hash),
TransferTargetMode::CreateAccount(target_account_hash) => Some(*target_account_hash),
}
}
}
Expand Down Expand Up @@ -311,12 +299,12 @@ impl TransferRuntimeArgsBuilder {
/// If the "target" account hash is not existing, then a special variant is returned that
/// indicates that the system has to create new account first.
///
/// Returns [`NewTransferTargetMode`] with a resolved variant.
/// Returns [`TransferTargetMode`] with a resolved variant.
pub fn resolve_transfer_target_mode<R>(
&mut self,
protocol_version: ProtocolVersion,
tracking_copy: Rc<RefCell<TrackingCopy<R>>>,
) -> Result<NewTransferTargetMode, TransferError>
) -> Result<TransferTargetMode, TransferError>
where
R: StateReader<Key, StoredValue, Error = GlobalStateError>,
{
Expand All @@ -342,7 +330,7 @@ impl TransferRuntimeArgsBuilder {
return Err(TransferError::InvalidPurse);
}

return Ok(NewTransferTargetMode::PurseExists {
return Ok(TransferTargetMode::PurseExists {
purse_uref,
target_account_hash,
});
Expand Down Expand Up @@ -371,12 +359,12 @@ impl TransferRuntimeArgsBuilder {
{
Ok((_, entity)) => {
let main_purse_addable = entity.main_purse().with_access_rights(AccessRights::ADD);
Ok(NewTransferTargetMode::ExistingAccount {
Ok(TransferTargetMode::ExistingAccount {
target_account_hash: account_hash,
main_purse: main_purse_addable,
})
}
Err(_) => Ok(NewTransferTargetMode::CreateAccount(account_hash)),
Err(_) => Ok(TransferTargetMode::CreateAccount(account_hash)),
}
}

Expand Down Expand Up @@ -428,15 +416,15 @@ impl TransferRuntimeArgsBuilder {
let (to, target) = match self
.resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy))?
{
NewTransferTargetMode::ExistingAccount {
TransferTargetMode::ExistingAccount {
main_purse: purse_uref,
target_account_hash: target_account,
} => (Some(target_account), purse_uref),
NewTransferTargetMode::PurseExists {
TransferTargetMode::PurseExists {
target_account_hash,
purse_uref,
} => (target_account_hash, purse_uref),
NewTransferTargetMode::CreateAccount(_) => {
TransferTargetMode::CreateAccount(_) => {
// Method "build()" is called after `resolve_transfer_target_mode` is first called
// and handled by creating a new account. Calling `resolve_transfer_target_mode`
// for the second time should never return `CreateAccount` variant.
Expand Down

0 comments on commit 6e8cbe0

Please sign in to comment.