fixes 32bit problem

This commit is contained in:
andri lim 2019-10-26 08:24:43 +07:00
parent 8a09e97d4d
commit dec3c7baee
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 30 additions and 25 deletions

View File

@ -69,13 +69,13 @@ type
c_isNewAccount*: bool c_isNewAccount*: bool
c_gasBalance*: GasInt c_gasBalance*: GasInt
c_contractGas*: Uint256 c_contractGas*: Uint256
c_currentMemSize*: Natural c_currentMemSize*: GasNatural
c_memOffset*: Natural c_memOffset*: GasNatural
c_memLength*: Natural c_memLength*: GasNatural
of Create: of Create:
cr_currentMemSize*: Natural cr_currentMemSize*: GasNatural
cr_memOffset*: Natural cr_memOffset*: GasNatural
cr_memLength*: Natural cr_memLength*: GasNatural
of SelfDestruct: of SelfDestruct:
sd_condition*: bool sd_condition*: bool
else: else:
@ -99,7 +99,7 @@ type
of GckDynamic: of GckDynamic:
d_handler*: proc(value: Uint256): GasInt {.nimcall, gcsafe.} d_handler*: proc(value: Uint256): GasInt {.nimcall, gcsafe.}
of GckMemExpansion: of GckMemExpansion:
m_handler*: proc(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall, gcsafe.} m_handler*: proc(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall, gcsafe.}
of GckComplex: of GckComplex:
c_handler*: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.} c_handler*: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.}
# We use gasCost/gasRefund for: # We use gasCost/gasRefund for:
@ -118,7 +118,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
# ############### Helper functions ############################## # ############### Helper functions ##############################
func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: Natural): GasInt {.inline.} = func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.inline.} =
# Input: size (in bytes) # Input: size (in bytes)
# Yellow Paper: # Yellow Paper:
@ -185,23 +185,23 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
gasParams.cr_memOffset, gasParams.cr_memOffset,
gasParams.cr_memLength) gasParams.cr_memLength)
func `prefix gasSha3`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasSha3`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasSha3]) + result += static(FeeSchedule[GasSha3]) +
static(FeeSchedule[GasSha3Word]) * (memLength).wordCount static(FeeSchedule[GasSha3Word]) * (memLength).wordCount
func `prefix gasCopy`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasCopy`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = static(FeeSchedule[GasVeryLow]) + result = static(FeeSchedule[GasVeryLow]) +
static(FeeSchedule[GasCopy]) * memLength.wordCount static(FeeSchedule[GasCopy]) * memLength.wordCount
result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
func `prefix gasExtCodeCopy`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasExtCodeCopy`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = static(FeeSchedule[GasExtCode]) + result = static(FeeSchedule[GasExtCode]) +
static(FeeSchedule[GasCopy]) * memLength.wordCount static(FeeSchedule[GasCopy]) * memLength.wordCount
result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
func `prefix gasLoadStore`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLoadStore`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = static(FeeSchedule[GasVeryLow]) result = static(FeeSchedule[GasVeryLow])
result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
@ -223,34 +223,34 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if value.isZero and not gasParams.s_isStorageEmpty: if value.isZero and not gasParams.s_isStorageEmpty:
result.gasRefund = static(FeeSchedule[RefundSclear]) result.gasRefund = static(FeeSchedule[RefundSclear])
func `prefix gasLog0`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLog0`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasLog]) + result += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength static(FeeSchedule[GasLogData]) * memLength
func `prefix gasLog1`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLog1`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasLog]) + result += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength + static(FeeSchedule[GasLogData]) * memLength +
static(FeeSchedule[GasLogTopic]) static(FeeSchedule[GasLogTopic])
func `prefix gasLog2`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLog2`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasLog]) + result += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength + static(FeeSchedule[GasLogData]) * memLength +
static(2 * FeeSchedule[GasLogTopic]) static(2 * FeeSchedule[GasLogTopic])
func `prefix gasLog3`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLog3`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasLog]) + result += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength + static(FeeSchedule[GasLogData]) * memLength +
static(3 * FeeSchedule[GasLogTopic]) static(3 * FeeSchedule[GasLogTopic])
func `prefix gasLog4`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasLog4`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) result = `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
result += static(FeeSchedule[GasLog]) + result += static(FeeSchedule[GasLog]) +
@ -343,7 +343,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if not value.isZero and gasParams.kind in {Call, CallCode}: if not value.isZero and gasParams.kind in {Call, CallCode}:
result.gasRefund += static(FeeSchedule[GasCallStipend]) result.gasRefund += static(FeeSchedule[GasCallStipend])
func `prefix gasHalt`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasHalt`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
`prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength) `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
func `prefix gasSelfDestruct`(value: Uint256, gasParams: Gasparams): GasResult {.nimcall.} = func `prefix gasSelfDestruct`(value: Uint256, gasParams: Gasparams): GasResult {.nimcall.} =
@ -352,7 +352,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if gasParams.sd_condition: if gasParams.sd_condition:
result.gasCost += static(FeeSchedule[GasNewAccount]) result.gasCost += static(FeeSchedule[GasNewAccount])
func `prefix gasCreate2`(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall.} = func `prefix gasCreate2`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall.} =
result = static(FeeSchedule[GasSha3Word]) * (memLength).wordCount result = static(FeeSchedule[GasSha3Word]) * (memLength).wordCount
# ################################################################################################### # ###################################################################################################
@ -369,7 +369,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
func dynamic(handler: proc(value: Uint256): GasInt {.nimcall, gcsafe.}): GasCost = func dynamic(handler: proc(value: Uint256): GasInt {.nimcall, gcsafe.}): GasCost =
GasCost(kind: GckDynamic, d_handler: handler) GasCost(kind: GckDynamic, d_handler: handler)
func memExpansion(handler: proc(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall, gcsafe.}): GasCost = func memExpansion(handler: proc(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall, gcsafe.}): GasCost =
GasCost(kind: GckMemExpansion, m_handler: handler) GasCost(kind: GckMemExpansion, m_handler: handler)
func complex(handler: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.}): GasCost = func complex(handler: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.}): GasCost =

View File

@ -10,6 +10,11 @@ import
eth/common/eth_types, eth/rlp, eth/common/eth_types, eth/rlp,
../../../constants ../../../constants
type
# cannot use range for unknown reason
# Nim bug?
GasNatural* = int64 # range[0'i64..high(int64)]
# some methods based on py-evm utils/numeric # some methods based on py-evm utils/numeric
func log2*[bits: static int](value: StUint[bits]): Natural {.inline.}= func log2*[bits: static int](value: StUint[bits]): Natural {.inline.}=
@ -18,7 +23,7 @@ func log2*[bits: static int](value: StUint[bits]): Natural {.inline.}=
func log256*(value: UInt256): Natural {.inline.}= func log256*(value: UInt256): Natural {.inline.}=
value.log2 shr 3 # div 8 (= log2(256), Logb x = Loga x/Loga b) value.log2 shr 3 # div 8 (= log2(256), Logb x = Loga x/Loga b)
func ceil32*(value: Natural): Natural {.inline.}= func ceil32*(value: GasNatural): GasNatural {.inline.}=
# Round input to the nearest bigger multiple of 32 # Round input to the nearest bigger multiple of 32
result = value result = value
@ -27,7 +32,7 @@ func ceil32*(value: Natural): Natural {.inline.}=
if remainder != 0: if remainder != 0:
return value + 32 - remainder return value + 32 - remainder
func wordCount*(length: Natural): Natural {.inline.}= func wordCount*(length: GasNatural): GasNatural {.inline.}=
# Returns the number of EVM words corresponding to a specific size. # Returns the number of EVM words corresponding to a specific size.
# EVM words is rounded up # EVM words is rounded up
length.ceil32 shr 5 # equivalent to `div 32` (32 = 2^5) length.ceil32 shr 5 # equivalent to `div 32` (32 = 2^5)