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_gasBalance*: GasInt
c_contractGas*: Uint256
c_currentMemSize*: Natural
c_memOffset*: Natural
c_memLength*: Natural
c_currentMemSize*: GasNatural
c_memOffset*: GasNatural
c_memLength*: GasNatural
of Create:
cr_currentMemSize*: Natural
cr_memOffset*: Natural
cr_memLength*: Natural
cr_currentMemSize*: GasNatural
cr_memOffset*: GasNatural
cr_memLength*: GasNatural
of SelfDestruct:
sd_condition*: bool
else:
@ -99,7 +99,7 @@ type
of GckDynamic:
d_handler*: proc(value: Uint256): GasInt {.nimcall, gcsafe.}
of GckMemExpansion:
m_handler*: proc(currentMemSize, memOffset, memLength: Natural): GasInt {.nimcall, gcsafe.}
m_handler*: proc(currentMemSize, memOffset, memLength: GasNatural): GasInt {.nimcall, gcsafe.}
of GckComplex:
c_handler*: proc(value: Uint256, gasParams: GasParams): GasResult {.nimcall, gcsafe.}
# We use gasCost/gasRefund for:
@ -118,7 +118,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
# ############### Helper functions ##############################
func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: Natural): GasInt {.inline.} =
func `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength: GasNatural): GasInt {.inline.} =
# Input: size (in bytes)
# Yellow Paper:
@ -185,23 +185,23 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
gasParams.cr_memOffset,
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 += static(FeeSchedule[GasSha3]) +
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]) +
static(FeeSchedule[GasCopy]) * memLength.wordCount
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]) +
static(FeeSchedule[GasCopy]) * memLength.wordCount
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 += `prefix gasMemoryExpansion`(currentMemSize, memOffset, memLength)
@ -223,34 +223,34 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if value.isZero and not gasParams.s_isStorageEmpty:
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 += static(FeeSchedule[GasLog]) +
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 += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength +
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 += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength +
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 += static(FeeSchedule[GasLog]) +
static(FeeSchedule[GasLogData]) * memLength +
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 += static(FeeSchedule[GasLog]) +
@ -343,7 +343,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if not value.isZero and gasParams.kind in {Call, CallCode}:
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)
func `prefix gasSelfDestruct`(value: Uint256, gasParams: Gasparams): GasResult {.nimcall.} =
@ -352,7 +352,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
if gasParams.sd_condition:
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
# ###################################################################################################
@ -369,7 +369,7 @@ template gasCosts(fork: Fork, prefix, ResultGasCostsName: untyped) =
func dynamic(handler: proc(value: Uint256): GasInt {.nimcall, gcsafe.}): GasCost =
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)
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,
../../../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
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.}=
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
result = value
@ -27,7 +32,7 @@ func ceil32*(value: Natural): Natural {.inline.}=
if remainder != 0:
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.
# EVM words is rounded up
length.ceil32 shr 5 # equivalent to `div 32` (32 = 2^5)
@ -68,7 +73,7 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0):
if toLen > hi-lo+1:
var temp: array[N, byte]
temp[0..hi-lo] = x.toOpenArray(lo, hi)
temp[0..hi-lo] = x.toOpenArray(lo, hi)
result = T.fromBytesBE(
temp,
allowPadding = false
@ -78,7 +83,7 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0):
x.toOpenArray(lo, hi),
allowPadding = true
)
proc rangeToPadded2*[T: StUint](x: openarray[byte], first, last: int, toLen = 0): T =
## Convert take a slice of a sequence of bytes interpret it as the big endian
## representation of an Uint256. Use padding for sequence shorter than 32 bytes
@ -92,7 +97,7 @@ proc rangeToPadded2*[T: StUint](x: openarray[byte], first, last: int, toLen = 0)
return # 0
var temp: array[N, byte]
temp[0..hi-lo] = x.toOpenArray(lo, hi)
temp[0..hi-lo] = x.toOpenArray(lo, hi)
result = T.fromBytesBE(
temp.toOpenArray(0, toLen-1),
allowPadding = true