Skip to content

use deduct_value() instead of delta_token_balance() for subtracting tokens #857

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

Merged
merged 1 commit into from
Mar 25, 2020
Merged
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
28 changes: 11 additions & 17 deletions quarkchain/evm/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
InvalidNativeToken,
)
from quarkchain.evm.slogging import get_logger
from quarkchain.utils import token_id_decode
from quarkchain.utils import token_id_decode, check
from quarkchain.evm.specials import SystemContract

log = get_logger("eth.block")
Expand Down Expand Up @@ -372,35 +372,29 @@ def apply_transaction(state, tx: transactions.Transaction, tx_wrapper_hash):
)

# buy startgas
assert (
state.get_balance(tx.sender, token_id=tx.gas_token_id)
>= tx.startgas * tx.gasprice
)
gasprice, refund_rate = tx.gasprice, 100
# convert gas if using non-genesis native token
if gasprice != 0 and tx.gas_token_id != state.genesis_token:
refund_rate, converted_genesis_token_gas_price = pay_native_token_as_gas(
state, tx.gas_token_id, tx.startgas, tx.gasprice
)
# guaranteed by validation
assert converted_genesis_token_gas_price > 0
check(converted_genesis_token_gas_price > 0)
gasprice = converted_genesis_token_gas_price
contract_addr = SystemContract.GENERAL_NATIVE_TOKEN.addr()
# guaranteed by validation
assert (
state.get_balance(contract_addr, token_id=state.genesis_token)
>= tx.startgas * converted_genesis_token_gas_price
)
state.delta_token_balance(
contract_addr,
state.genesis_token,
-tx.startgas * converted_genesis_token_gas_price,
check(
state.deduct_value(
contract_addr,
state.genesis_token,
tx.startgas * converted_genesis_token_gas_price,
)
)
state.delta_token_balance(
contract_addr, tx.gas_token_id, tx.startgas * tx.gasprice
)

state.delta_token_balance(tx.sender, tx.gas_token_id, -tx.startgas * tx.gasprice)
check(state.deduct_value(tx.sender, tx.gas_token_id, tx.startgas * tx.gasprice))

message_data = vm.CallData([safe_ord(x) for x in tx.data], 0, len(tx.data))
message = vm.Message(
Expand Down Expand Up @@ -435,7 +429,7 @@ def apply_transaction(state, tx: transactions.Transaction, tx_wrapper_hash):
# Currently, burn all gas
local_gas_used = tx.startgas
elif tx.to == b"":
state.delta_token_balance(tx.sender, tx.transfer_token_id, -tx.value)
check(state.deduct_value(tx.sender, tx.transfer_token_id, tx.value))
remote_gas_reserved = tx.startgas - intrinsic_gas
ext.add_cross_shard_transaction_deposit(
quarkchain.core.CrossShardTransactionDeposit(
Expand All @@ -460,7 +454,7 @@ def apply_transaction(state, tx: transactions.Transaction, tx_wrapper_hash):
)
success = 1
else:
state.delta_token_balance(tx.sender, tx.transfer_token_id, -tx.value)
check(state.deduct_value(tx.sender, tx.transfer_token_id, tx.value))
if (
state.qkc_config.ENABLE_EVM_TIMESTAMP is None
or state.timestamp >= state.qkc_config.ENABLE_EVM_TIMESTAMP
Expand Down