Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

187 remove token block #192

Merged
merged 6 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions evm_loader/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,37 +597,6 @@ fn fill_holder_account(
// (caller_private, caller_ether, caller_sol, caller_nonce, caller_token, caller_holder)
// }

fn create_block_token_account(
config: &Config,
caller_ether: &H160,
caller_sol: &Pubkey,
) -> Result<Pubkey, Error> {
use solana_sdk::program_pack::Pack;
let creator = &config.signer;
let minimum_balance_for_account = config.rpc_client.get_minimum_balance_for_rent_exemption(spl_token::state::Account::LEN)?;
let holder_seed = bs58::encode(&caller_ether.to_fixed_bytes()).into_string() + "hold";
let caller_holder = Pubkey::create_with_seed(caller_sol, &holder_seed, &spl_token::id())?;
if config.rpc_client.get_account_with_commitment(&caller_holder, CommitmentConfig::confirmed())?.value.is_none() {
let instruction = Instruction::new_with_bincode(
config.evm_loader,
&(4_u32, caller_sol, holder_seed, minimum_balance_for_account, spl_token::state::Account::LEN, spl_token::id(), caller_holder),
vec![
AccountMeta::new(creator.pubkey(), true),
AccountMeta::new(caller_holder, false),
AccountMeta::new(*caller_sol, false),
AccountMeta::new(caller_holder, false),
AccountMeta::new_readonly(evm_loader::token::token_mint::id(), false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
]
);

send_transaction(config, &[instruction])?;
}
Ok(caller_holder)
}

fn get_ether_account_nonce(
config: &Config,
caller_sol: &Pubkey
Expand Down Expand Up @@ -883,7 +852,6 @@ fn command_deploy(

// Get caller nonce
let (trx_count, caller_ether, caller_token) = get_ether_account_nonce(config, &caller_arg)?;
let block_token = create_block_token_account(config, &caller_ether, &caller_arg)?;

let (program_id, program_ether, program_nonce, program_token, program_code, program_seed) =
get_ethereum_contract_account_credentials(config, &caller_ether, trx_count);
Expand Down Expand Up @@ -918,8 +886,6 @@ fn command_deploy(

AccountMeta::new(creator.pubkey(), true),
AccountMeta::new(collateral_pool_acc, false),
AccountMeta::new(block_token, false),
AccountMeta::new(caller_token, false),
AccountMeta::new(system_program::id(), false),

AccountMeta::new(program_id, false),
Expand Down Expand Up @@ -951,7 +917,6 @@ fn command_deploy(
AccountMeta::new(creator.pubkey(), true),
AccountMeta::new(operator_token, false),
AccountMeta::new(caller_token, false),
AccountMeta::new(block_token, false),
AccountMeta::new(system_program::id(), false),

AccountMeta::new(program_id, false),
Expand Down
56 changes: 6 additions & 50 deletions evm_loader/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,12 @@ fn process_instruction<'a>(

let operator_sol_info = next_account_info(account_info_iter)?;
let collateral_pool_sol_info = next_account_info(account_info_iter)?;
let block_acc = next_account_info(account_info_iter)?;
let user_eth_info = next_account_info(account_info_iter)?;
let system_info = next_account_info(account_info_iter)?;

let holder_data = holder_info.data.borrow();
let (unsigned_msg, signature) = get_transaction_from_data(&holder_data)?;

let trx_accounts = &accounts[7..];
let trx_accounts = &accounts[5..];

if !operator_sol_info.is_signer {
return Err!(ProgramError::InvalidAccountData);
Expand Down Expand Up @@ -315,15 +313,6 @@ fn process_instruction<'a>(
operator_sol_info,
storage_info,
system_info)?;
let fee = trx.gas_limit
.checked_mul(trx.gas_price).ok_or_else(||E!(ProgramError::InvalidArgument))?;
token::block_token(
accounts,
user_eth_info,
block_acc,
account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&fee)?;

if trx.to.is_some() {
do_partial_call(&mut storage, step_count, &account_storage, trx_accounts, trx.call_data, trx.value, trx_gas_limit)?;
Expand Down Expand Up @@ -412,11 +401,9 @@ fn process_instruction<'a>(
let sysvar_info = next_account_info(account_info_iter)?;
let operator_sol_info = next_account_info(account_info_iter)?;
let collateral_pool_sol_info = next_account_info(account_info_iter)?;
let block_acc = next_account_info(account_info_iter)?;
let user_eth_info = next_account_info(account_info_iter)?;
let system_info = next_account_info(account_info_iter)?;

let trx_accounts = &accounts[7..];
let trx_accounts = &accounts[5..];

let caller = H160::from_slice(from_addr);
let trx: UnsignedTransaction = rlp::decode(unsigned_msg).map_err(|e| E!(ProgramError::InvalidInstructionData; "DecoderError={:?}", e))?;
Expand Down Expand Up @@ -448,15 +435,6 @@ fn process_instruction<'a>(
operator_sol_info,
storage_info,
system_info)?;
let fee = trx.gas_limit
.checked_mul(trx.gas_price).ok_or_else(||E!(ProgramError::InvalidArgument))?;
token::block_token(
accounts,
user_eth_info,
block_acc,
account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&fee)?;

do_partial_call(&mut storage, step_count, &account_storage, trx_accounts, trx.call_data, trx.value, trx_gas_limit)?;

Expand All @@ -469,10 +447,9 @@ fn process_instruction<'a>(
let operator_sol_info = next_account_info(account_info_iter)?;
let operator_eth_info = next_account_info(account_info_iter)?;
let user_eth_info = next_account_info(account_info_iter)?;
let block_acc = next_account_info(account_info_iter)?;
let system_info = next_account_info(account_info_iter)?;

let trx_accounts = &accounts[6..];
let trx_accounts = &accounts[5..];

if !operator_sol_info.is_signer {
return Err!(ProgramError::InvalidAccountData);
Expand Down Expand Up @@ -507,22 +484,13 @@ fn process_instruction<'a>(
let gas_price_wei = U256::from(gas_price);
let fee = U256::from(used_gas)
.checked_mul(gas_price_wei).ok_or_else(||E!(ProgramError::InvalidArgument))?;
let return_fee = U256::from(gas_limit - used_gas)
.checked_mul(gas_price_wei).ok_or_else(||E!(ProgramError::InvalidArgument))?;
token::pay_token(
token::transfer_token(
accounts,
block_acc,
user_eth_info,
operator_eth_info,
account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&fee)?;
token::return_token(
accounts,
block_acc,
user_eth_info,
account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&return_fee)?;

applies_and_invokes(
program_id,
Expand All @@ -542,11 +510,9 @@ fn process_instruction<'a>(

let operator_sol_info = next_account_info(account_info_iter)?;
let incinerator_info = next_account_info(account_info_iter)?;
let block_acc = next_account_info(account_info_iter)?;
let user_eth_info = next_account_info(account_info_iter)?;
let system_info = next_account_info(account_info_iter)?;

let trx_accounts = &accounts[6..];
let trx_accounts = &accounts[4..];

if !operator_sol_info.is_signer {
return Err!(ProgramError::InvalidAccountData);
Expand All @@ -572,20 +538,10 @@ fn process_instruction<'a>(
_ => return Err!(ProgramError::InvalidAccountData),
};

let (gas_limit, gas_price) = storage.get_gas_params()?;
let return_fee = U256::from(gas_limit)
.checked_mul(U256::from(gas_price)).ok_or_else(||E!(ProgramError::InvalidArgument))?;
payment::burn_operators_deposit(
storage_info,
incinerator_info,
system_info)?;
token::return_token(
accounts,
block_acc,
user_eth_info,
account_storage.get_caller_account_info().ok_or_else(||E!(ProgramError::InvalidArgument))?,
account_storage.get_caller_account().ok_or_else(||E!(ProgramError::InvalidArgument))?,
&return_fee)?;

storage.unblock_accounts_and_destroy(program_id, trx_accounts)?;

Expand Down
135 changes: 0 additions & 135 deletions evm_loader/program/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,140 +155,5 @@ pub fn transfer_token(
let (ether, nonce) = source_solidity_account.get_seeds();
invoke_signed(&instruction, accounts, &[&[ether.as_bytes(), &[nonce]]])?;

Ok(())
}


/// Transfer Tokens to block account
///
/// # Errors
///
/// Could return:
/// `ProgramError::InvalidInstructionData`
pub fn block_token(
accounts: &[AccountInfo],
source_token_account: &AccountInfo,
target_token_account: &AccountInfo,
source_account: &AccountInfo,
source_solidity_account: &SolidityAccount,
value: &U256,
) -> Result<(), ProgramError> {
let (ether, _nonce) = source_solidity_account.get_seeds();
debug_print!("block_token");
if *source_token_account.key != spl_associated_token_account::get_associated_token_address(source_account.key, &token_mint::id()) {
debug_print!("invalid user token account");
debug_print!("target: {}", source_token_account.key);
debug_print!("expected: {}", spl_associated_token_account::get_associated_token_address(source_account.key, &token_mint::id()));
return Err!(ProgramError::InvalidInstructionData; "Invalid token account")
}
if get_token_account_owner(target_token_account)? != *source_account.key {
debug_print!("target ownership");
debug_print!("target owner {}", get_token_account_owner(target_token_account)?);
debug_print!("source key {}", source_account.key);
return Err!(ProgramError::InvalidInstructionData; "Invalid account owner")
}
let holder_seed = bs58::encode(&ether.to_fixed_bytes()).into_string() + "hold";
if *target_token_account.key != Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())? {
debug_print!("invalid hold token account");
debug_print!("target: {}", target_token_account.key);
debug_print!("expected: {}", Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())?);
return Err!(ProgramError::InvalidInstructionData; "Invalid token account")
}

transfer_token(
accounts,
source_token_account,
target_token_account,
source_account,
source_solidity_account,
value,
)?;

Ok(())
}


/// Transfer Tokens from block account to operator
///
/// # Errors
///
/// Could return:
/// `ProgramError::InvalidInstructionData`
pub fn pay_token(
accounts: &[AccountInfo],
source_token_account: &AccountInfo,
target_token_account: &AccountInfo,
source_account: &AccountInfo,
source_solidity_account: &SolidityAccount,
value: &U256,
) -> Result<(), ProgramError> {
let (ether, _nonce) = source_solidity_account.get_seeds();
debug_print!("pay_token");
let holder_seed = bs58::encode(&ether.to_fixed_bytes()).into_string() + "hold";
if *source_token_account.key != Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())? {
debug_print!("invalid hold token account");
debug_print!("target: {}", source_token_account.key);
debug_print!("expected: {}", Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())?);
return Err!(ProgramError::InvalidInstructionData; "Invalid token account")
}

transfer_token(
accounts,
source_token_account,
target_token_account,
source_account,
source_solidity_account,
value,
)?;

Ok(())
}


/// Return Tokens from block account to user
///
/// # Errors
///
/// Could return:
/// `ProgramError::InvalidInstructionData`
pub fn return_token(
accounts: &[AccountInfo],
source_token_account: &AccountInfo,
target_token_account: &AccountInfo,
source_account: &AccountInfo,
source_solidity_account: &SolidityAccount,
value: &U256,
) -> Result<(), ProgramError> {
let (ether, _nonce) = source_solidity_account.get_seeds();
debug_print!("return_token");
let holder_seed = bs58::encode(&ether.to_fixed_bytes()).into_string() + "hold";
if *source_token_account.key != Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())? {
debug_print!("invalid hold token account");
debug_print!("target: {}", source_token_account.key);
debug_print!("expected: {}", Pubkey::create_with_seed(source_account.key, &holder_seed, &spl_token::id())?);
return Err!(ProgramError::InvalidInstructionData; "Invalid token account")
}
if get_token_account_owner(target_token_account)? != *source_account.key {
debug_print!("target ownership");
debug_print!("target owner {}", get_token_account_owner(target_token_account)?);
debug_print!("source key {}", source_account.key);
return Err!(ProgramError::InvalidInstructionData; "Invalid token account owner")
}
if *target_token_account.key != spl_associated_token_account::get_associated_token_address(source_account.key, &token_mint::id()) {
debug_print!("invalid user token account");
debug_print!("target: {}", target_token_account.key);
debug_print!("expected: {}", spl_associated_token_account::get_associated_token_address(source_account.key, &token_mint::id()));
return Err!(ProgramError::InvalidInstructionData; "Invalid token account")
}

transfer_token(
accounts,
source_token_account,
target_token_account,
source_account,
source_solidity_account,
value,
)?;

Ok(())
}
18 changes: 0 additions & 18 deletions evm_loader/solana_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ def __create_solana_ether_caller(self, ethereum_transaction):
2000,
get_associated_token_address(PublicKey(caller), ETH_TOKEN_MINT_ID)
)
self.caller_holder = get_caller_hold_token(self.evm_loader, self.solana_wallet, ethereum_transaction.ether_caller)
print("Solana ether caller account:", ethereum_transaction._solana_ether_caller)

def __create_storage_account(self, seed):
Expand Down Expand Up @@ -284,10 +283,6 @@ def __sol_instr_09_partial_call(self, ethereum_transaction, step_count, data):
AccountMeta(pubkey=self.solana_wallet.public_key(), is_signer=True, is_writable=True),
# Collateral pool address:
AccountMeta(pubkey=self.collateral_pool_address, is_signer=False, is_writable=True),
# Operator ETH address (stub for now):
AccountMeta(pubkey=self.caller_holder, is_signer=False, is_writable=True),
# User ETH address (stub for now):
AccountMeta(pubkey=get_associated_token_address(PublicKey(ethereum_transaction._solana_ether_caller), ETH_TOKEN_MINT_ID), is_signer=False, is_writable=True),
# System program account:
AccountMeta(pubkey=PublicKey(system), is_signer=False, is_writable=False),

Expand Down Expand Up @@ -317,8 +312,6 @@ def __sol_instr_10_continue(self, ethereum_transaction, step_count):
AccountMeta(pubkey=get_associated_token_address(self.solana_wallet.public_key(), ETH_TOKEN_MINT_ID), is_signer=False, is_writable=True),
# User ETH address (stub for now):
AccountMeta(pubkey=get_associated_token_address(PublicKey(ethereum_transaction._solana_ether_caller), ETH_TOKEN_MINT_ID), is_signer=False, is_writable=True),
# Operator ETH address (stub for now):
AccountMeta(pubkey=self.caller_holder, is_signer=False, is_writable=True),
# System program account:
AccountMeta(pubkey=PublicKey(system), is_signer=False, is_writable=False),

Expand Down Expand Up @@ -631,17 +624,6 @@ def createEtherAccountTrx(self, ether, code_acc=None):
return (trx, sol)


def get_caller_hold_token(evm_loader, acc, caller_ether):
caller = evm_loader.ether2program(caller_ether)[0]
holder_seed = b58encode(caller_ether).decode('utf8') + "hold"
caller_holder = accountWithSeed(PublicKey(caller), holder_seed, PublicKey(tokenkeg))
if getBalance(caller_holder) == 0:
trx = Transaction()
trx.add(create_with_seed_loader_instruction(evm_loader.loader_id, acc.public_key(), caller_holder, PublicKey(caller), holder_seed, 10**9, ACCOUNT_LEN, PublicKey(tokenkeg)))
send_transaction(client, trx, acc)
return caller_holder


def create_with_seed_loader_instruction(evm_loader_id, funding, created, base, seed, lamports, space, owner):
return TransactionInstruction(
program_id=evm_loader_id,
Expand Down
2 changes: 0 additions & 2 deletions evm_loader/test_cli_emulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ def setUpClass(cls):
cls.loader.createEtherAccount(cls.ethereum_caller)
cls.spl_token.transfer(ETH_TOKEN_MINT_ID, 2000, get_associated_token_address(PublicKey(cls.caller), ETH_TOKEN_MINT_ID))
print("Done\n")

cls.caller_holder = get_caller_hold_token(cls.loader, cls.acc, cls.ethereum_caller)

print('Account: {} ({})'.format(cls.acc.public_key(), bytes(cls.acc.public_key()).hex()))
print('Ethereum Caller: {}-{}'.format(cls.ethereum_caller.hex(), cls.caller_nonce))
Expand Down
Loading