Skip to content

Commit

Permalink
use deduct_value() instead of delta_token_balance() for substracting …
Browse files Browse the repository at this point in the history
…tokens (#857)
  • Loading branch information
qizhou authored Mar 25, 2020
1 parent 52e0d5f commit f37754c
Showing 1 changed file with 11 additions and 17 deletions.
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

0 comments on commit f37754c

Please sign in to comment.