mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-27 04:26:07 +00:00
integrate evmc 'getBalance'
This commit is contained in:
parent
fdbc888840
commit
8c53371c49
@ -86,6 +86,12 @@ template getStorage*(c: Computation, slot: Uint256): Uint256 =
|
|||||||
else:
|
else:
|
||||||
c.vmState.readOnlyStateDB.getStorage(c.msg.contractAddress, slot)[0]
|
c.vmState.readOnlyStateDB.getStorage(c.msg.contractAddress, slot)[0]
|
||||||
|
|
||||||
|
template getBalance*(c: Computation, address: EthAddress): Uint256 =
|
||||||
|
when evmc_enabled:
|
||||||
|
c.host.getBalance(address)
|
||||||
|
else:
|
||||||
|
c.vmState.readOnlyStateDB.getBalance(address)
|
||||||
|
|
||||||
proc newComputation*(vmState: BaseVMState, message: Message): Computation =
|
proc newComputation*(vmState: BaseVMState, message: Message): Computation =
|
||||||
new result
|
new result
|
||||||
result.vmState = vmState
|
result.vmState = vmState
|
||||||
|
@ -61,7 +61,8 @@ proc setStorage*(ctx: HostContext, address: EthAddress,
|
|||||||
|
|
||||||
proc getBalance*(ctx: HostContext, address: EthAddress): Uint256 =
|
proc getBalance*(ctx: HostContext, address: EthAddress): Uint256 =
|
||||||
var address = toEvmc(address)
|
var address = toEvmc(address)
|
||||||
Uint256.fromEvmc ctx.host.get_balance(ctx.context, address.addr)
|
{.gcsafe.}:
|
||||||
|
Uint256.fromEvmc ctx.host.get_balance(ctx.context, address.addr)
|
||||||
|
|
||||||
proc getCodeSize*(ctx: HostContext, address: EthAddress): int =
|
proc getCodeSize*(ctx: HostContext, address: EthAddress): int =
|
||||||
var address = toEvmc(address)
|
var address = toEvmc(address)
|
||||||
|
@ -236,7 +236,7 @@ op address, inline = true:
|
|||||||
op balance, inline = true:
|
op balance, inline = true:
|
||||||
## 0x31, Get balance of the given account.
|
## 0x31, Get balance of the given account.
|
||||||
let address = c.stack.popAddress()
|
let address = c.stack.popAddress()
|
||||||
push: c.vmState.readOnlyStateDB.getBalance(address)
|
push: c.getBalance(address)
|
||||||
|
|
||||||
op origin, inline = true:
|
op origin, inline = true:
|
||||||
## 0x32, Get execution origination address.
|
## 0x32, Get execution origination address.
|
||||||
@ -378,8 +378,7 @@ op chainId, inline = true:
|
|||||||
|
|
||||||
op selfBalance, inline = true:
|
op selfBalance, inline = true:
|
||||||
## 0x47, Get current contract's balance.
|
## 0x47, Get current contract's balance.
|
||||||
let stateDb = c.vmState.readOnlyStateDb
|
push: c.getBalance(c.msg.contractAddress)
|
||||||
push: stateDb.getBalance(c.msg.contractAddress)
|
|
||||||
|
|
||||||
# ##########################################
|
# ##########################################
|
||||||
# 50s: Stack, Memory, Storage and Flow Operations
|
# 50s: Stack, Memory, Storage and Flow Operations
|
||||||
@ -517,9 +516,7 @@ proc canTransfer(c: Computation, memPos, memLen: int, value: Uint256, opCode: st
|
|||||||
# the sender is childmsg sender, not parent msg sender
|
# the sender is childmsg sender, not parent msg sender
|
||||||
# perhaps we need to move this code somewhere else
|
# perhaps we need to move this code somewhere else
|
||||||
# to avoid confusion
|
# to avoid confusion
|
||||||
let senderBalance =
|
let senderBalance = c.getBalance(c.msg.contractAddress)
|
||||||
c.vmState.readOnlyStateDb().
|
|
||||||
getBalance(c.msg.contractAddress)
|
|
||||||
|
|
||||||
if senderBalance < value:
|
if senderBalance < value:
|
||||||
debug "Computation Failure", reason = "Insufficient funds available to transfer", required = c.msg.value, balance = senderBalance
|
debug "Computation Failure", reason = "Insufficient funds available to transfer", required = c.msg.value, balance = senderBalance
|
||||||
@ -729,7 +726,7 @@ template genCall(callName: untyped, opCode: Op): untyped =
|
|||||||
c.memory.extend(memOutPos, memOutLen)
|
c.memory.extend(memOutPos, memOutLen)
|
||||||
|
|
||||||
when opCode in {CallCode, Call}:
|
when opCode in {CallCode, Call}:
|
||||||
let senderBalance = c.vmState.readOnlyStateDb().getBalance(sender)
|
let senderBalance = c.getBalance(sender)
|
||||||
if senderBalance < value:
|
if senderBalance < value:
|
||||||
debug "Insufficient funds", available = senderBalance, needed = c.msg.value
|
debug "Insufficient funds", available = senderBalance, needed = c.msg.value
|
||||||
# return unused gas
|
# return unused gas
|
||||||
@ -826,8 +823,8 @@ proc selfDestructImpl(c: Computation, beneficiary: EthAddress) =
|
|||||||
# In particular, EIP150 and EIP161 have extra requirements.
|
# In particular, EIP150 and EIP161 have extra requirements.
|
||||||
c.vmState.mutateStateDB:
|
c.vmState.mutateStateDB:
|
||||||
let
|
let
|
||||||
localBalance = db.getBalance(c.msg.contractAddress)
|
localBalance = c.getBalance(c.msg.contractAddress)
|
||||||
beneficiaryBalance = db.getBalance(beneficiary)
|
beneficiaryBalance = c.getBalance(beneficiary)
|
||||||
|
|
||||||
# Transfer to beneficiary
|
# Transfer to beneficiary
|
||||||
db.setBalance(beneficiary, localBalance + beneficiaryBalance)
|
db.setBalance(beneficiary, localBalance + beneficiaryBalance)
|
||||||
@ -867,7 +864,7 @@ op selfDestructEip161, inline = false:
|
|||||||
beneficiary = c.stack.popAddress()
|
beneficiary = c.stack.popAddress()
|
||||||
stateDb = c.vmState.readOnlyStateDb
|
stateDb = c.vmState.readOnlyStateDb
|
||||||
isDead = stateDb.isDeadAccount(beneficiary)
|
isDead = stateDb.isDeadAccount(beneficiary)
|
||||||
balance = stateDb.getBalance(c.msg.contractAddress)
|
balance = c.getBalance(c.msg.contractAddress)
|
||||||
|
|
||||||
let gasParams = GasParams(kind: SelfDestruct,
|
let gasParams = GasParams(kind: SelfDestruct,
|
||||||
sd_condition: isDead and not balance.isZero
|
sd_condition: isDead and not balance.isZero
|
||||||
|
Loading…
x
Reference in New Issue
Block a user