Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Write and test a Template for Genesis Accounts (Simple Coin transfer) #426

Open
Tracked by #89
neysofu opened this issue Nov 30, 2021 · 2 comments
Open
Tracked by #89
Assignees
Labels
AA Related to the Accounts Abstraction question simple-coin-iteration-1 svm

Comments

@neysofu
Copy link
Contributor

neysofu commented Nov 30, 2021

Genesis accounts should not be template-less, but rather they should have a template just like all other accounts (besides stubs as described in Self-Spawn). We must write a test a template that fits the bill that will spawn all accounts at genesis as created by go-svm.

@neysofu neysofu added must-for-mainnet Must be done before Mainnet v0.3 svm question labels Nov 30, 2021
@neysofu neysofu self-assigned this Nov 30, 2021
@neysofu neysofu changed the title Write and test a template for genesis accounts, which will only allow for svm_transfer Write and test a template for genesis accounts (simple coin transfer) Dec 8, 2021
@YaronWittenstein
Copy link
Contributor

Once the Template will work I believe we can discard these pieces of code:

  1. FFI svm_transfer API:

    /// Sends coins from the current executing account to a destination account.
    ///
    /// # Panics
    ///
    /// Panics when the destination account does not exist.
    #[must_use]
    #[no_mangle]
    pub unsafe extern "C" fn svm_transfer(
    runtime_ptr: *mut c_void,
    src_addr: *const u8,
    dst_addr: *const u8,
    amount: u64,
    ) -> svm_result_t {
    catch_unwind_or_fail(|| {
    let runtime = get_runtime(runtime_ptr);
    let src_account_addr = Address::new(std::slice::from_raw_parts(src_addr, Address::N));
    let dst_account_addr = Address::new(std::slice::from_raw_parts(dst_addr, Address::N));
    runtime.transfer(&src_account_addr, &dst_account_addr, amount);
    svm_result_t::OK
    })
    }

  2. Runtime transfer:

    /// Sends coins from the current executing account to a destination account.
    ///
    /// # Panics
    ///
    /// Panics when the destination account does not exist.
    pub fn transfer(&self, src_addr: &Address, dst_addr: &Address, amount: u64) {
    let mut src_account = AccountStorage::load(self.gs.clone(), src_addr).unwrap();
    let mut dst_account = if let Some((_bal, _counter, _addr)) = self.get_account(dst_addr) {
    AccountStorage::load(self.gs.clone(), dst_addr).unwrap()
    } else {
    panic!("Destination account does not exist")
    };
    let src_bal = src_account.balance().unwrap();
    let dst_bal = dst_account.balance().unwrap();
    if src_bal < amount {
    panic!("Not enough balance to execute transfer");
    }
    src_account
    .set_balance(src_bal.checked_sub(amount).unwrap())
    .unwrap();
    dst_account
    .set_balance(dst_bal.checked_add(amount).unwrap())
    .unwrap();
    }
    }

@YaronWittenstein YaronWittenstein changed the title Write and test a template for genesis accounts (simple coin transfer) Write and test a Template for Genesis Accounts (Simple Coin transfer) Dec 14, 2021
@YaronWittenstein YaronWittenstein added AA Related to the Accounts Abstraction and removed v0.3 must-for-mainnet Must be done before Mainnet labels Dec 15, 2021
@neysofu
Copy link
Contributor Author

neysofu commented Feb 16, 2022

Work for this issue is underway in the simple-coin-transfer-template/ crate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
AA Related to the Accounts Abstraction question simple-coin-iteration-1 svm
Projects
None yet
Development

No branches or pull requests

2 participants